-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_harness.py
311 lines (245 loc) · 10.6 KB
/
test_harness.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
'''
This file is analogous to packager, but with user-interaction components stripped out. This
makes it easier to write automated tests.
'''
from .codegen import *
from .analysis_engine import aengine as ae
from .dependency import depScanner
from .conScan import convenienceScanner
### Global for listing all chunks of code for which we have tried to create a python wrapper.
emuchunks = {}
# List of basic block chunks to package for BB mode
bbChunks = []
class t_Packager(object):
'''
Packager does the work of getting a codegen object
the information it needs for creating a suitable emulation environment.
'''
def __init__(self, isFunc, address, engine, ui = None, length=None):
self.isFunc = isFunc
self.address = address
self.length = length
self.engine = engine
self.ui = ui
# List of Contiguous code we're interested in
self.targetCode = []
self.codeobj = genwrapper('', isFunc)
self.arch = self.engine.get_arch()
self.codeobj.setArch(self.arch)
self.impCallStrategy = None
self.dataStrategy = None
self.resolve_arguments = None
self.codeobj.startaddr = int(self.address)
if (self.length != None):
self.codeobj.codelen = length
def convenience_passes(self):
'''
This function is a wrapper for determining which convenience features can
be enabled during code generation.
'''
c = convenienceScanner(self.engine)
if (self.isFunc == True and self.codeobj.arch in ['x64', 'x86', 'arm']):
self.codeobj.conPass['ret'] = True
args = None
if (self.isFunc):
if (self.resolve_arguments == True):
args = c.argIdent(self.address, self.isFunc)
if args:
self.codeobj.conPass['args'] = args
if not self.isFunc:
self.codeobj.conPass['unset_vars'] = c.uninit_vars(bbChunks)
def minimal_package_function(self, address=None):
'''
Adds basic information to CodeGen when packaging a function.
'''
if (address == None):
address = self.address
# Get the code to be emulated
localCode = self.engine.get_function_bytes(address=address)
if (localCode == None):
self.ui.msgBox("[ripr] Couldn't get function binary view. Maybe code arch is thumb2?")
return False
# Add each contiguous chunk of code to codeobj and make sure
# it will be mapped.
for startAddr in list(localCode.keys()):
self.codeobj.add_mmap(startAddr)
self.targetCode.append(localCode)
return True
def minimal_package_region(self):
targetCode = self.engine.get_region_bytes(address=self.address)
self.codeobj.add_data(targetCode[0], targetCode[1])
self.codeobj.add_mmap(targetCode[0])
def minimal_package_bb(self, address=None):
if (address == None):
address = self.address
targetCode = self.engine.get_basic_block_bytes(address)
self.engine.mark_gathered_basic_block(address)
bbChunks.append(targetCode)
def package_function(self, cname):
'''
This method handles filling in as much relevant information as possible into our current instance of codeobj
about the function to be emulated. It is a high-level encapsulation of multipe packaging and analysis methods.
'''
self.codeobj.name = cname
# Get the bare minimum required information.
if (self.minimal_package_function()==False):
return
# Try to find dependencies of our code.
self.resolve_dependencies()
# Figure out if we can add any convenience methods to our generated code
self.convenience_passes()
# Add gathered information to the code object.
self.update_codeobj()
# Add the codeobj to the global storage for listing in the UI (if available)
emuchunks[self.codeobj.name] = self.codeobj
self.ui.update_table(emuchunks)
# Generate what we currently have and show the results
self.codeobj.generate_class()
def package_bb(self):
'''
This method adds an entry to bbChunks, which can be used later to
generate a package containing only user-specified basic blocks.
'''
self.minimal_package_bb()
def generate_bb_code(self, cname):
global bbChunks
if len(bbChunks) == 0:
self.ui.msgBox("Basic Block package list is empty!")
return
self.codeobj.name = cname
if not self.codeobj.name:
return
# Set starting address to first basic block selected
self.codeobj.startaddr = list(bbChunks[0].keys())[0]
self.targetCode = bbChunks
self.resolve_dependencies()
self.convenience_passes()
self.update_codeobj()
# Clean up our modifications
self.cleanup_basic_blocks()
bbChunks = []
self.codeobj.generate_class()
self.engine.display_info("Generated Code: %s" % self.codeobj.name, self.codeobj.final)
self.ui.save_file(self.codeobj)
def cleanup_basic_blocks(self):
global bbChunks
for bb in bbChunks:
self.engine.clean_gathered_basic_block(list(bb.keys())[0])
def package_region(self):
'''
This method handles filling in as much information as possible about the target region to be emulated.
'''
self.minimal_package_region()
self.resolve_dependencies()
self.convenience_passes()
def _find_code_unit(self, faddr):
for found_code in self.targetCode:
for addr in found_code:
codeLen = len(found_code[addr].code_bytes)
if ( (faddr >= addr) and (faddr <= addr + codeLen)):
return found_code[addr]
def _nop_impFunc(self, impCalls):
for impCall in impCalls:
print ("[ripr] Nopping out Imported Call: 0x%x" % (impCall.address))
cSlice = self._find_code_unit(impCall.address)
codeLen = len(cSlice.code_bytes)
nop = self.engine.get_nop_opcode()
if (impCall.inst_len % len(nop) != 0):
print ("[ripr] Cannot NOP out instruction...")
return
# Create string of NOP opcodes and calculate where to place it
nopStr = nop * (impCall.inst_len / len(nop))
first = impCall.address - cSlice.address
second = first + len(nopStr)
newCode = cSlice.code_bytes[0:first] + nopStr + cSlice.code_bytes[second:]
cSlice.code_bytes = newCode
def update_codeobj(self):
# targetCode[0] corresponds to the dict of code units for the original function
# in the case of others being automatically mapped as dependencies
localCode = self.targetCode[0]
print (self.targetCode)
self.codeobj.codelen = sum([len(localCode[x].code_bytes) for x in list(localCode.keys())])
for found_code in self.targetCode:
for addr in found_code:
self.codeobj.add_code(found_code[addr])
def resolve_imported_calls(self, resolv):
if self.impCallStrategy == 'nop':
self._nop_impFunc(resolv.impCalls)
elif self.impCallStrategy == 'hook':
self.codeobj.impCallTargets += resolv.impCalls
else:
return
def map_dependent_pages(self, dataRefs):
pagesize = self.engine.get_page_size()
pages = []
for ref in dataRefs:
pageaddr = (ref.address & ~(pagesize - 1))
pages.append(pageaddr)
pages = set(pages)
for page in pages:
self.codeobj.add_data(self.engine.get_page_bytes(page), page)
self.codeobj.add_mmap(page)
def map_dependent_sections(self, dataRefs):
'''
Map any sections the target code touches.
'''
print ("Mapping Sections")
pagesize = self.engine.get_page_size()
secs = []
for ref in dataRefs:
section=self.engine.find_section(ref.address)
if section!=-1:
secs += [section]
for sec_start, sec_end, sec_name in secs:
self.codeobj.add_data(self.engine.read_bytes(sec_start, sec_end - sec_start), sec_start)
self.codeobj.add_mmap(sec_start)
def map_minimal_data(self, dataRefs):
'''
Attempt to only map the exact data the target code uses.
'''
pass
def resolve_data_dependencies(self, dataRefs):
'''
This function handles finding data that needs to get mapped.
'''
if (self.dataStrategy == "section"):
self.map_dependent_sections(dataRefs)
else:
self.map_dependent_pages(dataRefs)
def resolve_codeRefs(self, coderefs):
for ref in coderefs:
print ("Found CodeRef: %x::%s" % (ref.address, ref.type))
if (ref.type == 'call'):
if (self.minimal_package_function(address=ref.address)==False):
continue
self.resolve_dependencies(address=ref.address, isFunc=True)
def resolve_dependencies(self, address=None, isFunc=None):
'''
This method is a high-level wrapper for finding data our target code depends on.
'''
resolv = depScanner(self.engine, self.codeobj)
if (address == None):
address = self.address
if (isFunc == None):
isFunc = self.isFunc
print ("Resolving Dependencies for %x" % address)
if isFunc:
coderefs = resolv.branchScan(address, self.isFunc)
datarefs = resolv.dataScan(address)
else:
datarefs = resolv.dataScan(address)
coderefs = []
for bb in bbChunks:
coderefs += resolv.branchScan(list(bb.keys())[0], self.isFunc)
# Always try to resolve these kinds of dependencies in tests
if (resolv.impCalls != []):
self.resolve_imported_calls(resolv)
if (coderefs != []):
self.resolve_codeRefs(coderefs)
if (resolv.dataRefs != []):
# Try to map these automatically
print ("[ripr] Found these potential Data References")
for ref in resolv.dataRefs:
print ("Data Referenced: 0x%x" % (ref.address))
self.resolve_data_dependencies(resolv.dataRefs)
pass