blob: f668dd479faf90776ce00f62f5c26845070a4d7c [file] [log] [blame]
Harald Weltec1cd0272011-12-07 01:51:46 +01001#!/usr/bin/python
2
3"""
4Test script for (U)SIM authentication
5Copyright (C) 2011 Harald Welte <laforge@gnumonks.org>
6
7based heavily on the "card" library by Benoit Michau and pyscard
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License along
20with this program; if not, write to the Free Software Foundation, Inc.,
2151 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22"""
23
24from binascii import *
25from card.utils import *
26from optparse import OptionParser
27from card.USIM import USIM
28from card.SIM import SIM
29
Ludovic Rousseaud4053c32020-10-17 10:15:28 +020030
Harald Weltec1cd0272011-12-07 01:51:46 +010031def handle_usim(options, rand_bin, autn_bin):
Ludovic Rousseau9c886682020-10-17 10:13:14 +020032 u = USIM()
33 if not u:
34 print("Error opening USIM")
35 exit(1)
Harald Weltec1cd0272011-12-07 01:51:46 +010036
Ludovic Rousseau9c886682020-10-17 10:13:14 +020037 if options.debug:
Ludovic Rousseau58678c72020-10-17 10:16:11 +020038 u.dbg = 2
Harald Weltec1cd0272011-12-07 01:51:46 +010039
Ludovic Rousseau9c886682020-10-17 10:13:14 +020040 imsi = u.get_imsi()
41 print("Testing USIM card with IMSI %s" % imsi)
Harald Weltec1cd0272011-12-07 01:51:46 +010042
Ludovic Rousseau9c886682020-10-17 10:13:14 +020043 print("\nUMTS Authentication")
44 ret = u.authenticate(rand_bin, autn_bin, ctx='3G')
Ludovic Rousseau50579f12020-10-17 13:45:22 +020045 if ret is None:
Ludovic Rousseauf85c4ee2020-10-17 10:17:34 +020046 print("UMTS Authentication failed")
47 exit(1)
Ludovic Rousseau9c886682020-10-17 10:13:14 +020048 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 Weltec1cd0272011-12-07 01:51:46 +010056
Ludovic Rousseau9c886682020-10-17 10:13:14 +020057 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 Weltec1cd0272011-12-07 01:51:46 +010064
Ludovic Rousseaud4053c32020-10-17 10:15:28 +020065
Harald Weltec1cd0272011-12-07 01:51:46 +010066def handle_sim(options, rand_bin):
Ludovic Rousseau003aff32020-10-17 10:17:02 +020067 s = SIM()
Ludovic Rousseau9c886682020-10-17 10:13:14 +020068 if not s:
69 print("Error opening SIM")
70 exit(1)
Harald Weltec1cd0272011-12-07 01:51:46 +010071
Ludovic Rousseau9c886682020-10-17 10:13:14 +020072 imsi = s.get_imsi()
73 if not options.ipsec:
74 print("Testing SIM card with IMSI %s" % imsi)
75 print("\nGSM Authentication")
Harald Weltec1cd0272011-12-07 01:51:46 +010076
Ludovic Rousseau9c886682020-10-17 10:13:14 +020077 ret = s.run_gsm_alg(rand_bin)
Harald Welte4807dd92012-07-13 23:30:12 +020078
Ludovic Rousseau9c886682020-10-17 10:13:14 +020079 if not options.ipsec:
Ludovic Rousseau275e4c12020-10-17 13:40:15 +020080 print("SRES:\t%s" % byteToHex(ret[0]))
81 print("Kc:\t%s" % byteToHex(ret[1]))
Harald Welte4807dd92012-07-13 23:30:12 +020082
Ludovic Rousseau9c886682020-10-17 10:13:14 +020083 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 Weltec1cd0272011-12-07 01:51:46 +010085
Ludovic Rousseaud4053c32020-10-17 10:15:28 +020086
Gerard Pinto7c52d762017-06-03 00:21:09 -070087def handle_sim_info(options):
Ludovic Rousseau003aff32020-10-17 10:17:02 +020088 s = SIM()
Ludovic Rousseau9c886682020-10-17 10:13:14 +020089 if not s:
90 print("Error opening SIM")
91 exit(1)
Gerard Pinto7c52d762017-06-03 00:21:09 -070092
Ludovic Rousseau9c886682020-10-17 10:13:14 +020093 if options.debug:
94 s.dbg = 1
Gerard Pinto7c52d762017-06-03 00:21:09 -070095
Ludovic Rousseau9c886682020-10-17 10:13:14 +020096 s.caller.get(options.param)()
Harald Weltec1cd0272011-12-07 01:51:46 +010097
Ludovic Rousseaud4053c32020-10-17 10:15:28 +020098
Harald Weltec1cd0272011-12-07 01:51:46 +010099if __name__ == "__main__":
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200100 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 Weltec1cd0272011-12-07 01:51:46 +0100116
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200117 (options, args) = parser.parse_args()
Harald Weltec1cd0272011-12-07 01:51:46 +0100118
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200119 if options.param:
120 handle_sim_info(options)
121 exit(2)
Gerard Pinto7c52d762017-06-03 00:21:09 -0700122
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200123 if not options.rand:
124 print("You have to specify RAND")
125 exit(2)
Harald Weltec1cd0272011-12-07 01:51:46 +0100126
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200127 rand_bin = stringToByte(a2b_hex(options.rand))
128 if options.autn:
129 autn_bin = stringToByte(a2b_hex(options.autn))
Harald Weltec1cd0272011-12-07 01:51:46 +0100130
Ludovic Rousseau50579f12020-10-17 13:45:22 +0200131 if options.sim is True:
Ludovic Rousseau9c886682020-10-17 10:13:14 +0200132 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)