-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
driver_51x7.py
292 lines (209 loc) · 8.86 KB
/
driver_51x7.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
289
290
291
292
import random
import re
import socket
import subprocess
import crcmod
import goodix
import protocol
import tool
TARGET_FIRMWARE = "GF_ST411SEC_APP_12109"
IAP_FIRMWARE = "MILAN_ST411SEC_IAP_12001"
VALID_FIRMWARE = "GF_ST411SEC_APP_121[0-9]{2}"
PSK = bytes.fromhex(
"0000000000000000000000000000000000000000000000000000000000000000")
PSK_WHITE_BOX = bytes.fromhex(
"ec35ae3abb45ed3f12c4751f1e5c2cc05b3c5452e9104d9f2a3118644f37a04b"
"6fd66b1d97cf80f1345f76c84f03ff30bb51bf308f2a9875c41e6592cd2a2f9e"
"60809b17b5316037b69bb2fa5d4c8ac31edb3394046ec06bbdacc57da6a756c5")
PMK_HASH = bytes.fromhex(
"ba1a86037c1d3c71c3af344955bd69a9a9861d9e911fa24985b677e8dbd72d43")
DEVICE_CONFIG = bytes.fromhex(
"701160712c9d2cc91ce518fd00fd00fd03ba000180ca000400840015b3860000"
"c4880000ba8a0000b28c0000aa8e0000c19000bbbb9200b1b1940000a8960000"
"b6980000009a000000d2000000d4000000d6000000d800000050000105d00000"
"00700000007200785674003412200010402a0102042200012024003200800001"
"005c008000560004205800030232000c02660003007c000058820080152a0182"
"032200012024001400800001005c000001560004205800030232000c02660003"
"007c0000588200801b2a0108005c008000540010016200040364001900660003"
"007c0001582a0108005c0010015200080054000001660003007c000158008d1e")
SENSOR_WIDTH = 80
SENSOR_HEIGHT = 88
def init_device(product: int):
device = goodix.Device(product, protocol.USBProtocol)
device.nop()
device.enable_chip(True)
device.nop()
return device
def check_psk(device: goodix.Device):
success, flags, psk = device.preset_psk_read(0xbb020003)
if not success:
raise ValueError("Failed to read PSK")
if flags != 0xbb020003:
raise ValueError("Invalid flags")
return psk == PMK_HASH
def write_psk(device: goodix.Device):
if not device.preset_psk_write(0xbb010003, PSK_WHITE_BOX):
return False
if not check_psk(device):
return False
return True
def erase_firmware(device: goodix.Device):
device.mcu_erase_app(0, False)
device.disconnect()
def update_firmware(device: goodix.Device):
firmware_file = open(f"firmware/51x7/{TARGET_FIRMWARE}.bin", "rb")
firmware = firmware_file.read()
firmware_file.close()
try:
length = len(firmware)
for i in range(0, length, 1008):
if not device.write_firmware(i, firmware[i:i + 1008]):
raise ValueError("Failed to write firmware")
if not device.check_firmware(
0, length,
crcmod.predefined.mkCrcFun("crc-32-mpeg")(firmware)):
raise ValueError("Failed to check firmware")
except Exception as error:
print(
tool.warning(
f"The program went into serious problems while trying to "
f"update the firmware: {error}"))
erase_firmware(device)
raise error
device.reset(False, True, 20)
device.disconnect()
def run_driver(device: goodix.Device):
tls_server = subprocess.Popen([
"openssl", "s_server", "-nocert", "-psk",
PSK.hex(), "-port", "4433", "-quiet"
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
success, number = device.reset(True, False, 20)
if not success:
raise ValueError("Reset failed")
if number != 2048:
raise ValueError("Invalid reset number")
if device.read_sensor_register(0x0000, 4) != b"\xa2\x04\x25\x00":
raise ValueError("Invalid chip ID")
otp = device.read_otp()
if len(otp) < 32:
raise ValueError("Invalid OTP")
success, number = device.reset(True, False, 20)
if not success:
raise ValueError("Reset failed")
if number != 2048:
raise ValueError("Invalid reset number")
if not device.upload_config_mcu(DEVICE_CONFIG):
raise ValueError("Failed to upload config")
if not device.set_powerdown_scan_frequency(100):
raise ValueError("Failed to set powerdown scan frequency")
tls_client = socket.socket()
tls_client.connect(("localhost", 4433))
try:
tool.connect_device(device, tls_client)
device.tls_successfully_established()
device.nop()
device.query_mcu_state(b"\x55", True)
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\xb2\xb2\xc2\xc2\xa7\xa7"
b"\xb6\xb6\xa6\xa6\xb6\xb6", True)
device.nav()
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x80\xb0\x80\xc0\x80\xa4"
b"\x80\xb4\x80\xa3\x80\xb4", True)
device.read_sensor_register(0x0082, 2)
device.write_sensor_register(0x0220, b"\x78\x0b")
device.write_sensor_register(0x0236, b"\xb9\x00")
device.write_sensor_register(0x0238, b"\xb8\x00")
device.write_sensor_register(0x023a, b"\xb7\x00")
encrypted_image = device.mcu_get_image(
b"\x01\x00", goodix.FLAGS_TRANSPORT_LAYER_SECURITY)
tls_client.sendall(encrypted_image)
output_image = tls_server.stdout.read(10573)
output_image = output_image[8:-5]
tool.write_pgm(tool.decode_image(output_image), SENSOR_WIDTH,
SENSOR_HEIGHT, "clear-0.pgm")
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x80\xb1\x80\xc1\x80\xa6"
b"\x80\xb6\x80\xa5\x80\xb6", True)
device.mcu_switch_to_fdt_down(
b"\x0c\x01\x80\xb1\x80\xc1\x80\xa6"
b"\x80\xb6\x80\xa5\x80\xb6", True)
device.nop()
device.query_mcu_state(b"\x55", True)
print("Waiting for finger...")
device.mcu_switch_to_fdt_down(
b"\x0c\x01\x80\xb2\x80\xc2\x80\xa7"
b"\x80\xb6\x80\xa6\x80\xb6", True)
encrypted_image = device.mcu_get_image(
b"\x01\x00", goodix.FLAGS_TRANSPORT_LAYER_SECURITY)
tls_client.sendall(encrypted_image)
output_image = tls_server.stdout.read(10573)
output_image = output_image[8:-5]
tool.write_pgm(tool.decode_image(output_image), SENSOR_WIDTH,
SENSOR_HEIGHT, "fingerprint.pgm")
device.mcu_switch_to_fdt_up(b"\x0e\x01\x80\x90\x80\x9a\x80\x7b"
b"\x80\x95\x80\x8c\x80\xa3")
tls_client.sendall(
device.mcu_get_image(b"\x01\x00",
goodix.FLAGS_TRANSPORT_LAYER_SECURITY))
output_image = tls_server.stdout.read(10573)
output_image = output_image[8:-5]
tool.write_pgm(tool.decode_image(output_image), SENSOR_WIDTH,
SENSOR_HEIGHT, "clear-1.pgm")
device.nav()
# Required after capture to keep device responding
device.mcu_switch_to_fdt_down(
b"\x0c\x01\x80\xa7\x80\xb9\x80\xa3"
b"\x80\xb5\x80\xa4\x80\xb6", False)
finally:
tls_client.close()
finally:
tls_server.terminate()
def main(product: int):
print(
tool.warning(
"This program might break your device.\n"
"Consider that it may flash the device firmware.\n"
"Continue at your own risk.\n"
"But don't hold us responsible if your device is broken!\n"
"Don't run this program as part of a regular process."))
code = random.randint(0, 9999)
if input(f"Type {code} to continue and confirm that you are not a bot: "
) != str(code):
print("Abort")
return
previous_firmware = None
device = init_device(product)
while True:
firmware = device.firmware_version()
print(f"Firmware: {firmware}")
valid_psk = check_psk(device)
print(f"Valid PSK: {valid_psk}")
if firmware == previous_firmware:
raise ValueError("Unchanged firmware")
previous_firmware = firmware
if re.fullmatch(TARGET_FIRMWARE, firmware):
if not valid_psk:
erase_firmware(device)
device = init_device(product)
continue
run_driver(device)
return
if re.fullmatch(VALID_FIRMWARE, firmware):
erase_firmware(device)
device = init_device(product)
continue
if re.fullmatch(IAP_FIRMWARE, firmware):
if not valid_psk:
if not write_psk(device):
raise ValueError("Failed to write PSK")
update_firmware(device)
device = init_device(product)
continue
raise ValueError(
"Invalid firmware\n" +
tool.warning("Please consider that removing this security "
"is a very bad idea!"))