This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
unlock.py
136 lines (103 loc) · 4.18 KB
/
unlock.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
import json
import time
import math
import sys
import subprocess
welcomeText = """
*****************************************************************************
* Unlock Huawei Bootloader - Made by Haex inspired by SkyEmie *
* *
* Please enable USB DEBBUG and OEM UNLOCK if your device does not appear *
* *
* Be aware of lossing all your data => do backup ;) *
*****************************************************************************
"""
###############################################################################
failedAttemptsFilename = 'failedAttempts.json'
# if your device has a limit of false attempts before reboot
# otherwise set isLimitAttemptEnabled to false
limitAttempt = 5
isLimitAttemptEnabled = False
startingPoint = 1000000000000000
###############################################################################
def getFailedAttemptsFromFile(filename = 'failedAttempts.json'):
try:
with open(filename, 'r') as file:
array = json.load(file)
if (type(array) == list):
return set(array)
else:
return set([ ])
except:
return set([ ])
def writeFailedAttemptsToFile(filename = 'failedAttempts.json', failedAttempts = [ ]):
startTime = time.time()
with open(filename, 'w') as file:
json.dump(failedAttempts, file)
print('* saved file in {0} seconds *'.format(time.time() - startTime))
def algoIncrementChecksum(imei, checksum, genOEMcode):
genOEMcode += int(checksum + math.sqrt(imei) * 1024)
return genOEMcode
def luhn_checksum(imei):
def digits_of(n):
return [int(d) for d in str(n)]
digits = digits_of(imei)
oddDigits = digits[-1::-2]
evenDigits = digits[-2::-2]
checksum = 0
checksum += sum(oddDigits)
for i in evenDigits:
checksum += sum(digits_of(i * 2))
return checksum % 10
def tryUnlockBootloader(imei, checksum, failedAttempts = set([ ])):
unlocked = False
algoOEMcode = 1000000000000000
countAttempts = 0
while(unlocked == False):
countAttempts += 1
while algoOEMcode in failedAttempts or algoOEMcode < startingPoint:
algoOEMcode = algoIncrementChecksum(imei, checksum, algoOEMcode)
answer = subprocess.run(
['fastboot', 'oem', 'unlock', str(algoOEMcode)]
, stdout = subprocess.DEVNULL
, stderr = subprocess.DEVNULL
)
if answer.returncode == 0:
unlocked = True
return algoOEMcode
else:
failedAttempts.add(algoOEMcode)
count = len(failedAttempts)
print('* shot {0} with code {1} *'.format(count, algoOEMcode))
# reboot in bootloader mode after limit of attempts is reached
if (count % (limitAttempt - 1) == 0 and isLimitAttemptEnabled == True) or (count % 40000 == 0 and isLimitAttemptEnabled == False):
subprocess.run(
['fastboot', 'reboot', 'bootloader']
, stdout = subprocess.DEVNULL
, stderr = subprocess.DEVNULL
)
if (isLimitAttemptEnabled and count % (limitAttempt - 1) == 0) or (not isLimitAttemptEnabled and count % 100 == 0):
writeFailedAttemptsToFile(failedAttemptsFilename, list(failedAttempts))
algoOEMcode = algoIncrementChecksum(imei, checksum, algoOEMcode)
def main(args = [ ]):
print(welcomeText)
subprocess.run(['adb', 'devices'])
imei = int(args[1]) if len(args) > 1 else int(input('Type IMEI digit: '))
checksum = luhn_checksum(imei)
subprocess.run(
['adb', 'reboot', 'bootloader']
, stdout = subprocess.DEVNULL
, stderr = subprocess.DEVNULL
)
input('Press any key when your device is in fastboot mode\n')
failedAttempts = getFailedAttemptsFromFile(failedAttemptsFilename)
codeOEM = tryUnlockBootloader(imei, checksum, failedAttempts)
subprocess.run(['fastboot', 'getvar', 'unlocked'])
subprocess.run(['fastboot', 'reboot'])
print('\n\nDevice unlocked! OEM CODE: {0}'.format(codeOEM))
print('Keep it safe\n')
input('Press any key to exit...\n')
exit()
###############################################################################
if __name__ == '__main__':
main(sys.argv)