blob: b7fe1f2be06ce3ea370c8e97eb5a49a6bffc251d [file] [log] [blame]
Daniel Willmannde07b952020-10-19 10:32:34 +02001#!/usr/bin/env python3
Alexander Chemeris6e589142013-07-04 17:34:06 +04002
3#
4# Utility to display some informations about a SIM card
5#
6#
7# Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
8# Copyright (C) 2010 Harald Welte <laforge@gnumonks.org>
9# Copyright (C) 2013 Alexander Chemeris <alexander.chemeris@gmail.com>
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <http://www.gnu.org/licenses/>.
23#
24
25import hashlib
Harald Welte8fe1d202021-04-11 12:31:40 +020026import argparse
Alexander Chemeris6e589142013-07-04 17:34:06 +040027import os
28import random
29import re
30import sys
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +020031from pySim.ts_51_011 import EF, DF, EF_SST_map, EF_AD
Sebastian Viviani0dc8f692020-05-29 00:14:55 +010032from pySim.ts_31_102 import EF_UST_map, EF_USIM_ADF_map
Supreeth Herle5ad9aec2020-03-24 17:26:40 +010033from pySim.ts_31_103 import EF_IST_map, EF_ISIM_ADF_map
Alexander Chemeris6e589142013-07-04 17:34:06 +040034
Alexander Chemeris6e589142013-07-04 17:34:06 +040035from pySim.commands import SimCardCommands
Harald Welte8fe1d202021-04-11 12:31:40 +020036from pySim.transport import init_reader, argparse_add_reader_args
Philipp Maierabc23362021-11-15 17:24:44 +010037from pySim.exceptions import SwMatchError
Philipp Maierbb73e512021-05-05 16:14:00 +020038from pySim.cards import card_detect, SimCard, UsimCard, IsimCard
Philipp Maierff84c232020-05-12 17:24:18 +020039from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn
Philipp Maiere2c59a82021-04-30 14:57:41 +020040from pySim.utils import format_xplmn_w_act, dec_st
Supreeth Herle99d55552020-03-24 13:03:43 +010041from pySim.utils import h2s, format_ePDGSelection
Alexander Chemeris6e589142013-07-04 17:34:06 +040042
Harald Welte8fe1d202021-04-11 12:31:40 +020043option_parser = argparse.ArgumentParser(prog='pySim-read',
Harald Weltec91085e2022-02-10 18:05:45 +010044 description='Legacy tool for reading some parts of a SIM card',
45 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Harald Welte8fe1d202021-04-11 12:31:40 +020046argparse_add_reader_args(option_parser)
Alexander Chemeris6e589142013-07-04 17:34:06 +040047
Philipp Maierabc23362021-11-15 17:24:44 +010048
Harald Weltec91085e2022-02-10 18:05:45 +010049def select_app(adf: str, card: SimCard):
50 """Select application by its AID"""
51 sw = 0
52 try:
53 if card._scc.cla_byte == "00":
54 data, sw = card.select_adf_by_aid(adf)
55 except SwMatchError as e:
56 if e.sw_actual == "6a82":
57 # If we can't select the file because it does not exist, we just remain silent since it means
58 # that this card just does not have an USIM application installed, which is not an error.
59 pass
60 else:
61 print("ADF." + adf + ": Can't select application -- " + str(e))
62 except Exception as e:
63 print("ADF." + adf + ": Can't select application -- " + str(e))
64
65 return sw
66
Philipp Maierabc23362021-11-15 17:24:44 +010067
Alexander Chemeris6e589142013-07-04 17:34:06 +040068if __name__ == '__main__':
69
Harald Weltec91085e2022-02-10 18:05:45 +010070 # Parse options
71 opts = option_parser.parse_args()
Alexander Chemeris6e589142013-07-04 17:34:06 +040072
Harald Weltec91085e2022-02-10 18:05:45 +010073 # Init card reader driver
74 sl = init_reader(opts)
75 if sl is None:
76 exit(1)
Alexander Chemeris6e589142013-07-04 17:34:06 +040077
Harald Weltec91085e2022-02-10 18:05:45 +010078 # Create command layer
79 scc = SimCardCommands(transport=sl)
Alexander Chemeris6e589142013-07-04 17:34:06 +040080
Harald Weltec91085e2022-02-10 18:05:45 +010081 # Wait for SIM card
82 sl.wait_for_card()
Alexander Chemeris6e589142013-07-04 17:34:06 +040083
Harald Weltec91085e2022-02-10 18:05:45 +010084 # Assuming UICC SIM
85 scc.cla_byte = "00"
86 scc.sel_ctrl = "0004"
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010087
Harald Weltec91085e2022-02-10 18:05:45 +010088 # Testing for Classic SIM or UICC
89 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
90 if sw == '6e00':
91 # Just a Classic SIM
92 scc.cla_byte = "a0"
93 scc.sel_ctrl = "0000"
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010094
Harald Weltec91085e2022-02-10 18:05:45 +010095 # Read the card
96 print("Reading ...")
Alexander Chemeris6e589142013-07-04 17:34:06 +040097
Harald Weltec91085e2022-02-10 18:05:45 +010098 # Initialize Card object by auto detecting the card
99 card = card_detect("auto", scc) or SimCard(scc)
Supreeth Herle4c306ab2020-03-18 11:38:00 +0100100
Harald Weltec91085e2022-02-10 18:05:45 +0100101 # Read all AIDs on the UICC
102 card.read_aids()
Supreeth Herle3bf43632020-03-20 20:20:27 +0100103
Harald Weltec91085e2022-02-10 18:05:45 +0100104 # EF.ICCID
105 (res, sw) = card.read_iccid()
106 if sw == '9000':
107 print("ICCID: %s" % (res,))
108 else:
109 print("ICCID: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400110
Harald Weltec91085e2022-02-10 18:05:45 +0100111 # EF.IMSI
112 (res, sw) = card.read_imsi()
113 if sw == '9000':
114 print("IMSI: %s" % (res,))
115 else:
116 print("IMSI: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400117
Harald Weltec91085e2022-02-10 18:05:45 +0100118 # EF.GID1
119 try:
120 (res, sw) = card.read_gid1()
121 if sw == '9000':
122 print("GID1: %s" % (res,))
123 else:
124 print("GID1: Can't read, response code = %s" % (sw,))
125 except Exception as e:
126 print("GID1: Can't read file -- %s" % (str(e),))
Supreeth Herleab46d622020-03-05 15:30:22 +0100127
Harald Weltec91085e2022-02-10 18:05:45 +0100128 # EF.GID2
129 try:
130 (res, sw) = card.read_binary('GID2')
131 if sw == '9000':
132 print("GID2: %s" % (res,))
133 else:
134 print("GID2: Can't read, response code = %s" % (sw,))
135 except Exception as e:
136 print("GID2: Can't read file -- %s" % (str(e),))
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100137
Harald Weltec91085e2022-02-10 18:05:45 +0100138 # EF.SMSP
139 (res, sw) = card.read_record('SMSP', 1)
140 if sw == '9000':
141 print("SMSP: %s" % (res,))
142 else:
143 print("SMSP: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400144
Harald Weltec91085e2022-02-10 18:05:45 +0100145 # EF.SPN
146 try:
147 (res, sw) = card.read_spn()
148 if sw == '9000':
149 print("SPN: %s" % (res[0] or "Not available"))
150 print("Show in HPLMN: %s" % (res[1],))
151 print("Hide in OPLMN: %s" % (res[2],))
152 else:
153 print("SPN: Can't read, response code = %s" % (sw,))
154 except Exception as e:
155 print("SPN: Can't read file -- %s" % (str(e),))
Supreeth Herlef8299452019-06-08 07:49:08 +0200156
Harald Weltec91085e2022-02-10 18:05:45 +0100157 # EF.PLMNsel
158 try:
159 (res, sw) = card.read_binary('PLMNsel')
160 if sw == '9000':
161 print("PLMNsel: %s" % (res))
162 else:
163 print("PLMNsel: Can't read, response code = %s" % (sw,))
164 except Exception as e:
165 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200166
Harald Weltec91085e2022-02-10 18:05:45 +0100167 # EF.PLMNwAcT
168 try:
169 (res, sw) = card.read_plmn_act()
170 if sw == '9000':
171 print("PLMNwAcT:\n%s" % (res))
172 else:
173 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
174 except Exception as e:
175 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200176
Harald Weltec91085e2022-02-10 18:05:45 +0100177 # EF.OPLMNwAcT
178 try:
179 (res, sw) = card.read_oplmn_act()
180 if sw == '9000':
181 print("OPLMNwAcT:\n%s" % (res))
182 else:
183 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
184 except Exception as e:
185 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200186
Harald Weltec91085e2022-02-10 18:05:45 +0100187 # EF.HPLMNAcT
188 try:
189 (res, sw) = card.read_hplmn_act()
190 if sw == '9000':
191 print("HPLMNAcT:\n%s" % (res))
192 else:
193 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
194 except Exception as e:
195 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400196
Harald Weltec91085e2022-02-10 18:05:45 +0100197 # EF.ACC
198 (res, sw) = card.read_binary('ACC')
199 if sw == '9000':
200 print("ACC: %s" % (res,))
201 else:
202 print("ACC: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400203
Harald Weltec91085e2022-02-10 18:05:45 +0100204 # EF.MSISDN
205 try:
206 (res, sw) = card.read_msisdn()
207 if sw == '9000':
208 # (npi, ton, msisdn) = res
209 if res is not None:
210 print("MSISDN (NPI=%d ToN=%d): %s" % res)
211 else:
212 print("MSISDN: Not available")
213 else:
214 print("MSISDN: Can't read, response code = %s" % (sw,))
215 except Exception as e:
216 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400217
Harald Weltec91085e2022-02-10 18:05:45 +0100218 # EF.AD
219 (res, sw) = card.read_binary('AD')
220 if sw == '9000':
221 print("Administrative data: %s" % (res,))
222 ad = EF_AD()
223 decoded_data = ad.decode_hex(res)
224 print("\tMS operation mode: %s" % decoded_data['ms_operation_mode'])
225 if decoded_data['ofm']:
226 print("\tCiphering Indicator: enabled")
227 else:
228 print("\tCiphering Indicator: disabled")
229 else:
230 print("AD: Can't read, response code = %s" % (sw,))
Philipp Maieree908ae2019-03-21 16:21:12 +0100231
Harald Weltec91085e2022-02-10 18:05:45 +0100232 # EF.SST
233 (res, sw) = card.read_binary('SST')
234 if sw == '9000':
235 print("SIM Service Table: %s" % res)
236 # Print those which are available
237 print("%s" % dec_st(res))
238 else:
239 print("SIM Service Table: Can't read, response code = %s" % (sw,))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100240
Harald Weltec91085e2022-02-10 18:05:45 +0100241 # Check whether we have th AID of USIM, if so select it by its AID
242 # EF.UST - File Id in ADF USIM : 6f38
243 sw = select_app("USIM", card)
244 if sw == '9000':
245 # Select USIM profile
246 usim_card = UsimCard(scc)
herlesupreethcebf8b12021-01-21 05:57:06 +0100247
Harald Weltec91085e2022-02-10 18:05:45 +0100248 # EF.EHPLMN
249 if usim_card.file_exists(EF_USIM_ADF_map['EHPLMN']):
250 (res, sw) = usim_card.read_ehplmn()
251 if sw == '9000':
252 print("EHPLMN:\n%s" % (res))
253 else:
254 print("EHPLMN: Can't read, response code = %s" % (sw,))
herlesupreeth4a3580b2020-09-29 10:11:36 +0200255
Harald Weltec91085e2022-02-10 18:05:45 +0100256 # EF.UST
257 try:
258 if usim_card.file_exists(EF_USIM_ADF_map['UST']):
259 # res[0] - EF content of UST
260 # res[1] - Human readable format of services marked available in UST
261 (res, sw) = usim_card.read_ust()
262 if sw == '9000':
263 print("USIM Service Table: %s" % res[0])
264 print("%s" % res[1])
265 else:
266 print("USIM Service Table: Can't read, response code = %s" % (sw,))
267 except Exception as e:
268 print("USIM Service Table: Can't read file -- " + str(e))
Supreeth Herle96412992020-03-22 08:20:11 +0100269
Harald Weltec91085e2022-02-10 18:05:45 +0100270 # EF.ePDGId - Home ePDG Identifier
271 try:
272 if usim_card.file_exists(EF_USIM_ADF_map['ePDGId']):
273 (res, sw) = usim_card.read_epdgid()
274 if sw == '9000':
275 print("ePDGId:\n%s" %
276 (len(res) and res or '\tNot available\n',))
277 else:
278 print("ePDGId: Can't read, response code = %s" % (sw,))
279 except Exception as e:
280 print("ePDGId: Can't read file -- " + str(e))
Supreeth Herleb1634db2020-03-22 10:00:43 +0100281
Harald Weltec91085e2022-02-10 18:05:45 +0100282 # EF.ePDGSelection - ePDG Selection Information
283 try:
284 if usim_card.file_exists(EF_USIM_ADF_map['ePDGSelection']):
285 (res, sw) = usim_card.read_ePDGSelection()
286 if sw == '9000':
287 print("ePDGSelection:\n%s" % (res,))
288 else:
289 print("ePDGSelection: Can't read, response code = %s" % (sw,))
290 except Exception as e:
291 print("ePDGSelection: Can't read file -- " + str(e))
Supreeth Herle99d55552020-03-24 13:03:43 +0100292
Harald Weltec91085e2022-02-10 18:05:45 +0100293 # Select ISIM application by its AID
294 sw = select_app("ISIM", card)
295 if sw == '9000':
296 # Select USIM profile
297 isim_card = IsimCard(scc)
herlesupreethcebf8b12021-01-21 05:57:06 +0100298
Harald Weltec91085e2022-02-10 18:05:45 +0100299 # EF.P-CSCF - P-CSCF Address
300 try:
301 if isim_card.file_exists(EF_ISIM_ADF_map['PCSCF']):
302 res = isim_card.read_pcscf()
303 print("P-CSCF:\n%s" %
304 (len(res) and res or '\tNot available\n',))
305 except Exception as e:
306 print("P-CSCF: Can't read file -- " + str(e))
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100307
Harald Weltec91085e2022-02-10 18:05:45 +0100308 # EF.DOMAIN - Home Network Domain Name e.g. ims.mncXXX.mccXXX.3gppnetwork.org
309 try:
310 if isim_card.file_exists(EF_ISIM_ADF_map['DOMAIN']):
311 (res, sw) = isim_card.read_domain()
312 if sw == '9000':
313 print("Home Network Domain Name: %s" %
314 (len(res) and res or 'Not available',))
315 else:
316 print(
317 "Home Network Domain Name: Can't read, response code = %s" % (sw,))
318 except Exception as e:
319 print("Home Network Domain Name: Can't read file -- " + str(e))
Supreeth Herle05b28072020-03-25 10:23:48 +0100320
Harald Weltec91085e2022-02-10 18:05:45 +0100321 # EF.IMPI - IMS private user identity
322 try:
323 if isim_card.file_exists(EF_ISIM_ADF_map['IMPI']):
324 (res, sw) = isim_card.read_impi()
325 if sw == '9000':
326 print("IMS private user identity: %s" %
327 (len(res) and res or 'Not available',))
328 else:
329 print(
330 "IMS private user identity: Can't read, response code = %s" % (sw,))
331 except Exception as e:
332 print("IMS private user identity: Can't read file -- " + str(e))
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100333
Harald Weltec91085e2022-02-10 18:05:45 +0100334 # EF.IMPU - IMS public user identity
335 try:
336 if isim_card.file_exists(EF_ISIM_ADF_map['IMPU']):
337 res = isim_card.read_impu()
338 print("IMS public user identity:\n%s" %
339 (len(res) and res or '\tNot available\n',))
340 except Exception as e:
341 print("IMS public user identity: Can't read file -- " + str(e))
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100342
Harald Weltec91085e2022-02-10 18:05:45 +0100343 # EF.UICCIARI - UICC IARI
344 try:
345 if isim_card.file_exists(EF_ISIM_ADF_map['UICCIARI']):
346 res = isim_card.read_iari()
347 print("UICC IARI:\n%s" %
348 (len(res) and res or '\tNot available\n',))
349 except Exception as e:
350 print("UICC IARI: Can't read file -- " + str(e))
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200351
Harald Weltec91085e2022-02-10 18:05:45 +0100352 # EF.IST
353 (res, sw) = card.read_binary('6f07')
354 if sw == '9000':
355 print("ISIM Service Table: %s" % res)
356 # Print those which are available
357 print("%s" % dec_st(res, table="isim"))
358 else:
359 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
Supreeth Herleee15c772020-03-22 08:58:33 +0100360
Harald Weltec91085e2022-02-10 18:05:45 +0100361 # Done for this card and maybe for everything ?
362 print("Done !\n")