forked from goodix-fp-linux-dev/goodix-fp-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_53xd.py
321 lines (235 loc) · 10.6 KB
/
driver_53xd.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from hashlib import sha256
from hmac import new as hmac
from random import randint
from re import fullmatch
from socket import socket
from struct import pack as encode
from subprocess import PIPE, STDOUT, Popen
from goodix import FLAGS_TRANSPORT_LAYER_SECURITY_DATA, Device
from protocol import USBProtocol
from tool import connect_device, decode_image, warning, write_pgm
TARGET_FIRMWARE: str = "GF5298_GM168SEC_APP_13016"
IAP_FIRMWARE: str = "MILAN_GM168SEC_IAP_10007"
VALID_FIRMWARE: str = "GF5298_GM168SEC_APP_130[0-9]{2}"
PSK: bytes = bytes.fromhex(
"0000000000000000000000000000000000000000000000000000000000000000")
PSK_WHITE_BOX: bytes = bytes.fromhex(
"ec35ae3abb45ed3f12c4751f1e5c2cc05b3c5452e9104d9f2a3118644f37a04b"
"6fd66b1d97cf80f1345f76c84f03ff30bb51bf308f2a9875c41e6592cd2a2f9e"
"60809b17b5316037b69bb2fa5d4c8ac31edb3394046ec06bbdacc57da6a756c5")
PMK_HASH: bytes = bytes.fromhex(
"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925")
DEVICE_CONFIG: bytes = bytes.fromhex(
"701160712c9d2cc91ce518fd00fd00fd03ba000180ca0008008400bec38600b1"
"b68800baba8a00b3b38c00bcbc8e00b1b19000bbbb9200b1b194000000960000"
"00980000009a000000d2000000d4000000d6000000d800000050000105d00000"
"00700000007200785674003412200010402a0102042200012024003200800001"
"005c000101560024205800010232000402660000027c00005882007f082a0182"
"072200012024001400800001405c00e700560006145800040232000c02660000"
"027c000058820080082a0108005c000101540000016200080464001000660000"
"027c0000582a0108005c00dc005200080054000001660000027c00005820c51d")
SENSOR_WIDTH = 80
SENSOR_HEIGHT = 64
def init_device(product: int) -> Device:
device = Device(product, USBProtocol)
device.nop()
return device
def check_psk(device: Device) -> bool:
reply = device.preset_psk_read(0xbb020001, len(PMK_HASH), 0)
if not reply[0]:
raise ValueError("Failed to read PSK")
if reply[1] != 0xbb020001:
raise ValueError("Invalid flags")
return reply[2] == PMK_HASH
def write_psk(device: Device) -> bool:
if not device.preset_psk_write(0xbb010003, PSK_WHITE_BOX, 114, 0,
bytes.fromhex("56a5bb956b7c8d9e0000")):
return False
if not check_psk(device):
return False
return True
def erase_firmware(device: Device) -> None:
device.mcu_erase_app(50, True)
def update_firmware(device: Device) -> None:
firmware_file = open(f"firmware/53xd/{TARGET_FIRMWARE}.bin", "rb")
firmware = firmware_file.read()
firmware_file.close()
mod = b""
for i in range(1, 65):
mod += encode("<B", i)
raw_pmk = (encode(">H", len(PSK)) + PSK) * 2
pmk = sha256(raw_pmk).digest()
pmk_hmac = hmac(pmk, mod, sha256).digest()
firmware_hmac = hmac(pmk_hmac, firmware, sha256).digest()
try:
length = len(firmware)
for i in range(0, length, 256):
if not device.write_firmware(i, firmware[i:i + 256], 2):
raise ValueError("Failed to write firmware")
if not device.check_firmware(None, None, None, firmware_hmac):
raise ValueError("Failed to check firmware")
except Exception as error:
print(
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, 50)
device.disconnect()
def run_driver(device: Device):
tls_server = Popen([
"openssl", "s_server", "-nocert", "-psk",
PSK.hex(), "-port", "4433", "-quiet"
],
stdout=PIPE,
stderr=STDOUT)
try:
if not device.reset(True, False, 20)[0]:
raise ValueError("Reset failed")
device.read_sensor_register(0x0000, 4) # Read chip ID (0x00a6)
device.read_otp()
# OTP: 4e4e53304b2e0000517681a4aa89e409085c5c96800000f0a06ca56ea0a0746c
# e7280400980052f0072228249fa5a10000000000000000000000000009ff0000
tls_client = socket()
tls_client.connect(("localhost", 4433))
try:
connect_device(device, tls_client)
if not device.upload_config_mcu(DEVICE_CONFIG):
raise ValueError("Failed to upload config")
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00", False)
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x01", True)
device.write_sensor_register(0x022c, b"\x0a\x03")
tls_client.sendall(
device.mcu_get_image(
b"\x01\x03\x28\x01\x22\x01\x28\x01\x24\x01",
FLAGS_TRANSPORT_LAYER_SECURITY_DATA)[9:])
write_pgm(decode_image(tls_server.stdout.read(7684)[:-4]),
SENSOR_WIDTH, SENSOR_HEIGHT, "clear-0.pgm")
device.write_sensor_register(0x022c, b"\x0a\x02")
device.write_sensor_register(0x022c, b"\x0a\x03")
tls_client.sendall(
device.mcu_get_image(
b"\x81\x03\x28\x01\x22\x01\x28\x01\x24\x01",
FLAGS_TRANSPORT_LAYER_SECURITY_DATA)[9:])
write_pgm(decode_image(tls_server.stdout.read(7684)[:-4]),
SENSOR_WIDTH, SENSOR_HEIGHT, "clear-1.pgm")
device.write_sensor_register(0x022c, b"\x0a\x02")
device.write_sensor_register(0x022c, b"\x0a\x03")
tls_client.sendall(
device.mcu_get_image(
b"\x81\x03\x19\x01\x13\x01\x19\x01\x15\x01",
FLAGS_TRANSPORT_LAYER_SECURITY_DATA)[9:])
write_pgm(decode_image(tls_server.stdout.read(7684)[:-4]),
SENSOR_WIDTH, SENSOR_HEIGHT, "clear-2.pgm")
device.write_sensor_register(0x022c, b"\x0a\x02")
device.mcu_switch_to_fdt_mode(
b"\x8d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00", False)
device.mcu_switch_to_fdt_mode(
b"\x8d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x01", True)
device.write_sensor_register(0x022c, b"\x0a\x03")
tls_client.sendall(
device.mcu_get_image(
b"\x81\x03\x28\x01\x22\x01\x28\x01\x24\x01",
FLAGS_TRANSPORT_LAYER_SECURITY_DATA)[9:])
write_pgm(decode_image(tls_server.stdout.read(7684)[:-4]),
SENSOR_WIDTH, SENSOR_HEIGHT, "clear-3.pgm")
device.write_sensor_register(0x022c, b"\x0a\x02")
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00", False)
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x01", True)
device.mcu_switch_to_sleep_mode()
device.query_mcu_state(b"\x01\x01\x01", False)
device.mcu_switch_to_fdt_down(
b"\x8c\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x91\x91\x8b\x8b\x96\x96"
b"\x91\x91\x98\x98\x90\x90\x92\x92"
b"\x88\x88\x00", False)
print("Waiting for finger...")
device.mcu_switch_to_fdt_down(
b"\x8c\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x91\x91\x8b\x8b\x96\x96"
b"\x91\x91\x98\x98\x90\x90\x92\x92"
b"\x88\x88\x01", True)
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x91\x91\x8b\x8b\x96\x96"
b"\x91\x91\x98\x98\x90\x90\x92\x92"
b"\x88\x88\x00", False)
device.mcu_switch_to_fdt_mode(
b"\x0d\x01\x28\x01\x22\x01\x28\x01"
b"\x24\x01\x91\x91\x8b\x8b\x96\x96"
b"\x91\x91\x98\x98\x90\x90\x92\x92"
b"\x88\x88\x01", True)
device.write_sensor_register(0x022c, b"\x05\x03")
tls_client.sendall(
device.mcu_get_image(
b"\x41\x03\xa5\x00\x9f\x00\xa5\x00\xa1\x00",
FLAGS_TRANSPORT_LAYER_SECURITY_DATA)[9:])
write_pgm(decode_image(tls_server.stdout.read(7684)[:-4]),
SENSOR_WIDTH, SENSOR_HEIGHT, "fingerprint.pgm")
finally:
tls_client.close()
finally:
tls_server.terminate()
def main(product: int) -> None:
print(
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 = 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 fullmatch(TARGET_FIRMWARE, firmware):
if not valid_psk:
erase_firmware(device)
continue
run_driver(device)
return
if fullmatch(VALID_FIRMWARE, firmware):
erase_firmware(device)
continue
if 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" +
warning("Please consider that removing this security "
"is a very bad idea!"))