-
Notifications
You must be signed in to change notification settings - Fork 7
/
sephelper.py
204 lines (153 loc) · 7.57 KB
/
sephelper.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import sys
import idc
import idaapi
import ida_bytes
import ida_funcs
# Check the IDA version and Python version
ida_version = float(idaapi.get_kernel_version())
python_version = sys.version_info
if ida_version < 9.0:
# For IDA versions under 9.0, use additional imports
import ida_search
def search_for_bytes(prologue_bytes, ea, segment_end):
if ida_version < 9.0:
# For IDA versions under 9.0, use idc.search_bytes
return ida_search.find_binary(ea, segment_end, prologue_bytes, 0x10, ida_search.SEARCH_DOWN)
else:
# For IDA versions 9.0 and above, use ida_bytes.search
return idc.find_bytes(prologue_bytes, ea, range_end=segment_end)
def func64(base_ea, base_end_ea, name, sequence):
seq_ea = search_for_bytes(sequence, base_ea, base_end_ea)
if seq_ea != idc.BADADDR:
func = idaapi.get_func(seq_ea)
if func is not None:
print(" [sephelper]: %s = 0x%x" % (name, func.start_ea))
idc.set_name(func.start_ea, name, idc.SN_CHECK)
return func.start_ea
print(" [sephelper]: %s = NULL" % name)
return idc.BADADDR
# Registers.
# https://siguza.github.io/APRR/
# https://gist.github.com/bazad/42054285391c6e0dcd0ede4b5f969ad2
def find_function(seg_start, seg_end):
func64(seg_start, seg_end, "_DEROidCompare", "a1 01 00 b4 02 05 40 f9")
func64(seg_start, seg_end, "_DERImg4Decode", "61 03 00 54 88 26 40 a9")
func64(seg_start, seg_end, "_DERParseBoolean", "08 01 40 39 1f fd 03 71")
func64(seg_start, seg_end, "_DERParseInteger", "00 01 00 35 e8 07 40 f9")
func64(seg_start, seg_end, "_DERParseSequence", "e0 01 00 35 e8 07 40 f9")
func64(seg_start, seg_end, "_DERDecodeSeqNext", "e8 03 00 f9 28 01 08 cb")
func64(seg_start, seg_end, "_DERParseInteger64", "0b 15 40 38 4b dd 78 b3")
func64(seg_start, seg_end, "_DERParseBitString", "08 00 80 d2 5f 00 00 39")
func64(seg_start, seg_end, "_DERImg4DecodePayload", "33 03 00 b4 09 01 40 f9")
func64(seg_start, seg_end, "_DERImg4DecodeProperty", "e8 07 40 b9 08 09 43 b2")
func64(seg_start, seg_end, "_DERParseSequenceContent", "ec 03 8c 1a 2d 69 bc 9b")
func64(seg_start, seg_end, "_DERDecodeSeqContentInit", "09 04 40 f9 08 01 09 8b")
func64(seg_start, seg_end, "_DERImg4DecodeTagCompare", "f3 03 01 aa 08 04 40 f9")
func64(seg_start, seg_end, "_DERImg4DecodeRestoreInfo", "a1 29 a9 52 41 8a 86 72")
func64(seg_start, seg_end, "_DERImg4DecodeFindProperty", "00 00 80 52 a8 0a 43 b2")
func64(seg_start, seg_end, "_DERImg4DecodeFindInSequence", "60 02 80 3d fd 7b 44 a9")
func64(seg_start, seg_end, "_DERDecodeItemPartialBufferGetLength", "09 04 40 f9 3f 09 00 f1")
func64(seg_start, seg_end, "_DERImg4DecodeParseManifestProperties", "80 02 80 3d a1 3a 00 91")
func64(seg_start, seg_end, "_Img4DecodeEvaluateDictionaryProperties", "e0 03 1f 32 0a fd 7e d3")
func64(seg_start, seg_end, "_Img4DecodeGetPropertyBoolean", "21 08 43 b2 e0 03 00 91")
func64(seg_start, seg_end, "_Img4DecodeCopyPayloadDigest", "?? ?? 02 91 e0 03 15 aa")
func64(seg_start, seg_end, "_Img4DecodeGetPropertyData", "00 00 80 52 e8 17 40 f9")
func64(seg_start, seg_end, "_Img4DecodeGetPayload", "00 81 c9 3c 20 00 80 3d")
func64(seg_start, seg_end, "_Img4DecodeInit", "20 01 00 35 c0 c2 00 91")
func64(seg_start, seg_end, "_ccn_n", "63 04 00 91 5f 00 00 f1")
func64(seg_start, seg_end, "_ccn_cmp", "7f 00 05 eb c0 80 80 9a")
func64(seg_start, seg_end, "_ccn_sub", "84 00 04 eb 40 00 00 b5")
func64(seg_start, seg_end, "_ccn_add", "84 00 00 b1 40 00 00 b5")
func64(seg_start, seg_end, "_cc_muxp", "08 c1 20 cb 28 00 08 8a")
func64(seg_start, seg_end, "_cchmac_init", "69 22 00 91 8a 0b 80 52")
func64(seg_start, seg_end, "_ccdigest_init", "f4 03 00 aa 60 22 00 91")
func64(seg_start, seg_end, "_ccdigest_update", "e1 00 00 54 81 fe 46 d3")
func64(seg_start, seg_end, "_verify_chain_signatures", "?? 09 00 b4 68 12 40 f9")
func64(seg_start, seg_end, "_read_counter_py_reg_el0", "20 e0 3b d5")
func64(seg_start, seg_end, "_write_ktrr_unknown_el1", "a0 f2 1c d5")
func64(seg_start, seg_end, "_boot_check_panic", "49 00 c0 d2 09 21 a8 f2")
func64(seg_start, seg_end, "_verify_pkcs1_sig", "68 0e 00 54 a1 12 40 f9")
func64(seg_start, seg_end, "_parse_extensions", "e9 23 00 91 35 81 00 91")
func64(seg_start, seg_end, "_read_ctrr_lock", "40 f2 3c d5")
func64(seg_start, seg_end, "_reload_cache", "1f 87 08 d5")
func64(seg_start, seg_end, "_parse_chain", "5a 3d 00 12 77 3d 00 12")
func64(seg_start, seg_end, "_memset", "21 1c 40 92 e3 c3 00 b2")
func64(seg_start, seg_end, "_memcpy", "63 80 00 91 63 e8 7b 92")
func64(seg_start, seg_end, "_bzero", "63 e4 7a 92 42 00 00 8b")
func64(seg_start, seg_end, "_panic", "e8 03 00 91 16 81 00 91") # doubt
def file_info(version):
if python_version >= (3, 0):
version = version.decode('utf-8')
version = int(version.split(".")[0])
# 64bit (A17+), 64bit (A15+), 32bit (A7-A10X)
return (2, 0x2a0000000) if version >= 834 else (2, 0x25c000000) if version >= 520 else (1, 0x10000000)
def accept_file(fd, fname):
ret = 0
global segbit
global base_addr
if not isinstance(fname, str):
return ret
fd.seek(0xc00) # 64bit SEPROM versions are located at 0xC00
search = fd.read(0x1A)
if search.startswith(b"private_build...("):
segbit = 2
base_addr = 0x240000000 # 64bit (A11+)
return { "format": "SEPROM (AArch64)", "processor": "arm" }
elif search.startswith(b"AppleSEPROM-"):
version = search[12:]
segbit, base_addr = file_info(version)
return { "format": "SEPROM (AArch64)", "processor": "arm" }
fd.seek(0x800) # 32bit SEPROM versions are located at 0x800
search = fd.read(0x10)
if search.startswith(b"AppleSEPROM-"):
version = search[12:]
segbit, base_addr = file_info(version)
return { "format": "SEPROM (AArch32)", "processor": "arm" }
return ret
def load_file(fd, flags, format):
ea = 0
size = 0
print("[sephelper]: starting...")
if segbit == 1:
print("[sephelper]: detected a 32bit SEPROM !")
idaapi.set_processor_type("arm:armv7-m", idaapi.SETPROC_LOADER_NON_FATAL)
else:
print("[sephelper]: detected a 64bit SEPROM !")
idc.set_processor_type("arm:armv8-a", idc.SETPROC_LOADER_NON_FATAL)
if (flags & idaapi.NEF_RELOAD) != 0: return 1
fd.seek(0x0, idaapi.SEEK_END)
size = fd.tell()
segm = idaapi.segment_t()
segm.bitness = segbit
segm.start_ea = 0x0
segm.end_ea = size
idaapi.add_segm_ex(segm, "SEPROM", "CODE", idaapi.ADDSEG_OR_DIE)
fd.seek(0x0)
fd.file2base(0x0, 0x0, size, False)
print("[sephelper]: adding entry point...")
idaapi.add_entry(0x0, 0x0, "_start", 1)
ida_funcs.add_func(ea)
print("[sephelper]: base_addr = 0x%x" % base_addr)
idaapi.rebase_program(base_addr, idc.MSF_NOFIX)
print("[sephelper]: analyzing...")
ea = base_addr
segment_end = idc.get_segm_attr(base_addr, idc.SEGATTR_END)
hexcode = ["BF A9", "BD A9"] # there is not PAC on SEPROM
if segbit == 1:
hexcode = ["03 AF", "02 AF", "01 AF"]
for prologue in hexcode:
if python_version >= (3, 0):
prologue = bytes.fromhex(prologue.replace(" ", ""))
while ea < segment_end:
ea = search_for_bytes(prologue, ea, segment_end)
if ea != idc.BADADDR:
ea = ea - 0x2
if (ea % 0x4) == 0 and ida_bytes.get_full_flags(ea) < 0x200:
ida_funcs.add_func(ea)
ea = ea + 0x4
idc.plan_and_wait(base_addr, segment_end)
print('[sephelper]: finding some functions...')
if segbit == 2:
find_function(segm.start_ea, segment_end) # only for 64bit SEPROMs
print('[sephelper]: done !')
return 1