forked from m1stadev/Inferius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundlegen
executable file
·394 lines (316 loc) · 13 KB
/
bundlegen
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python3
from argparse import ArgumentParser
from mwclient import Site
from remotezip import RemoteZip
from tempfile import TemporaryDirectory
import bsdiff4
import json
import os
import platform
import plistlib
import requests
import shutil
import subprocess
import sys
import time
def check_bin(binary):
if shutil.which(binary) is None:
sys.exit(f"[ERROR] '{binary}' was not found on your system. Exiting.")
def create_im4p(file, output, tag=None, patch=None):
args = [
'img4',
'-i',
file,
'-o',
output
]
if tag:
args.append('-A')
args.append('-T')
args.append(tag)
if patch:
args.append('-P')
args.append(patch)
img4 = subprocess.run(args, stdout=subprocess.DEVNULL)
if img4.returncode != 0:
sys.exit(f"[ERROR] Packing '{file}' into im4p container failed. Exiting.")
def decrypt(file, output, kbag=None):
args = [
'img4',
'-i',
file,
'-o',
output
]
if kbag:
args.append('-k')
args.append(kbag)
decrypt = subprocess.run(args, stdout=subprocess.DEVNULL)
if decrypt.returncode != 0:
sys.exit(f"[ERROR] Decrypting '{file}' failed. Exiting.")
def diff_kernel(original, patched, output):
with open(original, 'rb') as f:
original = f.read()
with open(patched, 'rb') as f:
patched = f.read()
diff = list()
start_time = time.time()
for i in range(len(original)):
if round(time.time() - start_time) > 60:
sys.exit("[ERROR] Failed to generate img4 patchfile for kernel. Exiting.")
originalByte = original[i]
patchedByte = patched[i]
if originalByte != patchedByte:
diff_info = (str(hex(i)), str(hex(originalByte)), str(hex(patchedByte)))
diff.append(diff_info)
with open(output, 'w') as f:
for line in diff:
f.write(f"{' '.join(line)}\n")
def get_board(boards):
if len(boards) == 1:
return boards[0]
print('NOTE: There are multiple boardconfigs for your device! Please choose the correct boardconfig for your device:')
for board in range(len(boards)):
print(f" {board + 1}: {boards[board]['boardconfig']}")
board = input('Choice: ')
try:
return boards[int(board) - 1]
except:
sys.exit('[ERROR] Invalid input given. Exiting.')
def get_firm(firms):
if len(firms) == 1:
return firms[0]
print(f"NOTE: There are multiple builds for iOS {firms[0]['version']}! Please choose the version you wish to make a Firmware Bundle for:")
for firm in range(len(firms)):
print(f" {firm + 1}: {firms[firm]['buildid']}")
firm = input('Choice: ')
try:
return firms[int(firm) - 1]
except:
sys.exit('[ERROR] Invalid input given. Exiting.')
def mount_rdsk(rdsk, mountpoint):
args = (
'hdiutil',
'attach',
rdsk,
'-mountpoint',
mountpoint
)
mount = subprocess.run(args, stdout=subprocess.DEVNULL)
if mount.returncode != 0:
sys.exit(f"[ERROR] Mounting '{rdsk}' to '{mountpoint}' failed. Exiting.")
def parse_keys(page):
data = page.replace(' ', '').replace('|', '').splitlines()
wiki_version = page.replace('|', '').splitlines()[1].split('=')[1][1:].replace('MasterGM', 'Master|GM')
wikikeys = dict()
for x in list(data):
if x in (str(), '}}', '{{keys'):
continue
new_str = x.split('=')
try:
wikikeys[new_str[0].lower()] = new_str[1]
except IndexError:
continue
wikikeys['version'] = wiki_version
return wikikeys
def patch_bootchain(file, output):
args = (
'kairos',
file,
output
)
patch = subprocess.run(args, stdout=subprocess.DEVNULL)
if patch.returncode != 0:
sys.exit(f'[ERROR] Patching {file} failed. Exiting.')
def patch_kernel(file, output):
args = (
'Kernel64Patcher',
file,
output,
'-a'
)
patch = subprocess.run(args, stdout=subprocess.DEVNULL)
if patch.returncode != 0:
sys.exit('[ERROR] Patching kernel failed. Exiting.')
def patch_asr(file, output):
args = (
'asr64_patcher',
file,
output
)
patch = subprocess.run(args, stdout=subprocess.DEVNULL)
if patch.returncode != 0:
sys.exit(f"[ERROR] Patching '{file}' failed. Exiting.")
extract_ents = subprocess.run(f"ldid -e {file} > {file}.ents.plist", stdout=subprocess.DEVNULL, shell=True)
if extract_ents.returncode != 0:
sys.exit(f'[ERROR] Extracting {file} entitlements failed. Exiting.')
args = (
'ldid',
f'-S{file}.ents.plist',
output
)
resign = subprocess.run(args, stdout=subprocess.DEVNULL)
if resign.returncode != 0:
sys.exit(f'[ERROR] Resigning {output} failed. Exiting.')
def partialzip_extract(url, file, path):
try:
with RemoteZip(url) as f:
f.extract(file, path)
except:
sys.exit(f"[ERROR] Failed to extract '{file}'. Exiting.")
def partialzip_read(url, file):
try:
with RemoteZip(url) as f:
return f.read(file)
except:
sys.exit(f"[ERROR] Failed to read '{file}'. Exiting.")
def resize_rdsk(rdsk):
new_size = round(os.path.getsize(rdsk) / float(1<<20)) + 1
args = (
'hdiutil',
'resize',
'-size',
f'{new_size}M',
rdsk
)
resize = subprocess.run(args, stdout=subprocess.DEVNULL)
if resize.returncode != 0:
sys.exit(f'[ERROR] Resizing ramdisk failed. Exiting.')
def unmount_rdsk(mountpoint):
args = (
'hdiutil',
'detach',
mountpoint
)
unmount = subprocess.run(args, stdout=subprocess.DEVNULL)
if unmount.returncode != 0:
sys.exit(f"[ERROR] Unmounting ramdisk at '{mountpoint}' failed. Exiting.")
def main():
parser = ArgumentParser(description='Inferius Bundle Generator', usage="bundlegen -d 'Identifier' -i 'iOS version'")
parser.add_argument('-d', '--device', help='Device identifier', nargs=1)
parser.add_argument('-i', '--version', help='iOS version', nargs=1)
args = parser.parse_args()
if platform.system() != 'Darwin':
sys.exit('[ERROR] This script only works on macOS. Exiting.')
if not args.device or not args.version:
sys.exit(parser.print_help(sys.stderr))
check_bin('asr64_patcher')
check_bin('img4')
check_bin('kairos')
check_bin('Kernel64Patcher')
check_bin('ldid')
identifier = 'P'.join(args.device[0].lower().split('p'))
version = args.version[0]
try:
ipsw_api = requests.get(f'https://api.ipsw.me/v4/device/{identifier}?type=ipsw').json()
except:
sys.exit(f"[ERROR] '{identifier}' is not a valid device identifier. Exiting.")
if any(identifier.startswith(device) for device in ('iPhone8', 'iPad6,1')):
sys.exit('[ERROR] A9 devices are not currently supported. Exiting.')
if not any(firm['version'] == version for firm in ipsw_api['firmwares']):
sys.exit(f"[ERROR] 'iOS {version}' is not a valid iOS version. Exiting.")
if int(version.split('.')[0]) < 10:
sys.exit(f"[ERROR] iOS {version} is not supported at this time for firmware bundle creation. Exiting.")
with TemporaryDirectory() as tmpdir:
firm = get_firm([firm for firm in ipsw_api['firmwares'] if firm['version'] == version])
bundle_name = '_'.join((identifier, version, firm['buildid']))
bundle = f'{tmpdir}/{bundle_name}'
os.mkdir(bundle)
print(f"Creating Firmware Bundle for {identifier}, iOS {version}")
bm = plistlib.loads(partialzip_read(firm['url'], 'BuildManifest.plist'))
boardconfig = get_board(ipsw_api['boards'])['boardconfig']
identity = next(identity for identity in bm['BuildIdentities'] if identity['Info']['DeviceClass'].lower() == boardconfig.lower())
print('[1] Grabbing decryption keys...')
keypage_title = f"{identity['Info']['BuildTrain']}_{firm['buildid']}_({identifier})"
keypage = Site('www.theiphonewiki.com').pages[keypage_title]
if not keypage.exists:
sys.exit(f"[ERROR] Decryption keys for {identifier}, iOS {version} are not on The iPhone Wiki. Exiting.")
keys = parse_keys(keypage.text())
print('[2] Patching bootchain...')
for component in ('iBSS', 'iBEC'):
file = {
'name': identity['Manifest'][component]['Info']['Path'].split('/')[-1],
'ipsw_path': identity['Manifest'][component]['Info']['Path'],
'path': f"{tmpdir}/{identity['Manifest'][component]['Info']['Path']}"
}
partialzip_extract(firm['url'], file['ipsw_path'], tmpdir)
decrypt(file['path'], f"{file['path']}.raw", f"{keys[f'{component.lower()}iv']}{keys[f'{component.lower()}key']}")
patch_bootchain(f"{file['path']}.raw", f"{file['path']}.pwn")
create_im4p(f"{file['path']}.pwn", f"{file['path']}.im4p.pwn", component.lower())
bsdiff4.file_diff(file['path'], f"{file['path']}.im4p.pwn", f"{bundle}/{file['name'].rsplit('.', 1)[0]}.patch")
print('[3] Patching kernel...')
kernel = {
'name': identity['Manifest']['KernelCache']['Info']['Path'].split('/')[-1],
'ipsw_path': identity['Manifest']['KernelCache']['Info']['Path'],
'path': f"{tmpdir}/{identity['Manifest']['KernelCache']['Info']['Path']}"
}
partialzip_extract(firm['url'], kernel['ipsw_path'], tmpdir)
decrypt(kernel['path'], f"{kernel['path']}.raw")
patch_kernel(f"{kernel['path']}.raw", f"{kernel['path']}.pwn")
diff_kernel(f"{kernel['path']}.raw", f"{kernel['path']}.pwn", f"{kernel['path']}.diff")
create_im4p(kernel['path'], f"{kernel['path']}.im4p.pwn", patch=f"{kernel['path']}.diff")
bsdiff4.file_diff(kernel['path'], f"{kernel['path']}.im4p.pwn", f"{bundle}/{kernel['name']}.patch")
print('[4] Patching ramdisk. This may take a while, please wait...')
ramdisk = {
'name': identity['Manifest']['RestoreRamDisk']['Info']['Path'].split('/')[-1],
'ipsw_path': identity['Manifest']['RestoreRamDisk']['Info']['Path'],
'path': f"{tmpdir}/{identity['Manifest']['RestoreRamDisk']['Info']['Path']}"
}
partialzip_extract(firm['url'], ramdisk['ipsw_path'], tmpdir)
decrypt(ramdisk['path'], f"{ramdisk['path']}_rdsk.dmg")
mount_rdsk(f"{ramdisk['path']}_rdsk.dmg", f'{tmpdir}/ramdisk')
shutil.move(f'{tmpdir}/ramdisk/usr/sbin/asr', f'{tmpdir}/asr')
unmount_rdsk(f'{tmpdir}/ramdisk')
patch_asr(f'{tmpdir}/asr', f'{tmpdir}/asr.pwn')
os.chmod(f'{tmpdir}/asr.pwn', 0o755)
mount_rdsk(f"{ramdisk['path']}_rdsk.dmg", f'{tmpdir}/ramdisk')
try:
shutil.move(f'{tmpdir}/asr.pwn', f'{tmpdir}/ramdisk/usr/sbin/asr')
except OSError:
unmount_rdsk(f'{tmpdir}/ramdisk')
resize_rdsk(f"{ramdisk['path']}_rdsk.dmg")
mount_rdsk(f"{ramdisk['path']}_rdsk.dmg", f'{tmpdir}/ramdisk')
shutil.move(f'{tmpdir}/asr.pwn', f'{tmpdir}/ramdisk/usr/sbin/asr')
unmount_rdsk(f'{tmpdir}/ramdisk')
create_im4p(f"{ramdisk['path']}_rdsk.dmg", f"{ramdisk['path']}.im4p", 'rdsk')
bsdiff4.file_diff(ramdisk['path'], f"{ramdisk['path']}.im4p", f"{bundle}/{ramdisk['name'][:-4]}.asr.patch")
print('[5] Making Firmware Bundle...')
info = {
'identifier': identifier,
'buildid': firm['buildid'],
'update_support': False,
'boards': [
boardconfig
],
'patches': {
'required': [
{
'file': identity['Manifest']['iBSS']['Info']['Path'],
'patch': f"{identity['Manifest']['iBSS']['Info']['Path'].split('/')[-1].rsplit('.', 1)[0]}.patch"
},
{
'file': identity['Manifest']['iBEC']['Info']['Path'],
'patch': f"{identity['Manifest']['iBEC']['Info']['Path'].split('/')[-1].rsplit('.', 1)[0]}.patch"
},
{
'file': ramdisk['ipsw_path'],
'patch': f"{ramdisk['name'][:-4]}.asr.patch"
},
{
'file': kernel['ipsw_path'],
'patch': f"{kernel['name']}.patch"
}
]
}
}
with open(f'{bundle}/Info.json', 'w') as f:
json.dump(info, f, indent=4)
os.makedirs('FirmwareBundles', exist_ok=True)
bundle_path = f'FirmwareBundles/{bundle_name}.bundle'
if os.path.isfile(bundle_path):
os.remove(bundle_path)
shutil.make_archive(bundle_path, 'zip', bundle)
os.rename(f'{bundle_path}.zip', bundle_path)
print(f"Finished creating Firmware Bundle for {identifier}, iOS {version}: '{bundle_path}'.")
if __name__ == '__main__':
main()