blob: 2c10866d8309ad6e511e2a9ca22aa773230c94cf [file] [log] [blame]
Alexander Chemeris6e589142013-07-04 17:34:06 +04001#!/usr/bin/env python
2
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
31
32try:
33 import json
34except ImportError:
35 # Python < 2.5
36 import simplejson as json
37
38from pySim.commands import SimCardCommands
39from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid
40
41
42def parse_options():
43
44 parser = OptionParser(usage="usage: %prog [options]")
45
46 parser.add_option("-d", "--device", dest="device", metavar="DEV",
47 help="Serial Device for SIM access [default: %default]",
48 default="/dev/ttyUSB0",
49 )
50 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
51 help="Baudrate used for SIM access [default: %default]",
52 default=9600,
53 )
54 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
55 help="Which PC/SC reader number for SIM access",
56 default=None,
57 )
58
59 (options, args) = parser.parse_args()
60
61 if args:
62 parser.error("Extraneous arguments")
63
64 return options
65
66
67if __name__ == '__main__':
68
69 # Parse options
70 opts = parse_options()
71
72 # Connect to the card
73 if opts.pcsc_dev is None:
74 from pySim.transport.serial import SerialSimLink
75 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
76 else:
77 from pySim.transport.pcsc import PcscSimLink
78 sl = PcscSimLink(opts.pcsc_dev)
79
80 # Create command layer
81 scc = SimCardCommands(transport=sl)
82
83 # Wait for SIM card
84 sl.wait_for_card()
85
86 # Program the card
87 print("Reading ...")
88
89 # EF.ICCID
90 (res, sw) = scc.read_binary(['3f00', '2fe2'])
91 if sw == '9000':
92 print("ICCID: %s" % (dec_iccid(res),))
93 else:
94 print("ICCID: Can't read, response code = %s" % (sw,))
95
96 # EF.IMSI
97 (res, sw) = scc.read_binary(['3f00', '7f20', '6f07'])
98 if sw == '9000':
99 print("IMSI: %s" % (dec_imsi(res),))
100 else:
101 print("IMSI: Can't read, response code = %s" % (sw,))
102
103 # EF.SMSP
104 (res, sw) = scc.read_record(['3f00', '7f10', '6f42'], 1)
105 if sw == '9000':
106 print("SMSP: %s" % (res,))
107 else:
108 print("SMSP: Can't read, response code = %s" % (sw,))
109
110 # EF.HPLMN
111# (res, sw) = scc.read_binary(['3f00', '7f20', '6f30'])
112# if sw == '9000':
113# print("HPLMN: %s" % (res))
114# print("HPLMN: %s" % (dec_hplmn(res),))
115# else:
116# print("HPLMN: Can't read, response code = %s" % (sw,))
117 # FIXME
118
119 # EF.ACC
120 (res, sw) = scc.read_binary(['3f00', '7f20', '6f78'])
121 if sw == '9000':
122 print("ACC: %s" % (res,))
123 else:
124 print("ACC: Can't read, response code = %s" % (sw,))
125
126 # EF.MSISDN
Sylvain Munaut9f138972013-07-18 10:36:51 +0200127 try:
128 # print(scc.record_size(['3f00', '7f10', '6f40']))
129 (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
130 if sw == '9000':
131 if res[1] != 'f':
132 print("MSISDN: %s" % (res,))
133 else:
134 print("MSISDN: Not available")
Alexander Chemeris6e589142013-07-04 17:34:06 +0400135 else:
Sylvain Munaut9f138972013-07-18 10:36:51 +0200136 print("MSISDN: Can't read, response code = %s" % (sw,))
137 except:
138 print "MSISDN: Can't read. Probably not existing file"
Alexander Chemeris6e589142013-07-04 17:34:06 +0400139
140 # Done for this card and maybe for everything ?
141 print "Done !\n"