-
Notifications
You must be signed in to change notification settings - Fork 20
/
mrspicky.py
347 lines (296 loc) · 12.9 KB
/
mrspicky.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import idaapi
import idautils
import idc
# MrsPicky - An IDAPython decompiler script that helps auditing calls
# to the memcpy() and memmove() functions.
#
# This scripts mainly exists for learning purposes on how to use the
# Hex-Rays decompiler API to process decompiled code. Its purpose is to
# create a list of memcpy() function calls and to identify and point out
# potentially dangerous ones.
#
# It will display a list of identified calls that can be and is meant to
# be searched, sorted and filtered interactively using IDA's built-in
# filtering features. Double clicking a list's entry will show a respective
# call, in the currently active IDA or Decompiler views.
#
# In cases where a memcpy(dst, src, n) "n" argument can be resolved statically
# by this script, the resulting list will have a "max n" tab that reflects
# the maximum number of bytes that a destination buffer "dst" can be written to.
# In other words: any number larger than that will corrupt whatever follows the
# current stack frame, which usually is a return address.
#
# There is also a "problems" tab on the resulting list, which may contain any of
# the following keywords:
#
# * "memcorr" - indicates a confirmed memory corruption
# * "argptr" - the "dst" pointer points beyond the local stack frame
# (this may not actually be a problem per se but...)
#
# The script's code is heavily commented so I would like to invite you to play
# around with it and customize it.
#
# For further help, check out the include header file "hexrays.hpp" that comes
# with the Hex-Rays decompiler SDK and the HRDevHelper plugin on my github.
#
# /*
# * ----------------------------------------------------------------------------
# * "THE BEER-WARE LICENSE" (Revision 42):
# * Dennis Elser wrote this file. As long as you retain this notice you
# * can do whatever you want with this stuff. If we meet some day, and you think
# * this stuff is worth it, you can buy me a beer in return.
# * ----------------------------------------------------------------------------
# */
__author__ = "Dennis Elser"
MEMCPY_FAM = ["memcpy", "memmove"]
ID_NA = "."
# -----------------------------------------------------------------------------
class MemcpyLocation():
"""Internal data that's represented by IDA's listview (Wchooser")
"""
def __init__(self, ea, name, dst, src, n, dst_type, n_max, problems):
self.ea = ea
self.name = name
self.dst = dst
self.src = src
self.n = n
self.dst_type = dst_type
self.n_max = n_max
self.problems = problems
# -----------------------------------------------------------------------------
class MrsPickyListView(idaapi.Choose):
"""Listview that displays results to the user
"""
def __init__(self, title, flags=0, width=None, height=None, embedded=False, modal=False):
idaapi.Choose.__init__(
self,
title,
[ ["caller", 20 | idaapi.CHCOL_FNAME],
["function", 8 | idaapi.CHCOL_FNAME],
["dst", 8],
["src", 8],
["n", 8 | idaapi.CHCOL_DEC],
["dst type", 8],
["max n", 8 | idaapi.CHCOL_DEC],
["problems", 20] ],
flags = flags,
width = width,
height = height,
embedded = embedded)
self.items = []
def OnClose(self):
"""empty internal data if view is closed
"""
self.items = []
def OnSelectLine(self, n):
"""navigate to code location
"""
idaapi.jumpto(self.items[n].ea)
def OnGetLine(self, n):
"""generate textual representation
"""
return self._make_listview_entry(n)
def OnGetSize(self):
"""get number of listview items
"""
n = len(self.items)
return n
def feed(self, data):
"""add entry to listview dynamically
"""
for item in data:
self.items.append(item)
self.Refresh()
return
def _make_listview_entry(self, n):
"""generate textual representation of a single
line"""
ea = "%s" % idc.get_func_off_str(self.items[n].ea)
name = "%s" % self.items[n].name
dst = self.items[n].dst
src = self.items[n].src
_n = self.items[n].n
_n = _n if type(_n) == str else str(_n)
max_n = self.items[n].n_max
max_n = max_n if type(max_n) == str else str(max_n)
dst_type = self.items[n].dst_type
return [ea, name, dst, src, _n, dst_type, max_n, ", ".join(self.items[n].problems)]
# -----------------------------------------------------------------------------
class func_parser_t(idaapi.ctree_visitor_t):
def __init__(self, cfunc):
idaapi.ctree_visitor_t.__init__(self, idaapi.CV_FAST)
self.cfunc = cfunc
self.data = []
return
# for every expression / cexpr_t of the AST
def visit_expr(self, e):
# if expression type is call
if e.op == idaapi.cot_call:
name = idaapi.tag_remove(e.x.print1(None))
# and if the function name is supported
if name in MEMCPY_FAM:
# parse the call
self._parse_memcpy(e)
return 0
def _parse_memcpy(self, e):
# get memcpy func args
args = e.a # carglist_t
# check number of args
if args.size() != 3: # len(args) works as well
return False
# init some vars
n_value = ID_NA
dst_name = ID_NA
dst_type = ID_NA
max_sane_write_count = ID_NA
problems = []
# extract memcpy() arguments dst, src and n
arg1, arg2, arg3 = args # carg_t
# process arg3:
# check whether third argument of memcpy is a fixed number
# and get its value
if arg3.op == idaapi.cot_num:
# alternative: var_n = arg3.n.value(arg3.type)
n_value = arg3.numval()
# process arg2:
# do not handle for now
# process arg1:
# check whether first argument (dst) is a reference to a
# stack variable
# does the "op" member of arg1 indicate a reference?
if arg1.op == idaapi.cot_ref: # cexpr_t
# if it does, get the referenced expression's "x"
# member, which is also a cexpr_t
ref = arg1.x # cexpr_t
# then check whether referenced expression is a variable
if ref.op == idaapi.cot_var: # cexpr_t
# if this is the case, get the variable's name
# and see whether it is a stack variable.
# v is a member of the cexpr_t class that becomes
# valid for cot_var types. its type is "var_ref_t",
# which has an index member. This "idx" member can be
# used to access lvar_t members of the the lvars_t class
# which is returned by calling get_lvars()
lvars_idx = ref.v.idx # get index
lvars = self.cfunc.get_lvars() # lvars_t
# get the var_t instance of the referenced variable
# which is our memcpy() call's "dst" variable
var_dst = lvars[lvars_idx] # var_t
dst_name = var_dst.name
# check whether "dst" is on a stack frame by calling
# is_stk_var()
if var_dst.is_stk_var():
# get its offset (on a sidenote, the decompiler's
# stack var offsets are different to that of IDA's.
# They can be converted using stkoff_vd2ida() and
# stkoff_ida2vd(), respectively
offs = var_dst.get_stkoff()
dst_type = "stack+0x%x" % offs
# compute stack size and the max value for
# the memcpy() call's "n" function argument.
# refer to hexrays.hpp for explanations
frsize = idc.get_frame_lvar_size(e.ea)
frregs = idc.get_frame_regs_size(e.ea)
if offs <= frsize + frregs:
max_sane_write_count = frsize + frregs - offs
else:
problems.append("argptr")
# anything values for n > max_sane_write_count indicates
# code that may corrupt the stack.
#
# n.b.: there exist further values for "n" that may be
# causing problems but that'd mean we'd have to figure
# out the size of all individual local variables, which is
# beyond this script's scope.
#
# so the following situation will cause this script to log a
# confirmed memory corruption:
# -> whenever the sum of a current "dst" stack variable's
# offset and the number "n" of bytes to be written by a memcpy()
# call is bigger than the current stack frame's size.
# Shouldn't realistically be the case in this context
# but once you are gonna extended this code with data flow
# analysis by tracking var assignments it starts to make sense ;)
#
if type(n_value) != str and type(max_sane_write_count) != str:
if n_value > max_sane_write_count:
problems.append("memcorr")
# get function name with its color tags removed
name = idaapi.tag_remove(e.x.print1(None))
self._add_func_call(MemcpyLocation(e.ea, # address of call
name, # name of function
dst_name, # name of var "dst"
ID_NA, # var "src"
n_value, # name/val of var "n"
dst_type, # dst type
max_sane_write_count, # max number of writable
problems)) # overflow?
return True
def _add_func_call(self, func_info):
self.data.append(func_info)
return
# -----------------------------------------------------------------------------
def is_min_sdk_ver(min_ver_required):
return idaapi.IDA_SDK_VERSION >= min_ver_required
# -----------------------------------------------------------------------------
def get_callers(name):
for xr in idautils.CodeRefsTo(idaapi.get_name_ea(idaapi.BADADDR, name), True):
fn = idaapi.get_func(xr)
if fn:
yield fn.start_ea
# -----------------------------------------------------------------------------
def run_script():
if not idaapi.init_hexrays_plugin():
idaapi.msg("This script requires the HexRays decompiler plugin.")
else:
func_list = []
for name in MEMCPY_FAM:
func_list += get_callers(name)
func_list = set(func_list)
nfuncs = len(func_list)
idaapi.msg("Checking %d functions." % (nfuncs))
lv = MrsPickyListView("MrsPicky")
lv.Show()
if is_min_sdk_ver(7.3):
aborted = False
i = 0
x = nfuncs / 10 if nfuncs >= 10 else nfuncs
idaapi.show_wait_box("Working...")
for ea in func_list:
bars = (int(round(i/(x), 0)))
funcname = idaapi.get_func_name(ea)
funcname = funcname if len(funcname) < 20 else funcname[:20] + "..."
progress = "[%s%s] : %3.2f%%" % (bars*'#', (10-bars)*'=',
(float(i)/float(nfuncs))*100.0)
idaapi.replace_wait_box("Total progress: %s\n\nScanning: %s\n\n" % (
progress, funcname))
try:
cfunc = idaapi.decompile(ea, flags = idaapi.DECOMP_NO_WAIT)
except idaapi.DecompilationFailure:
idaapi.msg("Error decompiling function @ 0x%x" % ea)
cfunc = None
if cfunc:
fp = func_parser_t(cfunc)
fp.apply_to(cfunc.body, None)
lv.feed(fp.data)
if idaapi.user_cancelled():
aborted = True
break
i += 1
idaapi.hide_wait_box()
if aborted:
idaapi.warning("Aborted.")
# IDA <= 7.2
else:
for ea in func_list:
try:
cfunc = idaapi.decompile(ea)
except idaapi.DecompilationFailure:
idaapi.msg("Error decompiling function @ 0x%x" % ea)
cfunc = None
if cfunc:
fp = func_parser_t(cfunc)
fp.apply_to(cfunc.body, None)
lv.feed(fp.data)
idaapi.msg("Done.")
run_script()