blob: bafaf262bfb4deedede6c3c9389848a155608657 [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)
77 if sl is None:
78 exit(1)
Alexander Chemeris6e589142013-07-04 17:34:06 +040079
Harald Weltec91085e2022-02-10 18:05:45 +010080 # Create command layer
81 scc = SimCardCommands(transport=sl)
Alexander Chemeris6e589142013-07-04 17:34:06 +040082
Harald Weltec91085e2022-02-10 18:05:45 +010083 # Wait for SIM card
84 sl.wait_for_card()
Alexander Chemeris6e589142013-07-04 17:34:06 +040085
Harald Weltec91085e2022-02-10 18:05:45 +010086 # Assuming UICC SIM
87 scc.cla_byte = "00"
88 scc.sel_ctrl = "0004"
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010089
Harald Weltec91085e2022-02-10 18:05:45 +010090 # Testing for Classic SIM or UICC
91 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
92 if sw == '6e00':
93 # Just a Classic SIM
94 scc.cla_byte = "a0"
95 scc.sel_ctrl = "0000"
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010096
Harald Weltec91085e2022-02-10 18:05:45 +010097 # Read the card
98 print("Reading ...")
Alexander Chemeris6e589142013-07-04 17:34:06 +040099
Harald Weltec91085e2022-02-10 18:05:45 +0100100 # Initialize Card object by auto detecting the card
101 card = card_detect("auto", scc) or SimCard(scc)
Supreeth Herle4c306ab2020-03-18 11:38:00 +0100102
Harald Weltec91085e2022-02-10 18:05:45 +0100103 # Read all AIDs on the UICC
104 card.read_aids()
Supreeth Herle3bf43632020-03-20 20:20:27 +0100105
Harald Weltec91085e2022-02-10 18:05:45 +0100106 # EF.ICCID
107 (res, sw) = card.read_iccid()
108 if sw == '9000':
109 print("ICCID: %s" % (res,))
110 else:
111 print("ICCID: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400112
Harald Weltec91085e2022-02-10 18:05:45 +0100113 # EF.IMSI
114 (res, sw) = card.read_imsi()
115 if sw == '9000':
116 print("IMSI: %s" % (res,))
117 else:
118 print("IMSI: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400119
Harald Weltec91085e2022-02-10 18:05:45 +0100120 # EF.GID1
121 try:
122 (res, sw) = card.read_gid1()
123 if sw == '9000':
124 print("GID1: %s" % (res,))
125 else:
126 print("GID1: Can't read, response code = %s" % (sw,))
127 except Exception as e:
128 print("GID1: Can't read file -- %s" % (str(e),))
Supreeth Herleab46d622020-03-05 15:30:22 +0100129
Harald Weltec91085e2022-02-10 18:05:45 +0100130 # EF.GID2
131 try:
132 (res, sw) = card.read_binary('GID2')
133 if sw == '9000':
134 print("GID2: %s" % (res,))
135 else:
136 print("GID2: Can't read, response code = %s" % (sw,))
137 except Exception as e:
138 print("GID2: Can't read file -- %s" % (str(e),))
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100139
Harald Weltec91085e2022-02-10 18:05:45 +0100140 # EF.SMSP
141 (res, sw) = card.read_record('SMSP', 1)
142 if sw == '9000':
143 print("SMSP: %s" % (res,))
144 else:
145 print("SMSP: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400146
Harald Weltec91085e2022-02-10 18:05:45 +0100147 # EF.SPN
148 try:
149 (res, sw) = card.read_spn()
150 if sw == '9000':
151 print("SPN: %s" % (res[0] or "Not available"))
152 print("Show in HPLMN: %s" % (res[1],))
153 print("Hide in OPLMN: %s" % (res[2],))
154 else:
155 print("SPN: Can't read, response code = %s" % (sw,))
156 except Exception as e:
157 print("SPN: Can't read file -- %s" % (str(e),))
Supreeth Herlef8299452019-06-08 07:49:08 +0200158
Harald Weltec91085e2022-02-10 18:05:45 +0100159 # EF.PLMNsel
160 try:
161 (res, sw) = card.read_binary('PLMNsel')
162 if sw == '9000':
163 print("PLMNsel: %s" % (res))
164 else:
165 print("PLMNsel: Can't read, response code = %s" % (sw,))
166 except Exception as e:
167 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200168
Harald Weltec91085e2022-02-10 18:05:45 +0100169 # EF.PLMNwAcT
170 try:
171 (res, sw) = card.read_plmn_act()
172 if sw == '9000':
173 print("PLMNwAcT:\n%s" % (res))
174 else:
175 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
176 except Exception as e:
177 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200178
Harald Weltec91085e2022-02-10 18:05:45 +0100179 # EF.OPLMNwAcT
180 try:
181 (res, sw) = card.read_oplmn_act()
182 if sw == '9000':
183 print("OPLMNwAcT:\n%s" % (res))
184 else:
185 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
186 except Exception as e:
187 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200188
Harald Weltec91085e2022-02-10 18:05:45 +0100189 # EF.HPLMNAcT
190 try:
191 (res, sw) = card.read_hplmn_act()
192 if sw == '9000':
193 print("HPLMNAcT:\n%s" % (res))
194 else:
195 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
196 except Exception as e:
197 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400198
Harald Weltec91085e2022-02-10 18:05:45 +0100199 # EF.ACC
200 (res, sw) = card.read_binary('ACC')
201 if sw == '9000':
202 print("ACC: %s" % (res,))
203 else:
204 print("ACC: Can't read, response code = %s" % (sw,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400205
Harald Weltec91085e2022-02-10 18:05:45 +0100206 # EF.MSISDN
207 try:
208 (res, sw) = card.read_msisdn()
209 if sw == '9000':
210 # (npi, ton, msisdn) = res
211 if res is not None:
212 print("MSISDN (NPI=%d ToN=%d): %s" % res)
213 else:
214 print("MSISDN: Not available")
215 else:
216 print("MSISDN: Can't read, response code = %s" % (sw,))
217 except Exception as e:
218 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400219
Harald Weltec91085e2022-02-10 18:05:45 +0100220 # EF.AD
221 (res, sw) = card.read_binary('AD')
222 if sw == '9000':
223 print("Administrative data: %s" % (res,))
224 ad = EF_AD()
225 decoded_data = ad.decode_hex(res)
226 print("\tMS operation mode: %s" % decoded_data['ms_operation_mode'])
227 if decoded_data['ofm']:
228 print("\tCiphering Indicator: enabled")
229 else:
230 print("\tCiphering Indicator: disabled")
231 else:
232 print("AD: Can't read, response code = %s" % (sw,))
Philipp Maieree908ae2019-03-21 16:21:12 +0100233
Harald Weltec91085e2022-02-10 18:05:45 +0100234 # EF.SST
235 (res, sw) = card.read_binary('SST')
236 if sw == '9000':
237 print("SIM Service Table: %s" % res)
238 # Print those which are available
239 print("%s" % dec_st(res))
240 else:
241 print("SIM Service Table: Can't read, response code = %s" % (sw,))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100242
Harald Weltec91085e2022-02-10 18:05:45 +0100243 # Check whether we have th AID of USIM, if so select it by its AID
244 # EF.UST - File Id in ADF USIM : 6f38
245 sw = select_app("USIM", card)
246 if sw == '9000':
247 # Select USIM profile
248 usim_card = UsimCard(scc)
herlesupreethcebf8b12021-01-21 05:57:06 +0100249
Harald Weltec91085e2022-02-10 18:05:45 +0100250 # EF.EHPLMN
251 if usim_card.file_exists(EF_USIM_ADF_map['EHPLMN']):
252 (res, sw) = usim_card.read_ehplmn()
253 if sw == '9000':
254 print("EHPLMN:\n%s" % (res))
255 else:
256 print("EHPLMN: Can't read, response code = %s" % (sw,))
herlesupreeth4a3580b2020-09-29 10:11:36 +0200257
Matan Perelman777ee9e2023-05-14 08:58:50 +0300258 # EF.FPLMN
259 if usim_card.file_exists(EF_USIM_ADF_map['FPLMN']):
260 res, sw = usim_card.read_fplmn()
261 if sw == '9000':
262 print(f'FPLMN:\n{res}')
263 else:
264 print(f'FPLMN: Can\'t read, response code = {sw}')
265
Harald Weltec91085e2022-02-10 18:05:45 +0100266 # EF.UST
267 try:
268 if usim_card.file_exists(EF_USIM_ADF_map['UST']):
269 # res[0] - EF content of UST
270 # res[1] - Human readable format of services marked available in UST
271 (res, sw) = usim_card.read_ust()
272 if sw == '9000':
273 print("USIM Service Table: %s" % res[0])
274 print("%s" % res[1])
275 else:
276 print("USIM Service Table: Can't read, response code = %s" % (sw,))
277 except Exception as e:
278 print("USIM Service Table: Can't read file -- " + str(e))
Supreeth Herle96412992020-03-22 08:20:11 +0100279
Harald Weltec91085e2022-02-10 18:05:45 +0100280 # EF.ePDGId - Home ePDG Identifier
281 try:
282 if usim_card.file_exists(EF_USIM_ADF_map['ePDGId']):
283 (res, sw) = usim_card.read_epdgid()
284 if sw == '9000':
285 print("ePDGId:\n%s" %
286 (len(res) and res or '\tNot available\n',))
287 else:
288 print("ePDGId: Can't read, response code = %s" % (sw,))
289 except Exception as e:
290 print("ePDGId: Can't read file -- " + str(e))
Supreeth Herleb1634db2020-03-22 10:00:43 +0100291
Harald Weltec91085e2022-02-10 18:05:45 +0100292 # EF.ePDGSelection - ePDG Selection Information
293 try:
294 if usim_card.file_exists(EF_USIM_ADF_map['ePDGSelection']):
295 (res, sw) = usim_card.read_ePDGSelection()
296 if sw == '9000':
297 print("ePDGSelection:\n%s" % (res,))
298 else:
299 print("ePDGSelection: Can't read, response code = %s" % (sw,))
300 except Exception as e:
301 print("ePDGSelection: Can't read file -- " + str(e))
Supreeth Herle99d55552020-03-24 13:03:43 +0100302
Harald Weltec91085e2022-02-10 18:05:45 +0100303 # Select ISIM application by its AID
304 sw = select_app("ISIM", card)
305 if sw == '9000':
306 # Select USIM profile
307 isim_card = IsimCard(scc)
herlesupreethcebf8b12021-01-21 05:57:06 +0100308
Harald Weltec91085e2022-02-10 18:05:45 +0100309 # EF.P-CSCF - P-CSCF Address
310 try:
311 if isim_card.file_exists(EF_ISIM_ADF_map['PCSCF']):
312 res = isim_card.read_pcscf()
313 print("P-CSCF:\n%s" %
314 (len(res) and res or '\tNot available\n',))
315 except Exception as e:
316 print("P-CSCF: Can't read file -- " + str(e))
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100317
Harald Weltec91085e2022-02-10 18:05:45 +0100318 # EF.DOMAIN - Home Network Domain Name e.g. ims.mncXXX.mccXXX.3gppnetwork.org
319 try:
320 if isim_card.file_exists(EF_ISIM_ADF_map['DOMAIN']):
321 (res, sw) = isim_card.read_domain()
322 if sw == '9000':
323 print("Home Network Domain Name: %s" %
324 (len(res) and res or 'Not available',))
325 else:
326 print(
327 "Home Network Domain Name: Can't read, response code = %s" % (sw,))
328 except Exception as e:
329 print("Home Network Domain Name: Can't read file -- " + str(e))
Supreeth Herle05b28072020-03-25 10:23:48 +0100330
Harald Weltec91085e2022-02-10 18:05:45 +0100331 # EF.IMPI - IMS private user identity
332 try:
333 if isim_card.file_exists(EF_ISIM_ADF_map['IMPI']):
334 (res, sw) = isim_card.read_impi()
335 if sw == '9000':
336 print("IMS private user identity: %s" %
337 (len(res) and res or 'Not available',))
338 else:
339 print(
340 "IMS private user identity: Can't read, response code = %s" % (sw,))
341 except Exception as e:
342 print("IMS private user identity: Can't read file -- " + str(e))
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100343
Harald Weltec91085e2022-02-10 18:05:45 +0100344 # EF.IMPU - IMS public user identity
345 try:
346 if isim_card.file_exists(EF_ISIM_ADF_map['IMPU']):
347 res = isim_card.read_impu()
348 print("IMS public user identity:\n%s" %
349 (len(res) and res or '\tNot available\n',))
350 except Exception as e:
351 print("IMS public user identity: Can't read file -- " + str(e))
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100352
Harald Weltec91085e2022-02-10 18:05:45 +0100353 # EF.UICCIARI - UICC IARI
354 try:
355 if isim_card.file_exists(EF_ISIM_ADF_map['UICCIARI']):
356 res = isim_card.read_iari()
357 print("UICC IARI:\n%s" %
358 (len(res) and res or '\tNot available\n',))
359 except Exception as e:
360 print("UICC IARI: Can't read file -- " + str(e))
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200361
Harald Weltec91085e2022-02-10 18:05:45 +0100362 # EF.IST
363 (res, sw) = card.read_binary('6f07')
364 if sw == '9000':
365 print("ISIM Service Table: %s" % res)
366 # Print those which are available
367 print("%s" % dec_st(res, table="isim"))
368 else:
369 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
Supreeth Herleee15c772020-03-22 08:58:33 +0100370
Harald Weltec91085e2022-02-10 18:05:45 +0100371 # Done for this card and maybe for everything ?
372 print("Done !\n")