-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
219 lines (178 loc) · 6.91 KB
/
__init__.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
import typing
from PySide6.QtCore import Qt
from PySide6.QtGui import QImage
from pathlib import Path
from PySide6.QtWidgets import QVBoxLayout, QLabel, QWidget, QSizePolicy
from binaryninjaui import (
DisassemblyContainer,
Sidebar,
SidebarWidget,
SidebarWidgetType,
FlowGraphWidget,
View,
)
from binaryninja import (
BinaryView,
Function,
FlowGraph,
)
from binaryninja.enums import FunctionGraphType
root = Path(__file__).parent
class HierarchyGraph(FlowGraphWidget):
def __init__(self, parent: QWidget, view: BinaryView, graph) -> None:
FlowGraphWidget.__init__(self, parent, view, None)
class HierarchyWindow(SidebarWidget):
supported_view_types = [
FunctionGraphType.LiftedILFunctionGraph,
FunctionGraphType.LowLevelILFunctionGraph,
FunctionGraphType.MediumLevelILFunctionGraph,
FunctionGraphType.HighLevelILFunctionGraph,
FunctionGraphType.LowLevelILSSAFormFunctionGraph,
FunctionGraphType.MediumLevelILSSAFormFunctionGraph,
FunctionGraphType.HighLevelILSSAFormFunctionGraph,
]
def __init__(self, name, _frame, bv: typing.Optional[BinaryView] = None):
SidebarWidget.__init__(self, name)
self._function = None
self._bv = None
self._il_index = None
self._offset = None
self._view_type = None
# Configures configured_arch, _bv, and arch_explainer
self._layout = QVBoxLayout(self)
self._layout.setAlignment(Qt.AlignVCenter | Qt.AlignHCenter)
self._hidden = QLabel()
self._hidden.setText("No IL instruction selected")
self._hidden.setWordWrap(True)
self._layout.addWidget(self._hidden)
self._graph = HierarchyGraph(self, bv, None)
self._graph.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self._graph.hide()
self._layout.addWidget(self._graph)
self.update_needed = False
self.bv = bv
self.function = None
self.view_widget = None
def update(self):
if not self.update_needed:
return
graph = self.current_il_hierarchy
if graph:
self._graph.show()
self._hidden.hide()
self._graph.setGraph(self.current_il_hierarchy)
else:
self._graph.hide()
self._hidden.show()
self.update_needed = False
@property
def bv(self) -> typing.Optional[BinaryView]:
return self._bv
@bv.setter
def bv(self, new_bv: typing.Optional[BinaryView]):
self._bv = new_bv
@property
def offset(self) -> typing.Optional[int]:
return self._offset
@property
def view_widget(self) -> typing.Optional[View]:
return self._view_widget
@view_widget.setter
def view_widget(self, value):
if isinstance(value, DisassemblyContainer):
value = value.getView()
if not isinstance(value, View):
value = None
self._view_widget = value
if not value:
self.view_type = None
self.il_index = None
self.function = None
else:
self.view_type = self._view_widget.getILViewType()
self.il_index = self._view_widget.getCurrentILInstructionIndex()
self.function = self._view_widget.getCurrentFunction()
@property
def current_il(self):
if any([
self.il_index is None,
self.function is None,
self.bv is None,
self.view_type not in self.supported_view_types,
]):
return None
try:
if self.view_type == FunctionGraphType.LiftedILFunctionGraph:
return self.function.lifted_il[self.il_index]
if self.view_type == FunctionGraphType.LowLevelILFunctionGraph:
return self.function.llil[self.il_index]
if self.view_type == FunctionGraphType.MediumLevelILFunctionGraph:
return self.function.mlil[self.il_index]
if self.view_type == FunctionGraphType.HighLevelILFunctionGraph:
return self.function.hlil[self.il_index]
if self.view_type == FunctionGraphType.LowLevelILSSAFormFunctionGraph:
return self.function.llil.ssa_form[self.il_index]
if self.view_type == FunctionGraphType.MediumLevelILSSAFormFunctionGraph:
return self.function.mlil.ssa_form[self.il_index]
if self.view_type == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
return self.function.hlil.ssa_form[self.il_index]
except IndexError:
return None
@property
def current_il_hierarchy(self) -> typing.Optional[FlowGraph]:
if not self.current_il:
return None
return self.current_il.add_subgraph(FlowGraph(), {})
@property
def view_type(self) -> FunctionGraphType:
return self._view_type
@view_type.setter
def view_type(self, value: FunctionGraphType):
if self._view_type != value:
self.update_needed = True
self._view_type = value
self.update()
@property
def il_index(self) -> typing.Optional[int]:
return self._il_index
@il_index.setter
def il_index(self, value: typing.Optional[int]):
if value == 0xffff_ffff_ffff_ffff:
value = None
if self._il_index != value:
self.update_needed = True
self._il_index = value
self.update()
@property
def function(self) -> typing.Optional[Function]:
return self._function
@function.setter
def function(self, value: typing.Optional[Function]):
if self._function != value:
self.update_needed = True
self._function = value
self.update()
def offset(self, offset: typing.Optional[int]):
self._offset = offset
def notifyOffsetChanged(self, offset):
self.offset = offset
def notifyViewChanged(self, view_frame):
if view_frame is None:
self.bv = None
self.view_widget = None
else:
self.view_widget = view_frame.getCurrentWidget()
view = view_frame.getCurrentViewInterface()
self.bv = view.getData()
class HierarchySidebarWidgetType(SidebarWidgetType):
def __init__(self):
# Icon is Hierarchy by Bieu Tuong from NounProject.com
# https://thenounproject.com/icon/hierarchy-4512362/
icon = QImage(str(root.joinpath("icon.png")))
SidebarWidgetType.__init__(self, icon, "IL Hierarchy")
def createWidget(self, frame, data):
# This callback is called when a widget needs to be created for a given context. Different
# widgets are created for each unique BinaryView. They are created on demand when the sidebar
# widget is visible and the BinaryView becomes active.
return HierarchyWindow("IL Hierarchy", frame, data)
Sidebar.addSidebarWidgetType(HierarchySidebarWidgetType())