Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Test script for (U)SIM authentication |
| 5 | Copyright (C) 2011 Harald Welte <laforge@gnumonks.org> |
| 6 | |
| 7 | based heavily on the "card" library by Benoit Michau and pyscard |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | """ |
| 23 | |
| 24 | from binascii import * |
| 25 | from card.utils import * |
| 26 | from optparse import OptionParser |
| 27 | from card.USIM import USIM |
| 28 | from card.SIM import SIM |
| 29 | |
Ludovic Rousseau | d4053c3 | 2020-10-17 10:15:28 +0200 | [diff] [blame] | 30 | |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 31 | def handle_usim(options, rand_bin, autn_bin): |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 32 | u = USIM() |
| 33 | if not u: |
| 34 | print("Error opening USIM") |
| 35 | exit(1) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 36 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 37 | if options.debug: |
Ludovic Rousseau | 58678c7 | 2020-10-17 10:16:11 +0200 | [diff] [blame] | 38 | u.dbg = 2 |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 39 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 40 | imsi = u.get_imsi() |
| 41 | print("Testing USIM card with IMSI %s" % imsi) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 42 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 43 | print("\nUMTS Authentication") |
| 44 | ret = u.authenticate(rand_bin, autn_bin, ctx='3G') |
Ludovic Rousseau | 50579f1 | 2020-10-17 13:45:22 +0200 | [diff] [blame] | 45 | if ret is None: |
Ludovic Rousseau | f85c4ee | 2020-10-17 10:17:34 +0200 | [diff] [blame] | 46 | print("UMTS Authentication failed") |
| 47 | exit(1) |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 48 | if len(ret) == 1: |
| 49 | print("AUTS:\t%s" % b2a_hex(byteToString(ret[0]))) |
| 50 | else: |
| 51 | print("RES:\t%s" % b2a_hex(byteToString(ret[0]))) |
| 52 | print("CK:\t%s" % b2a_hex(byteToString(ret[1]))) |
| 53 | print("IK:\t%s" % b2a_hex(byteToString(ret[2]))) |
| 54 | if len(ret) == 4: |
| 55 | print("Kc:\t%s" % b2a_hex(byteToString(ret[3]))) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 56 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 57 | print("\nGSM Authentication") |
| 58 | ret = u.authenticate(rand_bin, autn_bin, ctx='2G') |
| 59 | if not len(ret) == 2: |
| 60 | print("Error during 2G authentication") |
| 61 | exit(1) |
| 62 | print("SRES:\t%s" % b2a_hex(byteToString(ret[0]))) |
| 63 | print("Kc:\t%s" % b2a_hex(byteToString(ret[1]))) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 64 | |
Ludovic Rousseau | d4053c3 | 2020-10-17 10:15:28 +0200 | [diff] [blame] | 65 | |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 66 | def handle_sim(options, rand_bin): |
Ludovic Rousseau | 003aff3 | 2020-10-17 10:17:02 +0200 | [diff] [blame] | 67 | s = SIM() |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 68 | if not s: |
| 69 | print("Error opening SIM") |
| 70 | exit(1) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 71 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 72 | imsi = s.get_imsi() |
| 73 | if not options.ipsec: |
| 74 | print("Testing SIM card with IMSI %s" % imsi) |
| 75 | print("\nGSM Authentication") |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 76 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 77 | ret = s.run_gsm_alg(rand_bin) |
Harald Welte | 4807dd9 | 2012-07-13 23:30:12 +0200 | [diff] [blame] | 78 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 79 | if not options.ipsec: |
Ludovic Rousseau | 275e4c1 | 2020-10-17 13:40:15 +0200 | [diff] [blame] | 80 | print("SRES:\t%s" % byteToHex(ret[0])) |
| 81 | print("Kc:\t%s" % byteToHex(ret[1])) |
Harald Welte | 4807dd9 | 2012-07-13 23:30:12 +0200 | [diff] [blame] | 82 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 83 | if options.ipsec: |
| 84 | print("1%s@uma.mnc%s.mcc%s.3gppnetwork.org,%s,%s,%s" % (imsi, imsi[3:6], imsi[0:3], b2a_hex(byteToString(rand_bin)), b2a_hex(byteToString(ret[0])), b2a_hex(byteToString(ret[1])))) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 85 | |
Ludovic Rousseau | d4053c3 | 2020-10-17 10:15:28 +0200 | [diff] [blame] | 86 | |
Gerard Pinto | 7c52d76 | 2017-06-03 00:21:09 -0700 | [diff] [blame] | 87 | def handle_sim_info(options): |
Ludovic Rousseau | 003aff3 | 2020-10-17 10:17:02 +0200 | [diff] [blame] | 88 | s = SIM() |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 89 | if not s: |
| 90 | print("Error opening SIM") |
| 91 | exit(1) |
Gerard Pinto | 7c52d76 | 2017-06-03 00:21:09 -0700 | [diff] [blame] | 92 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 93 | if options.debug: |
| 94 | s.dbg = 1 |
Gerard Pinto | 7c52d76 | 2017-06-03 00:21:09 -0700 | [diff] [blame] | 95 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 96 | s.caller.get(options.param)() |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 97 | |
Ludovic Rousseau | d4053c3 | 2020-10-17 10:15:28 +0200 | [diff] [blame] | 98 | |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 99 | if __name__ == "__main__": |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 100 | parser = OptionParser() |
| 101 | parser.add_option("-a", "--autn", dest="autn", |
| 102 | help="AUTN parameter from AuC") |
| 103 | parser.add_option("-r", "--rand", dest="rand", |
| 104 | help="RAND parameter from AuC") |
| 105 | parser.add_option("-d", "--debug", dest="debug", |
| 106 | help="Enable debug output", |
| 107 | action="store_true") |
| 108 | parser.add_option("-s", "--sim", dest="sim", |
| 109 | help="SIM mode (default: USIM)", |
| 110 | action="store_true", default=False) |
| 111 | parser.add_option("-I", "--ipsec", dest="ipsec", |
| 112 | help="IPSEC mode for strongswan triplets.dat", |
| 113 | action="store_true") |
| 114 | parser.add_option("-p", "--param", dest="param", |
| 115 | help="Retrieve SIM card parameter (mode: SIM) KC|IMSI|LOCI|HPPLMN|PLMN_SEL|ICCID|ACC|FPLMN|MSISDN|SMSP") |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 116 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 117 | (options, args) = parser.parse_args() |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 118 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 119 | if options.param: |
| 120 | handle_sim_info(options) |
| 121 | exit(2) |
Gerard Pinto | 7c52d76 | 2017-06-03 00:21:09 -0700 | [diff] [blame] | 122 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 123 | if not options.rand: |
| 124 | print("You have to specify RAND") |
| 125 | exit(2) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 126 | |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 127 | rand_bin = stringToByte(a2b_hex(options.rand)) |
| 128 | if options.autn: |
| 129 | autn_bin = stringToByte(a2b_hex(options.autn)) |
Harald Welte | c1cd027 | 2011-12-07 01:51:46 +0100 | [diff] [blame] | 130 | |
Ludovic Rousseau | 50579f1 | 2020-10-17 13:45:22 +0200 | [diff] [blame] | 131 | if options.sim is True: |
Ludovic Rousseau | 9c88668 | 2020-10-17 10:13:14 +0200 | [diff] [blame] | 132 | handle_sim(options, rand_bin) |
| 133 | else: |
| 134 | if not options.autn: |
| 135 | print("You have to specify AUTN") |
| 136 | exit(2) |
| 137 | handle_usim(options, rand_bin, autn_bin) |