blob: 4bfa900eaf74a48aaf829f56af3979e9c966716e [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
Daniel Laszlo Sitzer851e9c02018-12-04 19:40:08 +010040from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, format_xplmn_w_act
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
Philipp Maiera2650492018-07-11 23:05:58 +0200124 # EF.PLMNsel
125 try:
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200126 (res, sw) = scc.read_binary(EF['PLMNsel'])
127 if sw == '9000':
128 print("PLMNsel: %s" % (res))
129 else:
130 print("PLMNsel: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200131 except Exception as e:
132 print "HPLMNAcT: Can't read file -- " + str(e)
133
134 # EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200135 try:
136 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
137 if sw == '9000':
138 print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
139 else:
140 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200141 except Exception as e:
142 print "PLMNwAcT: Can't read file -- " + str(e)
143
144 # EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200145 try:
146 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
147 if sw == '9000':
148 print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
149 else:
150 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200151 except Exception as e:
152 print "OPLMNwAcT: Can't read file -- " + str(e)
153
154 # EF.HPLMNAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200155 try:
156 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
157 if sw == '9000':
158 print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res)))
159 else:
160 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
Philipp Maiera2650492018-07-11 23:05:58 +0200161 except Exception as e:
162 print "HPLMNAcT: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400163
164 # EF.ACC
165 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
166 if sw == '9000':
167 print("ACC: %s" % (res,))
168 else:
169 print("ACC: Can't read, response code = %s" % (sw,))
170
171 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200172 try:
173 # print(scc.record_size(['3f00', '7f10', '6f40']))
174 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
175 if sw == '9000':
176 if res[1] != 'f':
177 print("MSISDN: %s" % (res,))
178 else:
179 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400180 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200181 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200182 except Exception as e:
183 print "MSISDN: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400184
Philipp Maieree908ae2019-03-21 16:21:12 +0100185 # EF.AD
186 (res, sw) = scc.read_binary(['3f00', '7f20', '6fad'])
187 if sw == '9000':
188 print("AD: %s" % (res,))
189 else:
190 print("AD: Can't read, response code = %s" % (sw,))
191
Alexander Chemeris6e589142013-07-04 17:34:06 +0400192 # Done for this card and maybe for everything ?
193 print "Done !\n"