blob: 2be34bda0e044eef5600f83cc5132b1c0e733f91 [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
26from optparse import OptionParser
27import os
28import random
29import re
30import sys
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +070031from pySim.ts_51_011 import EF, DF, EF_SST_map, EF_AD_mode_map
Sebastian Viviani0dc8f692020-05-29 00:14:55 +010032from pySim.ts_31_102 import EF_UST_map, EF_USIM_ADF_map
Supreeth Herleee15c772020-03-22 08:58:33 +010033from pySim.ts_31_103 import EF_IST_map
Alexander Chemeris6e589142013-07-04 17:34:06 +040034
Alexander Chemeris6e589142013-07-04 17:34:06 +040035from pySim.commands import SimCardCommands
Supreeth Herle4c306ab2020-03-18 11:38:00 +010036from pySim.cards import card_detect, Card
Philipp Maierff84c232020-05-12 17:24:18 +020037from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn
Supreeth Herleb1634db2020-03-22 10:00:43 +010038from pySim.utils import format_xplmn_w_act, dec_spn, dec_st, init_reader, dec_epdgid
Supreeth Herle99d55552020-03-24 13:03:43 +010039from pySim.utils import h2s, format_ePDGSelection
Alexander Chemeris6e589142013-07-04 17:34:06 +040040
41def parse_options():
42
43 parser = OptionParser(usage="usage: %prog [options]")
44
45 parser.add_option("-d", "--device", dest="device", metavar="DEV",
46 help="Serial Device for SIM access [default: %default]",
47 default="/dev/ttyUSB0",
48 )
49 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
50 help="Baudrate used for SIM access [default: %default]",
51 default=9600,
52 )
53 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
54 help="Which PC/SC reader number for SIM access",
55 default=None,
56 )
Vadim Yanitskiy29ca8042020-05-09 21:23:37 +070057 parser.add_option("--modem-device", dest="modem_dev", metavar="DEV",
58 help="Serial port of modem for Generic SIM Access (3GPP TS 27.007)",
59 default=None,
60 )
61 parser.add_option("--modem-baud", dest="modem_baud", type="int", metavar="BAUD",
62 help="Baudrate used for modem's port [default: %default]",
63 default=115200,
64 )
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070065 parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
66 help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
67 default=None,
68 )
Alexander Chemeris6e589142013-07-04 17:34:06 +040069
70 (options, args) = parser.parse_args()
71
72 if args:
73 parser.error("Extraneous arguments")
74
75 return options
76
77
78if __name__ == '__main__':
79
80 # Parse options
81 opts = parse_options()
82
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070083 # Init card reader driver
Philipp Maierff84c232020-05-12 17:24:18 +020084 sl = init_reader(opts)
Alexander Chemeris6e589142013-07-04 17:34:06 +040085
86 # Create command layer
87 scc = SimCardCommands(transport=sl)
88
89 # Wait for SIM card
90 sl.wait_for_card()
91
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010092 # Assuming UICC SIM
93 scc.cla_byte = "00"
94 scc.sel_ctrl = "0004"
95
96 # Testing for Classic SIM or UICC
97 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
98 if sw == '6e00':
99 # Just a Classic SIM
100 scc.cla_byte = "a0"
101 scc.sel_ctrl = "0000"
102
Alexander Chemeris6e589142013-07-04 17:34:06 +0400103 # Program the card
104 print("Reading ...")
105
Supreeth Herle4c306ab2020-03-18 11:38:00 +0100106 # Initialize Card object by auto detecting the card
107 card = card_detect("auto", scc) or Card(scc)
108
Supreeth Herle3bf43632020-03-20 20:20:27 +0100109 # Read all AIDs on the UICC
110 card.read_aids()
111
Alexander Chemeris6e589142013-07-04 17:34:06 +0400112 # EF.ICCID
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100113 (res, sw) = card.read_iccid()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400114 if sw == '9000':
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100115 print("ICCID: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400116 else:
117 print("ICCID: Can't read, response code = %s" % (sw,))
118
119 # EF.IMSI
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100120 (res, sw) = card.read_imsi()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400121 if sw == '9000':
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100122 print("IMSI: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400123 else:
124 print("IMSI: Can't read, response code = %s" % (sw,))
125
Supreeth Herleab46d622020-03-05 15:30:22 +0100126 # EF.GID1
127 try:
Supreeth Herle98a69272020-03-18 12:14:48 +0100128 (res, sw) = card.read_gid1()
Supreeth Herleab46d622020-03-05 15:30:22 +0100129 if sw == '9000':
130 print("GID1: %s" % (res,))
131 else:
132 print("GID1: Can't read, response code = %s" % (sw,))
133 except Exception as e:
134 print("GID1: Can't read file -- %s" % (str(e),))
135
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100136 # EF.GID2
137 try:
Supreeth Herlee573ccb2020-04-01 09:21:20 +0200138 (res, sw) = card.read_binary('GID2')
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100139 if sw == '9000':
140 print("GID2: %s" % (res,))
141 else:
142 print("GID2: Can't read, response code = %s" % (sw,))
143 except Exception as e:
144 print("GID2: Can't read file -- %s" % (str(e),))
145
Alexander Chemeris6e589142013-07-04 17:34:06 +0400146 # EF.SMSP
Supreeth Herleebe6dba2020-03-19 12:08:20 +0100147 (res, sw) = card.read_record('SMSP', 1)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400148 if sw == '9000':
149 print("SMSP: %s" % (res,))
150 else:
151 print("SMSP: Can't read, response code = %s" % (sw,))
152
Supreeth Herlef8299452019-06-08 07:49:08 +0200153 # EF.SPN
154 try:
Supreeth Herle846cefb2020-03-19 12:11:25 +0100155 (res, sw) = card.read_spn()
Supreeth Herlef8299452019-06-08 07:49:08 +0200156 if sw == '9000':
Supreeth Herle846cefb2020-03-19 12:11:25 +0100157 print("SPN: %s" % (res[0] or "Not available"))
158 print("Display HPLMN: %s" % (res[1],))
159 print("Display OPLMN: %s" % (res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200160 else:
161 print("SPN: Can't read, response code = %s" % (sw,))
162 except Exception as e:
163 print("SPN: Can't read file -- %s" % (str(e),))
164
Philipp Maiera2650492018-07-11 23:05:58 +0200165 # EF.PLMNsel
166 try:
Supreeth Herle9efd8ef2020-03-19 12:14:10 +0100167 (res, sw) = card.read_binary('PLMNsel')
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200168 if sw == '9000':
169 print("PLMNsel: %s" % (res))
170 else:
171 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200172 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700173 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200174
175 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200176 try:
Supreeth Herle14084402020-03-19 12:42:10 +0100177 (res, sw) = card.read_plmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200178 if sw == '9000':
Supreeth Herle14084402020-03-19 12:42:10 +0100179 print("PLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200180 else:
181 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200182 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700183 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200184
185 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200186 try:
Supreeth Herle1757b262020-03-19 12:43:11 +0100187 (res, sw) = card.read_oplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200188 if sw == '9000':
Supreeth Herle1757b262020-03-19 12:43:11 +0100189 print("OPLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200190 else:
191 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200192 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700193 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200194
195 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200196 try:
Supreeth Herlea850a472020-03-19 12:44:11 +0100197 (res, sw) = card.read_hplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200198 if sw == '9000':
Supreeth Herlea850a472020-03-19 12:44:11 +0100199 print("HPLMNAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200200 else:
201 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200202 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700203 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400204
205 # EF.ACC
Supreeth Herled1fb6fc2020-03-19 12:45:45 +0100206 (res, sw) = card.read_binary('ACC')
Alexander Chemeris6e589142013-07-04 17:34:06 +0400207 if sw == '9000':
208 print("ACC: %s" % (res,))
209 else:
210 print("ACC: Can't read, response code = %s" % (sw,))
211
212 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200213 try:
Supreeth Herle6d66af62020-03-19 12:49:16 +0100214 (res, sw) = card.read_msisdn()
Sylvain Munaut9f138972013-07-18 10:36:51 +0200215 if sw == '9000':
Supreeth Herle6d66af62020-03-19 12:49:16 +0100216 # (npi, ton, msisdn) = res
217 if res is not None:
218 print("MSISDN (NPI=%d ToN=%d): %s" % res)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200219 else:
220 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400221 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200222 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200223 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700224 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400225
Philipp Maieree908ae2019-03-21 16:21:12 +0100226 # EF.AD
Supreeth Herle52ef6752020-03-19 12:50:27 +0100227 (res, sw) = card.read_binary('AD')
Philipp Maieree908ae2019-03-21 16:21:12 +0100228 if sw == '9000':
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700229 print("Administrative data: %s" % (res,))
230 if res[:2] in EF_AD_mode_map:
231 print("\tMS operation mode: %s" % (EF_AD_mode_map[res[:2]],))
232 else:
233 print("\tMS operation mode: (unknown 0x%s)" % (res[:2],))
234 if int(res[4:6], 16) & 0x01:
235 print("\tCiphering Indicator: enabled")
236 else:
237 print("\tCiphering Indicator: disabled")
Philipp Maieree908ae2019-03-21 16:21:12 +0100238 else:
239 print("AD: Can't read, response code = %s" % (sw,))
240
Supreeth Herlee26331e2020-03-20 18:50:39 +0100241 # EF.SST
Supreeth Herled3b13d02020-04-20 13:30:34 +0200242 (res, sw) = card.read_binary('SST')
Supreeth Herlee26331e2020-03-20 18:50:39 +0100243 if sw == '9000':
Supreeth Herled3b13d02020-04-20 13:30:34 +0200244 print("SIM Service Table: %s" % res)
Supreeth Herlee26331e2020-03-20 18:50:39 +0100245 # Print those which are available
Supreeth Herled3b13d02020-04-20 13:30:34 +0200246 print("%s" % dec_st(res))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100247 else:
248 print("SIM Service Table: Can't read, response code = %s" % (sw,))
249
Supreeth Herle96412992020-03-22 08:20:11 +0100250 # Check whether we have th AID of USIM, if so select it by its AID
251 # EF.UST - File Id in ADF USIM : 6f38
252 if '9000' == card.select_adf_by_aid():
Harald Welteca673942020-06-03 15:19:40 +0200253 # EF.EHPLMN
254 if card.file_exists(EF_USIM_ADF_map['EHPLMN']):
255 (res, sw) = card.read_ehplmn()
256 if sw == '9000':
257 print("EHPLMN:\n%s" % (res))
258 else:
259 print("EHPLMN: Can't read, response code = %s" % (sw,))
herlesupreeth4a3580b2020-09-29 10:11:36 +0200260
Supreeth Herle96412992020-03-22 08:20:11 +0100261 # EF.UST
herlesupreeth4a3580b2020-09-29 10:11:36 +0200262 try:
263 if card.file_exists(EF_USIM_ADF_map['UST']):
264 # res[0] - EF content of UST
265 # res[1] - Human readable format of services marked available in UST
266 (res, sw) = card.read_ust()
267 if sw == '9000':
268 print("USIM Service Table: %s" % res[0])
269 print("%s" % res[1])
270 else:
271 print("USIM Service Table: Can't read, response code = %s" % (sw,))
272 except Exception as e:
273 print("USIM Service Table: Can't read file -- " + str(e))
Supreeth Herle96412992020-03-22 08:20:11 +0100274
Supreeth Herleb1634db2020-03-22 10:00:43 +0100275 #EF.ePDGId - Home ePDG Identifier
276 try:
herlesupreethf8232db2020-09-29 10:03:06 +0200277 if card.file_exists(EF_USIM_ADF_map['ePDGId']):
278 (res, sw) = card.read_epdgid()
279 if sw == '9000':
280 print("ePDGId:\n%s" % (len(res) and res or '\tNot available\n',))
281 else:
282 print("ePDGId: Can't read, response code = %s" % (sw,))
Supreeth Herleb1634db2020-03-22 10:00:43 +0100283 except Exception as e:
284 print("ePDGId: Can't read file -- " + str(e))
285
Supreeth Herle99d55552020-03-24 13:03:43 +0100286 #EF.ePDGSelection - ePDG Selection Information
287 try:
288 if card.file_exists(EF_USIM_ADF_map['ePDGSelection']):
289 (res, sw) = card.read_ePDGSelection()
290 if sw == '9000':
291 print("ePDGSelection:\n%s" % (res,))
292 else:
293 print("ePDGSelection: Can't read, response code = %s" % (sw,))
294 except Exception as e:
295 print("ePDGSelection: Can't read file -- " + str(e))
296
Supreeth Herleee15c772020-03-22 08:58:33 +0100297 # Check whether we have th AID of ISIM, if so select it by its AID
298 # EF.IST - File Id in ADF ISIM : 6f07
299 if '9000' == card.select_adf_by_aid(adf="isim"):
300 # EF.IST
301 (res, sw) = card.read_binary('6f07')
302 if sw == '9000':
303 print("ISIM Service Table: %s" % res)
304 # Print those which are available
305 print("%s" % dec_st(res, table="isim"))
306 else:
307 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
308
Alexander Chemeris6e589142013-07-04 17:34:06 +0400309 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700310 print("Done !\n")