blob: 44e7dae6c5234c1006017749659ae0fd17c1a12b [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
Alexander Chemeris6e589142013-07-04 17:34:06 +0400121 # EF.SMSP
122 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
123 if sw == '9000':
124 print("SMSP: %s" % (res,))
125 else:
126 print("SMSP: Can't read, response code = %s" % (sw,))
127
Supreeth Herlef8299452019-06-08 07:49:08 +0200128 # EF.SPN
129 try:
130 (res, sw) = scc.read_binary(EF['SPN'])
131 if sw == '9000':
132 spn_res = dec_spn(res)
Supreeth Herle6af4c212020-01-21 19:50:31 +0100133 print("SPN: %s" % (spn_res[0] or "Not available"))
134 print("Display HPLMN: %s" % (spn_res[1],))
135 print("Display OPLMN: %s" % (spn_res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200136 else:
137 print("SPN: Can't read, response code = %s" % (sw,))
138 except Exception as e:
139 print("SPN: Can't read file -- %s" % (str(e),))
140
Philipp Maiera2650492018-07-11 23:05:58 +0200141 # EF.PLMNsel
142 try:
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200143 (res, sw) = scc.read_binary(EF['PLMNsel'])
144 if sw == '9000':
145 print("PLMNsel: %s" % (res))
146 else:
147 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200148 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700149 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200150
151 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200152 try:
153 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
154 if sw == '9000':
155 print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
156 else:
157 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200158 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700159 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200160
161 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200162 try:
163 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
164 if sw == '9000':
165 print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
166 else:
167 print("OPLMNwAcT: 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("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200170
171 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200172 try:
173 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
174 if sw == '9000':
175 print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res)))
176 else:
177 print("HPLMNAcT: 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("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400180
181 # EF.ACC
182 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
183 if sw == '9000':
184 print("ACC: %s" % (res,))
185 else:
186 print("ACC: Can't read, response code = %s" % (sw,))
187
188 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200189 try:
190 # print(scc.record_size(['3f00', '7f10', '6f40']))
191 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
192 if sw == '9000':
Supreeth Herle4b1c7632019-12-22 09:00:59 +0100193 res_dec = dec_msisdn(res)
194 if res_dec is not None:
195 # (npi, ton, msisdn) = res_dec
196 print("MSISDN (NPI=%d ToN=%d): %s" % res_dec)
Sylvain Munaut9f138972013-07-18 10:36:51 +0200197 else:
198 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400199 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200200 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200201 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700202 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400203
Philipp Maieree908ae2019-03-21 16:21:12 +0100204 # EF.AD
205 (res, sw) = scc.read_binary(['3f00', '7f20', '6fad'])
206 if sw == '9000':
207 print("AD: %s" % (res,))
208 else:
209 print("AD: Can't read, response code = %s" % (sw,))
210
Alexander Chemeris6e589142013-07-04 17:34:06 +0400211 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700212 print("Done !\n")