Pau Espin Pedrol | ac23ad5 | 2017-12-29 20:30:35 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 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 | |
| 25 | import hashlib |
| 26 | from optparse import OptionParser |
| 27 | import os |
| 28 | import random |
| 29 | import re |
| 30 | import sys |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 31 | from pySim.ts_51_011 import EF, DF |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 32 | |
| 33 | try: |
| 34 | import json |
| 35 | except ImportError: |
| 36 | # Python < 2.5 |
| 37 | import simplejson as json |
| 38 | |
| 39 | from pySim.commands import SimCardCommands |
| 40 | from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid |
| 41 | |
| 42 | |
| 43 | def 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 Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 59 | 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 Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 63 | |
| 64 | (options, args) = parser.parse_args() |
| 65 | |
| 66 | if args: |
| 67 | parser.error("Extraneous arguments") |
| 68 | |
| 69 | return options |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | |
| 74 | # Parse options |
| 75 | opts = parse_options() |
| 76 | |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 77 | # Init card reader driver |
| 78 | if opts.pcsc_dev is not None: |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 79 | from pySim.transport.pcsc import PcscSimLink |
| 80 | sl = PcscSimLink(opts.pcsc_dev) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 81 | elif opts.osmocon_sock is not None: |
| 82 | from pySim.transport.calypso import CalypsoSimLink |
| 83 | sl = CalypsoSimLink(sock_path=opts.osmocon_sock) |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 84 | else: # Serial reader is default |
| 85 | from pySim.transport.serial import SerialSimLink |
| 86 | sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 87 | |
| 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 Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 118 | # 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': |
| 132 | print("PLMNwAcT: %s" % (res)) |
| 133 | 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': |
| 142 | print("OPLMNwAcT: %s" % (res)) |
| 143 | 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': |
| 152 | print("HPLMNAcT: %s" % (res)) |
| 153 | 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 Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 157 | |
| 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 Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 166 | 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 Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 174 | else: |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 175 | print("MSISDN: Can't read, response code = %s" % (sw,)) |
Philipp Maier | ea6bdf0 | 2018-07-11 23:02:36 +0200 | [diff] [blame] | 176 | except Exception as e: |
| 177 | print "MSISDN: Can't read file -- " + str(e) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 178 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 179 | # 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 Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 186 | # Done for this card and maybe for everything ? |
| 187 | print "Done !\n" |