-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract_microcode.py
More file actions
executable file
·43 lines (41 loc) · 935 Bytes
/
extract_microcode.py
File metadata and controls
executable file
·43 lines (41 loc) · 935 Bytes
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
#!/usr/bin/env python3
import struct
import sys
import hashlib
for path in sys.argv[1:]:
print(f'Scanning {path}')
d = open(path, 'rb').read()
i = 0
j = None
threshold = 100
while i + 4 < len(d):
if j is None:
if i + 16 >= len(d):
break
w = struct.unpack('>IIII', d[i:i+16])
if (w[0] >> 17 == 0 and
w[1] >> 17 == 1 and
w[2] >> 17 == 2 and
w[3] >> 17 == 3):
#print(f'Maybe at {i}')
j = i
i += 4
else:
i += 1
else:
word = struct.unpack('>I', d[i:i+4])[0]
offset = word >> 17
#parity = (word >> 16) & 1
#payload = word & 0xffff
if offset == (i - j) // 4:
i += 4
else:
if i - j > threshold:
print(f'Found microcode at {hex(j)} of length {i - j}')
code = d[j:i]
name = hashlib.md5(code).hexdigest()
open(f'/tmp/code_{name}.bin', 'wb+').write(code)
else:
pass #print(f'Nope, too small ({i - j})')
i += 1
j = None