forked from borzacchiello/bncallgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
164 lines (144 loc) · 3.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
from binaryninja import (
DisassemblyTextLine,
InstructionTextToken,
InstructionTextTokenType,
FlowGraph,
FlowGraphNode,
BranchType,
enums,
PluginCommand,
Settings,
BackgroundTaskThread
)
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"
}
""")
class GraphWrapper(object):
def __init__(self, root_function):
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 = [
GraphWrapper._build_function_text(root_function)
]
self.graph.append(root_node)
self.nodes[root_function] = root_node
@staticmethod
def _build_function_text(function):
res = \
DisassemblyTextLine ([
InstructionTextToken(
InstructionTextTokenType.AddressDisplayToken,
"{:#x}".format(function.start),
value=function.start,
),
InstructionTextToken(
InstructionTextTokenType.OperandSeparatorToken,
" @ "
),
InstructionTextToken(
InstructionTextTokenType.CodeSymbolToken,
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 = [
GraphWrapper._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
self.graph.show("Callgraph starting from {}".format(self.root_function.name))
def callgraph(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.callees):
graph.add(child_func, func)
if child_func not in visited:
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")
)