-
Notifications
You must be signed in to change notification settings - Fork 25
/
idapyhelper.py
178 lines (158 loc) · 6.63 KB
/
idapyhelper.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
import ida_kernwin, ida_diskio, ida_pro
import os, inspect, sys
__author__ = "Dennis Elser"
DBG = False
# -----------------------------------------------------------------------------
def is_ida_version(min_ver_required):
return ida_pro.IDA_SDK_VERSION >= min_ver_required
# --------------------------------------------------------------------------
class FileViewer(ida_kernwin.Form):
"""A form that displays a text file's content."""
def __init__(self, title, content):
ida_kernwin.Form.__init__(self,
("BUTTON YES NONE\n"
"BUTTON NO NONE\n"
"BUTTON CANCEL NONE\n"
"%s\n\n"
"<##Docstring##:{cbEditable}>"
) % title,
{'cbEditable': ida_kernwin.Form.MultiLineTextControl(text=content,
flags=ida_kernwin.textctrl_info_t.TXTF_READONLY |
ida_kernwin.textctrl_info_t.TXTF_FIXEDFONT)})
# --------------------------------------------------------------------------
class DocstringViewer(ida_kernwin.Form):
"""A form that displays a docstring."""
def __init__(self, title, docstr):
ida_kernwin.Form.__init__(self,
("BUTTON YES NONE\n"
"BUTTON NO NONE\n"
"BUTTON CANCEL NONE\n"
"%s\n\n"
"<##Docstring##:{cbEditable}>"
) % title,
{'cbEditable': ida_kernwin.Form.MultiLineTextControl(text=docstr,
flags=ida_kernwin.textctrl_info_t.TXTF_READONLY |
ida_kernwin.textctrl_info_t.TXTF_FIXEDFONT)})
# --------------------------------------------------------------------------
class ChooserData:
"""Structure that holds information for the chooser to display."""
icon_ids = {"str": 80,
"int": 8,
"class": 89,
"function": 81,
"method": 99}
def __init__(self, mod_name, sym_name, file_name):
self.mod_name = mod_name
self.sym_name = sym_name
self.file_name = file_name
self.doc_str = ""
self.sym_type = ""
self.sym_value = ""
self.line_no = ""
def get_icon(self):
return self.icon_ids[self.sym_type]
# --------------------------------------------------------------------------
class PyHelperChooser(ida_kernwin.Choose):
"""A chooser filled with information about IDAPython bindings.
Output is supposed to be filtered with Ctrl-F."""
def __init__(self, title, nb=5):
ida_kernwin.Choose.__init__(self,
title,
[ ["Module", 10 | ida_kernwin.Choose.CHCOL_PLAIN],
["Symbol", 20 | ida_kernwin.Choose.CHCOL_PLAIN],
["Documentation", 10 | ida_kernwin.Choose.CHCOL_PLAIN],
["Type", 10 | ida_kernwin.Choose.CHCOL_PLAIN],
["Value", 10 | ida_kernwin.Choose.CHCOL_HEX],
["Line number", 10 | ida_kernwin.Choose.CHCOL_DEC],],
flags=ida_kernwin.Choose.CH_QFLT | ida_kernwin.Choose.CH_NOIDB)
self.items = []
self.icon = 0
self.build_items()
def build_items(self):
subdir = ""
if is_ida_version(740):
subdir, _, _, _, _ = sys.version_info
pydir = ida_diskio.idadir(os.path.join("python", str(subdir)))
for mod_name in os.listdir(pydir):
if mod_name.endswith(".py"):
mod_name, _ = os.path.splitext(mod_name)
if mod_name not in ["init", "idaapi"]:
mod = __import__(mod_name)
file_name = mod.__file__
for sym_name, obj in inspect.getmembers(mod):
if inspect.isfunction(obj):
data = ChooserData(mod_name, sym_name, file_name)
data.sym_type = "function"
data.line_no = "%d" % obj.__code__.co_firstlineno
data.doc_str = inspect.getdoc(obj)
self.items.append(data)
elif inspect.isclass(obj):
data = ChooserData(mod_name, sym_name, file_name)
data.sym_type = "class"
data.doc_str = inspect.getdoc(obj)
self.items.append(data)
elif inspect.ismethod(obj):
data = ChooserData(mod_name, sym_name, file_name)
data.sym_type = "method"
data.line_no = "%d" % obj.im_func.__code__.co_firstlineno
data.doc_str = inspect.getdoc(obj)
self.items.append(data)
elif type(obj) == int:
data = ChooserData(mod_name, sym_name, file_name)
data.sym_type = "int"
data.sym_value = "0x%x" % (obj)
self.items.append(data)
elif type(obj) == str:
data = ChooserData(mod_name, sym_name, file_name)
data.sym_type = "str"
data.sym_value = str(obj)
self.items.append(data)
else:
if DBG:
ida_kernwin.msg("%s: %s" % (type(obj), sym_name))
def OnGetLine(self, n):
data = self.items[n]
return [data.mod_name,
data.sym_name,
"%s" % data.doc_str,
data.sym_type,
data.sym_value,
data.line_no]
def OnGetIcon(self, n):
return self.items[n].get_icon()
def OnGetSize(self):
return len(self.items)
def OnSelectLine(self, n):
data = self.items[n]
postfix = " (%s)" % data.mod_name if len(data.mod_name) else ""
if not data.doc_str:
ida_kernwin.msg("No documentation available for \"%s\"\n" % data.sym_name)
else:
f = DocstringViewer("%s%s" % (data.sym_name, postfix), data.doc_str)
f.modal = False
f.openform_flags = ida_kernwin.PluginForm.WOPN_TAB
f, args = f.Compile()
f.Open()
return (ida_kernwin.Choose.NOTHING_CHANGED, )
def OnEditLine(self, n):
fn = self.items[n].file_name
if fn:
# ghetto
if fn.endswith(".pyc"):
fn = fn[:-1]
with open(fn) as fin:
f = FileViewer("%s" % (os.path.basename(fn)), fin.read())
f.modal = False
f.openform_flags = ida_kernwin.PluginForm.WOPN_TAB
f, args = f.Compile()
f.Open()
return (ida_kernwin.Choose.NOTHING_CHANGED, )
try:
pyhelper
except:
pyhelper=PyHelperChooser("IDAPyHelper")
else:
if DBG:
del pyhelper
pyhelper=PyHelperChooser("IDAPyHelper")
pyhelper.Show()