blob: 33e93a7ff19015d93f483b3111f3f50e473df542 [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
Supreeth Herled3b13d02020-04-20 13:30:34 +020037from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn, format_xplmn_w_act, dec_spn, dec_st
Alexander Chemeris6e589142013-07-04 17:34:06 +040038
39
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
75 if opts.pcsc_dev is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070076 print("Using PC/SC reader (dev=%d) interface"
77 % opts.pcsc_dev)
Alexander Chemeris6e589142013-07-04 17:34:06 +040078 from pySim.transport.pcsc import PcscSimLink
79 sl = PcscSimLink(opts.pcsc_dev)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070080 elif opts.osmocon_sock is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070081 print("Using Calypso-based (OsmocomBB, sock=%s) reader interface"
82 % opts.osmocon_sock)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070083 from pySim.transport.calypso import CalypsoSimLink
84 sl = CalypsoSimLink(sock_path=opts.osmocon_sock)
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070085 else: # Serial reader is default
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070086 print("Using serial reader (port=%s, baudrate=%d) interface"
87 % (opts.device, opts.baudrate))
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070088 from pySim.transport.serial import SerialSimLink
89 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
Alexander Chemeris6e589142013-07-04 17:34:06 +040090
91 # Create command layer
92 scc = SimCardCommands(transport=sl)
93
94 # Wait for SIM card
95 sl.wait_for_card()
96
Supreeth Herle3e6f16d2020-03-23 10:00:50 +010097 # Assuming UICC SIM
98 scc.cla_byte = "00"
99 scc.sel_ctrl = "0004"
100
101 # Testing for Classic SIM or UICC
102 (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
103 if sw == '6e00':
104 # Just a Classic SIM
105 scc.cla_byte = "a0"
106 scc.sel_ctrl = "0000"
107
Alexander Chemeris6e589142013-07-04 17:34:06 +0400108 # Program the card
109 print("Reading ...")
110
Supreeth Herle4c306ab2020-03-18 11:38:00 +0100111 # Initialize Card object by auto detecting the card
112 card = card_detect("auto", scc) or Card(scc)
113
Supreeth Herle3bf43632020-03-20 20:20:27 +0100114 # Read all AIDs on the UICC
115 card.read_aids()
116
Alexander Chemeris6e589142013-07-04 17:34:06 +0400117 # EF.ICCID
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100118 (res, sw) = card.read_iccid()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400119 if sw == '9000':
Supreeth Herle3566b8e2020-03-18 12:02:34 +0100120 print("ICCID: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400121 else:
122 print("ICCID: Can't read, response code = %s" % (sw,))
123
124 # EF.IMSI
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100125 (res, sw) = card.read_imsi()
Alexander Chemeris6e589142013-07-04 17:34:06 +0400126 if sw == '9000':
Supreeth Herlef9762dc2020-03-18 12:05:06 +0100127 print("IMSI: %s" % (res,))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400128 else:
129 print("IMSI: Can't read, response code = %s" % (sw,))
130
Supreeth Herleab46d622020-03-05 15:30:22 +0100131 # EF.GID1
132 try:
Supreeth Herle98a69272020-03-18 12:14:48 +0100133 (res, sw) = card.read_gid1()
Supreeth Herleab46d622020-03-05 15:30:22 +0100134 if sw == '9000':
135 print("GID1: %s" % (res,))
136 else:
137 print("GID1: Can't read, response code = %s" % (sw,))
138 except Exception as e:
139 print("GID1: Can't read file -- %s" % (str(e),))
140
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100141 # EF.GID2
142 try:
Supreeth Herlee573ccb2020-04-01 09:21:20 +0200143 (res, sw) = card.read_binary('GID2')
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100144 if sw == '9000':
145 print("GID2: %s" % (res,))
146 else:
147 print("GID2: Can't read, response code = %s" % (sw,))
148 except Exception as e:
149 print("GID2: Can't read file -- %s" % (str(e),))
150
Alexander Chemeris6e589142013-07-04 17:34:06 +0400151 # EF.SMSP
Supreeth Herleebe6dba2020-03-19 12:08:20 +0100152 (res, sw) = card.read_record('SMSP', 1)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400153 if sw == '9000':
154 print("SMSP: %s" % (res,))
155 else:
156 print("SMSP: Can't read, response code = %s" % (sw,))
157
Supreeth Herlef8299452019-06-08 07:49:08 +0200158 # EF.SPN
159 try:
Supreeth Herle846cefb2020-03-19 12:11:25 +0100160 (res, sw) = card.read_spn()
Supreeth Herlef8299452019-06-08 07:49:08 +0200161 if sw == '9000':
Supreeth Herle846cefb2020-03-19 12:11:25 +0100162 print("SPN: %s" % (res[0] or "Not available"))
163 print("Display HPLMN: %s" % (res[1],))
164 print("Display OPLMN: %s" % (res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200165 else:
166 print("SPN: Can't read, response code = %s" % (sw,))
167 except Exception as e:
168 print("SPN: Can't read file -- %s" % (str(e),))
169
Philipp Maiera2650492018-07-11 23:05:58 +0200170 # EF.PLMNsel
171 try:
Supreeth Herle9efd8ef2020-03-19 12:14:10 +0100172 (res, sw) = card.read_binary('PLMNsel')
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200173 if sw == '9000':
174 print("PLMNsel: %s" % (res))
175 else:
176 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200177 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700178 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200179
180 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200181 try:
Supreeth Herle14084402020-03-19 12:42:10 +0100182 (res, sw) = card.read_plmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200183 if sw == '9000':
Supreeth Herle14084402020-03-19 12:42:10 +0100184 print("PLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200185 else:
186 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200187 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700188 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200189
190 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200191 try:
Supreeth Herle1757b262020-03-19 12:43:11 +0100192 (res, sw) = card.read_oplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200193 if sw == '9000':
Supreeth Herle1757b262020-03-19 12:43:11 +0100194 print("OPLMNwAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200195 else:
196 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200197 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700198 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200199
200 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200201 try:
Supreeth Herlea850a472020-03-19 12:44:11 +0100202 (res, sw) = card.read_hplmn_act()
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200203 if sw == '9000':
Supreeth Herlea850a472020-03-19 12:44:11 +0100204 print("HPLMNAcT:\n%s" % (res))
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200205 else:
206 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200207 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700208 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400209
210 # EF.ACC
Supreeth Herled1fb6fc2020-03-19 12:45:45 +0100211 (res, sw) = card.read_binary('ACC')
Alexander Chemeris6e589142013-07-04 17:34:06 +0400212 if sw == '9000':
213 print("ACC: %s" % (res,))
214 else:
215 print("ACC: Can't read, response code = %s" % (sw,))
216
217 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200218 try:
Supreeth Herle6d66af62020-03-19 12:49:16 +0100219 (res, sw) = card.read_msisdn()
Sylvain Munaut9f138972013-07-18 10:36:51 +0200220 if sw == '9000':
Supreeth Herle6d66af62020-03-19 12:49:16 +0100221 # (npi, ton, msisdn) = res
222 if res is not None:
223 print("MSISDN (NPI=%d ToN=%d): %s" % res)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200224 else:
225 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400226 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200227 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200228 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700229 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400230
Philipp Maieree908ae2019-03-21 16:21:12 +0100231 # EF.AD
Supreeth Herle52ef6752020-03-19 12:50:27 +0100232 (res, sw) = card.read_binary('AD')
Philipp Maieree908ae2019-03-21 16:21:12 +0100233 if sw == '9000':
234 print("AD: %s" % (res,))
235 else:
236 print("AD: Can't read, response code = %s" % (sw,))
237
Supreeth Herlee26331e2020-03-20 18:50:39 +0100238 # EF.SST
Supreeth Herled3b13d02020-04-20 13:30:34 +0200239 (res, sw) = card.read_binary('SST')
Supreeth Herlee26331e2020-03-20 18:50:39 +0100240 if sw == '9000':
Supreeth Herled3b13d02020-04-20 13:30:34 +0200241 print("SIM Service Table: %s" % res)
Supreeth Herlee26331e2020-03-20 18:50:39 +0100242 # Print those which are available
Supreeth Herled3b13d02020-04-20 13:30:34 +0200243 print("%s" % dec_st(res))
Supreeth Herlee26331e2020-03-20 18:50:39 +0100244 else:
245 print("SIM Service Table: Can't read, response code = %s" % (sw,))
246
Supreeth Herle96412992020-03-22 08:20:11 +0100247 # Check whether we have th AID of USIM, if so select it by its AID
248 # EF.UST - File Id in ADF USIM : 6f38
249 if '9000' == card.select_adf_by_aid():
250 # EF.UST
251 (res, sw) = card.read_binary('6f38')
252 if sw == '9000':
253 print("USIM Service Table: %s" % res)
254 # Print those which are available
255 print("%s" % dec_st(res, table="usim"))
256 else:
257 print("USIM Service Table: Can't read, response code = %s" % (sw,))
258
Supreeth Herleee15c772020-03-22 08:58:33 +0100259 # Check whether we have th AID of ISIM, if so select it by its AID
260 # EF.IST - File Id in ADF ISIM : 6f07
261 if '9000' == card.select_adf_by_aid(adf="isim"):
262 # EF.IST
263 (res, sw) = card.read_binary('6f07')
264 if sw == '9000':
265 print("ISIM Service Table: %s" % res)
266 # Print those which are available
267 print("%s" % dec_st(res, table="isim"))
268 else:
269 print("ISIM Service Table: Can't read, response code = %s" % (sw,))
270
Alexander Chemeris6e589142013-07-04 17:34:06 +0400271 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700272 print("Done !\n")