blob: d753c8bf0d2f18dcaf33831552057f1b84b39b51 [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
33try:
34 import json
35except ImportError:
36 # Python < 2.5
37 import simplejson as json
38
39from pySim.commands import SimCardCommands
Supreeth Herlef8299452019-06-08 07:49:08 +020040from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, format_xplmn_w_act, dec_spn
Alexander Chemeris6e589142013-07-04 17:34:06 +040041
42
43def parse_options():
44
45 parser = OptionParser(usage="usage: %prog [options]")
46
47 parser.add_option("-d", "--device", dest="device", metavar="DEV",
48 help="Serial Device for SIM access [default: %default]",
49 default="/dev/ttyUSB0",
50 )
51 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
52 help="Baudrate used for SIM access [default: %default]",
53 default=9600,
54 )
55 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
56 help="Which PC/SC reader number for SIM access",
57 default=None,
58 )
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070059 parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
60 help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
61 default=None,
62 )
Alexander Chemeris6e589142013-07-04 17:34:06 +040063
64 (options, args) = parser.parse_args()
65
66 if args:
67 parser.error("Extraneous arguments")
68
69 return options
70
71
72if __name__ == '__main__':
73
74 # Parse options
75 opts = parse_options()
76
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070077 # Init card reader driver
78 if opts.pcsc_dev is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070079 print("Using PC/SC reader (dev=%d) interface"
80 % opts.pcsc_dev)
Alexander Chemeris6e589142013-07-04 17:34:06 +040081 from pySim.transport.pcsc import PcscSimLink
82 sl = PcscSimLink(opts.pcsc_dev)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070083 elif opts.osmocon_sock is not None:
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070084 print("Using Calypso-based (OsmocomBB, sock=%s) reader interface"
85 % opts.osmocon_sock)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070086 from pySim.transport.calypso import CalypsoSimLink
87 sl = CalypsoSimLink(sock_path=opts.osmocon_sock)
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070088 else: # Serial reader is default
Vadim Yanitskiy35a96ed2018-10-29 02:02:14 +070089 print("Using serial reader (port=%s, baudrate=%d) interface"
90 % (opts.device, opts.baudrate))
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070091 from pySim.transport.serial import SerialSimLink
92 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
Alexander Chemeris6e589142013-07-04 17:34:06 +040093
94 # Create command layer
95 scc = SimCardCommands(transport=sl)
96
97 # Wait for SIM card
98 sl.wait_for_card()
99
100 # Program the card
101 print("Reading ...")
102
103 # EF.ICCID
Max89cfded2019-01-03 11:02:08 +0100104 (res, sw) = scc.read_binary(EF['ICCID'])
Alexander Chemeris6e589142013-07-04 17:34:06 +0400105 if sw == '9000':
106 print("ICCID: %s" % (dec_iccid(res),))
107 else:
108 print("ICCID: Can't read, response code = %s" % (sw,))
109
110 # EF.IMSI
111 (res, sw) = scc.read_binary(['3f00', '7f20', '6f07'])
112 if sw == '9000':
113 print("IMSI: %s" % (dec_imsi(res),))
114 else:
115 print("IMSI: Can't read, response code = %s" % (sw,))
116
117 # EF.SMSP
118 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
119 if sw == '9000':
120 print("SMSP: %s" % (res,))
121 else:
122 print("SMSP: Can't read, response code = %s" % (sw,))
123
Supreeth Herlef8299452019-06-08 07:49:08 +0200124 # EF.SPN
125 try:
126 (res, sw) = scc.read_binary(EF['SPN'])
127 if sw == '9000':
128 spn_res = dec_spn(res)
Supreeth Herle6af4c212020-01-21 19:50:31 +0100129 print("SPN: %s" % (spn_res[0] or "Not available"))
130 print("Display HPLMN: %s" % (spn_res[1],))
131 print("Display OPLMN: %s" % (spn_res[2],))
Supreeth Herlef8299452019-06-08 07:49:08 +0200132 else:
133 print("SPN: Can't read, response code = %s" % (sw,))
134 except Exception as e:
135 print("SPN: Can't read file -- %s" % (str(e),))
136
Philipp Maiera2650492018-07-11 23:05:58 +0200137 # EF.PLMNsel
138 try:
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200139 (res, sw) = scc.read_binary(EF['PLMNsel'])
140 if sw == '9000':
141 print("PLMNsel: %s" % (res))
142 else:
143 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200144 except Exception as e:
Vadim Yanitskiya3bb3342020-01-25 12:45:37 +0700145 print("PLMNsel: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200146
147 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200148 try:
149 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
150 if sw == '9000':
151 print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
152 else:
153 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200154 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700155 print("PLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200156
157 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200158 try:
159 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
160 if sw == '9000':
161 print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
162 else:
163 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200164 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700165 print("OPLMNwAcT: Can't read file -- " + str(e))
Philipp Maiera2650492018-07-11 23:05:58 +0200166
167 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200168 try:
169 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
170 if sw == '9000':
171 print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res)))
172 else:
173 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200174 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700175 print("HPLMNAcT: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400176
177 # EF.ACC
178 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
179 if sw == '9000':
180 print("ACC: %s" % (res,))
181 else:
182 print("ACC: Can't read, response code = %s" % (sw,))
183
184 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200185 try:
186 # print(scc.record_size(['3f00', '7f10', '6f40']))
187 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
188 if sw == '9000':
189 if res[1] != 'f':
190 print("MSISDN: %s" % (res,))
191 else:
192 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400193 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200194 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200195 except Exception as e:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700196 print("MSISDN: Can't read file -- " + str(e))
Alexander Chemeris6e589142013-07-04 17:34:06 +0400197
Philipp Maieree908ae2019-03-21 16:21:12 +0100198 # EF.AD
199 (res, sw) = scc.read_binary(['3f00', '7f20', '6fad'])
200 if sw == '9000':
201 print("AD: %s" % (res,))
202 else:
203 print("AD: Can't read, response code = %s" % (sw,))
204
Alexander Chemeris6e589142013-07-04 17:34:06 +0400205 # Done for this card and maybe for everything ?
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700206 print("Done !\n")