-
Notifications
You must be signed in to change notification settings - Fork 8
/
sysmo_usim.py
227 lines (171 loc) · 5.8 KB
/
sysmo_usim.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gadgets to modify SYSMO USIM SJS1 parameters
(C) 2017 by Sysmocom s.f.m.c. GmbH
All Rights Reserved
Author: Philipp Maier
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, see <http://www.gnu.org/licenses/>.
"""
from card import *
from simcard import *
from utils import *
import sys
# CHV Types
SYSMO_USIM_ADM1 = 0x0A
class Sysmo_usim:
sim = None
def __init__(self, atr):
print("Initializing smartcard terminal...")
self.sim = Simcard(GSM_USIM, toBytes(atr))
self.sim.card.SELECT_ADF_USIM()
print(" * Detected Card IMSI: %s" % self.sim.card.get_imsi())
if self.sim.has_isim:
print(" ISIM Application installed")
if self.sim.has_usim:
print(" USIM Application installed")
print("")
def _warn_failed_auth(self, attempts = 3, keytype = "ADM1"):
print(" === Authentication problem! The Card will permanently ===")
print(" === lock down after %d failed attemts! Double check %s! ===" % (attempts, keytype))
print("")
def _warn_remaining_auth(self, attempts = 3, keytype = "ADM1"):
print(" * Error: Only two authentication attempts remaining, we don't")
print(" want to risk another failed authentication attempt!")
print(" (double check ADM1 and use option -f to override)")
print("")
# Initalize card (select master file)
def _init(self):
print(" * Initalizing...")
self.sim.select(GSM_SIM_MF)
# Read files sensitively
def _read_binary(self, length, offset=0):
res = self.sim.read_binary(length, offset)
if len(res.apdu) != length:
print(" Error: could not read file (sw=%02x%02x) -- abort!\n" % (res.sw[0], res.sw[1]))
exit(1)
return res
# Authenticate as administrator
def admin_auth(self, adm1, force = False):
print("Authenticating...")
rc = True
rem_attemts = self.sim.chv_retrys(SYSMO_USIM_ADM1)
print(" * Remaining attempts: " + str(rem_attemts))
# Stop if a decreased ADM1 retry counter is detected
if(rem_attemts < 3) and force == False:
self._warn_remaining_auth()
self._warn_failed_auth()
return False
if(len(adm1) != 8):
print(" * Error: Short ADM1, a valid ADM1 is 8 digits long!")
print("")
self._warn_failed_auth()
return False
# Try to authenticate
try:
print(" * Authenticating...")
res = self.sim.verify_chv(adm1, SYSMO_USIM_ADM1)
if res.sw != [0x90, 0x00]:
raise
print(" * Authentication successful")
except:
print(" * Error: Authentication failed!")
rc = False
# Read back and display remaining attemts
rem_attemts = self.sim.chv_retrys(SYSMO_USIM_ADM1)
print(" * Remaining attempts: " + str(rem_attemts))
print("")
if rc == False:
self._warn_failed_auth()
return rc
# Show current ICCID value
def show_iccid(self):
print("Reading ICCID value...")
self._init()
print(" * Reading...")
print(" * Card ICCID: %s" % self.sim.card.get_ICCID())
print("")
# Program new ICCID value
# Note: Since the ICCID is a stanard file writing to it works the same
# way for all card models. However, the ICCID may be linked to the
# license management of the card O/S, so changing it might cause
# problems for some cards models. (USE WITH CAUTION!)
def write_iccid(self, iccid):
print("Writing ICCID value...")
self._init()
print(" * New ICCID setting:")
print(" ICCID: " + hexdump(iccid))
self.sim.select(GSM_SIM_EF_ICCID)
print(" * Programming...")
self.sim.update_binary(swap_nibbles(iccid))
print("")
# Program new IMSI value
def write_imsi(self, imsi):
print("Writing IMSI value...")
self._init()
print(" * New ISMI setting:")
print(" IMSI: " + hexdump(imsi))
self.sim.select(GSM_SIM_DF_GSM)
self.sim.select(GSM_SIM_EF_IMSI)
imsi = [len(imsi)] + swap_nibbles(imsi)
print(" * Programming...")
self.sim.update_binary(imsi)
print("")
# Show current mnc length value
def show_mnclen(self):
print("Reading MNCLEN value...")
self._init()
print(" * Reading...")
self.sim.select(GSM_SIM_DF_GSM)
self.sim.select(GSM_SIM_EF_AD)
res = self.sim.read_binary(4)
print(" * Current MNCLEN setting:")
print(" MNCLEN: " + "0x%02x" % res.apdu[3])
print("")
# Program new mnc length value
def write_mnclen(self, mnclen):
print("Writing MNCLEN value...")
self._init()
print(" * New MNCLEN setting:")
print(" MNCLEN: " + "0x" + hexdump(mnclen))
if len(mnclen) != 1:
print(" * Error: mnclen value must consist of a single byte!")
return
print(" * Programming...")
# EF.AD in DF.GSM
self.sim.select(GSM_SIM_DF_GSM)
self.sim.select(GSM_SIM_EF_AD)
res = self.sim.read_binary(4)
new_ad = res.apdu[0:3] + mnclen
self.sim.update_binary(new_ad)
# EF.AD in ADF.USIM
self.sim.card.SELECT_ADF_USIM()
self.sim.select(GSM_SIM_EF_AD)
res = self.sim.read_binary(4)
new_ad = res.apdu[0:3] + mnclen
self.sim.update_binary(new_ad)
print("")
# Show installed applications (AIDs)
def show_aid(self):
print("Reading application directory...");
self._init()
self.sim.card.get_AID()
AID = self.sim.card.AID
for a in AID:
if a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02]:
appstr = "USIM"
elif a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04]:
appstr = "ISIM"
else:
appstr = "(unknown)"
print(" AID: " + hexdump(a[0:5]) + " " + hexdump(a[5:7]) + " " + hexdump(a[7:]) + " ==> " + appstr)
print("")