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 |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 31 | from pySim.ts_51_011 import EF, DF, EF_SST_map |
Supreeth Herle | 9641299 | 2020-03-22 08:20:11 +0100 | [diff] [blame] | 32 | from pySim.ts_31_102 import EF_UST_map |
Supreeth Herle | ee15c77 | 2020-03-22 08:58:33 +0100 | [diff] [blame] | 33 | from pySim.ts_31_103 import EF_IST_map |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 34 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 35 | from pySim.commands import SimCardCommands |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 36 | from pySim.cards import card_detect, Card |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 37 | from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn |
| 38 | from pySim.utils import format_xplmn_w_act, dec_spn, dec_st, init_reader |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 39 | |
| 40 | def parse_options(): |
| 41 | |
| 42 | parser = OptionParser(usage="usage: %prog [options]") |
| 43 | |
| 44 | parser.add_option("-d", "--device", dest="device", metavar="DEV", |
| 45 | help="Serial Device for SIM access [default: %default]", |
| 46 | default="/dev/ttyUSB0", |
| 47 | ) |
| 48 | parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD", |
| 49 | help="Baudrate used for SIM access [default: %default]", |
| 50 | default=9600, |
| 51 | ) |
| 52 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC", |
| 53 | help="Which PC/SC reader number for SIM access", |
| 54 | default=None, |
| 55 | ) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 56 | parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH", |
| 57 | help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)", |
| 58 | default=None, |
| 59 | ) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 60 | |
| 61 | (options, args) = parser.parse_args() |
| 62 | |
| 63 | if args: |
| 64 | parser.error("Extraneous arguments") |
| 65 | |
| 66 | return options |
| 67 | |
| 68 | |
| 69 | if __name__ == '__main__': |
| 70 | |
| 71 | # Parse options |
| 72 | opts = parse_options() |
| 73 | |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 74 | # Init card reader driver |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 75 | sl = init_reader(opts) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 76 | |
| 77 | # Create command layer |
| 78 | scc = SimCardCommands(transport=sl) |
| 79 | |
| 80 | # Wait for SIM card |
| 81 | sl.wait_for_card() |
| 82 | |
Supreeth Herle | 3e6f16d | 2020-03-23 10:00:50 +0100 | [diff] [blame] | 83 | # Assuming UICC SIM |
| 84 | scc.cla_byte = "00" |
| 85 | scc.sel_ctrl = "0004" |
| 86 | |
| 87 | # Testing for Classic SIM or UICC |
| 88 | (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00") |
| 89 | if sw == '6e00': |
| 90 | # Just a Classic SIM |
| 91 | scc.cla_byte = "a0" |
| 92 | scc.sel_ctrl = "0000" |
| 93 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 94 | # Program the card |
| 95 | print("Reading ...") |
| 96 | |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 97 | # Initialize Card object by auto detecting the card |
| 98 | card = card_detect("auto", scc) or Card(scc) |
| 99 | |
Supreeth Herle | 3bf4363 | 2020-03-20 20:20:27 +0100 | [diff] [blame] | 100 | # Read all AIDs on the UICC |
| 101 | card.read_aids() |
| 102 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 103 | # EF.ICCID |
Supreeth Herle | 3566b8e | 2020-03-18 12:02:34 +0100 | [diff] [blame] | 104 | (res, sw) = card.read_iccid() |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 105 | if sw == '9000': |
Supreeth Herle | 3566b8e | 2020-03-18 12:02:34 +0100 | [diff] [blame] | 106 | print("ICCID: %s" % (res,)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 107 | else: |
| 108 | print("ICCID: Can't read, response code = %s" % (sw,)) |
| 109 | |
| 110 | # EF.IMSI |
Supreeth Herle | f9762dc | 2020-03-18 12:05:06 +0100 | [diff] [blame] | 111 | (res, sw) = card.read_imsi() |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 112 | if sw == '9000': |
Supreeth Herle | f9762dc | 2020-03-18 12:05:06 +0100 | [diff] [blame] | 113 | print("IMSI: %s" % (res,)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 114 | else: |
| 115 | print("IMSI: Can't read, response code = %s" % (sw,)) |
| 116 | |
Supreeth Herle | ab46d62 | 2020-03-05 15:30:22 +0100 | [diff] [blame] | 117 | # EF.GID1 |
| 118 | try: |
Supreeth Herle | 98a6927 | 2020-03-18 12:14:48 +0100 | [diff] [blame] | 119 | (res, sw) = card.read_gid1() |
Supreeth Herle | ab46d62 | 2020-03-05 15:30:22 +0100 | [diff] [blame] | 120 | if sw == '9000': |
| 121 | print("GID1: %s" % (res,)) |
| 122 | else: |
| 123 | print("GID1: Can't read, response code = %s" % (sw,)) |
| 124 | except Exception as e: |
| 125 | print("GID1: Can't read file -- %s" % (str(e),)) |
| 126 | |
Supreeth Herle | 0e90e6c | 2020-03-05 15:33:00 +0100 | [diff] [blame] | 127 | # EF.GID2 |
| 128 | try: |
Supreeth Herle | e573ccb | 2020-04-01 09:21:20 +0200 | [diff] [blame] | 129 | (res, sw) = card.read_binary('GID2') |
Supreeth Herle | 0e90e6c | 2020-03-05 15:33:00 +0100 | [diff] [blame] | 130 | if sw == '9000': |
| 131 | print("GID2: %s" % (res,)) |
| 132 | else: |
| 133 | print("GID2: Can't read, response code = %s" % (sw,)) |
| 134 | except Exception as e: |
| 135 | print("GID2: Can't read file -- %s" % (str(e),)) |
| 136 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 137 | # EF.SMSP |
Supreeth Herle | ebe6dba | 2020-03-19 12:08:20 +0100 | [diff] [blame] | 138 | (res, sw) = card.read_record('SMSP', 1) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 139 | if sw == '9000': |
| 140 | print("SMSP: %s" % (res,)) |
| 141 | else: |
| 142 | print("SMSP: Can't read, response code = %s" % (sw,)) |
| 143 | |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 144 | # EF.SPN |
| 145 | try: |
Supreeth Herle | 846cefb | 2020-03-19 12:11:25 +0100 | [diff] [blame] | 146 | (res, sw) = card.read_spn() |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 147 | if sw == '9000': |
Supreeth Herle | 846cefb | 2020-03-19 12:11:25 +0100 | [diff] [blame] | 148 | print("SPN: %s" % (res[0] or "Not available")) |
| 149 | print("Display HPLMN: %s" % (res[1],)) |
| 150 | print("Display OPLMN: %s" % (res[2],)) |
Supreeth Herle | f829945 | 2019-06-08 07:49:08 +0200 | [diff] [blame] | 151 | else: |
| 152 | print("SPN: Can't read, response code = %s" % (sw,)) |
| 153 | except Exception as e: |
| 154 | print("SPN: Can't read file -- %s" % (str(e),)) |
| 155 | |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 156 | # EF.PLMNsel |
| 157 | try: |
Supreeth Herle | 9efd8ef | 2020-03-19 12:14:10 +0100 | [diff] [blame] | 158 | (res, sw) = card.read_binary('PLMNsel') |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 159 | if sw == '9000': |
| 160 | print("PLMNsel: %s" % (res)) |
| 161 | else: |
| 162 | print("PLMNsel: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 163 | except Exception as e: |
Vadim Yanitskiy | a3bb334 | 2020-01-25 12:45:37 +0700 | [diff] [blame] | 164 | print("PLMNsel: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 165 | |
| 166 | # EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 167 | try: |
Supreeth Herle | 1408440 | 2020-03-19 12:42:10 +0100 | [diff] [blame] | 168 | (res, sw) = card.read_plmn_act() |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 169 | if sw == '9000': |
Supreeth Herle | 1408440 | 2020-03-19 12:42:10 +0100 | [diff] [blame] | 170 | print("PLMNwAcT:\n%s" % (res)) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 171 | else: |
| 172 | print("PLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 173 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 174 | print("PLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 175 | |
| 176 | # EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 177 | try: |
Supreeth Herle | 1757b26 | 2020-03-19 12:43:11 +0100 | [diff] [blame] | 178 | (res, sw) = card.read_oplmn_act() |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 179 | if sw == '9000': |
Supreeth Herle | 1757b26 | 2020-03-19 12:43:11 +0100 | [diff] [blame] | 180 | print("OPLMNwAcT:\n%s" % (res)) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 181 | else: |
| 182 | print("OPLMNwAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 183 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 184 | print("OPLMNwAcT: Can't read file -- " + str(e)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 185 | |
| 186 | # EF.HPLMNAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 187 | try: |
Supreeth Herle | a850a47 | 2020-03-19 12:44:11 +0100 | [diff] [blame] | 188 | (res, sw) = card.read_hplmn_act() |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 189 | if sw == '9000': |
Supreeth Herle | a850a47 | 2020-03-19 12:44:11 +0100 | [diff] [blame] | 190 | print("HPLMNAcT:\n%s" % (res)) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 191 | else: |
| 192 | print("HPLMNAcT: Can't read, response code = %s" % (sw,)) |
Philipp Maier | a265049 | 2018-07-11 23:05:58 +0200 | [diff] [blame] | 193 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 194 | print("HPLMNAcT: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 195 | |
| 196 | # EF.ACC |
Supreeth Herle | d1fb6fc | 2020-03-19 12:45:45 +0100 | [diff] [blame] | 197 | (res, sw) = card.read_binary('ACC') |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 198 | if sw == '9000': |
| 199 | print("ACC: %s" % (res,)) |
| 200 | else: |
| 201 | print("ACC: Can't read, response code = %s" % (sw,)) |
| 202 | |
| 203 | # EF.MSISDN |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 204 | try: |
Supreeth Herle | 6d66af6 | 2020-03-19 12:49:16 +0100 | [diff] [blame] | 205 | (res, sw) = card.read_msisdn() |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 206 | if sw == '9000': |
Supreeth Herle | 6d66af6 | 2020-03-19 12:49:16 +0100 | [diff] [blame] | 207 | # (npi, ton, msisdn) = res |
| 208 | if res is not None: |
| 209 | print("MSISDN (NPI=%d ToN=%d): %s" % res) |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 210 | else: |
| 211 | print("MSISDN: Not available") |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 212 | else: |
Sylvain Munaut | 9f13897 | 2013-07-18 10:36:51 +0200 | [diff] [blame] | 213 | print("MSISDN: Can't read, response code = %s" % (sw,)) |
Philipp Maier | ea6bdf0 | 2018-07-11 23:02:36 +0200 | [diff] [blame] | 214 | except Exception as e: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 215 | print("MSISDN: Can't read file -- " + str(e)) |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 216 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 217 | # EF.AD |
Supreeth Herle | 52ef675 | 2020-03-19 12:50:27 +0100 | [diff] [blame] | 218 | (res, sw) = card.read_binary('AD') |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 219 | if sw == '9000': |
| 220 | print("AD: %s" % (res,)) |
| 221 | else: |
| 222 | print("AD: Can't read, response code = %s" % (sw,)) |
| 223 | |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 224 | # EF.SST |
Supreeth Herle | d3b13d0 | 2020-04-20 13:30:34 +0200 | [diff] [blame] | 225 | (res, sw) = card.read_binary('SST') |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 226 | if sw == '9000': |
Supreeth Herle | d3b13d0 | 2020-04-20 13:30:34 +0200 | [diff] [blame] | 227 | print("SIM Service Table: %s" % res) |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 228 | # Print those which are available |
Supreeth Herle | d3b13d0 | 2020-04-20 13:30:34 +0200 | [diff] [blame] | 229 | print("%s" % dec_st(res)) |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 230 | else: |
| 231 | print("SIM Service Table: Can't read, response code = %s" % (sw,)) |
| 232 | |
Supreeth Herle | 9641299 | 2020-03-22 08:20:11 +0100 | [diff] [blame] | 233 | # Check whether we have th AID of USIM, if so select it by its AID |
| 234 | # EF.UST - File Id in ADF USIM : 6f38 |
| 235 | if '9000' == card.select_adf_by_aid(): |
| 236 | # EF.UST |
| 237 | (res, sw) = card.read_binary('6f38') |
| 238 | if sw == '9000': |
| 239 | print("USIM Service Table: %s" % res) |
| 240 | # Print those which are available |
| 241 | print("%s" % dec_st(res, table="usim")) |
| 242 | else: |
| 243 | print("USIM Service Table: Can't read, response code = %s" % (sw,)) |
| 244 | |
Supreeth Herle | ee15c77 | 2020-03-22 08:58:33 +0100 | [diff] [blame] | 245 | # Check whether we have th AID of ISIM, if so select it by its AID |
| 246 | # EF.IST - File Id in ADF ISIM : 6f07 |
| 247 | if '9000' == card.select_adf_by_aid(adf="isim"): |
| 248 | # EF.IST |
| 249 | (res, sw) = card.read_binary('6f07') |
| 250 | if sw == '9000': |
| 251 | print("ISIM Service Table: %s" % res) |
| 252 | # Print those which are available |
| 253 | print("%s" % dec_st(res, table="isim")) |
| 254 | else: |
| 255 | print("ISIM Service Table: Can't read, response code = %s" % (sw,)) |
| 256 | |
Alexander Chemeris | 6e58914 | 2013-07-04 17:34:06 +0400 | [diff] [blame] | 257 | # Done for this card and maybe for everything ? |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 258 | print("Done !\n") |