-
Notifications
You must be signed in to change notification settings - Fork 4
/
iBoot.py
189 lines (167 loc) · 7.18 KB
/
iBoot.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
from binaryninja import *
class iBootView(BinaryView):
long_name = "iBoot"
name = "iBoot"
PROLOGUES = [b"\xBD\xA9", b"\xBF\xA9"]
def log(self, msg, error=False):
msg = f"[iBoot-Loader] {msg}"
if not error:
log_info(msg)
else:
log_error(msg)
def __init__(self, data):
self.raw = data
self.reader = BinaryReader(data, Endianness.LittleEndian)
self.writer = BinaryWriter(data, Endianness.LittleEndian)
BinaryView.__init__(self, file_metadata=data.file, parent_view=data)
@classmethod
def is_valid_for_data(cls, data):
if (
data.read(0x200, 5) == b'iBoot'
or data.read(0x200, 4) == (b'iBEC' or b'iBSS')
or data.read(0x200, 9) == b'SecureROM'
or data.read(0x200, 9) == b'AVPBooter'
):
return True
return False
def set_name_from_func_xref(self, name, addr):
refs = list(self.get_code_refs(addr))
if len(refs) != 0:
functions = self.get_functions_containing(refs[0].address)
if len(functions) != 0:
functions[0].name = name
return functions[0].lowest_address
return None
def set_name_from_str_xref(self, name, string, partial=0):
if partial:
string_offset = [str_.start for str_ in self.strings if string in str_.value]
else:
string_offset = [str_.start for str_ in self.strings if str_.value == string]
if len(string_offset) != 0:
refs = list(self.get_code_refs(string_offset[0]))
if len(refs) != 0:
functions = list(self.get_functions_containing(refs[0].address))
if len(functions) != 0:
functions[0].name = name
return string_offset
return None
def set_name_from_pattern(self, name, pattern):
pattern_offset = self.find_next_data(0, pattern)
if pattern_offset is not None:
functions = self.get_functions_containing(pattern_offset)
if len(functions) != 0:
functions[0].name = name
return functions[0].lowest_address
return None
def on_complete(self):
self.log("AA Complete")
macho_valid_addr = self.set_name_from_pattern(
"_macho_valid", b"\x49\x01\x8B\x9A"
)
if macho_valid_addr != None:
loaded_kernelcache_addr = self.set_name_from_func_xref(
"_loaded_kernelcache", macho_valid_addr
)
if loaded_kernelcache_addr != None:
self.set_name_from_func_xref(
"_load_kernelcache", loaded_kernelcache_addr
)
self.set_name_from_str_xref("_panic", "double panic in ")
self.set_name_from_str_xref("_platform_get_usb_serial_number_string", "CPID:")
self.set_name_from_str_xref("_platform_get_usb_more_other_string", " NONC:")
self.set_name_from_str_xref("_image4_get_partial", "IMG4")
self.set_name_from_str_xref("_UpdateDeviceTree", "fuse-revision")
self.set_name_from_str_xref("_main_task", "debug-uarts")
self.set_name_from_str_xref("_platform_init_display", "backlight-level")
self.set_name_from_str_xref('_do_printf', '<null>')
self.set_name_from_str_xref('_do_memboot', 'Combo image too large\n')
self.set_name_from_str_xref('_do_go', 'Memory image not valid\n')
self.set_name_from_str_xref("_task_init", "idle task")
self.set_name_from_str_xref(
'_sys_setup_default_environment',
'/System/Library/Caches/com.apple.kernelcaches/kernelcache',
)
self.set_name_from_str_xref(
'_check_autoboot', 'aborting autoboot due to user intervention.\n'
)
self.set_name_from_str_xref(
'_do_setpict', 'picture too large, size:%zu\n'
)
self.set_name_from_str_xref(
'_arm_exception_abort', 'ARM %s abort at 0x%016llx:', 1
)
self.set_name_from_str_xref(
'_do_devicetree', 'Device Tree image not valid\n'
)
self.set_name_from_str_xref('_do_ramdisk', 'Ramdisk image not valid\n')
self.set_name_from_str_xref(
'_usb_serial_init', 'Apple USB Serial Interface'
)
self.set_name_from_str_xref(
'_nvme_bdev_create',
'Couldn\'t construct blockdev for namespace %d',
)
self.set_name_from_str_xref(
'_image4_dump_list',
'image %p: bdev %p type %c%c%c%c offset 0x%llx',
)
self.set_name_from_str_xref("_prepare_and_jump", "======== End of %s serial output. ========\n")
self.set_name_from_str_xref('_boot_upgrade_system', '/boot/kernelcache')
self.set_name_from_pattern("_plaform_early_init", b"\x60\x02\x40\x39")
self.set_name_from_pattern("_aes_crypto_cmd", b"\x89\x2C\x00\x72")
usb_vendor_id = self.set_name_from_pattern(
"_platform_get_usb_vendor_id", b"\x80\xb5\x80\x52"
)
usb_core_init = self.set_name_from_func_xref("_usb_core_init", usb_vendor_id)
self.set_name_from_func_xref("_usb_init_with_controller", usb_core_init)
def init(self):
self.arch = Architecture["aarch64"]
self.platform = Architecture["aarch64"].standalone_platform
self.isSecureROM = self.raw.read(0x200, 9) == b"SecureROM"
self.log(f"Loading {'SecureROM' if self.isSecureROM else 'iBoot'}")
self.base = None
for addr in range(0, 0x200, 4):
inst = self.raw.get_disassembly(addr, Architecture['aarch64'])
if "ldr" in inst:
self.reader.seek(int(inst.split(" ")[-1], 16))
self.base = self.reader.read64()
break
if self.base == None:
self.log("Failed to find entry point", error=True)
return False
self.log(f"Found base {hex(self.base)}")
self.add_auto_segment(
self.base,
self.parent_view.length,
0,
self.parent_view.length,
SegmentFlag.SegmentReadable | SegmentFlag.SegmentExecutable,
)
self.add_user_section(
self.name,
self.base,
self.raw.length,
SectionSemantics.ReadOnlyCodeSectionSemantics,
)
self.add_entry_point(self.base)
self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, self.base, "_start"))
bin_start = self.start
_next = 0
if self.find_next_text(0, "pacibsp"):
self.PROLOGUES.append(self.start, b"\x7F\x23\x03\xD5")
for prologue in self.PROLOGUES:
while True:
_next = self.find_next_data(
bin_start, prologue, FindFlag.FindCaseSensitive
)
if _next == bin_start:
break
elif _next is None:
continue
else:
self.create_user_function(_next)
bin_start = _next
AnalysisCompletionEvent(self, self.on_complete)
self.add_entry_point(self.base)
self.get_function_at(self.base).name = "start"
return True