Skip to content

Commit 9c73931

Browse files
committed
Refactor utilities
1 parent 8927b84 commit 9c73931

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

ipwndfu

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,21 @@
22
# ipwndfu: open-source jailbreaking tool for older iOS devices
33
# Author: axi0mX
44

5-
import binascii, datetime, getopt, hashlib, struct, subprocess, sys, time
5+
import binascii, datetime, getopt, hashlib, struct, sys, time
66
import usb # pyusb: use 'pip install pyusb' to install this module
7-
import dfu, recovery, steaks4uce, limera1n, SHAtter
7+
import dfu, recovery, steaks4uce, limera1n, SHAtter, utilities
88

99
EXEC_MAGIC = 'exec'[::-1]
1010
AES_BLOCK_SIZE = 16
1111
AES_GID_KEY = 0x20000200
1212
AES_UID_KEY = 0x20000201
1313
AES_ENCRYPT = 16
1414
AES_DECRYPT = 17
15-
SECUREROM_FILENAME_FORMAT = 'SecureROM-%s-RELEASE.dump'
16-
SRTG_FORMAT = 'SRTG:[iBoot-%s]'
1715

1816
def empty_img3_data(size):
1917
assert size >= 20
2018
return struct.pack('<4s3I4s', 'Img3'[::-1], size, 0, 0, 'zero'[::-1]) + '\x00' * (size - 20)
2119

22-
def apply_patches(file, patches):
23-
# TODO: Should always be the same for decrypted IMG3s from this script, but don't hardcode this.
24-
IMG3_DATA_OFFSET = 0x40
25-
for (offset, data) in patches:
26-
file = file[:IMG3_DATA_OFFSET + offset] + data + file[IMG3_DATA_OFFSET + offset + len(data):]
27-
return file
28-
29-
def aes_decrypt(payload, iv, key):
30-
if len(key) == 32:
31-
aesType = 128
32-
elif len(key) == 64:
33-
aesType = 256
34-
else:
35-
print 'ERROR: Bad AES key given to aes_decrypt. Exiting.'
36-
sys.exit(1)
37-
p = subprocess.Popen(['openssl', 'enc', '-aes-%s-cbc' % aesType, '-d', '-nopad', '-iv', iv, '-K', key],
38-
stdout=subprocess.PIPE,
39-
stdin=subprocess.PIPE,
40-
stderr=subprocess.PIPE)
41-
(stdoutdata, stderrdata) = p.communicate(input=payload)
42-
43-
if len(stderrdata) > 0:
44-
print 'ERROR: OpenSSL reported error: %s' % stderrdata
45-
sys.exit(1)
46-
return stdoutdata
47-
4820
class Image3:
4921
def __init__(self, data):
5022
(self.magic, self.totalSize, self.dataSize, self.signedSize, self.type) = struct.unpack('4s3I4s', data[0:20])
@@ -101,7 +73,7 @@ class Image3:
10173
keybag = self.getKeybag()
10274
device = PwnedDFUDevice()
10375
decrypted_keybag = device.decrypt_keybag(keybag)
104-
return aes_decrypt(self.getPayload(), binascii.hexlify(decrypted_keybag[:16]), binascii.hexlify(decrypted_keybag[16:]))
76+
return utilities.aes_decrypt(self.getPayload(), binascii.hexlify(decrypted_keybag[:16]), binascii.hexlify(decrypted_keybag[16:]))
10577

10678
def newDecryptedImage3(self):
10779
typeTag = self.getTags('TYPE'[::-1])
@@ -321,7 +293,7 @@ class PwnedDFUDevice():
321293

322294
self.config = None
323295
for config in configs:
324-
if SRTG_FORMAT % config.version in self.identifier:
296+
if 'SRTG:[iBoot-%s]' % config.version in self.identifier:
325297
self.config = config
326298
break
327299
if self.config is None:
@@ -571,7 +543,7 @@ class PwnedDFUDevice():
571543
(0x14954, 'run\x00'), # patch 'reset' command string to 'run'
572544
(0x17654, struct.pack('<I', 0x41000001)), # patch 'reset' command handler to LOAD_ADDRESS + 1
573545
]
574-
patchediBSS = apply_patches(decryptediBSS, n88ap_iBSS_435_patches)
546+
patchediBSS = decryptediBSS[:64] + utilities.apply_patches(decryptediBSS[64:], n88ap_iBSS_435_patches)
575547

576548
device = dfu.acquire_device()
577549
assert self.identifier == device.serial_number
@@ -733,12 +705,12 @@ if __name__ == '__main__':
733705

734706
device = PwnedDFUDevice()
735707
dump = device.read_memory(address, length)
736-
subprocess.Popen(['xxd', '-o', str(address)], stdin=subprocess.PIPE).communicate(input=dump)
708+
print utilities.hex_dump(dump, address),
737709

738710
if opt == '--dump-rom':
739711
device = PwnedDFUDevice()
740712
securerom = device.securerom_dump()
741-
filename = SECUREROM_FILENAME_FORMAT % device.config.version
713+
filename = 'SecureROM-%s-RELEASE.dump' % device.config.version
742714
f = open(filename, 'wb')
743715
f.write(securerom)
744716
f.close()

libusbfinder/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def __init__(self, version, bottle, bottle_sha256, dylib_patches, dylib_sha256):
4646
DYLIB_PATH_FORMAT = os.path.join(dir, '%s.dylib')
4747
DYLIB_NAME = 'libusb-1.0.0.dylib'
4848

49-
def apply_patches(file, patches):
49+
def apply_patches(binary, patches):
5050
for (offset, data) in patches:
51-
file = file[:offset] + data + file[offset + len(data):]
52-
return file
51+
binary = binary[:offset] + data + binary[offset + len(data):]
52+
return binary
5353

5454
def libusb1_path_internal():
5555
version = platform.mac_ver()[0]

utilities.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import subprocess, sys
2+
3+
def apply_patches(binary, patches):
4+
for (offset, data) in patches:
5+
binary = binary[:offset] + data + binary[offset + len(data):]
6+
return binary
7+
8+
def aes_decrypt(data, iv, key):
9+
if len(key) == 32:
10+
aes = 128
11+
elif len(key) == 64:
12+
aes = 256
13+
else:
14+
print 'ERROR: Bad AES key given to aes_decrypt. Exiting.'
15+
sys.exit(1)
16+
17+
p = subprocess.Popen(['openssl', 'enc', '-aes-%s-cbc' % aes, '-d', '-nopad', '-iv', iv, '-K', key],
18+
stdout=subprocess.PIPE,
19+
stdin=subprocess.PIPE,
20+
stderr=subprocess.PIPE)
21+
(stdout, stderr) = p.communicate(input=data)
22+
23+
if p.returncode != 0 or len(stderr) > 0:
24+
print 'ERROR: openssl failed: %s' % stderr
25+
sys.exit(1)
26+
27+
return stdout
28+
29+
def hex_dump(data, address):
30+
p = subprocess.Popen(['xxd', '-o', str(address)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
31+
(stdout, stderr) = p.communicate(input=data)
32+
33+
if p.returncode != 0 or len(stderr) > 0:
34+
print 'ERROR: xxd failed: %s' % stderr
35+
sys.exit(1)
36+
37+
return stdout

0 commit comments

Comments
 (0)