blob: 55b43d7aeaebb94608b21741e80fcbdbee6905eb [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
Philipp Maiera2650492018-07-11 23:05:58 +020031from pySim.ts_51_011 import EF, DF
Alexander Chemeris6e589142013-07-04 17:34:06 +040032
Alexander Chemeris6e589142013-07-04 17:34:06 +040033from pySim.commands import SimCardCommands
Supreeth Herle4b1c7632019-12-22 09:00:59 +010034from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn, format_xplmn_w_act, dec_spn
Alexander Chemeris6e589142013-07-04 17:34:06 +040035
36
37def parse_options():
38
39 parser = OptionParser(usage="usage: %prog [options]")
40
41 parser.add_option("-d", "--device", dest="device", metavar="DEV",
42 help="Serial Device for SIM access [default: %default]",
43 default="/dev/ttyUSB0",
44 )
45 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
46 help="Baudrate used for SIM access [default: %default]",
47 default=9600,
48 )
49 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
50 help="Which PC/SC reader number for SIM access",
51 default=None,
52 )
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070053 parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
54 help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
55 default=None,
56 )
Alexander Chemeris6e589142013-07-04 17:34:06 +040057
58 (options, args) = parser.parse_args()
59
60 if args:
61 parser.error("Extraneous arguments")
62
63 return options
64
65
66if __name__ == '__main__':
67
68 # Parse options
69 opts = parse_options()
70
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070071 # Init card reader driver
72 if opts.pcsc_dev is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070073 print("Using PC/SC reader (dev=%d) interface"
74 % opts.pcsc_dev)
Alexander Chemeris6e589142013-07-04 17:34:06 +040075 from pySim.transport.pcsc import PcscSimLink
76 sl = PcscSimLink(opts.pcsc_dev)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070077 elif opts.osmocon_sock is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070078 print("Using Calypso-based (OsmocomBB, sock=%s) reader interface"
79 % opts.osmocon_sock)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070080 from pySim.transport.calypso import CalypsoSimLink
81 sl = CalypsoSimLink(sock_path=opts.osmocon_sock)
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070082 else: # Serial reader is default
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070083 print("Using serial reader (port=%s, baudrate=%d) interface"
84 % (opts.device, opts.baudrate))
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070085 from pySim.transport.serial import SerialSimLink
86 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
Alexander Chemeris6e589142013-07-04 17:34:06 +040087
88 # Create command layer
89 scc = SimCardCommands(transport=sl)
90
91 # Wait for SIM card
92 sl.wait_for_card()
93
94 # Program the card
95 print("Reading ...")
96
97 # EF.ICCID
Max89cfded2019-01-03 11:02:08 +010098 (res, sw) = scc.read_binary(EF['ICCID'])
Alexander Chemeris6e589142013-07-04 17:34:06 +040099 if sw == '9000':
100 print("ICCID: %s" % (dec_iccid(res),))
101 else:
102 print("ICCID: Can't read, response code = %s" % (sw,))
103
104 # EF.IMSI
105 (res, sw) = scc.read_binary(['3f00', '7f20', '6f07'])
106 if sw == '9000':
107 print("IMSI: %s" % (dec_imsi(res),))
108 else:
109 print("IMSI: Can't read, response code = %s" % (sw,))
110
Supreeth Herleab46d622020-03-05 15:30:22 +0100111 # EF.GID1
112 try:
113 (res, sw) = scc.read_binary(EF['GID1'])
114 if sw == '9000':
115 print("GID1: %s" % (res,))
116 else:
117 print("GID1: Can't read, response code = %s" % (sw,))
118 except Exception as e:
119 print("GID1: Can't read file -- %s" % (str(e),))
120
Supreeth Herle0e90e6c2020-03-05 15:33:00 +0100121 # EF.GID2
122 try:
123 (res, sw) = scc.read_binary(EF['GID2'])
124 if sw == '9000':
125 print("GID2: %s" % (res,))
126 else:
127 print("GID2: Can't read, response code = %s" % (sw,))
128 except Exception as e:
129 print("GID2: Can't read file -- %s" % (str(e),))
130
Alexander Chemeris6e589142013-07-04 17:34:06 +0400131 # EF.SMSP
132 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
133 if sw == '9000':
134 print("SMSP: %s" % (res,))
135 else:
136 print("SMSP: Can't read, response code = %s" % (sw,))
137
Supreeth Herlef8299452019-06-08 07:49:08 +0200138 # EF.SPN
139 try:
140 (res, sw) = scc.read_binary(EF['SPN'])
141 if sw == '9000':
142 spn_res = dec_spn(res)
Supreeth Herle6af4c212020-01-21 19:50:31 +0100143 print("SPN: %s" % (spn_res[0] or "Not available"))
144 print("Display HPLMN: %s" % (spn_res[1],))
145 print("Display OPLMN: %s" % (spn_res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200146 else:
147 print("SPN: Can't read, response code = %s" % (sw,))
148 except Exception as e:
149 print("SPN: Can't read file -- %s" % (str(e),))
150
Philipp Maiera2650492018-07-11 23:05:58 +0200151 # EF.PLMNsel
152 try:
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200153 (res, sw) = scc.read_binary(EF['PLMNsel'])
154 if sw == '9000':
155 print("PLMNsel: %s" % (res))
156 else:
157 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200158 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700159 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200160
161 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200162 try:
163 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
164 if sw == '9000':
165 print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
166 else:
167 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200168 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700169 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200170
171 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200172 try:
173 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
174 if sw == '9000':
175 print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
176 else:
177 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200178 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700179 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200180
181 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200182 try:
183 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
184 if sw == '9000':
185 print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res)))
186 else:
187 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200188 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700189 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400190
191 # EF.ACC
192 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
193 if sw == '9000':
194 print("ACC: %s" % (res,))
195 else:
196 print("ACC: Can't read, response code = %s" % (sw,))
197
198 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200199 try:
200 # print(scc.record_size(['3f00', '7f10', '6f40']))
201 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
202 if sw == '9000':
Supreeth Herle4b1c7632019-12-22 09:00:59 +0100203 res_dec = dec_msisdn(res)
204 if res_dec is not None:
205 # (npi, ton, msisdn) = res_dec
206 print("MSISDN (NPI=%d ToN=%d): %s" % res_dec)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200207 else:
208 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400209 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200210 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200211 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700212 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400213
Philipp Maieree908ae2019-03-21 16:21:12 +0100214 # EF.AD
215 (res, sw) = scc.read_binary(['3f00', '7f20', '6fad'])
216 if sw == '9000':
217 print("AD: %s" % (res,))
218 else:
219 print("AD: Can't read, response code = %s" % (sw,))
220
Alexander Chemeris6e589142013-07-04 17:34:06 +0400221 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700222 print("Done !\n")