-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathidassist_plugin.py
More file actions
305 lines (240 loc) · 9.86 KB
/
idassist_plugin.py
File metadata and controls
305 lines (240 loc) · 9.86 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
"""
IDAssist - IDA Pro Plugin for LLM-Assisted Reverse Engineering
This is the IDA plugin_t entry point. It registers IDAssist as a persistent
plugin that opens a dockable window with AI-powered analysis tabs.
Target: IDA Pro 9.x (uses ida_typeinf unified API, PySide6).
"""
import os
import sys
import idaapi
import ida_kernwin
import ida_idp
# Ensure our package root is importable
_PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
if _PLUGIN_DIR not in sys.path:
sys.path.insert(0, _PLUGIN_DIR)
PLUGIN_NAME = "IDAssist"
PLUGIN_HOTKEY = "Ctrl+Shift+A"
PLUGIN_COMMENT = "LLM-assisted reverse engineering"
PLUGIN_HELP = "Opens the IDAssist panel for AI-powered binary analysis"
def _load_version():
import json
try:
meta_path = os.path.join(_PLUGIN_DIR, "ida-plugin.json")
with open(meta_path, "r") as f:
return json.load(f)["plugin"]["version"]
except Exception:
return "0.0.0"
PLUGIN_VERSION = _load_version()
# ---------------------------------------------------------------------------
# Context menu action handlers
# ---------------------------------------------------------------------------
class ExplainFunctionAction(ida_kernwin.action_handler_t):
"""Context menu: Explain the current function."""
def activate(self, ctx):
try:
from src.views.idassist_form import IDAssistForm
IDAssistForm.open()
form = IDAssistForm._instance
if form and hasattr(form, 'explain_controller'):
form.tabs.setCurrentWidget(form.explain_view)
form.explain_controller.explain_function()
except Exception as e:
ida_kernwin.msg(f"[IDAssist] ERROR: {e}\n")
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_FOR_WIDGET
class RenameSuggestionsAction(ida_kernwin.action_handler_t):
"""Context menu: Get rename suggestions for the current function."""
def activate(self, ctx):
try:
from src.views.idassist_form import IDAssistForm
IDAssistForm.open()
form = IDAssistForm._instance
if form and hasattr(form, 'actions_controller'):
form.tabs.setCurrentWidget(form.actions_view)
form.actions_controller.analyze_function()
except Exception as e:
ida_kernwin.msg(f"[IDAssist] ERROR: {e}\n")
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_FOR_WIDGET
class AskAboutSelectionAction(ida_kernwin.action_handler_t):
"""Context menu: Ask about selected code in Query tab."""
def activate(self, ctx):
try:
from src.views.idassist_form import IDAssistForm
IDAssistForm.open()
form = IDAssistForm._instance
if form and hasattr(form, 'query_controller'):
form.tabs.setCurrentWidget(form.query_view)
# Pre-fill with #func macro
if hasattr(form.query_view, 'set_query_text'):
form.query_view.set_query_text("What does this function do? #func")
except Exception as e:
ida_kernwin.msg(f"[IDAssist] ERROR: {e}\n")
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_FOR_WIDGET
class ExplainHotkeyAction(ida_kernwin.action_handler_t):
"""Hotkey Ctrl+Shift+E: Explain current function."""
def activate(self, ctx):
ExplainFunctionAction().activate(ctx)
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_ALWAYS
class QueryHotkeyAction(ida_kernwin.action_handler_t):
"""Hotkey Ctrl+Shift+Q: Focus Query tab with context."""
def activate(self, ctx):
AskAboutSelectionAction().activate(ctx)
return 1
def update(self, ctx):
return ida_kernwin.AST_ENABLE_ALWAYS
# ---------------------------------------------------------------------------
# IDB Hooks for auto-refresh
# ---------------------------------------------------------------------------
class IDAssistIDBHooks(ida_idp.IDB_Hooks):
"""IDB hooks to detect renames and function changes."""
def renamed(self, ea, new_name, is_local):
"""A name was changed in the IDB."""
try:
from src.views.idassist_form import IDAssistForm
form = IDAssistForm._instance
if form and hasattr(form, 'semantic_graph_controller'):
# Mark graph as potentially stale
controller = form.semantic_graph_controller
if hasattr(controller, 'on_name_changed'):
controller.on_name_changed(ea, new_name)
except Exception:
pass
return 0
def func_added(self, pfn):
"""A new function was created."""
return 0
def func_updated(self, pfn):
"""A function was modified."""
return 0
# ---------------------------------------------------------------------------
# Popup hooks for context menus
# ---------------------------------------------------------------------------
class IDAssistPopupHooks(ida_kernwin.UI_Hooks):
"""UI hooks to add IDAssist actions to context menus."""
def finish_populating_widget_popup(self, widget, popup):
wtype = ida_kernwin.get_widget_type(widget)
# Add to disassembly and pseudocode views
if wtype in (ida_kernwin.BWN_DISASM, ida_kernwin.BWN_PSEUDOCODE):
ida_kernwin.attach_action_to_popup(
widget, popup, "idassist:explain_function", "IDAssist/")
ida_kernwin.attach_action_to_popup(
widget, popup, "idassist:rename_suggestions", "IDAssist/")
ida_kernwin.attach_action_to_popup(
widget, popup, "idassist:ask_about_selection", "IDAssist/")
# ---------------------------------------------------------------------------
# Plugin class
# ---------------------------------------------------------------------------
class _DeferredOpenHook(ida_kernwin.UI_Hooks):
"""One-shot hook to open IDAssist panel after IDA's UI is fully ready."""
def __init__(self, plugin):
super().__init__()
self._plugin = plugin
def ready_to_run(self):
self._plugin.run(0)
self.unhook()
class IDAssistPlugin(idaapi.plugin_t):
"""IDA plugin_t implementation for IDAssist."""
flags = idaapi.PLUGIN_KEEP
comment = PLUGIN_COMMENT
help = PLUGIN_HELP
wanted_name = PLUGIN_NAME
wanted_hotkey = PLUGIN_HOTKEY
def __init__(self):
super().__init__()
self._idb_hooks = None
self._popup_hooks = None
self._deferred_hook = None
def init(self):
"""Called when IDA loads the plugin. Return PLUGIN_KEEP to stay resident."""
try:
from src.ida_compat import log
log.log_info(f"IDAssist v{PLUGIN_VERSION} loaded")
# Register context menu actions
self._register_actions()
# Install IDB hooks
self._idb_hooks = IDAssistIDBHooks()
self._idb_hooks.hook()
# Install popup hooks for context menus
self._popup_hooks = IDAssistPopupHooks()
self._popup_hooks.hook()
# Defer panel open until IDA's UI is fully ready
self._deferred_hook = _DeferredOpenHook(self)
self._deferred_hook.hook()
return idaapi.PLUGIN_KEEP
except Exception as e:
ida_kernwin.msg(f"[IDAssist] ERROR: Failed to initialize: {e}\n")
return idaapi.PLUGIN_SKIP
def _register_actions(self):
"""Register all context menu and hotkey actions."""
actions = [
# Context menu actions
ida_kernwin.action_desc_t(
"idassist:explain_function",
"IDAssist: Explain Function",
ExplainFunctionAction(),
"Ctrl+Shift+E",
"Explain the current function using AI",
-1
),
ida_kernwin.action_desc_t(
"idassist:rename_suggestions",
"IDAssist: Rename Suggestions",
RenameSuggestionsAction(),
None,
"Get AI-powered rename suggestions",
-1
),
ida_kernwin.action_desc_t(
"idassist:ask_about_selection",
"IDAssist: Ask About Selection",
AskAboutSelectionAction(),
"Ctrl+Shift+Q",
"Ask AI about the current code",
-1
),
]
for desc in actions:
ida_kernwin.register_action(desc)
def run(self, arg):
"""Called when the user activates the plugin (hotkey or menu)."""
try:
from src.views.idassist_form import IDAssistForm
IDAssistForm.open()
except Exception as e:
ida_kernwin.msg(f"[IDAssist] ERROR: Failed to open panel: {e}\n")
import traceback
ida_kernwin.msg(traceback.format_exc() + "\n")
def term(self):
"""Called when IDA is shutting down."""
try:
# Unregister actions
for action_id in ["idassist:explain_function",
"idassist:rename_suggestions",
"idassist:ask_about_selection"]:
ida_kernwin.unregister_action(action_id)
# Unhook
if self._idb_hooks:
self._idb_hooks.unhook()
if self._popup_hooks:
self._popup_hooks.unhook()
# Shutdown services
from src.services.service_registry import get_service_registry
registry = get_service_registry()
if registry.is_initialized():
registry.shutdown()
from src.ida_compat import log
log.log_info("IDAssist unloaded")
except Exception:
pass
def PLUGIN_ENTRY():
"""IDA plugin entry point."""
return IDAssistPlugin()