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 | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 34 | from pySim.cards import card_detect, Card |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 35 | 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] | 36 | |
| 37 | |
| 38 | def parse_options(): |
| 39 | |
| 40 | parser = OptionParser(usage="usage: %prog [options]") |
| 41 | |
| 42 | parser.add_option("-d", "--device", dest="device", metavar="DEV", |
| 43 | help="Serial Device for SIM access [default: %default]", |
| 44 | default="/dev/ttyUSB0", |
| 45 | ) |
| 46 | parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD", |
| 47 | help="Baudrate used for SIM access [default: %default]", |
| 48 | default=9600, |
| 49 | ) |
| 50 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC", |
| 51 | help="Which PC/SC reader number for SIM access", |
| 52 | default=None, |
| 53 | ) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 54 | parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH", |
| 55 | help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)", |
| 56 | default=None, |
| 57 | ) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 58 | |
| 59 | (options, args) = parser.parse_args() |
| 60 | |
| 61 | if args: |
| 62 | parser.error("Extraneous arguments") |
| 63 | |
| 64 | return options |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | |
| 69 | # Parse options |
| 70 | opts = parse_options() |
| 71 | |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 72 | # Init card reader driver |
| 73 | if opts.pcsc_dev is not None: |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 74 | print("Using PC/SC reader (dev=%d) interface" |
| 75 | % opts.pcsc_dev) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 76 | from pySim.transport.pcsc import PcscSimLink |
| 77 | sl = PcscSimLink(opts.pcsc_dev) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 78 | elif opts.osmocon_sock is not None: |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 79 | print("Using Calypso-based (OsmocomBB, sock=%s) reader interface" |
| 80 | % opts.osmocon_sock) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 81 | from pySim.transport.calypso import CalypsoSimLink |
| 82 | sl = CalypsoSimLink(sock_path=opts.osmocon_sock) |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 83 | else: # Serial reader is default |
Vadim Yanitskiy | 35a96ed | 2018-10-29 02:02:14 +0700 | [diff] [blame] | 84 | print("Using serial reader (port=%s, baudrate=%d) interface" |
| 85 | % (opts.device, opts.baudrate)) |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 86 | from pySim.transport.serial import SerialSimLink |
| 87 | sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 88 | |
| 89 | # Create command layer |
| 90 | scc = SimCardCommands(transport=sl) |
| 91 | |
| 92 | # Wait for SIM card |
| 93 | sl.wait_for_card() |
| 94 | |
| 95 | # Program the card |
| 96 | print("Reading ...") |
| 97 | |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 98 | # Initialize Card object by auto detecting the card |
| 99 | card = card_detect("auto", scc) or Card(scc) |
| 100 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 101 | # EF.ICCID |
Supreeth Herle | 3566b8e | 2020-03-18 12:02:34 +0100 | [diff] [blame] | 102 | (res, sw) = card.read_iccid() |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 103 | if sw == '9000': |
Supreeth Herle | 3566b8e | 2020-03-18 12:02:34 +0100 | [diff] [blame] | 104 | print("ICCID: %s" % (res,)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 105 | else: |
| 106 | print("ICCID: Can't read, response code = %s" % (sw,)) |
| 107 | |
| 108 | # EF.IMSI |
Supreeth Herle | f9762dc | 2020-03-18 12:05:06 +0100 | [diff] [blame] | 109 | (res, sw) = card.read_imsi() |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 110 | if sw == '9000': |
Supreeth Herle | f9762dc | 2020-03-18 12:05:06 +0100 | [diff] [blame] | 111 | print("IMSI: %s" % (res,)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 112 | else: |
| 113 | print("IMSI: Can't read, response code = %s" % (sw,)) |
| 114 | |
Supreeth Herle | ab46d62 | 2020-03-05 15:30:22 +0100 | [diff] [blame] | 115 | # EF.GID1 |
| 116 | try: |
Supreeth Herle | 98a6927 | 2020-03-18 12:14:48 +0100 | [diff] [blame] | 117 | (res, sw) = card.read_gid1() |
Supreeth Herle | ab46d62 | 2020-03-05 15:30:22 +0100 | [diff] [blame] | 118 | if sw == '9000': |
| 119 | print("GID1: %s" % (res,)) |
| 120 | else: |
| 121 | print("GID1: Can't read, response code = %s" % (sw,)) |
| 122 | except Exception as e: |
| 123 | print("GID1: Can't read file -- %s" % (str(e),)) |
| 124 | |
Supreeth Herle | 0e90e6c | 2020-03-05 15:33:00 +0100 | [diff] [blame] | 125 | # EF.GID2 |
| 126 | try: |
Supreeth Herle | e573ccb | 2020-04-01 09:21:20 +0200 | [diff] [blame] | 127 | (res, sw) = card.read_binary('GID2') |
Supreeth Herle | 0e90e6c | 2020-03-05 15:33:00 +0100 | [diff] [blame] | 128 | if sw == '9000': |
| 129 | print("GID2: %s" % (res,)) |
| 130 | else: |
| 131 | print("GID2: Can't read, response code = %s" % (sw,)) |
| 132 | except Exception as e: |
| 133 | print("GID2: Can't read file -- %s" % (str(e),)) |
| 134 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 135 | # EF.SMSP |
Supreeth Herle | ebe6dba | 2020-03-19 12:08:20 +0100 | [diff] [blame^] | 136 | (res, sw) = card.read_record('SMSP', 1) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 137 | if sw == '9000': |
| 138 | print("SMSP: %s" % (res,)) |
| 139 | else: |
| 140 | print("SMSP: Can't read, response code = %s" % (sw,)) |
| 141 | |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 142 | # EF.SPN |
| 143 | try: |
| 144 | (res, sw) = scc.read_binary(EF['SPN']) |
| 145 | if sw == '9000': |
| 146 | spn_res = dec_spn(res) |
Supreeth Herle | 6af4c21 | 2020-01-21 19:50:31 +0100 | [diff] [blame] | 147 | print("SPN: %s" % (spn_res[0] or "Not available")) |
| 148 | print("Display HPLMN: %s" % (spn_res[1],)) |
| 149 | print("Display OPLMN: %s" % (spn_res[2],)) |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 150 | else: |
| 151 | print("SPN: Can't read, response code = %s" % (sw,)) |
| 152 | except Exception as e: |
| 153 | print("SPN: Can't read file -- %s" % (str(e),)) |
| 154 | |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 155 | # EF.PLMNsel |
| 156 | try: |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 157 | (res, sw) = scc.read_binary(EF['PLMNsel']) |
| 158 | if sw == '9000': |
| 159 | print("PLMNsel: %s" % (res)) |
| 160 | else: |
| 161 | print("PLMNsel: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 162 | except Exception as e: |
Vadim Yanitskiy | a3bb334 | 2020-01-25 12:45:37 +0700 | [diff] [blame] | 163 | print("PLMNsel: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 164 | |
| 165 | # EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 166 | try: |
| 167 | (res, sw) = scc.read_binary(EF['PLMNwAcT']) |
| 168 | if sw == '9000': |
| 169 | print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res))) |
| 170 | else: |
| 171 | print("PLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 172 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 173 | print("PLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 174 | |
| 175 | # EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 176 | try: |
| 177 | (res, sw) = scc.read_binary(EF['OPLMNwAcT']) |
| 178 | if sw == '9000': |
| 179 | print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res))) |
| 180 | else: |
| 181 | print("OPLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 182 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 183 | print("OPLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 184 | |
| 185 | # EF.HPLMNAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 186 | try: |
| 187 | (res, sw) = scc.read_binary(EF['HPLMNAcT']) |
| 188 | if sw == '9000': |
| 189 | print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res))) |
| 190 | else: |
| 191 | print("HPLMNAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 192 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 193 | print("HPLMNAcT: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 194 | |
| 195 | # EF.ACC |
| 196 | (res, sw) = scc.read_binary(['3f00', '7f20', '6f78']) |
| 197 | if sw == '9000': |
| 198 | print("ACC: %s" % (res,)) |
| 199 | else: |
| 200 | print("ACC: Can't read, response code = %s" % (sw,)) |
| 201 | |
| 202 | # EF.MSISDN |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 203 | try: |
| 204 | # print(scc.record_size(['3f00', '7f10', '6f40'])) |
| 205 | (res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1) |
| 206 | if sw == '9000': |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 207 | res_dec = dec_msisdn(res) |
| 208 | if res_dec is not None: |
| 209 | # (npi, ton, msisdn) = res_dec |
| 210 | print("MSISDN (NPI=%d ToN=%d): %s" % res_dec) |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 211 | else: |
| 212 | print("MSISDN: Not available") |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 213 | else: |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 214 | print("MSISDN: Can't read, response code = %s" % (sw,)) |
Philipp Maier | ea6bdf0 | 2018-07-11 23:02:36 +0200 | [diff] [blame] | 215 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 216 | print("MSISDN: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 217 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 218 | # EF.AD |
| 219 | (res, sw) = scc.read_binary(['3f00', '7f20', '6fad']) |
| 220 | if sw == '9000': |
| 221 | print("AD: %s" % (res,)) |
| 222 | else: |
| 223 | print("AD: Can't read, response code = %s" % (sw,)) |
| 224 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 225 | # Done for this card and maybe for everything ? |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 226 | print("Done !\n") |