-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAPI.py
More file actions
284 lines (214 loc) · 8.15 KB
/
Copy pathSearchAPI.py
File metadata and controls
284 lines (214 loc) · 8.15 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
__AUTHOR__ = 'Alya Gomaa'
PLUGIN_NAME = "Search API"
PLUGIN_HOTKEY = 'Ctrl+Alt+]'
VERSION = 'v1.0'
import os
import idc
import idaapi
import idautils
import webbrowser
major, minor = map(int, idaapi.get_kernel_version().split("."))
using_ida7api = (major > 6)
using_pyqt5 = using_ida7api or (major == 6 and minor >= 9)
if using_pyqt5:
import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
from PyQt5.Qt import QApplication
else:
import PySide.QtGui as QtGui
import PySide.QtCore as QtCore
QtWidgets = QtGui
QtCore.pyqtSignal = QtCore.Signal
QtCore.pyqtSlot = QtCore.Slot
from PySide.QtGui import QApplication
def PLUGIN_ENTRY():
"""
Required plugin entry point for IDAPython Plugins.
"""
return api_search()
class api_search(idaapi.plugin_t):
"""
The IDA Plugin for google search.
"""
flags = idaapi.PLUGIN_PROC | idaapi.PLUGIN_HIDE
comment = "Google Search API"
help = "Click instruction and right-click 'Google Search'"
wanted_name = "Search API"
wanted_hotkey = PLUGIN_HOTKEY
#--------------------------------------------------------------------------
# Plugin Overloads
#--------------------------------------------------------------------------
def init(self):
"""
This is called by IDA when it is loading the plugin.
"""
# initialize the menu actions our plugin will inject
self._init_action_google_search()
# initialize plugin hooks
self._init_hooks()
idaapi.msg("%s %s initialized...\n" % (self.wanted_name, VERSION))
return idaapi.PLUGIN_KEEP
def run(self, arg):
"""
This is called by IDA when this file is loaded as a script.
"""
idaapi.msg("%s cannot be run as a script.\n" % self.wanted_name)
def term(self):
"""
This is called by IDA when it is unloading the plugin.
"""
# unhook our plugin hooks
self._hooks.unhook()
# unregister our actions & free their resources
self._del_action_google_search()
# done
idaapi.msg("%s terminated...\n" % self.wanted_name)
#--------------------------------------------------------------------------
# Plugin Hooks
#--------------------------------------------------------------------------
def _init_hooks(self):
"""
Install plugin hooks into IDA.
"""
self._hooks = Hooks()
self._hooks.ready_to_run = self._init_hexrays_hooks
self._hooks.hook()
def _init_hexrays_hooks(self):
"""
Install Hex-Rrays hooks (when available).
NOTE: This is called when the ui_ready_to_run event fires.
"""
if idaapi.init_hexrays_plugin():
idaapi.install_hexrays_callback(self._hooks.hxe_callback)
#--------------------------------------------------------------------------
# IDA Actions
#--------------------------------------------------------------------------
ACTION_GOOGLE_SEARCH = "prefix:google_search"
def _init_action_google_search(self):
"""
Register the google search action with IDA.
"""
# describe the action
action_desc = idaapi.action_desc_t(
self.ACTION_GOOGLE_SEARCH, # The action name.
"Google Search", # The action text.
IDACtxEntry(google_search), # The action handler.
None, # Optional: action shortcut
"Search for the selected API" # Optional: tooltip
)
# register the action with IDA
assert idaapi.register_action(action_desc), "Action registration failed"
def _del_action_google_search(self):
"""
Delete the action from IDA.
"""
idaapi.unregister_action(self.ACTION_GOOGLE_SEARCH)
#------------------------------------------------------------------------------
# Plugin Hooks
#------------------------------------------------------------------------------
class Hooks(idaapi.UI_Hooks):
def ready_to_run(self):
"""
UI ready to run -- an IDA event fired when everything is spunup.
NOTE: this is a placeholder func, it gets replaced on a live instance
but we need it defined here for IDA 7.2+ to properly hook it.
"""
pass
def finish_populating_widget_popup(self, widget, popup):
"""
A right click menu is about to be shown. (IDA 7)
"""
inject_api_search_actions(widget, popup, idaapi.get_widget_type(widget))
return 0
def finish_populating_tform_popup(self, form, popup):
"""
A right click menu is about to be shown. (IDA 6.x)
"""
inject_api_search_actions(form, popup, idaapi.get_tform_type(form))
return 0
def hxe_callback(self, event, *args):
"""
HexRays event callback.
We lump this under the (UI) Hooks class for organizational reasons.
"""
#
# if the event callback indicates that this is a popup menu event
# (in the hexrays window), we may want to install our menu
# actions depending on what the cursor right clicked.
#
if event == idaapi.hxe_populating_popup:
form, popup, vu = args
idaapi.attach_action_to_popup(
form,
popup,
api_search.ACTION_GOOGLE_SEARCH,
"Google Search",
idaapi.SETMENU_APP
)
# done
return 0
#------------------------------------------------------------------------------
# Wrappers
#------------------------------------------------------------------------------
def inject_api_search_actions(form, popup, form_type): #inject class actions
"""
Inject actions to popup menu(s) based on context.
"""
if form_type == idaapi.BWN_DISASMS: # disassembly window
# insert the action entry into the menu
idaapi.attach_action_to_popup(
form,
popup,
api_search.ACTION_GOOGLE_SEARCH,
"Google Search",
idaapi.SETMENU_APP
)
# done
return 0
#------------------------------------------------------------------------------
# Google Search
#------------------------------------------------------------------------------
def google_search():
"""
Search for the API function at cursor.
"""
if using_ida7api:
current_instruction = idaapi.get_screen_ea() #get the address at the cursor
API= idc.print_operand(current_instruction,0) #get the function name
if ' ' in API or len(API)<4 :
print('SearchApi: Please select a call instruction.')
else:
#in case a segment register gets copied with the API
API = API[ API.index(':')+1:] if ':' in API else API
webbrowser.open_new_tab('https://www.google.com/search?q=' + API + '+MSDN')
else:
current_instruction = idaapi.ScreenEA()
API= idc.GetOpnd(current_instruction,0)
if ' ' in API or len(API)<4:
print('SearchApi: Please select a call instruction.')
else:
API = API[ API.index(':')+1:] if ':' in API else API
webbrowser.open_new_tab('https://www.google.com/search?q=' + API + '+MSDN')
return
#------------------------------------------------------------------------------
# IDA ctxt
#------------------------------------------------------------------------------
class IDACtxEntry(idaapi.action_handler_t):
"""
A basic Context Menu class to utilize IDA's action handlers.
"""
def __init__(self, action_function):
idaapi.action_handler_t.__init__(self)
self.action_function = action_function
def activate(self, ctx):
"""
Execute the embedded action_function when this context menu is invoked.
"""
self.action_function()
return 1
def update(self, ctx):
"""
Ensure the context menu is always available in IDA.
"""
return idaapi.AST_ENABLE_ALWAYS