blob: 16607cfeb6d8c88dae5e9748c02d86bc5ef191bd [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:
Alexander Chemeris6e589142013-07-04 17:34:06 +040079 from pySim.transport.pcsc import PcscSimLink
80 sl = PcscSimLink(opts.pcsc_dev)
Vadim Yanitskiy9f9f5a62018-10-27 02:10:34 +070081 elif opts.osmocon_sock is not None:
82 from pySim.transport.calypso import CalypsoSimLink
83 sl = CalypsoSimLink(sock_path=opts.osmocon_sock)
Vadim Yanitskiy588f3ac2018-10-27 06:30:33 +070084 else: # Serial reader is default
85 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
98 (res, sw) = scc.read_binary(['3f00', '2fe2'])
99 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
111 # EF.SMSP
112 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
113 if sw == '9000':
114 print("SMSP: %s" % (res,))
115 else:
116 print("SMSP: Can't read, response code = %s" % (sw,))
117
Philipp Maiera2650492018-07-11 23:05:58 +0200118 # EF.PLMNsel
119 try:
120 (res, sw) = scc.read_binary(EF['PLMNsel'])
121 if sw == '9000':
122 print("PLMNsel: %s" % (res))
123 else:
124 print("PLMNsel: Can't read, response code = %s" % (sw,))
125 except Exception as e:
126 print "HPLMNAcT: Can't read file -- " + str(e)
127
128 # EF.PLMNwAcT
129 try:
130 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
131 if sw == '9000':
Daniel Laszlo Sitzer851e9c02018-12-04 19:40:08 +0100132 print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
Philipp Maiera2650492018-07-11 23:05:58 +0200133 else:
134 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
135 except Exception as e:
136 print "PLMNwAcT: Can't read file -- " + str(e)
137
138 # EF.OPLMNwAcT
139 try:
140 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
141 if sw == '9000':
Daniel Laszlo Sitzer851e9c02018-12-04 19:40:08 +0100142 print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res)))
Philipp Maiera2650492018-07-11 23:05:58 +0200143 else:
144 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
145 except Exception as e:
146 print "OPLMNwAcT: Can't read file -- " + str(e)
147
148 # EF.HPLMNAcT
149 try:
150 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
151 if sw == '9000':
Daniel Laszlo Sitzer851e9c02018-12-04 19:40:08 +0100152 print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res)))
Philipp Maiera2650492018-07-11 23:05:58 +0200153 else:
154 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
155 except Exception as e:
156 print "HPLMNAcT: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400157
158 # EF.ACC
159 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
160 if sw == '9000':
161 print("ACC: %s" % (res,))
162 else:
163 print("ACC: Can't read, response code = %s" % (sw,))
164
165 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200166 try:
167 # print(scc.record_size(['3f00', '7f10', '6f40']))
168 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
169 if sw == '9000':
170 if res[1] != 'f':
171 print("MSISDN: %s" % (res,))
172 else:
173 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400174 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200175 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200176 except Exception as e:
177 print "MSISDN: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400178
Philipp Maieree908ae2019-03-21 16:21:12 +0100179 # EF.AD
180 (res, sw) = scc.read_binary(['3f00', '7f20', '6fad'])
181 if sw == '9000':
182 print("AD: %s" % (res,))
183 else:
184 print("AD: Can't read, response code = %s" % (sw,))
185
Alexander Chemeris6e589142013-07-04 17:34:06 +0400186 # Done for this card and maybe for everything ?
187 print "Done !\n"