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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
# Copyright (c) 2015-2020 Vector 35 Inc
#
# 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 ctypes
import binaryninja
from binaryninja import _binaryninjacore as core
from binaryninja import highlight
from binaryninja import function
from binaryninja import basicblock
from binaryninja.enums import LinearViewObjectIdentifierType
class LinearDisassemblyLine(object):
def __init__(self, line_type, func, block, contents):
self.type = line_type
self.function = func
self.block = block
self.contents = contents
def __str__(self):
return str(self.contents)
def __repr__(self):
return repr(self.contents)
class LinearViewObjectIdentifier(object):
def __init__(self, name, start = None, end = None):
self._name = name
self._start = start
self._end = end
@property
def name(self):
return self._name
@property
def address(self):
return self._start
@property
def start(self):
return self._start
@property
def end(self):
return self._end
@property
def has_address(self):
return self._start is not None
@property
def has_range(self):
return self._start is not None and self._end is not None
def __str__(self):
if not self.has_address:
return self._name
if self.has_range:
return "%s 0x%x-0x%x" % (self._name, self._start, self._end)
return "%s 0x%x" % (self._name, self._start)
def __repr__(self):
return "<LinearViewObjectIdentifier: " + str(self) + ">"
def _to_api_object(self, obj = None):
if obj is None:
result = core.BNLinearViewObjectIdentifier()
else:
result = obj
result.name = self.name
if self.has_range:
result.type = LinearViewObjectIdentifierType.AddressRangeLinearViewObject
result.start = self.start
result.end = self.end
elif self.has_address:
result.type = LinearViewObjectIdentifierType.AddressLinearViewObject
result.start = self.start
result.end = self.start
else:
result.type = LinearViewObjectIdentifierType.SingleLinearViewObject
result.start = 0
result.end = 0
return result
@classmethod
def _from_api_object(cls, obj):
if obj.type == LinearViewObjectIdentifierType.AddressLinearViewObject:
result = LinearViewObjectIdentifier(obj.name, obj.start)
elif obj.type == LinearViewObjectIdentifierType.AddressRangeLinearViewObject:
result = LinearViewObjectIdentifier(obj.name, obj.start, obj.end)
else:
result = LinearViewObjectIdentifier(obj.name)
return result
class LinearViewObject(object):
def __init__(self, handle, parent = None):
self.handle = handle
self._parent = parent
def __del__(self):
core.BNFreeLinearViewObject(self.handle)
@property
def first_child(self):
result = core.BNGetFirstLinearViewObjectChild(self.handle)
if not result:
return None
return LinearViewObject(result, self)
@property
def last_child(self):
result = core.BNGetLastLinearViewObjectChild(self.handle)
if not result:
return None
return LinearViewObject(result, self)
@property
def previous(self):
if parent is None:
return None
result = core.BNGetPreviousLinearViewObjectChild(self._parent.handle, self.handle)
if not result:
return None
return LinearViewObject(result, self._parent)
@property
def next(self):
if parent is None:
return None
result = core.BNGetNextLinearViewObjectChild(self._parent.handle, self.handle)
if not result:
return None
return LinearViewObject(result, self._parent)
@property
def start(self):
return core.BNGetLinearViewObjectStart(self.handle)
@property
def end(self):
return core.BNGetLinearViewObjectEnd(self.handle)
@property
def parent(self):
return self._parent
@property
def identifier(self):
ident = core.BNGetLinearViewObjectIdentifier(self.handle)
result = LinearViewObjectIdentifier._from_api_object(ident)
core.BNFreeLinearViewObjectIdentifier(ident)
return result
@property
def cursor(self):
root = self
while root.parent is not None:
root = root.parent
return LinearViewCursor(root)
@property
def ordering_index(self):
if self.parent is None:
return 0
return self.parent.ordering_index_for_child(self)
@property
def ordering_index_total(self):
return core.BNGetLinearViewObjectOrderingIndexTotal(self.handle)
def __len__(self):
return self.end - self.start
def __str__(self):
result = str(self.identifier)
if self._parent is not None:
result = str(self._parent) + "/" + result
return result
def __repr__(self):
return "<LinearViewObject: " + str(self) + ">"
def child_for_address(self, addr):
result = core.BNGetLinearViewObjectChildForAddress(self.handle, addr)
if not result:
return None
return LinearViewObject(result, self)
def child_for_identifier(self, ident):
ident_obj = ident._to_api_object()
result = core.BNGetLinearViewObjectChildForIdentifier(self.handle, ident_obj)
if not result:
return None
return LinearViewObject(result, self)
def child_for_ordering_index(self, idx):
result = core.BNGetLinearViewObjectChildForOrderingIndex(self.handle, idx)
if not result:
return None
return LinearViewObject(result, self)
def compare_children(self, a, b):
return core.BNCompareLinearViewObjectChildren(self.handle, a.handle, b.handle)
def get_lines(self, prev_obj, next_obj):
if prev_obj is not None:
prev_obj = prev_obj.handle
if next_obj is not None:
next_obj = next_obj.handle
count = ctypes.c_ulonglong(0)
lines = core.BNGetLinearViewObjectLines(self.handle, prev_obj, next_obj, count)
result = []
for i in range(0, count.value):
func = None
block = None
if lines[i].function:
func = binaryninja.function.Function(self, core.BNNewFunctionReference(lines[i].function))
if lines[i].block:
block = binaryninja.basicblock.BasicBlock(core.BNNewBasicBlockReference(lines[i].block), self)
color = highlight.HighlightColor._from_core_struct(lines[i].contents.highlight)
addr = lines[i].contents.addr
tokens = binaryninja.function.InstructionTextToken.get_instruction_lines(lines[i].contents.tokens, lines[i].contents.count)
contents = binaryninja.function.DisassemblyTextLine(tokens, addr, color = color)
result.append(LinearDisassemblyLine(lines[i].type, func, block, contents))
core.BNFreeLinearDisassemblyLines(lines, count.value)
return result
def ordering_index_for_child(self, child):
return core.BNGetLinearViewObjectOrderingIndexForChild(self.handle, child.handle)
@classmethod
def disassembly(cls, view, settings = None):
if settings is not None:
settings = settings.handle
return LinearViewObject(core.BNCreateLinearViewDisassembly(view.handle, settings))
class LinearViewCursor(object):
def __init__(self, root_object, handle = None):
if handle is not None:
self.handle = handle
else:
self.handle = core.BNCreateLinearViewCursor(root_object.handle)
def __del__(self):
core.BNFreeLinearViewCursor(self.handle)
@property
def before_begin(self):
return core.BNIsLinearViewCursorBeforeBegin(self.handle)
@property
def after_end(self):
return core.BNIsLinearViewCursorAfterEnd(self.handle)
@property
def valid(self):
return not (self.before_begin or self.after_end)
@property
def current_object(self):
count = ctypes.c_ulonglong(0)
path = core.BNGetLinearViewCursorPathObjects(self.handle, count)
result = None
for i in range(0, count.value):
result = LinearViewObject(core.BNNewLinearViewObjectReference(path[i]), result)
core.BNFreeLinearViewCursorPathObjects(path, count.value)
return result
@property
def path(self):
count = ctypes.c_ulonglong(0)
path = core.BNGetLinearViewCursorPath(self.handle, count)
result = []
for i in range(0, count.value):
result.append(LinearViewObjectIdentifier._from_api_object(path[i]))
core.BNFreeLinearViewCursorPath(path, count.value)
return result
@property
def path_objects(self):
count = ctypes.c_ulonglong(0)
path = core.BNGetLinearViewCursorPathObjects(self.handle, count)
result = []
parent = None
for i in range(0, count.value):
obj = LinearViewObject(core.BNNewLinearViewObjectReference(path[i]), parent)
result.append(obj)
parent = obj
core.BNFreeLinearViewCursorPathObjects(path, count.value)
return result
@property
def ordering_index(self):
return core.BNGetLinearViewCursorOrderingIndex(self.handle)
@property
def ordering_index_total(self):
return core.BNGetLinearViewCursorOrderingIndexTotal(self.handle)
def seek_to_begin(self):
core.BNSeekLinearViewCursorToBegin(self.handle)
def seek_to_end(self):
core.BNSeekLinearViewCursorToEnd(self.handle)
def seek_to_address(self, addr):
core.BNSeekLinearViewCursorToAddress(self.handle, addr)
def seek_to_path(self, path, addr = None):
if isinstance(path, LinearViewCursor):
if addr is None:
return core.BNSeekLinearViewCursorToCursorPath(self.handle, path.handle)
return core.BNSeekLinearViewCursorToCursorPathAndAddress(self.handle, path.handle, addr)
path_objs = (core.BNLinearViewObjectIdentifier * len(path))()
for i in range(0, len(path)):
path[i]._to_api_object(path_objs[i])
if addr is None:
return core.BNSeekLinearViewCursorToPath(self.handle, path_objs, len(path))
return core.BNSeekLinearViewCursorToPathAndAddress(self.handle, path_objs, len(path), addr)
def seek_to_ordering_index(self, idx):
core.BNSeekLinearViewCursorToOrderingIndex(self.handle, idx)
def previous(self):
return core.BNLinearViewCursorPrevious(self.handle)
def next(self):
return core.BNLinearViewCursorNext(self.handle)
@property
def lines(self):
count = ctypes.c_ulonglong(0)
lines = core.BNGetLinearViewCursorLines(self.handle, count)
result = []
for i in range(0, count.value):
func = None
block = None
if lines[i].function:
func = binaryninja.function.Function(self, core.BNNewFunctionReference(lines[i].function))
if lines[i].block:
block = binaryninja.basicblock.BasicBlock(core.BNNewBasicBlockReference(lines[i].block), self)
color = highlight.HighlightColor._from_core_struct(lines[i].contents.highlight)
addr = lines[i].contents.addr
tokens = binaryninja.function.InstructionTextToken.get_instruction_lines(lines[i].contents.tokens, lines[i].contents.count)
contents = binaryninja.function.DisassemblyTextLine(tokens, addr, color = color)
result.append(LinearDisassemblyLine(lines[i].type, func, block, contents))
core.BNFreeLinearDisassemblyLines(lines, count.value)
return result
def duplicate(self):
return LinearViewCursor(None, handle = core.BNDuplicateLinearViewCursor(self.handle))
def __str__(self):
return str(self.current_object)
def __repr__(self):
return "<LinearViewCursor: " + str(self.current_object) + ">"
def __eq__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) == 0
return False
def __ne__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) != 0
return True
def __lt__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) < 0
return False
def __le__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) <= 0
return False
def __gt__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) > 0
return False
def __ge__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other) >= 0
return False
def __cmp__(self, other):
if isinstance(other, LinearViewCursor):
return LinearViewCursor.compare(self, other)
return 0
@classmethod
def compare(cls, a, b):
return core.BNCompareLinearViewCursors(a.handle, b.handle)
|