Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # |
| 4 | # Utility to deal with sim cards and program the 'magic' ones easily |
| 5 | # |
| 6 | # |
| 7 | # Part of the sim link code of inspired by pySimReader-Serial-src-v2 |
| 8 | # |
| 9 | # |
| 10 | # Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com> |
| 11 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 12 | # |
| 13 | # This program is free software: you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License as published by |
| 15 | # the Free Software Foundation, either version 2 of the License, or |
| 16 | # (at your option) any later version. |
| 17 | # |
| 18 | # This program is distributed in the hope that it will be useful, |
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | # GNU General Public License for more details. |
| 22 | # |
| 23 | # You should have received a copy of the GNU General Public License |
| 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | # |
| 26 | |
| 27 | import hashlib |
| 28 | from optparse import OptionParser |
| 29 | import random |
| 30 | import re |
| 31 | import sys |
| 32 | |
| 33 | from pySim.commands import SimCardCommands |
| 34 | from pySim.cards import _cards_classes |
| 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 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 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 | ) |
Sylvain Munaut | e9fdecb | 2010-12-08 22:33:19 +0100 | [diff] [blame^] | 49 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", metavar="PCSC", |
| 50 | help="Which PC/SC reader number for SIM access", |
| 51 | default=None, |
| 52 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 53 | parser.add_option("-t", "--type", dest="type", |
| 54 | help="Card type (user -t list to view) [default: %default]", |
| 55 | default="auto", |
| 56 | ) |
| 57 | parser.add_option("-e", "--erase", dest="erase", action='store_true', |
| 58 | help="Erase beforehand [default: %default]", |
| 59 | default=False, |
| 60 | ) |
| 61 | |
| 62 | parser.add_option("-n", "--name", dest="name", |
| 63 | help="Operator name [default: %default]", |
| 64 | default="Magic", |
| 65 | ) |
| 66 | parser.add_option("-c", "--country", dest="country", type="int", metavar="CC", |
| 67 | help="Country code [default: %default]", |
| 68 | default=1, |
| 69 | ) |
| 70 | parser.add_option("-x", "--mcc", dest="mcc", type="int", |
| 71 | help="Mobile Country Code [default: %default]", |
| 72 | default=901, |
| 73 | ) |
| 74 | parser.add_option("-y", "--mnc", dest="mnc", type="int", |
| 75 | help="Mobile Network Code", |
| 76 | default=55, |
| 77 | ) |
| 78 | parser.add_option("-m", "--smsp", dest="smsp", |
| 79 | help="SMSP [default: '00 + country code + 5555']", |
| 80 | ) |
| 81 | |
| 82 | parser.add_option("-s", "--iccid", dest="iccid", metavar="ID", |
| 83 | help="Integrated Circuit Card ID", |
| 84 | ) |
| 85 | parser.add_option("-i", "--imsi", dest="imsi", |
| 86 | help="International Mobile Subscriber Identity", |
| 87 | ) |
| 88 | parser.add_option("-k", "--ki", dest="ki", |
| 89 | help="Ki (default is to randomize)", |
| 90 | ) |
| 91 | |
| 92 | parser.add_option("-z", "--secret", dest="secret", metavar="STR", |
| 93 | help="Secret used for ICCID/IMSI autogen", |
| 94 | ) |
| 95 | parser.add_option("-j", "--num", dest="num", type=int, |
| 96 | help="Card # used for ICCID/IMSI autogen", |
| 97 | ) |
| 98 | |
| 99 | (options, args) = parser.parse_args() |
| 100 | |
| 101 | if options.type == 'list': |
| 102 | for kls in _cards_classes: |
| 103 | print kls.name |
| 104 | sys.exit(0) |
| 105 | |
| 106 | if ((options.imsi is None) or (options.iccid is None)) and (options.num is None): |
| 107 | parser.error("If either IMSI or ICCID isn't specified, num is required") |
| 108 | |
| 109 | if args: |
| 110 | parser.error("Extraneous arguments") |
| 111 | |
| 112 | return options |
| 113 | |
| 114 | |
| 115 | def _digits(secret, usage, len, num): |
| 116 | s = hashlib.sha1(secret + usage + '%d' % num) |
| 117 | d = ''.join(['%02d'%ord(x) for x in s.digest()]) |
| 118 | return d[0:len] |
| 119 | |
| 120 | def _mcc_mnc_digits(mcc, mnc): |
| 121 | return ('%03d%03d' if mnc > 100 else '%03d%02d') % (mcc, mnc) |
| 122 | |
| 123 | def _cc_digits(cc): |
| 124 | return ('%03d' if cc > 100 else '%02d') % cc |
| 125 | |
| 126 | def _isnum(s, l=-1): |
| 127 | return s.isdigit() and ((l== -1) or (len(s) == l)) |
| 128 | |
| 129 | |
| 130 | def gen_parameters(opts): |
| 131 | """Generates Name, ICCID, MCC, MNC, IMSI, SMSP, Ki from the |
| 132 | options given by the user""" |
| 133 | |
| 134 | # MCC/MNC |
| 135 | mcc = opts.mcc |
| 136 | mnc = opts.mnc |
| 137 | |
| 138 | if not ((0 < mcc < 999) and (0 < mnc < 999)): |
| 139 | raise ValueError('mcc & mnc must be between 0 and 999') |
| 140 | |
| 141 | # Digitize country code (2 or 3 digits) |
| 142 | cc_digits = _cc_digits(opts.country) |
| 143 | |
| 144 | # Digitize MCC/MNC (5 or 6 digits) |
| 145 | plmn_digits = _mcc_mnc_digits(mcc, mnc) |
| 146 | |
| 147 | # ICCID (20 digits) |
| 148 | if opts.iccid is not None: |
| 149 | iccid = opts.iccid |
| 150 | if not _isnum(iccid, 20): |
| 151 | raise ValueError('ICCID must be 20 digits !'); |
| 152 | |
| 153 | else: |
| 154 | if opts.num is None: |
| 155 | raise ValueError('Neither ICCID nor card number specified !') |
| 156 | |
| 157 | iccid = ( |
| 158 | '89' + # Common prefix (telecom) |
| 159 | cc_digits + # Country Code on 2/3 digits |
| 160 | plmn_digits # MCC/MNC on 5/6 digits |
| 161 | ) |
| 162 | |
| 163 | ml = 20 - len(iccid) |
| 164 | |
| 165 | if opts.secret is None: |
| 166 | # The raw number |
| 167 | iccid += ('%%0%dd' % ml) % opts.num |
| 168 | else: |
| 169 | # Randomized digits |
| 170 | iccid += _digits(opts.secret, 'ccid', ml, opts.num) |
| 171 | |
| 172 | # IMSI (15 digits usually) |
| 173 | if opts.imsi is not None: |
| 174 | imsi = opts.imsi |
| 175 | if not _isnum(imsi): |
| 176 | raise ValueError('IMSI must be digits only !') |
| 177 | |
| 178 | else: |
| 179 | if opts.num is None: |
| 180 | raise ValueError('Neither IMSI nor card number specified !') |
| 181 | |
| 182 | ml = 15 - len(plmn_digits) |
| 183 | |
| 184 | if opts.secret is None: |
| 185 | # The raw number |
| 186 | msin = ('%%0%dd' % ml) % opts.num |
| 187 | else: |
| 188 | # Randomized digits |
| 189 | msin = _digits(opts.secret, 'imsi', ml, opts.num) |
| 190 | |
| 191 | imsi = ( |
| 192 | plmn_digits + # MCC/MNC on 5/6 digits |
| 193 | msin # MSIN |
| 194 | ) |
| 195 | |
| 196 | # SMSP |
| 197 | if opts.smsp is not None: |
| 198 | smsp = opts.smsp |
| 199 | if not _isnum(smsp): |
| 200 | raise ValueError('SMSP must be digits only !') |
| 201 | |
| 202 | else: |
| 203 | smsp = '00%d' % opts.country + '5555' # Hack ... |
| 204 | |
| 205 | # Ki (random) |
| 206 | if opts.ki is not None: |
| 207 | ki = opts.ki |
| 208 | if not re.match('^[0-9a-fA-F]{32}$', ki): |
| 209 | raise ValueError('Ki needs to be 128 bits, in hex format') |
| 210 | |
| 211 | else: |
| 212 | ki = ''.join(['%02x' % random.randrange(0,256) for i in range(16)]) |
| 213 | |
| 214 | # Return that |
| 215 | return { |
| 216 | 'name' : opts.name, |
| 217 | 'iccid' : iccid, |
| 218 | 'mcc' : mcc, |
| 219 | 'mnc' : mnc, |
| 220 | 'imsi' : imsi, |
| 221 | 'smsp' : smsp, |
| 222 | 'ki' : ki, |
| 223 | } |
| 224 | |
| 225 | |
| 226 | def print_parameters(params): |
| 227 | |
| 228 | print """Generated card parameters : |
| 229 | > Name : %(name)s |
| 230 | > SMSP : %(smsp)s |
| 231 | > ICCID : %(iccid)s |
| 232 | > MCC/MNC : %(mcc)d/%(mnc)d |
| 233 | > IMSI : %(imsi)s |
| 234 | > Ki : %(ki)s |
| 235 | """ % params |
| 236 | |
| 237 | |
| 238 | if __name__ == '__main__': |
| 239 | |
| 240 | # Get/Gen the parameters |
| 241 | opts = parse_options() |
| 242 | cp = gen_parameters(opts) |
| 243 | print_parameters(cp) |
| 244 | |
| 245 | # Connect to the card |
| 246 | if opts.pcsc_dev is None: |
| 247 | from pySim.transport.serial import SerialSimLink |
| 248 | sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) |
| 249 | else: |
| 250 | from pySim.transport.pcsc import PcscSimLink |
| 251 | sl = PcscSimLink(0, observer=0) |
| 252 | scc = SimCardCommands(transport=sl) |
| 253 | |
| 254 | # Detect type if needed |
| 255 | card = None |
| 256 | ctypes = dict([(kls.name, kls) for kls in _cards_classes]) |
| 257 | |
| 258 | if opts.type == "auto": |
| 259 | for kls in _cards_classes: |
| 260 | card = kls.autodetect(scc) |
| 261 | if card: |
| 262 | print "Autodetected card type %s" % card.name |
| 263 | card.reset() |
| 264 | break |
| 265 | |
| 266 | if card is None: |
| 267 | print "Autodetection failed" |
| 268 | sys.exit(-1) |
| 269 | |
| 270 | elif opts.type in ctypes: |
| 271 | card = ctypes[opts.type](scc) |
| 272 | |
| 273 | else: |
| 274 | print "Unknown card type %s" % opts.type |
| 275 | sys.exit(-1) |
| 276 | |
| 277 | # Erase it if asked |
| 278 | if opts.erase: |
| 279 | print "Formatting ..." |
| 280 | card.erase() |
| 281 | card.reset() |
| 282 | |
| 283 | # Program it |
| 284 | print "Programming ..." |
| 285 | card.program(cp) |
| 286 | |
| 287 | print "Done !" |
| 288 | |