-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
245 lines (180 loc) · 7 KB
/
helpers.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
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
import idc
import ida_kernwin
import idaapi
import ida_auto
import ida_hexrays
import ida_funcs
import ida_nalt
from typing import List
from enum import Enum
def warn_and_exit():
ida_kernwin.warning("The Rust reverser helper has stopped early.")
exit()
def info_ex(message: str):
print(message)
ida_kernwin.info(message)
def get_instructions_from_function(function_address: int) -> List[int]:
instructions: List[int] = []
current_instruction: int = function_address
function_end_address: int = idc.find_func_end(function_address)
while current_instruction < function_end_address:
instructions.append(current_instruction)
current_instruction = idc.find_code(current_instruction, idc.SEARCH_DOWN)
return instructions
def decompile_function(func_start: int):
hf = ida_hexrays.hexrays_failure_t()
ida_hexrays.decompile_func(ida_funcs.get_func(func_start), hf)
ida_auto.auto_wait()
def get_function_details(address: int):
function_tinfo = idaapi.tinfo_t()
ida_nalt.get_tinfo(function_tinfo, address)
function_details = idaapi.func_type_data_t()
function_tinfo.get_func_details(function_details)
return function_details
class Platform():
class FileFormat(Enum):
NONE = 0
ELF = 1
PE = 2
class Architecture(Enum):
NONE = 0
X86 = 1
X64 = 2
ARM32 = 3
ARM64 = 4
# TODO: this static init stuff is bad, what if idb is changed, for example?
def __init__(self):
self.file_format = Platform.FileFormat.NONE
self.architecture = Platform.Architecture.NONE
self.platform = (self.file_format, self.architecture)
def init(self):
platform_type: str = idaapi.get_file_type_name().lower()
if "pe" in platform_type:
self.file_format = Platform.FileFormat.PE
elif "elf" in platform_type:
self.file_format = Platform.FileFormat.ELF
if "64" in platform_type:
if "amd64" in platform_type or "x86" in platform_type:
self.architecture = Platform.Architecture.X64
elif "arm" in platform_type:
self.architecture = Platform.Architecture.ARM64
elif "32" in platform_type:
if "x86" in platform_type:
self.architecture = Platform.Architecture.X86
elif "arm" in platform_type:
self.architecture = Platform.Architecture.ARM32
self.platform = (self.file_format, self.architecture)
proven_combinations = [
(Platform.FileFormat.PE, Platform.Architecture.X64),
(Platform.FileFormat.ELF, Platform.Architecture.X64),
(Platform.FileFormat.ELF, Platform.Architecture.ARM64),
]
if self.file_format == Platform.FileFormat.NONE or self.architecture == Platform.Architecture.NONE or self.platform not in proven_combinations:
print(f"Architecture is not supported yet: '{platform_type}'.")
warn_and_exit()
def is_intel_x86(self) -> bool:
return self.architecture == Platform.Architecture.X64 or self.architecture == Platform.Architecture.X86
def is_x64(self) -> bool:
return self.architecture == Platform.Architecture.X64
def is_x86(self) -> bool:
return self.architecture == Platform.Architecture.X86
def is_arm(self) -> bool:
return self.architecture == Platform.Architecture.ARM64 or self.architecture == Platform.Architecture.ARM32
def is_arm64(self) -> bool:
return self.architecture == Platform.Architecture.ARM64
def is_arm32(self) -> bool:
return self.architecture == Platform.Architecture.ARM32
def is_pe_x64(self) -> bool:
return self.platform == (Platform.FileFormat.PE, Platform.Architecture.X64)
def is_elf_x64(self) -> bool:
return self.platform == (Platform.FileFormat.ELF, Platform.Architecture.X64)
def is_64_bit(self) -> bool:
return self.architecture == Platform.Architecture.X64 or self.architecture == Platform.Architecture.ARM64
def get_platform() -> Platform:
return get_platform.platform
get_platform.platform: Platform = Platform()
def is_second_return_reg_in_operand(address: int, position: int) -> bool:
platform = get_platform()
operand: str = idc.print_operand(address, position)
if operand == "":
return False
if platform.is_intel_x86():
if "+rdx" in operand or "+edx" in operand or "+dx" in operand or "+dl" in operand:
return False
return "rdx" in operand or "edx" in operand or "dx" in operand or "dl" in operand
else:
return False
def is_moving_instruction(address: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
if platform.is_intel_x86():
return "mov" in operator
else:
return False
def is_calling_instruction(address: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
if platform.is_intel_x86():
return operator == "call"
else:
return False
def is_returning_instruction(address: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
if platform.is_intel_x86():
return operator == "retn"
else:
return False
def get_multiple_return_size() -> int:
if get_platform().is_64_bit():
return 16
else:
return 8
def is_jump(address: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
if platform.is_intel_x86():
return operator == "jmp"
else:
return False
def is_jump_outside(address: int, function_start: int, function_end: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
operand: str = idc.print_operand(address, 0)
if platform.is_intel_x86():
if operator != "jmp":
return False
if operand == "rax" or operand == "eax":
return True
destination: int = idc.get_operand_value(address, 0)
return destination < function_start or destination > function_end
else:
return False
def is_jump_dynamic(address: int) -> bool:
platform = get_platform()
operator: str = idc.print_insn_mnem(address)
operand: str = idc.print_operand(address, 0)
if platform.is_intel_x86():
if operator != "jmp":
return False
return operand == "rax" or operand == "eax"
else:
return False
def is_load_address_instruction(address: int) -> bool:
return idc.print_insn_mnem(address) == get_load_address_instruction()
def get_load_address_instruction() -> str:
platform = get_platform()
if platform.is_intel_x86():
return "lea"
elif platform.is_arm():
return "ADRL"
else:
return ""
def get_first_argument_register() -> str:
platform = get_platform()
if platform.is_x64():
return "rcx"
elif platform.is_x86():
return "ecx"
else:
return ""