forked from OpenRCE/paimei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_backtrace.py
212 lines (164 loc) · 7.16 KB
/
var_backtrace.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
from idaapi import *
from idautils import *
from idc import *
from pida import *
#import pida
import time
'''
Variable backtrace script for IDA.
This does not work independantly of IDA at the moment, but does however rely on the PIDA
extensions for manipulating varivakes
$Id: var_backtrace.py 194 2007-04-05 15:31:53Z cameron $
'''
ida_log = lambda x: sys.stdout.write(x + "\n")
def extract_internal_registers(operand):
'''
Extracts any registers nested inside a reference.
@type operand: String
@param operand: The operand to inspect
@rtype: String List
@returns: A list of registers embedded in the given reference
'''
stripped = operand.lstrip('[').rstrip(']')
components = stripped.replace('*', '+').split('+')
ret_val = []
for reg in components:
if not (reg[-1] == "h" and reg.rstrip('h').isdigit()):
ida_log("adding %s" % reg)
ret_val.append(reg)
return ret_val
def choose_backtrace_target(ea):
'''
Prompts the user for the target operand in the current instruction.
TODO: allow user to manually enter a target if none are found
@type ea: DWORD
@param ea: The address to search for operands
@rtype: String
@returns: The text representation of the variable to backtrace
'''
targets = []
for op_index in xrange(3):
current_tgt = GetOpnd(ea, op_index)
if (current_tgt != None and current_tgt != "" and not (current_tgt in targets)):
targets.append(current_tgt)
if current_tgt.find('dword ptr [') == 0:
targets.append(current_tgt.lstrip('dword ptr '))
targets += extract_internal_registers(current_tgt.lstrip('dword ptr '))
elif current_tgt[0] == '[' and current_tgt[-1] == ']':
targets += extract_internal_registers(current_tgt)
for target in targets:
prompt_result = AskYN(1, "Backtrace %s?" % target)
if prompt_result == -1:
return None
elif prompt_result == 1:
return target
return None
def trace_block(heads, initial_target, initial_ea=None):
'''
Traces backwards through a basic block looking for adjustments to the
target variable.
@type initial_target: String
@param initial_target: The value of the current variable being traced
@type initial_ea: DWORD
@param initial_ea: The initial address to begin the trace from. If empty, it will start at the end of the block.
@rtype: tuple(String,String,DWORD)
@returns: a Tuple consisting of the new target, the type of source if any and the address if a source is found.
'''
heads.reverse()
target = [initial_target]
if target[0][0] == '[':
target.append(extract_internal_registers(target[0])[0]) # only look for the base
# if len(target) > 1:
# ida_log("also %s" % target[1])
if initial_ea == None:
initial_ea = heads[0].ea
ida_log("%08x: starting block trace. %d instructions." % (heads[0].ea, len(heads)))
mod_type = None
mod_addr = None
for ins in heads: # Go from the end
if ins.ea > initial_ea:
pass
elif ("eax" in target) and (ins.mnem == "call") and ins.ea != initial_ea:
# trace into call
mod_type = "call"
mod_addr = ins.ea
target = None
break
elif (ins.mnem == "mov") and (ins.op1 in target):
target = [ins.op2]
if target[0][0] == '[':
target.append(extract_internal_registers(target[0])[0]) # only look for the base
ida_log("%08x: Switched trace to %s" % (ins.ea, target[0]))
elif (ins.mnem == "lea") and (ins.op1 in target):
target = [ins.op2]
if target[0][0] == '[':
target.append(extract_internal_registers(target[0])[0]) # only look for the base
ida_log("%08x: Switched trace to %s" % (ins.ea, target[0]))
elif (ins.mnem == "xor") and (ins.op1 in target) and (ins.op2 in target):
mod_type = "zero"
mod_addr = ins.ea
target = None
break
elif (ins.mnem == "pop") and (ins.op1 in target):
mod_type = "pop"
mod_addr = ins.ea
target = None
break
if target != None:
target = target[0]
return (target, mod_type, mod_addr)
target = choose_backtrace_target(ScreenEA())
if target == None:
ida_log("No target chosen")
else:
ida_log("Target \"%s\" chosen for backtrace" % target)
current_ea = ScreenEA()
fn = function(current_ea)
bb = fn.find_basic_block(current_ea)
target,mod,addr = trace_block(bb.sorted_instructions(), target, current_ea)
kill_count = 0
var_src = {}
if target == None:
var_src[addr] = mod
else:
bb_hits = {}
bb_targets = {}
new_travel = [bb.function.nodes[edge.src] for edge in bb.function.edges_to(bb.id)]
if (new_travel == None) or (len(new_travel) == 0):
ida_log("%08x: No blocks found." % bb.start_ea)
else:
for block in new_travel:
ida_log("Adding source: %08x" % block.ea_start)
bb_targets[block] = target
while len(bb_targets) > 0:
bb = bb_targets.keys()[0]
target = bb_targets[bb]
del bb_targets[bb]
if not bb.ea_start in bb_hits:
target,mod,addr = trace_block(bb.sorted_instructions(), target)
bb_hits[bb.ea_start] = target
new_travel = [bb.function.nodes[edge.src] for edge in bb.function.edges_to(bb.id)]
if mod != None:
var_src[addr] = mod
elif (new_travel == None) or (len(new_travel) == 0):
if (bb.ea_start == bb.function.ea_start):
var_src[bb.ea_start] = "fn_arg:" + target
else:
ida_log("%08x: No blocks found." % bb.ea_start)
else:
for block in new_travel:
bb_targets[block] = target
# kill_count += 1
if kill_count == 20:
ida_log("Hit kill count")
break
ida_log("Possible sources detected: %d" % len(var_src))
for key in var_src.keys():
if var_src[key] == "zero":
ida_log("%08x: Memory Zeroed" % key)
elif var_src[key] == "call":
ida_log("%08x: Return value from CALL" % key)
elif var_src[key].find("fn_arg") == 0:
ida_log("%08x: Passed in to the function via %s" % (key ,var_src[key].lstrip("fn_arg:")))
xrefs = CodeRefsTo(key, 0)
ida_log("found %d xrefs" % len(xrefs))