-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsync.py
265 lines (220 loc) · 8.84 KB
/
dsync.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
# https://github.com/patois/dsync
import idaapi
from idaapi import *
"""
This plugin for IDA synchronizes Hexrays decompiler views and disassembly views (from
decompiled code to disassembly by default - use the TAB key for synchronizing from
disassembly to decompiled code).
It also highlights all addresses (code and data definitions) involved. Pressing the
hotkey Ctrl-Shift-S switches synchronization on and off. Hovering over pseudocode
items will display corresponding disassembled code in a hint window. The item that
belongs to the item that is located under the cursor will be highlighted and pointed
to by an arrow.
The plugin requires IDA 7.2.
"""
__author__ = 'Dennis Elser'
HL_COLOR = 0xAD8044
# -----------------------------------------------------------------------
class idb_hook_t(IDB_Hooks):
def __init__(self, hxehook):
self.hxehook = hxehook
IDB_Hooks.__init__(self)
def savebase(self):
self.hxehook._reset_all_colors()
return 0
# -----------------------------------------------------------------------
class hxe_hook_t(Hexrays_Hooks):
def __init__(self):
Hexrays_Hooks.__init__(self)
self.idbhook = idb_hook_t(self)
self.idbhook.hook()
self.pseudocode_instances = {}
self.n_spaces = 40
# HTC - begin
self.arranging = False
self.arrange_widgets() # Arrange already open pseudocode widgets
def arrange_widgets(self):
if self.arranging:
return
self.arranging = True
try:
wlist = []
for ch in range(ord("A"), ord("Z")):
wname = "Pseudocode-" + chr(ch)
if idaapi.find_widget(wname):
wlist.append(wname)
if len(wlist) > 0:
idaapi.set_dock_pos(wlist[0], "IDA View-A", idaapi.DP_RIGHT)
for i in range(1, len(wlist)):
idaapi.set_dock_pos(wlist[i], wlist[i - 1], idaapi.DP_TAB)
finally:
self.arranging = False
def open_pseudocode(self, vd):
self.arrange_widgets()
return 0
# HTC - end
def close_pseudocode(self, vd):
self._reset_colors(vd.view_idx, ignore_vd=True)
refresh_idaview_anyway()
return 0
def create_hint(self, vd):
result = self._get_vd_context(vd)
if result:
_, _, _, item_ea_list = result
if len(item_ea_list):
if vd.get_current_item(USE_MOUSE):
cur_item_ea = vd.item.it.ea
else:
cur_item_ea = BADADDR
lines = []
for ea in item_ea_list:
disasm_line = generate_disasm_line(ea, 0)
if disasm_line:
addr = "0x%x: " % ea
if cur_item_ea == ea:
prefix = COLSTR("==> %s" % addr, SCOLOR_INSN)
else:
prefix = " " + addr
lines.append(prefix + disasm_line)
lines.append("")
lines.append(self.n_spaces * "-")
lines.append("")
custom_hints = "\n".join(lines)
# ask decompiler to append default hints
return (2, custom_hints, len(lines))
return 0
def curpos(self, vd):
# workaround for a bug in IDA/Decompiler <= 7.2
vd.refresh_cpos(USE_KEYBOARD)
self._reset_all_colors()
self._apply_colors(vd)
refresh_idaview_anyway()
return 0
def refresh_pseudocode(self, vd):
self._reset_all_colors(ignore_vd=True)
return 0
def cleanup(self):
self._reset_all_colors()
refresh_idaview_anyway()
if self.idbhook:
self.idbhook.unhook()
self.idbhook = None
return
def _reset_colors(self, idx, ignore_vd=False):
if idx not in self.pseudocode_instances.keys(): # HTC - add
return
v = self.pseudocode_instances[idx]
if v:
pseudocode, lineno, color, disasm_lines = v
if not ignore_vd and pseudocode:
try:
pseudocode[lineno].bgcolor = color
except: # wtf
pass
for ea, color in disasm_lines:
set_item_color(ea, color)
self.pseudocode_instances.pop(idx)
return
def _reset_all_colors(self, ignore_vd=False):
# restore colors
if self.pseudocode_instances:
for k in self.pseudocode_instances.keys():
self._reset_colors(k, ignore_vd)
self.pseudocode_instances = {}
return
def _apply_colors(self, vd):
result = self._get_vd_context(vd)
if result:
pseudocode, lineno, col, item_ea_list = result
disasm_lines = [(ea, get_item_color(ea)) for ea in item_ea_list]
if len(item_ea_list):
jumpto(item_ea_list[0], -1, UIJMP_IDAVIEW | UIJMP_DONTPUSH)
self.pseudocode_instances[vd.view_idx] = (pseudocode, lineno, col, disasm_lines)
if pseudocode:
try:
pseudocode[lineno].bgcolor = HL_COLOR
except: # wtf
pass
for ea, _ in disasm_lines:
set_item_color(ea, HL_COLOR)
return
def _get_item_indexes(self, line):
indexes = []
tag = COLOR_ON + chr(COLOR_ADDR)
pos = line.find(tag)
while pos != -1 and len(line[pos+len(tag):]) >= COLOR_ADDR_SIZE:
addr = line[pos+len(tag):pos+len(tag)+COLOR_ADDR_SIZE]
idx = int(addr, 16)
a = ctree_anchor_t()
a.value = idx
if a.is_valid_anchor() and a.is_citem_anchor():
"""
print "a.value %s %d lvar %s citem %s itp %s blkcmt %s" % (
a.is_valid_anchor(),
a.get_index(),
a.is_lvar_anchor(),
a.is_citem_anchor(),
a.is_itp_anchor(),
a.is_blkcmt_anchor())
"""
indexes.append(a.get_index())
pos = line.find(tag, pos+len(tag)+COLOR_ADDR_SIZE)
return indexes
def _get_vd_context(self, vd):
if vd:
lineno = vd.cpos.lnnum
pseudocode = vd.cfunc.get_pseudocode()
if pseudocode and lineno != -1:
try:
color = pseudocode[lineno].bgcolor
line = pseudocode[lineno].line
item_idxs = self._get_item_indexes(line)
ea_list = {}
for i in item_idxs:
try:
item = vd.cfunc.treeitems.at(i)
if item and item.ea != BADADDR:
ea_list[item.ea] = None
except:
pass
return (pseudocode, lineno, color, sorted(ea_list.keys()))
except:
pass
return None
# -----------------------------------------------------------------------
def is_ida_version(min_ver_required):
return IDA_SDK_VERSION >= min_ver_required
# -----------------------------------------------------------------------
class Dsync(ida_idaapi.plugin_t):
comment = ''
help = ''
flags = PLUGIN_MOD
wanted_name = 'dsync'
wanted_hotkey = 'Ctrl-Shift-S'
hxehook = None
def init(self):
required_ver = 720
if not is_ida_version(required_ver) or not init_hexrays_plugin():
msg("[!] '%s' is inactive (IDA v%d and decompiler required).\n" % (Dsync.wanted_name, required_ver))
return PLUGIN_SKIP
msg("[%s] loaded. %s activates/deactivates synchronization.\n" % (Dsync.wanted_name, Dsync.wanted_hotkey))
return PLUGIN_KEEP
def run(self, arg):
if not Dsync.hxehook:
Dsync.hxehook = hxe_hook_t()
Dsync.hxehook.hook()
else:
Dsync.hxehook.unhook()
Dsync.hxehook.cleanup()
Dsync.hxehook = None
msg("[%s] is %sabled now.\n" % (Dsync.wanted_name, "en" if Dsync.hxehook else "dis"))
msg("[%s] hotkey is %s\n" % (Dsync.wanted_name, Dsync.wanted_hotkey))
def term(self):
msg("[%s] unloaded.\n" % (Dsync.wanted_name))
if Dsync.hxehook:
Dsync.hxehook.unhook()
Dsync.hxehook.cleanup()
Dsync.hxehook = None
# -----------------------------------------------------------------------
def PLUGIN_ENTRY():
return Dsync()