blob: d34ddc35d3602a0a1d3ea667625970fe8196c846 [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
Harald Welteb314b9b2023-07-09 22:19:49 +020031
Harald Welte57ad38e2023-07-09 22:14:09 +020032from pySim.ts_51_011 import EF_SST_map, EF_AD
33from pySim.legacy.ts_51_011 import EF, DF
Harald Welteb314b9b2023-07-09 22:19:49 +020034from pySim.ts_31_102 import EF_UST_map
35from pySim.legacy.ts_31_102 import EF_USIM_ADF_map
36from pySim.ts_31_103 import EF_IST_map
37from pySim.legacy.ts_31_103 import EF_ISIM_ADF_map
Alexander Chemeris6e589142013-07-04 17:34:06 +040038
Alexander Chemeris6e589142013-07-04 17:34:06 +040039from pySim.commands import SimCardCommands
Harald Welte8fe1d202021-04-11 12:31:40 +020040from pySim.transport import init_reader, argparse_add_reader_args
Philipp Maierabc23362021-11-15 17:24:44 +010041from pySim.exceptions import SwMatchError
Harald Weltef8d2e2b2023-07-09 17:58:38 +020042from pySim.legacy.cards import card_detect, SimCard, UsimCard, IsimCard
43from pySim.utils import h2b, h2s, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn
44from pySim.legacy.utils import format_xplmn_w_act, dec_st
Alexander Chemeris6e589142013-07-04 17:34:06 +040045
Harald Weltef422eb12023-06-09 11:15:09 +020046option_parser = argparse.ArgumentParser(description='Legacy tool for reading some parts of a SIM card',
Harald Weltec91085e2022-02-10 18:05:45 +010047 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Harald Welte8fe1d202021-04-11 12:31:40 +020048argparse_add_reader_args(option_parser)
Alexander Chemeris6e589142013-07-04 17:34:06 +040049
Philipp Maierabc23362021-11-15 17:24:44 +010050
Harald Weltec91085e2022-02-10 18:05:45 +010051def select_app(adf: str, card: SimCard):
52 """Select application by its AID"""
53 sw = 0
54 try:
55 if card._scc.cla_byte == "00":
56 data, sw = card.select_adf_by_aid(adf)
57 except SwMatchError as e:
58 if e.sw_actual == "6a82":
59 # If we can't select the file because it does not exist, we just remain silent since it means
60 # that this card just does not have an USIM application installed, which is not an error.
61 pass
62 else:
63 print("ADF." + adf + ": Can't select application -- " + str(e))
64 except Exception as e:
65 print("ADF." + adf + ": Can't select application -- " + str(e))
66
67 return sw
68
Philipp Maierabc23362021-11-15 17:24:44 +010069
Alexander Chemeris6e589142013-07-04 17:34:06 +040070if __name__ == '__main__':
71
Harald Weltec91085e2022-02-10 18:05:45 +010072 # Parse options
73 opts = option_parser.parse_args()
Alexander Chemeris6e589142013-07-04 17:34:06 +040074
Harald Weltec91085e2022-02-10 18:05:45 +010075 # Init card reader driver
76 sl = init_reader(opts)
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
Matan Perelman777ee9e2023-05-14 08:58:50 +0300256 # EF.FPLMN
257 if usim_card.file_exists(EF_USIM_ADF_map['FPLMN']):
258 res, sw = usim_card.read_fplmn()
259 if sw == '9000':
260 print(f'FPLMN:\n{res}')
261 else:
262 print(f'FPLMN: Can\'t read, response code = {sw}')
263
Harald Weltec91085e2022-02-10 18:05:45 +0100264 # EF.UST
265 try:
266 if usim_card.file_exists(EF_USIM_ADF_map['UST']):
267 # res[0] - EF content of UST
268 # res[1] - Human readable format of services marked available in UST
269 (res, sw) = usim_card.read_ust()
270 if sw == '9000':
271 print("USIM Service Table: %s" % res[0])
272 print("%s" % res[1])
273 else:
274 print("USIM Service Table: Can't read, response code = %s" % (sw,))
275 except Exception as e:
276 print("USIM Service Table: Can't read file -- " + str(e))
Supreeth Herle96412992020-03-22 08:20:11 +0100277
Harald Weltec91085e2022-02-10 18:05:45 +0100278 # EF.ePDGId - Home ePDG Identifier
279 try:
280 if usim_card.file_exists(EF_USIM_ADF_map['ePDGId']):
281 (res, sw) = usim_card.read_epdgid()
282 if sw == '9000':
283 print("ePDGId:\n%s" %
284 (len(res) and res or '\tNot available\n',))
285 else:
286 print("ePDGId: Can't read, response code = %s" % (sw,))
287 except Exception as e:
288 print("ePDGId: Can't read file -- " + str(e))
Supreeth Herleb1634db2020-03-22 10:00:43 +0100289
Harald Weltec91085e2022-02-10 18:05:45 +0100290 # EF.ePDGSelection - ePDG Selection Information
291 try:
292 if usim_card.file_exists(EF_USIM_ADF_map['ePDGSelection']):
293 (res, sw) = usim_card.read_ePDGSelection()
294 if sw == '9000':
295 print("ePDGSelection:\n%s" % (res,))
296 else:
297 print("ePDGSelection: Can't read, response code = %s" % (sw,))
298 except Exception as e:
299 print("ePDGSelection: Can't read file -- " + str(e))
Supreeth Herle99d55552020-03-24 13:03:43 +0100300
Harald Weltec91085e2022-02-10 18:05:45 +0100301 # Select ISIM application by its AID
302 sw = select_app("ISIM", card)
303 if sw == '9000':
304 # Select USIM profile
305 isim_card = IsimCard(scc)
herlesupreethcebf8b12021-01-21 05:57:06 +0100306
Harald Weltec91085e2022-02-10 18:05:45 +0100307 # EF.P-CSCF - P-CSCF Address
308 try:
309 if isim_card.file_exists(EF_ISIM_ADF_map['PCSCF']):
310 res = isim_card.read_pcscf()
311 print("P-CSCF:\n%s" %
312 (len(res) and res or '\tNot available\n',))
313 except Exception as e:
314 print("P-CSCF: Can't read file -- " + str(e))
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100315
Harald Weltec91085e2022-02-10 18:05:45 +0100316 # EF.DOMAIN - Home Network Domain Name e.g. ims.mncXXX.mccXXX.3gppnetwork.org
317 try:
318 if isim_card.file_exists(EF_ISIM_ADF_map['DOMAIN']):
319 (res, sw) = isim_card.read_domain()
320 if sw == '9000':
321 print("Home Network Domain Name: %s" %
322 (len(res) and res or 'Not available',))
323 else:
324 print(
325 "Home Network Domain Name: Can't read, response code = %s" % (sw,))
326 except Exception as e:
327 print("Home Network Domain Name: Can't read file -- " + str(e))
Supreeth Herle05b28072020-03-25 10:23:48 +0100328
Harald Weltec91085e2022-02-10 18:05:45 +0100329 # EF.IMPI - IMS private user identity
330 try:
331 if isim_card.file_exists(EF_ISIM_ADF_map['IMPI']):
332 (res, sw) = isim_card.read_impi()
333 if sw == '9000':
334 print("IMS private user identity: %s" %
335 (len(res) and res or 'Not available',))
336 else:
337 print(
338 "IMS private user identity: Can't read, response code = %s" % (sw,))
339 except Exception as e:
340 print("IMS private user identity: Can't read file -- " + str(e))
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100341
Harald Weltec91085e2022-02-10 18:05:45 +0100342 # EF.IMPU - IMS public user identity
343 try:
344 if isim_card.file_exists(EF_ISIM_ADF_map['IMPU']):
345 res = isim_card.read_impu()
346 print("IMS public user identity:\n%s" %
347 (len(res) and res or '\tNot available\n',))
348 except Exception as e:
349 print("IMS public user identity: Can't read file -- " + str(e))
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100350
Harald Weltec91085e2022-02-10 18:05:45 +0100351 # EF.UICCIARI - UICC IARI
352 try:
353 if isim_card.file_exists(EF_ISIM_ADF_map['UICCIARI']):
354 res = isim_card.read_iari()
355 print("UICC IARI:\n%s" %
356 (len(res) and res or '\tNot available\n',))
357 except Exception as e:
358 print("UICC IARI: Can't read file -- " + str(e))
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200359
Harald Weltec91085e2022-02-10 18:05:45 +0100360 # EF.IST
361 (res, sw) = card.read_binary('6f07')
362 if sw == '9000':
363 print("ISIM Service Table: %s" % res)
364 # Print those which are available
365 print("%s" % dec_st(res, table="isim"))
366 else:
367 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
Supreeth Herleee15c772020-03-22 08:58:33 +0100368
Harald Weltec91085e2022-02-10 18:05:45 +0100369 # Done for this card and maybe for everything ?
370 print("Done !\n")