blob: 00194b4e3ad2d225e73a91a6d23e79903788e930 [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 Maierbb73e512021-05-05 16:14:00 +020037from pySim.cards import card_detect, SimCard, UsimCard, IsimCard
Philipp Maierff84c232020-05-12 17:24:18 +020038from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn
Philipp Maiere2c59a82021-04-30 14:57:41 +020039from pySim.utils import format_xplmn_w_act, dec_st
Supreeth Herle99d55552020-03-24 13:03:43 +010040from pySim.utils import h2s, format_ePDGSelection
Alexander Chemeris6e589142013-07-04 17:34:06 +040041
Harald Welte8fe1d202021-04-11 12:31:40 +020042option_parser = argparse.ArgumentParser(prog='pySim-read',
43 description='Legacy tool for reading some parts of a SIM card',
44 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
45argparse_add_reader_args(option_parser)
Alexander Chemeris6e589142013-07-04 17:34:06 +040046
47if __name__ == '__main__':
48
49 # Parse options
Harald Welte8fe1d202021-04-11 12:31:40 +020050 opts = option_parser.parse_args()
Alexander Chemeris6e589142013-07-04 17:34:06 +040051
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070052 # Init card reader driver
Philipp Maierff84c232020-05-12 17:24:18 +020053 sl = init_reader(opts)
Philipp Maierc8caec22021-02-22 16:07:53 +010054 if sl is None:
55 exit(1)
Alexander Chemeris6e589142013-07-04 17:34:06 +040056
57 # Create command layer
58 scc = SimCardCommands(transport=sl)
59
60 # Wait for SIM card
61 sl.wait_for_card()
62
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010063 # Assuming UICC SIM
64 scc.cla_byte = "00"
65 scc.sel_ctrl = "0004"
66
67 # Testing for Classic SIM or UICC
68 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
69 if sw == '6e00':
70 # Just a Classic SIM
71 scc.cla_byte = "a0"
72 scc.sel_ctrl = "0000"
73
Philipp Maier4210a702021-05-05 14:17:59 +020074 # Read the card
Alexander Chemeris6e589142013-07-04 17:34:06 +040075 print("Reading ...")
76
Supreeth Herle4c306ab2020-03-18 11:38:00 +010077 # Initialize Card object by auto detecting the card
Philipp Maierbb73e512021-05-05 16:14:00 +020078 card = card_detect("auto", scc) or SimCard(scc)
Supreeth Herle4c306ab2020-03-18 11:38:00 +010079
Supreeth Herle3bf43632020-03-20 20:20:27 +010080 # Read all AIDs on the UICC
81 card.read_aids()
82
Alexander Chemeris6e589142013-07-04 17:34:06 +040083 # EF.ICCID
Supreeth Herle3566b8e2020-03-18 12:02:34 +010084 (res, sw) = card.read_iccid()
Alexander Chemeris6e589142013-07-04 17:34:06 +040085 if sw == '9000':
Supreeth Herle3566b8e2020-03-18 12:02:34 +010086 print("ICCID: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +040087 else:
88 print("ICCID: Can't read, response code = %s" % (sw,))
89
90 # EF.IMSI
Supreeth Herlef9762dc2020-03-18 12:05:06 +010091 (res, sw) = card.read_imsi()
Alexander Chemeris6e589142013-07-04 17:34:06 +040092 if sw == '9000':
Supreeth Herlef9762dc2020-03-18 12:05:06 +010093 print("IMSI: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +040094 else:
95 print("IMSI: Can't read, response code = %s" % (sw,))
96
Supreeth Herleab46d622020-03-05 15:30:22 +010097 # EF.GID1
98 try:
Supreeth Herle98a69272020-03-18 12:14:48 +010099 (res, sw) = card.read_gid1()
Supreeth Herleab46d622020-03-05 15:30:22 +0100100 if sw == '9000':
101 print("GID1: %s" % (res,))
102 else:
103 print("GID1: Can't read, response code = %s" % (sw,))
104 except Exception as e:
105 print("GID1: Can't read file -- %s" % (str(e),))
106
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100107 # EF.GID2
108 try:
Supreeth Herlee573ccb2020-04-01 09:21:20 +0200109 (res, sw) = card.read_binary('GID2')
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100110 if sw == '9000':
111 print("GID2: %s" % (res,))
112 else:
113 print("GID2: Can't read, response code = %s" % (sw,))
114 except Exception as e:
115 print("GID2: Can't read file -- %s" % (str(e),))
116
Alexander Chemeris6e589142013-07-04 17:34:06 +0400117 # EF.SMSP
Supreeth Herleebe6dba2020-03-19 12:08:20 +0100118 (res, sw) = card.read_record('SMSP', 1)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400119 if sw == '9000':
120 print("SMSP: %s" % (res,))
121 else:
122 print("SMSP: Can't read, response code = %s" % (sw,))
123
Supreeth Herlef8299452019-06-08 07:49:08 +0200124 # EF.SPN
125 try:
Supreeth Herle846cefb2020-03-19 12:11:25 +0100126 (res, sw) = card.read_spn()
Supreeth Herlef8299452019-06-08 07:49:08 +0200127 if sw == '9000':
Supreeth Herle846cefb2020-03-19 12:11:25 +0100128 print("SPN: %s" % (res[0] or "Not available"))
Robert Falkenberg5933c3b2021-05-17 18:24:23 +0200129 print("Show in HPLMN: %s" % (res[1],))
130 print("Hide in OPLMN: %s" % (res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200131 else:
132 print("SPN: Can't read, response code = %s" % (sw,))
133 except Exception as e:
134 print("SPN: Can't read file -- %s" % (str(e),))
135
Philipp Maiera2650492018-07-11 23:05:58 +0200136 # EF.PLMNsel
137 try:
Supreeth Herle9efd8ef2020-03-19 12:14:10 +0100138 (res, sw) = card.read_binary('PLMNsel')
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200139 if sw == '9000':
140 print("PLMNsel: %s" % (res))
141 else:
142 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200143 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700144 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200145
146 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200147 try:
Supreeth Herle14084402020-03-19 12:42:10 +0100148 (res, sw) = card.read_plmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200149 if sw == '9000':
Supreeth Herle14084402020-03-19 12:42:10 +0100150 print("PLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200151 else:
152 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200153 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700154 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200155
156 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200157 try:
Supreeth Herle1757b262020-03-19 12:43:11 +0100158 (res, sw) = card.read_oplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200159 if sw == '9000':
Supreeth Herle1757b262020-03-19 12:43:11 +0100160 print("OPLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200161 else:
162 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200163 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700164 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200165
166 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200167 try:
Supreeth Herlea850a472020-03-19 12:44:11 +0100168 (res, sw) = card.read_hplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200169 if sw == '9000':
Supreeth Herlea850a472020-03-19 12:44:11 +0100170 print("HPLMNAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200171 else:
172 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200173 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700174 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400175
176 # EF.ACC
Supreeth Herled1fb6fc2020-03-19 12:45:45 +0100177 (res, sw) = card.read_binary('ACC')
Alexander Chemeris6e589142013-07-04 17:34:06 +0400178 if sw == '9000':
179 print("ACC: %s" % (res,))
180 else:
181 print("ACC: Can't read, response code = %s" % (sw,))
182
183 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200184 try:
Supreeth Herle6d66af62020-03-19 12:49:16 +0100185 (res, sw) = card.read_msisdn()
Sylvain Munaut9f138972013-07-18 10:36:51 +0200186 if sw == '9000':
Supreeth Herle6d66af62020-03-19 12:49:16 +0100187 # (npi, ton, msisdn) = res
188 if res is not None:
189 print("MSISDN (NPI=%d ToN=%d): %s" % res)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200190 else:
191 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400192 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200193 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200194 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700195 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400196
Philipp Maieree908ae2019-03-21 16:21:12 +0100197 # EF.AD
Supreeth Herle52ef6752020-03-19 12:50:27 +0100198 (res, sw) = card.read_binary('AD')
Philipp Maieree908ae2019-03-21 16:21:12 +0100199 if sw == '9000':
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700200 print("Administrative data: %s" % (res,))
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200201 ad = EF_AD()
202 decoded_data = ad.decode_hex(res)
203 print("\tMS operation mode: %s" % decoded_data['ms_operation_mode'])
204 if decoded_data['ofm']:
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700205 print("\tCiphering Indicator: enabled")
206 else:
207 print("\tCiphering Indicator: disabled")
Philipp Maieree908ae2019-03-21 16:21:12 +0100208 else:
209 print("AD: Can't read, response code = %s" % (sw,))
210
Supreeth Herlee26331e2020-03-20 18:50:39 +0100211 # EF.SST
Supreeth Herled3b13d02020-04-20 13:30:34 +0200212 (res, sw) = card.read_binary('SST')
Supreeth Herlee26331e2020-03-20 18:50:39 +0100213 if sw == '9000':
Supreeth Herled3b13d02020-04-20 13:30:34 +0200214 print("SIM Service Table: %s" % res)
Supreeth Herlee26331e2020-03-20 18:50:39 +0100215 # Print those which are available
Supreeth Herled3b13d02020-04-20 13:30:34 +0200216 print("%s" % dec_st(res))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100217 else:
218 print("SIM Service Table: Can't read, response code = %s" % (sw,))
219
Supreeth Herle96412992020-03-22 08:20:11 +0100220 # Check whether we have th AID of USIM, if so select it by its AID
221 # EF.UST - File Id in ADF USIM : 6f38
Philipp Maiercba6dbc2021-03-11 13:03:18 +0100222 data, sw = card.select_adf_by_aid(adf="usim")
223 if sw == '9000':
herlesupreethcebf8b12021-01-21 05:57:06 +0100224 # Select USIM profile
225 usim_card = UsimCard(scc)
226
Harald Welteca673942020-06-03 15:19:40 +0200227 # EF.EHPLMN
herlesupreethcebf8b12021-01-21 05:57:06 +0100228 if usim_card.file_exists(EF_USIM_ADF_map['EHPLMN']):
229 (res, sw) = usim_card.read_ehplmn()
Harald Welteca673942020-06-03 15:19:40 +0200230 if sw == '9000':
231 print("EHPLMN:\n%s" % (res))
232 else:
233 print("EHPLMN: Can't read, response code = %s" % (sw,))
herlesupreeth4a3580b2020-09-29 10:11:36 +0200234
Supreeth Herle96412992020-03-22 08:20:11 +0100235 # EF.UST
herlesupreeth4a3580b2020-09-29 10:11:36 +0200236 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100237 if usim_card.file_exists(EF_USIM_ADF_map['UST']):
herlesupreeth4a3580b2020-09-29 10:11:36 +0200238 # res[0] - EF content of UST
239 # res[1] - Human readable format of services marked available in UST
herlesupreethcebf8b12021-01-21 05:57:06 +0100240 (res, sw) = usim_card.read_ust()
herlesupreeth4a3580b2020-09-29 10:11:36 +0200241 if sw == '9000':
242 print("USIM Service Table: %s" % res[0])
243 print("%s" % res[1])
244 else:
245 print("USIM Service Table: Can't read, response code = %s" % (sw,))
246 except Exception as e:
247 print("USIM Service Table: Can't read file -- " + str(e))
Supreeth Herle96412992020-03-22 08:20:11 +0100248
Supreeth Herleb1634db2020-03-22 10:00:43 +0100249 #EF.ePDGId - Home ePDG Identifier
250 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100251 if usim_card.file_exists(EF_USIM_ADF_map['ePDGId']):
252 (res, sw) = usim_card.read_epdgid()
herlesupreethf8232db2020-09-29 10:03:06 +0200253 if sw == '9000':
254 print("ePDGId:\n%s" % (len(res) and res or '\tNot available\n',))
255 else:
256 print("ePDGId: Can't read, response code = %s" % (sw,))
Supreeth Herleb1634db2020-03-22 10:00:43 +0100257 except Exception as e:
258 print("ePDGId: Can't read file -- " + str(e))
259
Supreeth Herle99d55552020-03-24 13:03:43 +0100260 #EF.ePDGSelection - ePDG Selection Information
261 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100262 if usim_card.file_exists(EF_USIM_ADF_map['ePDGSelection']):
263 (res, sw) = usim_card.read_ePDGSelection()
Supreeth Herle99d55552020-03-24 13:03:43 +0100264 if sw == '9000':
265 print("ePDGSelection:\n%s" % (res,))
266 else:
267 print("ePDGSelection: Can't read, response code = %s" % (sw,))
268 except Exception as e:
269 print("ePDGSelection: Can't read file -- " + str(e))
270
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100271 # Select ISIM application by its AID
Philipp Maiercba6dbc2021-03-11 13:03:18 +0100272 data, sw = card.select_adf_by_aid(adf="isim")
273 if sw == '9000':
herlesupreethcebf8b12021-01-21 05:57:06 +0100274 # Select USIM profile
275 isim_card = IsimCard(scc)
276
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100277 #EF.P-CSCF - P-CSCF Address
278 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100279 if isim_card.file_exists(EF_ISIM_ADF_map['PCSCF']):
280 res = isim_card.read_pcscf()
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100281 print("P-CSCF:\n%s" % (len(res) and res or '\tNot available\n',))
282 except Exception as e:
283 print("P-CSCF: Can't read file -- " + str(e))
284
Supreeth Herle05b28072020-03-25 10:23:48 +0100285 # EF.DOMAIN - Home Network Domain Name e.g. ims.mncXXX.mccXXX.3gppnetwork.org
286 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100287 if isim_card.file_exists(EF_ISIM_ADF_map['DOMAIN']):
288 (res, sw) = isim_card.read_domain()
Supreeth Herle05b28072020-03-25 10:23:48 +0100289 if sw == '9000':
290 print("Home Network Domain Name: %s" % (len(res) and res or 'Not available',))
291 else:
292 print("Home Network Domain Name: Can't read, response code = %s" % (sw,))
293 except Exception as e:
294 print("Home Network Domain Name: Can't read file -- " + str(e))
295
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100296 # EF.IMPI - IMS private user identity
297 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100298 if isim_card.file_exists(EF_ISIM_ADF_map['IMPI']):
299 (res, sw) = isim_card.read_impi()
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100300 if sw == '9000':
301 print("IMS private user identity: %s" % (len(res) and res or 'Not available',))
302 else:
303 print("IMS private user identity: Can't read, response code = %s" % (sw,))
304 except Exception as e:
305 print("IMS private user identity: Can't read file -- " + str(e))
306
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100307 # EF.IMPU - IMS public user identity
308 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100309 if isim_card.file_exists(EF_ISIM_ADF_map['IMPU']):
310 res = isim_card.read_impu()
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100311 print("IMS public user identity:\n%s" % (len(res) and res or '\tNot available\n',))
312 except Exception as e:
313 print("IMS public user identity: Can't read file -- " + str(e))
314
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200315 # EF.UICCIARI - UICC IARI
316 try:
herlesupreethcebf8b12021-01-21 05:57:06 +0100317 if isim_card.file_exists(EF_ISIM_ADF_map['UICCIARI']):
318 res = isim_card.read_iari()
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200319 print("UICC IARI:\n%s" % (len(res) and res or '\tNot available\n',))
320 except Exception as e:
321 print("UICC IARI: Can't read file -- " + str(e))
322
Supreeth Herleee15c772020-03-22 08:58:33 +0100323 # Check whether we have th AID of ISIM, if so select it by its AID
324 # EF.IST - File Id in ADF ISIM : 6f07
Philipp Maiercba6dbc2021-03-11 13:03:18 +0100325 data, sw = card.select_adf_by_aid(adf="isim")
326 if sw == '9000':
Supreeth Herleee15c772020-03-22 08:58:33 +0100327 # EF.IST
328 (res, sw) = card.read_binary('6f07')
329 if sw == '9000':
330 print("ISIM Service Table: %s" % res)
331 # Print those which are available
332 print("%s" % dec_st(res, table="isim"))
333 else:
334 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
335
Alexander Chemeris6e589142013-07-04 17:34:06 +0400336 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700337 print("Done !\n")