aboutsummaryrefslogtreecommitdiffstats
path: root/osmo-sim-auth.py
blob: 6ba955706fcdc635ebb51d5381c7ceb824d03ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python

"""
Test script for (U)SIM authentication
Copyright (C) 2011 Harald Welte <laforge@gnumonks.org>

based heavily on the "card" library by Benoit Michau and pyscard

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from binascii import *
from card.utils import *
from optparse import OptionParser
from card.USIM import USIM
from card.SIM import SIM

def handle_usim(options, rand_bin, autn_bin):
	u = USIM()
	if not u:
		print "Error opening USIM"
		exit(1)

	if options.debug:
		u.dbg = 2;

	imsi = u.get_imsi()
	print "Testing USIM card with IMSI %s" % imsi

	print "\nUMTS Authentication"
	ret = u.authenticate(rand_bin, autn_bin, ctx='3G')
        if ret == None:
                print "UMTS Authentication failed"
                exit(1)
	if len(ret) == 1:
		print "AUTS:\t%s" % b2a_hex(byteToString(ret[0]))
	else:
		print "RES:\t%s" % b2a_hex(byteToString(ret[0]))
		print "CK:\t%s" % b2a_hex(byteToString(ret[1]))
		print "IK:\t%s" % b2a_hex(byteToString(ret[2]))
		if len(ret) == 4:
			print "Kc:\t%s" % b2a_hex(byteToString(ret[3]))

	print "\nGSM Authentication"
	ret = u.authenticate(rand_bin, autn_bin, ctx='2G')
	if not len(ret) == 2:
		print "Error during 2G authentication"
		exit(1)
	print "SRES:\t%s" % b2a_hex(byteToString(ret[0]))
	print "Kc:\t%s" % b2a_hex(byteToString(ret[1]))

def handle_sim(options, rand_bin):
	s= SIM()
	if not s:
		print "Error opening SIM"
		exit(1)

	imsi = s.get_imsi()
	if not options.ipsec:
		print "Testing SIM card with IMSI %s" % imsi
		print "\nGSM Authentication"

	ret = s.run_gsm_alg(rand_bin)

	if not options.ipsec:
		print "SRES:\t%s" % b2a_hex(byteToString(ret[0]))
		print "Kc:\t%s" % b2a_hex(byteToString(ret[1]))

	if options.ipsec:
		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])))

def handle_sim_info(options):
	s= SIM()
	if not s:
		print "Error opening SIM"
		exit(1)

	if options.debug:
		s.dbg = 1

	s.caller.get(options.param)()

if __name__ == "__main__":
	parser = OptionParser()
	parser.add_option("-a", "--autn", dest="autn",
			  help="AUTN parameter from AuC")
	parser.add_option("-r", "--rand", dest="rand",
			  help="RAND parameter from AuC")
	parser.add_option("-d", "--debug", dest="debug",
			  help="Enable debug output",
			  action="store_true")
	parser.add_option("-s", "--sim", dest="sim",
			  help="SIM mode (default: USIM)",
			  action="store_true", default=False)
	parser.add_option("-I", "--ipsec", dest="ipsec",
			  help="IPSEC mode for strongswan triplets.dat",
			  action="store_true")
	parser.add_option("-p", "--param", dest="param",
			  help="Retrieve SIM card parameter (mode: SIM) KC|IMSI|LOCI|HPPLMN|PLMN_SEL|ICCID|ACC|FPLMN|MSISDN|SMSP")

	(options, args) = parser.parse_args()

	if options.param:
		handle_sim_info(options)
		exit(2)

	if not options.rand:
		print "You have to specify RAND"
		exit(2)

	rand_bin = stringToByte(a2b_hex(options.rand))
	if options.autn:
		autn_bin = stringToByte(a2b_hex(options.autn))

	if options.sim == True:
		handle_sim(options, rand_bin)
	else:
		if not options.autn:
			print "You have to specify AUTN"
			exit(2)
		handle_usim(options, rand_bin, autn_bin)