blob: e807e3e891e2b276ed046b9805e9a7be322b519a [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
40from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid
41
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 )
59
60 (options, args) = parser.parse_args()
61
62 if args:
63 parser.error("Extraneous arguments")
64
65 return options
66
67
68if __name__ == '__main__':
69
70 # Parse options
71 opts = parse_options()
72
73 # Connect to the card
74 if opts.pcsc_dev is None:
75 from pySim.transport.serial import SerialSimLink
76 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
77 else:
78 from pySim.transport.pcsc import PcscSimLink
79 sl = PcscSimLink(opts.pcsc_dev)
80
81 # Create command layer
82 scc = SimCardCommands(transport=sl)
83
84 # Wait for SIM card
85 sl.wait_for_card()
86
87 # Program the card
88 print("Reading ...")
89
90 # EF.ICCID
91 (res, sw) = scc.read_binary(['3f00', '2fe2'])
92 if sw == '9000':
93 print("ICCID: %s" % (dec_iccid(res),))
94 else:
95 print("ICCID: Can't read, response code = %s" % (sw,))
96
97 # EF.IMSI
98 (res, sw) = scc.read_binary(['3f00', '7f20', '6f07'])
99 if sw == '9000':
100 print("IMSI: %s" % (dec_imsi(res),))
101 else:
102 print("IMSI: Can't read, response code = %s" % (sw,))
103
104 # EF.SMSP
105 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
106 if sw == '9000':
107 print("SMSP: %s" % (res,))
108 else:
109 print("SMSP: Can't read, response code = %s" % (sw,))
110
Philipp Maiera2650492018-07-11 23:05:58 +0200111 # EF.PLMNsel
112 try:
113 (res, sw) = scc.read_binary(EF['PLMNsel'])
114 if sw == '9000':
115 print("PLMNsel: %s" % (res))
116 else:
117 print("PLMNsel: Can't read, response code = %s" % (sw,))
118 except Exception as e:
119 print "HPLMNAcT: Can't read file -- " + str(e)
120
121 # EF.PLMNwAcT
122 try:
123 (res, sw) = scc.read_binary(EF['PLMNwAcT'])
124 if sw == '9000':
125 print("PLMNwAcT: %s" % (res))
126 else:
127 print("PLMNwAcT: Can't read, response code = %s" % (sw,))
128 except Exception as e:
129 print "PLMNwAcT: Can't read file -- " + str(e)
130
131 # EF.OPLMNwAcT
132 try:
133 (res, sw) = scc.read_binary(EF['OPLMNwAcT'])
134 if sw == '9000':
135 print("OPLMNwAcT: %s" % (res))
136 else:
137 print("OPLMNwAcT: Can't read, response code = %s" % (sw,))
138 except Exception as e:
139 print "OPLMNwAcT: Can't read file -- " + str(e)
140
141 # EF.HPLMNAcT
142 try:
143 (res, sw) = scc.read_binary(EF['HPLMNAcT'])
144 if sw == '9000':
145 print("HPLMNAcT: %s" % (res))
146 else:
147 print("HPLMNAcT: Can't read, response code = %s" % (sw,))
148 except Exception as e:
149 print "HPLMNAcT: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400150
151 # EF.ACC
152 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
153 if sw == '9000':
154 print("ACC: %s" % (res,))
155 else:
156 print("ACC: Can't read, response code = %s" % (sw,))
157
158 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200159 try:
160 # print(scc.record_size(['3f00', '7f10', '6f40']))
161 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
162 if sw == '9000':
163 if res[1] != 'f':
164 print("MSISDN: %s" % (res,))
165 else:
166 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400167 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200168 print("MSISDN: Can't read, response code = %s" % (sw,))
Philipp Maierea6bdf02018-07-11 23:02:36 +0200169 except Exception as e:
170 print "MSISDN: Can't read file -- " + str(e)
Alexander Chemeris6e589142013-07-04 17:34:06 +0400171
172 # Done for this card and maybe for everything ?
173 print "Done !\n"