blob: df21531f30d80a32317ed4dcdd9fefef054e7766 [file] [log] [blame]
Pau Espin Pedrolac23ad52017-12-29 20:30:35 +01001#!/usr/bin/env python2
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
Supreeth Herlee26331e2020-03-20 18:50:39 +010031from pySim.ts_51_011 import EF, DF, EF_SST_map
Supreeth Herle96412992020-03-22 08:20:11 +010032from pySim.ts_31_102 import EF_UST_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
38from pySim.utils import format_xplmn_w_act, dec_spn, dec_st, init_reader
Alexander Chemeris6e589142013-07-04 17:34:06 +040039
40def parse_options():
41
42 parser = OptionParser(usage="usage: %prog [options]")
43
44 parser.add_option("-d", "--device", dest="device", metavar="DEV",
45 help="Serial Device for SIM access [default: %default]",
46 default="/dev/ttyUSB0",
47 )
48 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
49 help="Baudrate used for SIM access [default: %default]",
50 default=9600,
51 )
52 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
53 help="Which PC/SC reader number for SIM access",
54 default=None,
55 )
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070056 parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
57 help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
58 default=None,
59 )
Alexander Chemeris6e589142013-07-04 17:34:06 +040060
61 (options, args) = parser.parse_args()
62
63 if args:
64 parser.error("Extraneous arguments")
65
66 return options
67
68
69if __name__ == '__main__':
70
71 # Parse options
72 opts = parse_options()
73
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070074 # Init card reader driver
Philipp Maierff84c232020-05-12 17:24:18 +020075 sl = init_reader(opts)
Alexander Chemeris6e589142013-07-04 17:34:06 +040076
77 # Create command layer
78 scc = SimCardCommands(transport=sl)
79
80 # Wait for SIM card
81 sl.wait_for_card()
82
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010083 # Assuming UICC SIM
84 scc.cla_byte = "00"
85 scc.sel_ctrl = "0004"
86
87 # Testing for Classic SIM or UICC
88 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
89 if sw == '6e00':
90 # Just a Classic SIM
91 scc.cla_byte = "a0"
92 scc.sel_ctrl = "0000"
93
Alexander Chemeris6e589142013-07-04 17:34:06 +040094 # Program the card
95 print("Reading ...")
96
Supreeth Herle4c306ab2020-03-18 11:38:00 +010097 # Initialize Card object by auto detecting the card
98 card = card_detect("auto", scc) or Card(scc)
99
Supreeth Herle3bf43632020-03-20 20:20:27 +0100100 # Read all AIDs on the UICC
101 card.read_aids()
102
Alexander Chemeris6e589142013-07-04 17:34:06 +0400103 # EF.ICCID
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100104 (res, sw) = card.read_iccid()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400105 if sw == '9000':
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100106 print("ICCID: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400107 else:
108 print("ICCID: Can't read, response code = %s" % (sw,))
109
110 # EF.IMSI
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100111 (res, sw) = card.read_imsi()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400112 if sw == '9000':
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100113 print("IMSI: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400114 else:
115 print("IMSI: Can't read, response code = %s" % (sw,))
116
Supreeth Herleab46d622020-03-05 15:30:22 +0100117 # EF.GID1
118 try:
Supreeth Herle98a69272020-03-18 12:14:48 +0100119 (res, sw) = card.read_gid1()
Supreeth Herleab46d622020-03-05 15:30:22 +0100120 if sw == '9000':
121 print("GID1: %s" % (res,))
122 else:
123 print("GID1: Can't read, response code = %s" % (sw,))
124 except Exception as e:
125 print("GID1: Can't read file -- %s" % (str(e),))
126
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100127 # EF.GID2
128 try:
Supreeth Herlee573ccb2020-04-01 09:21:20 +0200129 (res, sw) = card.read_binary('GID2')
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100130 if sw == '9000':
131 print("GID2: %s" % (res,))
132 else:
133 print("GID2: Can't read, response code = %s" % (sw,))
134 except Exception as e:
135 print("GID2: Can't read file -- %s" % (str(e),))
136
Alexander Chemeris6e589142013-07-04 17:34:06 +0400137 # EF.SMSP
Supreeth Herleebe6dba2020-03-19 12:08:20 +0100138 (res, sw) = card.read_record('SMSP', 1)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400139 if sw == '9000':
140 print("SMSP: %s" % (res,))
141 else:
142 print("SMSP: Can't read, response code = %s" % (sw,))
143
Supreeth Herlef8299452019-06-08 07:49:08 +0200144 # EF.SPN
145 try:
Supreeth Herle846cefb2020-03-19 12:11:25 +0100146 (res, sw) = card.read_spn()
Supreeth Herlef8299452019-06-08 07:49:08 +0200147 if sw == '9000':
Supreeth Herle846cefb2020-03-19 12:11:25 +0100148 print("SPN: %s" % (res[0] or "Not available"))
149 print("Display HPLMN: %s" % (res[1],))
150 print("Display OPLMN: %s" % (res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200151 else:
152 print("SPN: Can't read, response code = %s" % (sw,))
153 except Exception as e:
154 print("SPN: Can't read file -- %s" % (str(e),))
155
Philipp Maiera2650492018-07-11 23:05:58 +0200156 # EF.PLMNsel
157 try:
Supreeth Herle9efd8ef2020-03-19 12:14:10 +0100158 (res, sw) = card.read_binary('PLMNsel')
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200159 if sw == '9000':
160 print("PLMNsel: %s" % (res))
161 else:
162 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200163 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700164 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200165
166 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200167 try:
Supreeth Herle14084402020-03-19 12:42:10 +0100168 (res, sw) = card.read_plmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200169 if sw == '9000':
Supreeth Herle14084402020-03-19 12:42:10 +0100170 print("PLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200171 else:
172 print("PLMNwAcT: 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("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200175
176 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200177 try:
Supreeth Herle1757b262020-03-19 12:43:11 +0100178 (res, sw) = card.read_oplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200179 if sw == '9000':
Supreeth Herle1757b262020-03-19 12:43:11 +0100180 print("OPLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200181 else:
182 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200183 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700184 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200185
186 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200187 try:
Supreeth Herlea850a472020-03-19 12:44:11 +0100188 (res, sw) = card.read_hplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200189 if sw == '9000':
Supreeth Herlea850a472020-03-19 12:44:11 +0100190 print("HPLMNAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200191 else:
192 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200193 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700194 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400195
196 # EF.ACC
Supreeth Herled1fb6fc2020-03-19 12:45:45 +0100197 (res, sw) = card.read_binary('ACC')
Alexander Chemeris6e589142013-07-04 17:34:06 +0400198 if sw == '9000':
199 print("ACC: %s" % (res,))
200 else:
201 print("ACC: Can't read, response code = %s" % (sw,))
202
203 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200204 try:
Supreeth Herle6d66af62020-03-19 12:49:16 +0100205 (res, sw) = card.read_msisdn()
Sylvain Munaut9f138972013-07-18 10:36:51 +0200206 if sw == '9000':
Supreeth Herle6d66af62020-03-19 12:49:16 +0100207 # (npi, ton, msisdn) = res
208 if res is not None:
209 print("MSISDN (NPI=%d ToN=%d): %s" % res)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200210 else:
211 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400212 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200213 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200214 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700215 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400216
Philipp Maieree908ae2019-03-21 16:21:12 +0100217 # EF.AD
Supreeth Herle52ef6752020-03-19 12:50:27 +0100218 (res, sw) = card.read_binary('AD')
Philipp Maieree908ae2019-03-21 16:21:12 +0100219 if sw == '9000':
220 print("AD: %s" % (res,))
221 else:
222 print("AD: Can't read, response code = %s" % (sw,))
223
Supreeth Herlee26331e2020-03-20 18:50:39 +0100224 # EF.SST
Supreeth Herled3b13d02020-04-20 13:30:34 +0200225 (res, sw) = card.read_binary('SST')
Supreeth Herlee26331e2020-03-20 18:50:39 +0100226 if sw == '9000':
Supreeth Herled3b13d02020-04-20 13:30:34 +0200227 print("SIM Service Table: %s" % res)
Supreeth Herlee26331e2020-03-20 18:50:39 +0100228 # Print those which are available
Supreeth Herled3b13d02020-04-20 13:30:34 +0200229 print("%s" % dec_st(res))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100230 else:
231 print("SIM Service Table: Can't read, response code = %s" % (sw,))
232
Supreeth Herle96412992020-03-22 08:20:11 +0100233 # Check whether we have th AID of USIM, if so select it by its AID
234 # EF.UST - File Id in ADF USIM : 6f38
235 if '9000' == card.select_adf_by_aid():
236 # EF.UST
237 (res, sw) = card.read_binary('6f38')
238 if sw == '9000':
239 print("USIM Service Table: %s" % res)
240 # Print those which are available
241 print("%s" % dec_st(res, table="usim"))
242 else:
243 print("USIM Service Table: Can't read, response code = %s" % (sw,))
244
Supreeth Herleee15c772020-03-22 08:58:33 +0100245 # Check whether we have th AID of ISIM, if so select it by its AID
246 # EF.IST - File Id in ADF ISIM : 6f07
247 if '9000' == card.select_adf_by_aid(adf="isim"):
248 # EF.IST
249 (res, sw) = card.read_binary('6f07')
250 if sw == '9000':
251 print("ISIM Service Table: %s" % res)
252 # Print those which are available
253 print("%s" % dec_st(res, table="isim"))
254 else:
255 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
256
Alexander Chemeris6e589142013-07-04 17:34:06 +0400257 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700258 print("Done !\n")