-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__init__.py
267 lines (234 loc) · 7.6 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
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
from binaryninja import (
DisassemblyTextLine,
InstructionTextToken,
InstructionTextTokenType,
FlowGraph,
FlowGraphNode,
BranchType,
enums,
PluginCommand,
Settings,
BackgroundTaskThread,
demangle
)
Settings().register_group("bn-callgraph", "BN CallGraph")
Settings().register_setting("bn-callgraph.showColorRoot", """
{
"title" : "Colorize Root",
"type" : "boolean",
"default" : true,
"description" : "Show root node in green"
}
""")
Settings().register_setting("bn-callgraph.showColorLeaves", """
{
"title" : "Colorize Leaves",
"type" : "boolean",
"default" : true,
"description" : "Show leaves node in red"
}
""")
Settings().register_setting("bn-callgraph.showIndirectCalls", """
{
"title" : "Show Indirect Calls",
"type" : "boolean",
"default" : true,
"description" : "Show indirect calls as undetermined nodes in the graph"
}
""")
class UndeterminedFunction(object):
id_num = 0
def __init__(self, addr):
self.id = UndeterminedFunction.id_num
self.start = addr
self.name = "UndFunction_%d" % self.id
UndeterminedFunction.id_num += 1
def __hash__(self):
return hash("und_%d" % self.id)
def __eq__(self, other):
return isinstance(other, UndeterminedFunction) and self.id == other.id
class ExternalFunction(object):
def __init__(self, name):
self.start = 0
self.name = "Ext_" + name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return isinstance(other, ExternalFunction) and self.name == other.name
def demangle_name(bv, name, max_size=64):
res = name
if bv.platform.name.startswith("linux-") or bv.platform.name.startswith("mac-"):
_, demangled = demangle.demangle_gnu3(bv.arch, name)
if not isinstance(demangled, list):
res = demangled
else:
res = demangle.simplify_name_to_string(demangle.get_qualified_name(demangled))
elif bv.platform.name.startswith("windows-"):
_, demangled = demangle.demangle_ms(bv.arch, name)
if not isinstance(demangled, list):
res = demangled
else:
res = demangle.simplify_name_to_string(demangle.get_qualified_name(demangled))
if len(res) > max_size:
res = res[:max_size//2-3] + "..." + res[-max_size//2:]
return res
class GraphWrapper(object):
def __init__(self, bv, root_function):
self.bv = bv
self.nodes = {}
self.edges = set()
self.graph = FlowGraph()
self.root_function = root_function
root_node = FlowGraphNode(self.graph)
if Settings().get_bool("bn-callgraph.showColorRoot"):
root_node.highlight = enums.HighlightStandardColor.GreenHighlightColor
root_node.lines = [
self._build_function_text(root_function)
]
self.graph.append(root_node)
self.nodes[root_function] = root_node
def _build_function_text(self, function):
res = \
DisassemblyTextLine ([
InstructionTextToken(
InstructionTextTokenType.AddressDisplayToken,
"{:#x}".format(function.start),
value=function.start,
),
InstructionTextToken(
InstructionTextTokenType.OperandSeparatorToken,
" @ "
),
InstructionTextToken(
InstructionTextTokenType.CodeSymbolToken,
demangle_name(self.bv, function.name),
function.start
)
])
return res
def add(self, function, father_function):
assert father_function in self.nodes
if (father_function, function) in self.edges:
return
if function in self.nodes:
node = self.nodes[function]
else:
node = FlowGraphNode(self.graph)
node.lines = [
self._build_function_text(function)
]
self.graph.append(node)
self.nodes[function] = node
father = self.nodes[father_function]
father.add_outgoing_edge(
BranchType.UnconditionalBranch,
node
)
self.edges.add(
(father_function, function)
)
def show(self):
if Settings().get_bool("bn-callgraph.showColorLeaves"):
nodes_dst = set([edge[1] for edge in self.edges])
nodes_src = set([edge[0] for edge in self.edges])
leaves = nodes_dst - nodes_src
for leave in leaves:
self.nodes[leave].highlight = enums.HighlightStandardColor.RedHighlightColor
if Settings().get_bool("bn-callgraph.showIndirectCalls"):
for fun in self.nodes:
if isinstance(fun, UndeterminedFunction):
self.nodes[fun].highlight = enums.HighlightStandardColor.BlueHighlightColor
self.graph.show("Callgraph starting from {} @ {:#x}".format(
demangle_name(self.bv, self.root_function.name), self.root_function.start))
def callgraph(bv, current_function):
bv.update_analysis_and_wait()
graph = GraphWrapper(bv, current_function)
show_indirect = False
if Settings().get_bool("bn-callgraph.showIndirectCalls"):
show_indirect = True
visited = set()
stack = [current_function]
while stack:
func = stack.pop()
calls = set()
indirect_calls = set()
external_calls = set()
for llil_block in func.llil:
for llil in llil_block:
if llil.operation.name in {"LLIL_CALL", "LLIL_TAILCALL"}:
if llil.dest.possible_values.type.name in {"ImportedAddressValue", "UndeterminedValue"}:
if llil.dest.operation.name == "LLIL_LOAD" and llil.dest.src.possible_values.type.name == "ConstantPointerValue":
# External function
is_in_binary = False
dst_addr = llil.dest.src.possible_values.value
if dst_addr != 0:
dst_fun_addr_raw = bv.read(dst_addr, bv.arch.address_size)
if len(dst_fun_addr_raw) == bv.arch.address_size:
# Its in the binary. Probably a shared library that exports a symbol that uses
dst_fun_addr = int.from_bytes(
dst_fun_addr_raw, "little" if bv.arch.endianness.name == "LittleEndian" else "big")
dst_funs = bv.get_functions_at(dst_fun_addr)
for dst_fun in dst_funs:
calls.add(dst_fun)
is_in_binary = True
if not is_in_binary:
# The function is not here
symb = bv.get_symbol_at(dst_addr)
if symb is not None:
external_calls.add(ExternalFunction(symb.name))
elif llil.dest.possible_values.type.name == "UndeterminedValue" and show_indirect:
# Indirect call
indirect_calls.add(UndeterminedFunction(llil.address))
elif llil.dest.possible_values.type.name == "ConstantPointerValue":
dst_funs = bv.get_functions_at(llil.dest.possible_values.value)
for dst_fun in dst_funs:
calls.add(dst_fun)
elif show_indirect:
# Indirect call
indirect_calls.add(UndeterminedFunction(llil.address))
for child_func in calls | indirect_calls | external_calls:
graph.add(child_func, func)
if child_func not in visited:
if not isinstance(child_func, UndeterminedFunction) and not isinstance(child_func, ExternalFunction):
stack.append(child_func)
visited.add(func)
graph.show()
def callgraph_reversed(bv, current_function):
bv.update_analysis_and_wait()
graph = GraphWrapper(current_function)
visited = set()
stack = [current_function]
while stack:
func = stack.pop()
for child_func in set(func.callers):
graph.add(child_func, func)
if child_func not in visited:
stack.append(child_func)
visited.add(func)
graph.show()
class CallgraphThread(BackgroundTaskThread):
def __init__(self, view, function, mode):
super().__init__('Computing callgraph from {} [{}]...'.format(function.name, mode))
self.view = view
self.function = function
self.mode = mode
def run(self):
if self.mode == "reversed":
callgraph_reversed(self.view, self.function)
else:
callgraph(self.view, self.function)
def _wrapper(mode):
def f(view, function):
thread = CallgraphThread(view, function, mode)
thread.start()
return f
PluginCommand.register_for_function(
"BNCallGraph\\Compute callgraph",
"",
_wrapper("normal")
)
PluginCommand.register_for_function(
"BNCallGraph\\Compute reversed callgraph",
"",
_wrapper("reversed")
)