-
Notifications
You must be signed in to change notification settings - Fork 29
/
Prelude.py
107 lines (75 loc) · 3.17 KB
/
Prelude.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
"""
This file is part of SEA.
reserbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
reserbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SEA. If not, see <http://www.gnu.org/licenses/>.
Copyright 2013 by neuromancer
"""
from Trace import REIL_Trace
from Inputs import parse_inputs
from Memory import MemAccessREIL
from Parameters import FuncParametersREIL
from Callstack import CallstackREIL
from Allocation import Allocation
from Instruction import *
from Reil import parse_reil
from Operand import *
def mkTrace(trace_filename, first, last, raw_inputs):
print "Loading trace.."
reil_code = REIL_Trace(trace_filename, first, last)
Inputs = parse_inputs(raw_inputs)
if (raw_inputs <> []):
print "Using these inputs.."
for op in Inputs:
print op,"=", Inputs[op]
print "Detecting callstack layout..."
Callstack = CallstackREIL(reil_code)#, Inputs) #TODO: it should recieve inputs also!
reil_code.reset()
print Callstack
AllocationLog = Allocation()
MemAccess = MemAccessREIL()
FuncParameters = FuncParametersREIL()
reil_size = len(reil_code)
start = 0
Callstack.reset()
print "Detecting memory accesses and function parameters.."
for (end,ins_str) in enumerate(reil_code):
pins = parse_reil(ins_str)
ins = Instruction(pins,None)
Callstack.nextInstruction(ins_str)
if ins.instruction in ["stm", "ldm"]:
MemAccess.detectMemAccess(reil_code[start:end+1], Callstack, Inputs, end)
AllocationLog.check(MemAccess.getAccess(end), end)
elif ins.instruction == "call" and ins.called_function <> None:
#print "detect parameters of", ins.called_function, "at", ins_str
FuncParameters.detectFuncParameters(reil_code[start:end+1], MemAccess, Callstack, Inputs, end)
if (ins.called_function == "malloc"):
try:
size = int(FuncParameters.getParameters(end)[0][1].name)
except ValueError:
size = None
AllocationLog.alloc(ins.address, end, size)
elif (ins.called_function == "free"):
ptr = (FuncParameters.getParameters(end)[0][1].mem_source)
AllocationLog.free(ptr, end)
print MemAccess
print FuncParameters
AllocationLog.report()
Callstack.reset()
reil_code.reset()
# trace definition
trace = dict()
trace["code"] = reil_code
trace["initial_conditions"] = Inputs
trace["final_conditions"] = dict()
trace["callstack"] = Callstack
trace["mem_access"] = MemAccess
trace["func_parameters"] = FuncParameters
return trace