This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathunlocker.py
executable file
·288 lines (268 loc) · 7.99 KB
/
unlocker.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
#
# unlocker.py
# - Remove simlock on huawei modems
#
# Copyright (C) 2013 Neil McPhail
#
# Unlock code generator Copyright (C) 2010 dogbert
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import time, serial, re, hashlib, glob
# Intro
def intro():
print 80 * "*"
print "\tHuawei modem unlocker"
print "\tBy Neil McPhail and dogbert"
print "\tThis is Free Software as defined by the GNU GENERAL PUBLIC"
print "\tLICENSE version 2"
print 80 * "*"
print "\tThis software comes with NO WARRANTY"
print "\tThis software can damage your hardware"
print "\tUse it at your own risk"
print 80 * "*"
print "\tNot all modems can be unlocked with this software."
print "\tUsers have reported problems with the following devices:"
print "\t\tE220, E353"
print "\tAttempting to unlock these devices with this software is not"
print "\trecommended. I hope to fix this in a later release."
print 80 * "*"
if not _requireYes():
print "Bye"
exit(0)
# Helper function
# Require an explicit "YES" in upper case
# Returns True if "YES"
# Asks for explicit uppercase "YES" if mixed or lower case used
# Returns False for anything else
def _requireYes():
print "If you wish to proceed, please type YES at the prompt"
while 1:
response = raw_input(">> ")
if response == "YES":
return True
if response.upper() == "YES":
print "You must type YES in upper case to proceed"
continue
else:
return False
# These modems seem to open 3 USB serial ports. Only one is the control port
# and this seems to vary from device to device. The other 2 ports appear to
# remain silent
def identifyPort():
print "Trying to find which port is the active modem connection."
print "Please be patient as this can take a while.\n\n"
for p in glob.glob('/dev/ttyUSB*'):
print "Testing port " + p
ser = serial.Serial(port = p,
timeout = 15, rtscts = True, dsrdtr = True)
ser.write('AT\r\n')
activity = ser.read(5)
if activity == '':
print "\tNo activity\n"
ser.close()
continue
print "\tActivity detected\n"
ser.close()
return p
return ''
# The modem should respond with the IMEI with the AT+CGSN command
def obtainImei(port):
print "\nTrying to obtain IMEI."
print "The modem will be given 5 seconds to respond."
ser = serial.Serial(port = port,
timeout = 0, rtscts = True, dsrdtr = True)
ser.flushInput()
ser.write('AT+CGSN\r\n')
time.sleep(5)
response = ser.read(4096)
ser.close()
match = re.search('\r\n(\d{15})\r\n', response)
if match:
print "Found probable IMEI: " + match.group(1)
return match.group(1)
else:
print "IMEI not found"
return ''
# Check the IMEI is correct
# Adapted from dogbert's original
def testImeiChecksum(imei):
digits = []
for i in imei:
digits.append(int(i))
_sum = 0
alt = False
for d in reversed(digits):
assert 0 <= d <= 9
if alt:
d *= 2
if d > 9:
d -= 9
_sum += d
alt = not alt
return (_sum % 10) == 0
# Display a warning if first digit of IMEI indicates a potentially troublesome
# modem
def checkImeiCompatibility(imei):
if ('8' == imei[0]):
print 80 * "*"
print "\n\tWarning"
print "\n\tYour modem's IMEI begins with '8'"
print "\tIt is likely to be incompatible with this script"
print "\tProceed at your own risk"
print "\n\tPlease provide feedback: see README and HELPME\n"
print 80 * "*"
# Interrogate the lock status
# Returns a dictionary with the lock status, remaining unlock attempts
# and the - largely unused - carrier code
#
# lockStatus 0 = unobtainable
# 1 = locked but can be unlocked
# 2 = unlocked to the inserted sim
# 3 = locked and cannot be unlocked
def checkLockStatus(port):
status = {'lockStatus': 0, 'remaining': 0, 'carrier': 0}
print "\nChecking the lock status of the SIM."
print "The modem will be given 5 seconds to respond."
ser = serial.Serial(port = port,
timeout = 0, rtscts = True, dsrdtr = True)
ser.flushInput()
ser.write('AT^CARDLOCK?\r\n')
time.sleep(5)
response = ser.read(4096)
ser.close()
match = re.search('CARDLOCK: (\d),(\d\d?),(\d+)\r', response)
if match:
status['lockStatus'] = int(match.group(1))
status['remaining'] = int(match.group(2))
status['carrier'] = int(match.group(3))
return status
# Compute the unlock code
# Adapted from dogbert's original
def computeUnlockCode(imei):
salt = '5e8dd316726b0335'
digest = hashlib.md5((imei+salt).lower()).digest()
code = 0
for i in range(0,4):
code += (ord(digest[i])^ord(digest[4+i])^ord(digest[8+i])^ord(digest[12+i])) << (3-i)*8
code &= 0x1ffffff
code |= 0x2000000
return code
# Send AT codes to unlock the modem
def unlockModem(port, lockCode):
ser = serial.Serial(port = port, rtscts = True, dsrdtr = True)
command = 'AT^CARDLOCK="'+ str(lockCode) + '"\r\n'
ser.write(command)
ser.close()
#
# Main routine
#
def main():
intro()
# Work out which is the control port
try:
activePort = identifyPort()
except:
print "\nAn error occurred when probing for active ports."
print "This may be because you need to run this program as root."
exit(1)
else:
if (activePort==''):
print "\nCould not identify active port."
exit(1)
# Obtain and check IMEI
try:
imei = obtainImei(activePort)
except:
print "\nAn error occurred when trying to check the IMEI."
exit(1)
else:
if (imei==''):
print "\nCould not obtain IMEI."
print "Check the modem is properly inserted"
print "Check a SIM card is in place"
print "Check you are not already connected"
print "Try removing and reinserting the device"
exit(1)
else:
if not testImeiChecksum(imei):
print "\nIMEI checksum invalid."
exit(1)
else:
print "IMEI checksum OK."
checkImeiCompatibility(imei)
# Obtain lockstatus
try:
lockInfo = checkLockStatus(activePort)
except:
print "\nAn error occurred when trying to check the SIM lock."
exit(1)
else:
ls = lockInfo['lockStatus']
if ls == 0:
print "\nCouldn't obtain SIM lock status."
print "Further operations would be dangerous."
exit(1)
elif ls == 2:
print "\nThe modem is already unlocked for this SIM."
exit(0)
elif ls == 3:
print "\nThe modem is hard locked,"
print "This program cannot help you."
exit(1)
else:
print "\nThis SIM should be unlockable..."
print "Remaining attempts: ", lockInfo['remaining']
print "Exceeding this will hard-lock the modem"
unlockCode = computeUnlockCode(imei)
print "\nUnlock code = ", unlockCode
print "Please be aware that a failed unlocking attempt could break your modem."
print "This is a risky procedure."
if not _requireYes():
print "Unlocking aborted"
exit(0)
print "\nAttempting to unlock..."
try:
unlockModem(activePort, unlockCode)
except:
print "\nAn error occurred when trying to unlock the modem."
exit(1)
print "\nWill check result in 5 seconds."
time.sleep(5)
# Check result
try:
lockInfo = checkLockStatus(activePort)
except:
print "\nAn error occurred when trying to check the SIM lock."
exit(1)
else:
ls = lockInfo['lockStatus']
if ls == 0:
print "\nCouldn't obtain SIM lock status."
print "Further operations would be dangerous."
exit(1)
elif ls == 1:
print "\nUnlocking unsuccessful. Sorry."
exit(1)
elif ls == 3:
print "\nUnlocking unsuccessful."
print "The modem appears to have been hard locked. Sorry."
exit(1)
else:
print "\nUnlocking successful!"
if __name__ == "__main__":
main()