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 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 33 | from pySim.commands import SimCardCommands |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 34 | from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn, format_xplmn_w_act, dec_spn |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def parse_options(): |
| 38 | |
| 39 | parser = OptionParser(usage="usage: %prog [options]") |
| 40 | |
| 41 | parser.add_option("-d", "--device", dest="device", metavar="DEV", |
| 42 | help="Serial Device for SIM access [default: %default]", |
| 43 | default="/dev/ttyUSB0", |
| 44 | ) |
| 45 | parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD", |
| 46 | help="Baudrate used for SIM access [default: %default]", |
| 47 | default=9600, |
| 48 | ) |
| 49 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC", |
| 50 | help="Which PC/SC reader number for SIM access", |
| 51 | default=None, |
| 52 | ) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 53 | parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH", |
| 54 | help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)", |
| 55 | default=None, |
| 56 | ) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 57 | |
| 58 | (options, args) = parser.parse_args() |
| 59 | |
| 60 | if args: |
| 61 | parser.error("Extraneous arguments") |
| 62 | |
| 63 | return options |
| 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | |
| 68 | # Parse options |
| 69 | opts = parse_options() |
| 70 | |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 71 | # Init card reader driver |
| 72 | if opts.pcsc_dev is not None: |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 73 | print("Using PC/SC reader (dev=%d) interface" |
| 74 | % opts.pcsc_dev) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 75 | from pySim.transport.pcsc import PcscSimLink |
| 76 | sl = PcscSimLink(opts.pcsc_dev) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 77 | elif opts.osmocon_sock is not None: |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 78 | print("Using Calypso-based (OsmocomBB, sock=%s) reader interface" |
| 79 | % opts.osmocon_sock) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 80 | from pySim.transport.calypso import CalypsoSimLink |
| 81 | sl = CalypsoSimLink(sock_path=opts.osmocon_sock) |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 82 | else: # Serial reader is default |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 83 | print("Using serial reader (port=%s, baudrate=%d) interface" |
| 84 | % (opts.device, opts.baudrate)) |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 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 |
Max | 89cfded | 2019-01-03 11:02:08 +0100 | [diff] [blame] | 98 | (res, sw) = scc.read_binary(EF['ICCID']) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 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 | |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 118 | # EF.SPN |
| 119 | try: |
| 120 | (res, sw) = scc.read_binary(EF['SPN']) |
| 121 | if sw == '9000': |
| 122 | spn_res = dec_spn(res) |
Supreeth Herle | 6af4c21 | 2020-01-21 19:50:31 +0100 | [diff] [blame] | 123 | print("SPN: %s" % (spn_res[0] or "Not available")) |
| 124 | print("Display HPLMN: %s" % (spn_res[1],)) |
| 125 | print("Display OPLMN: %s" % (spn_res[2],)) |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 126 | else: |
| 127 | print("SPN: Can't read, response code = %s" % (sw,)) |
| 128 | except Exception as e: |
| 129 | print("SPN: Can't read file -- %s" % (str(e),)) |
| 130 | |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 131 | # EF.PLMNsel |
| 132 | try: |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 133 | (res, sw) = scc.read_binary(EF['PLMNsel']) |
| 134 | if sw == '9000': |
| 135 | print("PLMNsel: %s" % (res)) |
| 136 | else: |
| 137 | print("PLMNsel: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 138 | except Exception as e: |
Vadim Yanitskiy | a3bb334 | 2020-01-25 12:45:37 +0700 | [diff] [blame] | 139 | print("PLMNsel: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 140 | |
| 141 | # EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 142 | try: |
| 143 | (res, sw) = scc.read_binary(EF['PLMNwAcT']) |
| 144 | if sw == '9000': |
| 145 | print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res))) |
| 146 | else: |
| 147 | print("PLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 148 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 149 | print("PLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 150 | |
| 151 | # EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 152 | try: |
| 153 | (res, sw) = scc.read_binary(EF['OPLMNwAcT']) |
| 154 | if sw == '9000': |
| 155 | print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res))) |
| 156 | else: |
| 157 | print("OPLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 158 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 159 | print("OPLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 160 | |
| 161 | # EF.HPLMNAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 162 | try: |
| 163 | (res, sw) = scc.read_binary(EF['HPLMNAcT']) |
| 164 | if sw == '9000': |
| 165 | print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res))) |
| 166 | else: |
| 167 | print("HPLMNAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 168 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 169 | print("HPLMNAcT: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 170 | |
| 171 | # EF.ACC |
| 172 | (res, sw) = scc.read_binary(['3f00', '7f20', '6f78']) |
| 173 | if sw == '9000': |
| 174 | print("ACC: %s" % (res,)) |
| 175 | else: |
| 176 | print("ACC: Can't read, response code = %s" % (sw,)) |
| 177 | |
| 178 | # EF.MSISDN |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 179 | try: |
| 180 | # print(scc.record_size(['3f00', '7f10', '6f40'])) |
| 181 | (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1) |
| 182 | if sw == '9000': |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 183 | res_dec = dec_msisdn(res) |
| 184 | if res_dec is not None: |
| 185 | # (npi, ton, msisdn) = res_dec |
| 186 | print("MSISDN (NPI=%d ToN=%d): %s" % res_dec) |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 187 | else: |
| 188 | print("MSISDN: Not available") |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 189 | else: |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 190 | print("MSISDN: Can't read, response code = %s" % (sw,)) |
Philipp Maier | ea6bdf0 | 2018-07-11 23:02:36 +0200 | [diff] [blame] | 191 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 192 | print("MSISDN: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 193 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 194 | # EF.AD |
| 195 | (res, sw) = scc.read_binary(['3f00', '7f20', '6fad']) |
| 196 | if sw == '9000': |
| 197 | print("AD: %s" % (res,)) |
| 198 | else: |
| 199 | print("AD: Can't read, response code = %s" % (sw,)) |
| 200 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 201 | # Done for this card and maybe for everything ? |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 202 | print("Done !\n") |