forked from axi0mX/ipwndfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
37 lines (29 loc) · 1.08 KB
/
utilities.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
import subprocess, sys
def apply_patches(binary, patches):
for (offset, data) in patches:
binary = binary[:offset] + data + binary[offset + len(data):]
return binary
def aes_decrypt(data, iv, key):
if len(key) == 32:
aes = 128
elif len(key) == 64:
aes = 256
else:
print 'ERROR: Bad AES key given to aes_decrypt. Exiting.'
sys.exit(1)
p = subprocess.Popen(['openssl', 'enc', '-aes-%s-cbc' % aes, '-d', '-nopad', '-iv', iv, '-K', key],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate(input=data)
if p.returncode != 0 or len(stderr) > 0:
print 'ERROR: openssl failed: %s' % stderr
sys.exit(1)
return stdout
def hex_dump(data, address):
p = subprocess.Popen(['xxd', '-o', str(address)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate(input=data)
if p.returncode != 0 or len(stderr) > 0:
print 'ERROR: xxd failed: %s' % stderr
sys.exit(1)
return stdout