forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_mlil.py
169 lines (143 loc) · 5.5 KB
/
linear_mlil.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
# Copyright (c) 2019-2025 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.
from binaryninja.function import DisassemblyTextRenderer, DisassemblyTextLine
from binaryninja.lineardisassembly import LinearDisassemblyLine
from binaryninja.enums import LinearDisassemblyLineType, DisassemblyOption
from binaryninjaui import TokenizedTextView, TokenizedTextViewHistoryEntry, ViewType
class LinearMLILView(TokenizedTextView):
def __init__(self, parent, data):
super(LinearMLILView, self).__init__(parent, data)
self.data = data
self.function = data.entry_function
if self.function is not None:
self.setFunction(self.function)
self.updateLines()
def generateLines(self):
if self.function is None:
return []
il = self.function.mlil
# Set up IL display options
renderer = DisassemblyTextRenderer(il)
renderer.settings.set_option(DisassemblyOption.ShowAddress)
renderer.settings.set_option(DisassemblyOption.ShowVariableTypesWhenAssigned)
# Sort basic blocks by IL instruction index
blocks = il.basic_blocks
list(blocks).sort(key=lambda block: block.start)
# Function header
result = []
result.append(
LinearDisassemblyLine(
LinearDisassemblyLineType.FunctionHeaderStartLineType, self.function, None,
DisassemblyTextLine([], self.function.start)
)
)
result.append(
LinearDisassemblyLine(
LinearDisassemblyLineType.FunctionHeaderLineType, self.function, None,
DisassemblyTextLine(self.function.type_tokens, self.function.start)
)
)
result.append(
LinearDisassemblyLine(
LinearDisassemblyLineType.FunctionHeaderEndLineType, self.function, None,
DisassemblyTextLine([], self.function.start)
)
)
# Display IL instructions in order
lastAddr = self.function.start
lastBlock = None
lineIndex = 0
for block in il:
if lastBlock is not None:
# Blank line between basic blocks
result.append(
LinearDisassemblyLine(
LinearDisassemblyLineType.CodeDisassemblyLineType, self.function, block, DisassemblyTextLine([], lastAddr)
)
)
for i in block:
lines = renderer.get_disassembly_text(i.instr_index)
lastAddr = i.address
lineIndex = 0
for line in lines:
result.append(
LinearDisassemblyLine(LinearDisassemblyLineType.CodeDisassemblyLineType, self.function, block, line[0])
)
lineIndex += 1
lastBlock = block
result.append(
LinearDisassemblyLine(
LinearDisassemblyLineType.FunctionEndLineType, self.function, lastBlock, DisassemblyTextLine([], lastAddr)
)
)
return result
def updateLines(self):
self.setUpdatedLines(self.generateLines())
def navigate(self, addr):
# Find correct function based on most recent use
block = self.data.get_recent_basic_block_at(addr)
if block is None:
# If function isn't done analyzing yet, it may have a function start but no basic blocks
func = self.data.get_recent_function_at(addr)
else:
func = block.function
if func is None:
# No function contains this address, fail navigation in this view
return False
self.function = func
self.setFunction(self.function)
self.setLines(self.generateLines())
return True
def getHistoryEntry(self):
class LinearMLILHistoryEntry(TokenizedTextViewHistoryEntry):
def __init__(self, function_start):
TokenizedTextViewHistoryEntry.__init__(self)
self.function_start = function_start
def serialize(self):
v = TokenizedTextViewHistoryEntry.serialize(self)
v["function_start"] = self.function_start
return v
def deserialize(self, v):
if not TokenizedTextViewHistoryEntry.deserialize(self, v):
return False
self.function_start = v["function_start"]
return True
result = LinearMLILHistoryEntry(self.function.start)
self.populateDefaultHistoryEntry(result)
return result
def navigateToHistoryEntry(self, entry):
if hasattr(entry, 'function_start'):
self.function = self.data.get_functions_at(entry.function_start)[0]
self.setFunction(self.function)
self.updateLines()
super(LinearMLILView, self).navigateToHistoryEntry(entry)
# View type for the new view
class LinearMLILViewType(ViewType):
def __init__(self):
super(LinearMLILViewType, self).__init__("Linear MLIL", "Linear MLIL")
def getPriority(self, data, filename):
if data.executable:
# Use low priority so that this view is not picked by default
return 1
return 0
def create(self, data, view_frame):
return LinearMLILView(view_frame, data)
# Register the view type so that it can be chosen by the user
ViewType.registerViewType(LinearMLILViewType())