-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLazyCross.py
More file actions
199 lines (158 loc) · 5.51 KB
/
LazyCross.py
File metadata and controls
199 lines (158 loc) · 5.51 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
import idaapi
import idautils
import idc
VERSION = "1.0.0"
ACTION_XREF = "lazycross:xref"
class XrefChoose(idaapi.Choose):
def __init__(self, title, items):
idaapi.Choose.__init__(self, title, [["Address", 30], ["Pseudocode line", 80]], embedded=False, width=100, icon=40)
self.items = items
def OnClose(self):
pass
def OnGetLine(self, n):
item = self.items[n]
return [idc.get_func_off_str(item["addr"]), item["line"]]
def OnGetSize(self):
return len(self.items)
def OnSelectLine(self, n):
idaapi.jumpto(self.items[n]["addr"])
class UI_Hook(idaapi.UI_Hooks):
def __init__(self):
idaapi.UI_Hooks.__init__(self)
def finish_populating_widget_popup(self, form, popup):
form_type = idaapi.get_widget_type(form)
if form_type == idaapi.BWN_DISASM:
ea = idaapi.get_screen_ea()
if ea != idaapi.BADADDR:
return
idaapi.attach_action_to_popup(form, popup, ACTION_XREF, None)
def HexRaysCallback(event, *args):
if event == idaapi.hxe_populating_popup:
widget, phandle, vu = args
if vu.item.get_ea() != idaapi.BADADDR:
idaapi.attach_action_to_popup(widget, phandle, ACTION_XREF, None)
return 0
class ActionHandler(idaapi.action_handler_t):
def __init__(self, action):
idaapi.action_handler_t.__init__(self)
self.action = action
def update(self, ctx):
return idaapi.AST_ENABLE_FOR_WIDGET if ctx.widget_type in [idaapi.BWN_DISASM, idaapi.BWN_PSEUDOCODE] else idaapi.AST_DISABLE_FOR_WIDGET
def activate(self, ctx):
if ctx.widget_type == idaapi.BWN_PSEUDOCODE:
vu = idaapi.get_widget_vdui(ctx.widget)
ea = vu.item.get_ea()
elif ctx.widget_type == idaapi.BWN_DISASM:
ea = idaapi.get_screen_ea()
else:
return 0
try:
idaapi.show_wait_box("Processing...")
show_xref(ea)
except KeyboardInterrupt:
print("LazyCross: User interrupted")
finally:
idaapi.hide_wait_box()
return 0
class ObjVisitor(idaapi.ctree_visitor_t):
def __init__(self, ea, cfunc):
idaapi.ctree_visitor_t.__init__(self, idaapi.CV_FAST)
self.found = []
self.target_ea = ea
self.cfunc = cfunc
def visit_expr(self, expr):
# check callee ea
if expr.obj_ea != self.target_ea:
return 0
# find top expr
e = expr
addr = expr.ea
while True:
p = self.cfunc.body.find_parent_of(e)
if not p or p.op > idaapi.cit_empty:
break
e = p
if e.ea != idaapi.BADADDR:
addr = e.ea
self.found.append({
"addr": addr,
"line": idaapi.tag_remove(e.print1(None))
})
return 0
def show_xref(ea):
name = idaapi.get_name(ea)
demangled = idc.demangle_name(name, idc.get_inf_attr(idc.INF_SHORT_DN))
if demangled:
name = demangled
print(f"LazyCross: Find cross reference to {name}...")
found = []
checked = []
for ref in idautils.XrefsTo(ea, False):
if idaapi.user_cancelled():
raise KeyboardInterrupt
frm = ref.frm
if not idaapi.is_code(idaapi.get_flags(frm)):
continue
func = idaapi.get_func(frm)
func_name = idaapi.get_func_name(frm)
if not func:
print(f"LazyCross: Reference is not from a function: 0x{frm:x}")
continue
if func.start_ea in checked:
continue
checked.append(func.start_ea)
try:
cfunc = idaapi.decompile(func)
except idaapi.DecompilationFailure as e:
print(f"LazyCross: Decompile {func_name} failed")
print(str(e))
continue
if not cfunc:
print(f"LazyCross: cfunc is none: {func_name}")
continue
cv = ObjVisitor(ea, cfunc)
try:
cv.apply_to(cfunc.body, None)
except Exception as e:
print(cfunc)
print(e)
found += cv.found
if found:
ch = XrefChoose(f"Cross references to {name}", found)
ch.Show()
else:
print("LazyCross: No xrefs found")
class LazyCross(idaapi.plugin_t):
flags = idaapi.PLUGIN_HIDE
comment = "LazyCross"
help = ""
wanted_name = "LazyCross"
wanted_hotkey = ""
def init(self):
self.hexrays_inited = False
self.action = idaapi.action_desc_t(ACTION_XREF, "LazyCross", ActionHandler(ACTION_XREF), "Ctrl+X")
idaapi.register_action(self.action)
self.ui_hook = UI_Hook()
self.ui_hook.hook()
if idaapi.init_hexrays_plugin():
addon = idaapi.addon_info_t()
addon.id = "tw.l4ys.lazycross"
addon.name = "LazyCross"
addon.producer = "Lays"
addon.url = "https://github.com/L4ys/LazyCross"
addon.version = VERSION
idaapi.register_addon(addon)
idaapi.install_hexrays_callback(HexRaysCallback)
self.hexrays_inited = True
print(f"LazyCross ({VERSION}) plugin has been loaded.")
return idaapi.PLUGIN_KEEP
def run(self, arg):
pass
def term(self):
self.ui_hook.unhook()
idaapi.unregister_action(self.action.name)
if self.hexrays_inited:
idaapi.remove_hexrays_callback(HexRaysCallback)
idaapi.term_hexrays_plugin()
def PLUGIN_ENTRY():
return LazyCross()