-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfunction_flow.py
More file actions
276 lines (184 loc) · 7.54 KB
/
function_flow.py
File metadata and controls
276 lines (184 loc) · 7.54 KB
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
import networkx as nx
import idaapi
from sark import get_codeblock, get_flowchart, get_block_start, get_nx_graph
try:
from sark.ui import ActionHandler
use_new_ui = True
except:
use_new_ui = False
COLOR_REACHABLE = 0x66EE11
COLOR_UNREACHABLE = 0x6611EE
COLOR_REACHING = 0x11EE66
COLOR_NOT_REACHING = 0x1166EE
COLOR_SOURCE = 0xEE6611
COLOR_NONE = 0xFFFFFFFF
COLOR_EXIT = 0x000048
def iter_exit_nodes(ea):
for block in get_flowchart(ea):
# Check if there are successors
for successor in block.next:
break
else:
yield block
def clear_func(ea):
for block in get_flowchart(ea):
block.color = COLOR_NONE
def mark_not_reaching_nodes(ea, source_color=COLOR_SOURCE, other_color=COLOR_NOT_REACHING):
graph = get_nx_graph(ea)
graph = graph.reverse()
block_ea = get_block_start(ea)
reaching = nx.descendants(graph, block_ea)
for node_ea in graph.nodes_iter():
if node_ea not in reaching:
get_codeblock(node_ea).color = other_color
get_codeblock(ea).color = source_color
def mark_reaching_nodes(ea, source_color=COLOR_SOURCE, other_color=COLOR_REACHING):
graph = get_nx_graph(ea)
graph = graph.reverse()
block_ea = get_block_start(ea)
for descendant in nx.descendants(graph, block_ea):
get_codeblock(descendant).color = other_color
get_codeblock(ea).color = source_color
def mark_unreachable_nodes(ea, source_color=COLOR_SOURCE, other_color=COLOR_UNREACHABLE):
graph = get_nx_graph(ea)
block_ea = get_block_start(ea)
descendants = nx.descendants(graph, block_ea)
for block in get_flowchart(ea):
if block.startEA not in descendants:
block.color = other_color
get_codeblock(ea).color = source_color
def mark_reachable_nodes(ea, source_color=COLOR_SOURCE, other_color=COLOR_REACHABLE):
graph = get_nx_graph(ea)
block_ea = get_block_start(ea)
for descendant in nx.descendants(graph, block_ea):
get_codeblock(descendant).color = other_color
get_codeblock(ea).color = source_color
def mark_exit_nodes(ea, node_color=COLOR_EXIT):
for block in iter_exit_nodes(ea):
block.color = node_color
if use_new_ui:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Start of IDA >= 6.7 Code
class MarkReachableNodesHandler(ActionHandler):
TEXT = "Reachable"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
mark_reachable_nodes(ctx.cur_ea)
class MarkUnReachableNodesHandler(ActionHandler):
TEXT = "Unreachable"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
mark_unreachable_nodes(ctx.cur_ea)
class MarkReachingNodesHandler(ActionHandler):
TEXT = "Reaching"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
mark_reaching_nodes(ctx.cur_ea)
class MarkNotReachingNodesHandler(ActionHandler):
TEXT = "Not Reaching"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
mark_not_reaching_nodes(ctx.cur_ea)
class MarkClearHandler(ActionHandler):
TEXT = "Clear"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
class MarkExits(ActionHandler):
TEXT = "Exits"
def _activate(self, ctx):
clear_func(ctx.cur_ea)
mark_exit_nodes(ctx.cur_ea)
idaapi.msg("\n" * 2)
for block in iter_exit_nodes(ctx.cur_ea):
idaapi.msg("Exit at 0x{:08X}\n".format(block.startEA))
class Hooks(idaapi.UI_Hooks):
def populating_tform_popup(self, form, popup):
# You can attach here.
pass
def finish_populating_tform_popup(self, form, popup):
# Or here, after the popup is done being populated by its owner.
if idaapi.get_tform_type(form) == idaapi.BWN_DISASM:
idaapi.attach_action_to_popup(form, popup, MarkReachableNodesHandler.get_name(), "Mark/")
idaapi.attach_action_to_popup(form, popup, MarkUnReachableNodesHandler.get_name(), "Mark/")
idaapi.attach_action_to_popup(form, popup, MarkReachingNodesHandler.get_name(), "Mark/")
idaapi.attach_action_to_popup(form, popup, MarkNotReachingNodesHandler.get_name(), "Mark/")
idaapi.attach_action_to_popup(form, popup, MarkExits.get_name(), "Mark/")
idaapi.attach_action_to_popup(form, popup, MarkClearHandler.get_name(), "Mark/")
class FunctionFlow(idaapi.plugin_t):
flags = idaapi.PLUGIN_PROC
comment = "Show Flow in Functions"
help = "Show code flow inside functions"
wanted_name = "Function Flow"
wanted_hotkey = ""
def init(self):
MarkReachableNodesHandler.register()
MarkUnReachableNodesHandler.register()
MarkReachingNodesHandler.register()
MarkNotReachingNodesHandler.register()
MarkExits.register()
MarkClearHandler.register()
self.hooks = Hooks()
self.hooks.hook()
return idaapi.PLUGIN_KEEP
def term(self):
MarkReachableNodesHandler.unregister()
MarkUnReachableNodesHandler.unregister()
MarkReachingNodesHandler.unregister()
MarkNotReachingNodesHandler.unregister()
MarkExits.unregister()
MarkClearHandler.unregister()
def run(self, arg):
pass
# End of IDA >= 6.7 Code
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else: # Old (< 6.7) ui code
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Start of IDA < 6.7 Code
def mark_reachable():
ea = idaapi.get_screen_ea()
clear_func(ea)
mark_reachable_nodes(ea)
def mark_unreachable():
ea = idaapi.get_screen_ea()
clear_func(ea)
mark_unreachable_nodes(ea)
def mark_reaching():
ea = idaapi.get_screen_ea()
clear_func(ea)
mark_reaching_nodes(ea)
def mark_not_reaching():
ea = idaapi.get_screen_ea()
clear_func(ea)
mark_not_reaching_nodes(ea)
def mark_exists():
ea = idaapi.get_screen_ea()
clear_func(ea)
mark_exit_nodes(ea)
idaapi.msg("\n" * 2)
for block in iter_exit_nodes(ea):
idaapi.msg("Exit at 0x{:08X}\n".format(block.startEA))
def mark_clear():
ea = idaapi.get_screen_ea()
clear_func(ea)
class FunctionFlow(idaapi.plugin_t):
flags = idaapi.PLUGIN_PROC
comment = "Show Flow in Functions"
help = "Show code flow inside functions"
wanted_name = "Function Flow"
wanted_hotkey = ""
def init(self):
idaapi.add_menu_item("View/Mark/", "Reachable", None, 0, mark_reachable, tuple())
idaapi.add_menu_item("View/Mark/", "Un-Reachable", None, 0, mark_unreachable, tuple())
idaapi.add_menu_item("View/Mark/", "Reaching", None, 0, mark_reaching, tuple())
idaapi.add_menu_item("View/Mark/", "Not Reaching", None, 0, mark_not_reaching, tuple())
idaapi.add_menu_item("View/Mark/", "Exists", None, 0, mark_exists, tuple())
idaapi.add_menu_item("View/Mark/", "Clear", None, 0, mark_clear, tuple())
return idaapi.PLUGIN_KEEP
def term(self):
pass
def run(self, arg):
pass
# End of IDA < 6.7 Code
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def PLUGIN_ENTRY():
return FunctionFlow()