-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshinobi.py
376 lines (312 loc) · 12.3 KB
/
shinobi.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
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
# Copyright (c) 2024 Daniel Roethlisberger
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import binaryninja as binja
#
# BinaryView finalized and initial analysis event decorators
#
def binaryview_finalized_event(func):
"""
Register decorated function as a callback to be called when a BinaryView is
finalized.
"""
binja.BinaryViewType.add_binaryview_finalized_event(func)
return func
def binaryview_initial_analysis_completion_event(func):
"""
Register decorated function as a callback to be called after the initial
analysis, as well as linear sweep and signature matcher (if they are
configured to run) completed for a BinaryView.
"""
binja.BinaryViewType.add_binaryview_initial_analysis_completion_event(func)
return func
#
# Plugin command decorators
#
def register(label, *args, **kvargs):
"""
Decorator to register the decorated function as a general plugin command.
"""
def decorator(func):
binja.PluginCommand.register(label, func.__doc__, func, *args, **kvargs)
return func
return decorator
def register_for_function(label, *args, **kvargs):
"""
Decorator to register the decorated function as a function plugin command.
"""
def decorator(func):
binja.PluginCommand.register_for_function(label, func.__doc__, func, *args, **kvargs)
return func
return decorator
def register_for_address(label, *args, **kvargs):
"""
Decorator to register the decorated function as an address plugin command.
"""
def decorator(func):
binja.PluginCommand.register_for_address(label, func.__doc__, func, *args, **kvargs)
return func
return decorator
def register_for_high_level_il_instruction(label, *args, **kvargs):
"""
Decorator to register the decorated function as a HLIL instruction plugin command.
"""
def decorator(func):
binja.PluginCommand.register_for_high_level_il_instruction(label, func.__doc__, func, *args, **kvargs)
return func
return decorator
class Task(binja.plugin.BackgroundTaskThread):
"""
Helper class to run an analysis on a background thread. Only one task per
BinaryView can be running at a given time; additional tasks are queued
until the running task has finished. Binary Ninja may or may not limit
concurrency even further. Tasks spawned before initial analysis has
completed are held until analysis is complete. Cancellation cancels queued
tasks as well.
"""
class Cancelled(Exception):
pass
def __init__(self, label, func, bv, *args, **kvargs):
if "can_cancel" in kvargs:
self.__can_cancel = kvargs["can_cancel"]
del kvargs["can_cancel"]
else:
self.__can_cancel = False
super().__init__(label, self.__can_cancel)
self.__label = label
self.__func = func
self.__bv = bv
assert isinstance(self.__bv, binja.BinaryView)
self.__args = args
self.__kvargs = kvargs
def set_progress(self, text):
self.progress = f"{self.__label}...{text}"
if self.__can_cancel and self.cancelled:
raise Task.Cancelled()
def run(self):
if not (self.__can_cancel and self.cancelled):
self.__func(self.__bv, *self.__args, **(self.__kvargs | {'set_progress': self.set_progress}))
self.finish()
assert self.__bv._x_running == self
self.__bv._x_running = None
if self.__can_cancel and self.cancelled:
while len(self.__bv._x_waiting) > 0:
task = self.__bv._x_waiting.pop(0)
task.finish()
return
assert self.__bv.has_initial_analysis()
if len(self.__bv._x_waiting) > 0:
self.__bv._x_running = self.__bv._x_waiting.pop(0)
self.__bv._x_running.start()
@classmethod
def spawn(cls, label, func, bv, *args, **kvargs):
if not hasattr(bv, "_x_running"):
bv._x_running = None
bv._x_waiting = []
task = cls(label, func, bv, *args, **kvargs)
if task.__bv._x_running is not None or not bv.has_initial_analysis():
task.__bv._x_waiting.append(task)
else:
assert task.__bv._x_running is None
assert task.__bv.has_initial_analysis()
task.__bv._x_running = task
task.__bv._x_running.start()
@classmethod
def binaryview_initial_analysis_completion_event(cls, bv):
assert bv.has_initial_analysis()
if not hasattr(bv, "_x_running"):
return
assert bv._x_running is None
if len(bv._x_waiting) > 0:
bv._x_running = bv._x_waiting.pop(0)
bv._x_running.start()
@binaryview_initial_analysis_completion_event
def _binaryview_initial_analysis_completion_event(bv):
Task.binaryview_initial_analysis_completion_event(bv)
def background_task(label="Plugin action", can_cancel=True):
"""
Decorator for plugin command functions to run them on a
background thread using Task.
This is useful, because some of Binary Ninja's APIs refuse
to work on the main thread or on a UI thread.
Unfortunately, despite running on a background thread,
there is still a lot of beach-balling going on in the UI.
But at least all the APIs can be used.
"""
def decorator(func):
def closure(*args, **kvargs):
Task.spawn(label, func, *args, can_cancel=can_cancel, **kvargs)
closure.__doc__ = func.__doc__
return closure
return decorator
def undoable(func):
"""
Decorator for plugin command functions to make them undoable.
"""
def closure(bv, *args, **kvargs):
state = bv.begin_undo_actions()
try:
func(bv, *args, **kvargs)
finally:
bv.commit_undo_actions(state)
closure.__doc__ = func.__doc__
return closure
#
# StructureBuilder extensions
#
def _append_with_offset_suffix(self, type_, name):
"""
Append a field with type and name to the StructureBuilder,
and append the offset of the field to the name.
Monkey-patching this in to avoid a lot of duplicate code.
"""
self.append(type_, name)
self.replace(len(self.members) - 1,
self.members[-1].type,
f"{self.members[-1].name}{self.members[-1].offset:x}")
return self
binja.StructureBuilder.append_with_offset_suffix = _append_with_offset_suffix
#
# BinaryView extensions
#
def _yield_symbols_of_type(self, name, type_):
"""
Find all symbols of a specific type and return a generator for them.
"""
for sym in filter(lambda x: x.type == type_, self.symbols.get(name, [])):
yield sym
binja.BinaryView.x_yield_symbols_of_type = _yield_symbols_of_type
def _get_symbol_of_type(self, name, type_):
"""
Find a symbol of a specific type and return the first one found.
"""
try:
return next(self.x_yield_symbols_of_type(name, type_))
except StopIteration:
return None
binja.BinaryView.x_get_symbol_of_type = _get_symbol_of_type
def _get_symbol_addresses_set(self, name):
"""
Find all symbols of a name and return a set of all their addresses.
"""
syms = self.symbols.get(name, [])
syms = filter(lambda sym: sym.address is not None and sym.address != 0, syms)
return set([sym.address for sym in syms])
binja.BinaryView.x_get_symbol_addresses_set = _get_symbol_addresses_set
def _parse_type(self, type_str):
"""
Like parse_type_string, but returns only the type and discards
the name. Typical use is to only pass a type without a name
in type_str.
"""
return self.parse_type_string(type_str)[0]
binja.BinaryView.x_parse_type = _parse_type
def _make_data_var(self, address, type_, name=None):
"""
Make a data var of given type and name at address.
If a data var already exists, its name and type are set.
If a data var does not exist, it is created.
"""
data_var = self.get_data_var_at(address)
if data_var is None:
if name is not None:
self.define_data_var(address, type_, name)
else:
self.define_data_var(address, type_)
else:
if name is not None:
data_var.name = name
data_var.type = type_
binja.BinaryView.x_make_data_var = _make_data_var
def _reload_hlil_instruction(self, hlil_insn, predicate=None):
"""
Refresh the instruction and the function it is associated with.
This is useful after setting the type of an operand in situations
where there is a need to examine the instruction and function
after applying the new type.
If no predicate is given, return the first instruction at the
same address. If a predicate is given, return the first
instruction at the same address that matches the predicate.
"""
reloaded_func = self.get_function_at(hlil_insn.function.source_function.start)
for insn in reloaded_func.hlil.instructions:
if insn.address == hlil_insn.address:
if predicate is not None and not predicate(insn):
continue
reloaded_insn = insn
break
else:
reloaded_insn = None
assert reloaded_insn is not None
return reloaded_insn
binja.BinaryView.x_reload_hlil_instruction = _reload_hlil_instruction
def _get_byte_string_at(self, addr, min_len=0):
"""
Read a NUL-terminated byte string from address.
Returns bytes including the terminating NUL.
The first min_len bytes can contain
non-terminating NUL characters.
Does not attempt to decode the string and will
happily read invalid UTF-8.
Does not create a StringReference.
Does not annotate anything.
"""
br = binja.BinaryReader(self)
br.seek(addr)
octets = []
while len(octets) < min_len:
octets.append(br.read8())
while len(octets) == 0 or octets[-1] != 0:
octets.append(br.read8())
if any([c is None for c in octets]):
return None
return bytes(octets)
binja.BinaryView.x_get_byte_string_at = _get_byte_string_at
#
# Other helpers
#
def yield_struct_field_assign_hlil_instructions_for_var_id(hlil_func, var_id):
"""
Find all HLIL instructions that assign to struct fields of
a struct with a given variable identifier.
Note that variable identifiers may change across type changes
in the function.
"""
for insn in hlil_func.instructions:
if not isinstance(insn, binja.HighLevelILAssign):
continue
if not isinstance(insn.dest, binja.HighLevelILStructField):
continue
if isinstance(insn.dest.src, binja.HighLevelILVar):
stack_var = insn.dest.src.var
elif isinstance(insn.dest.src, binja.HighLevelILArrayIndex):
if not isinstance(insn.dest.src.src, binja.HighLevelILVar):
continue
stack_var = insn.dest.src.src.var
elif isinstance(insn.dest.src, binja.HighLevelILStructField):
if not isinstance(insn.dest.src.src, binja.HighLevelILVar):
continue
stack_var = insn.dest.src.src.var
elif isinstance(insn.dest.src, binja.HighLevelILDerefField):
continue
else:
raise NotImplementedError(f"Unhandled destination source type {type(insn.dest.src).__name__} in assign insn {insn!r}, fix plugin")
if stack_var.identifier != var_id:
continue
yield insn