-
Notifications
You must be signed in to change notification settings - Fork 29
/
Typing.py
184 lines (129 loc) · 5.11 KB
/
Typing.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
"""
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 Operand import *
from Condition import *
def getInitialConditionsCall(callstack):
initial_values_at_call = dict()
if callstack.index == 1:
esp_val = 4
for i in range(0,40):
arg_i = Operand("argv[]@"+str(i),"BYTE", mem_source = "argv[]", mem_offset=i)
initial_values_at_call[arg_i] = Operand(str(0), "BYTE")
name = "s."+hex(callstack.callstack[1])+".1"
for i in range(12,16):
arg_v = Operand(name+"@"+str(i),"BYTE", mem_source = name, mem_offset=i)
initial_values_at_call[arg_v] = Operand(str(0), "BYTE")
#print ""
else:
esp_val = 8
ebp_val = 0
esp_op = Operand("esp","DWORD")
ebp_op = Operand("ebp","DWORD")
initial_values_at_call[esp_op] = Operand(str(esp_val), "DWORD")
initial_values_at_call[ebp_op] = Operand(str(ebp_val), "DWORD")
return initial_values_at_call
def getInitialConditionsAlloc():
ret_op = Operand("eax","DWORD")
ret_val = 0
initial_values_at_alloc = dict()
initial_values_at_alloc[ret_op] = Operand(str(ret_val), "DWORD")
return initial_values_at_alloc
def setInitialConditions(ssa, initial_values, smt_conds):
ssa_map = ssa.getMap(set(), set(), set(initial_values.keys()))
eq = Eq(None, None)
for iop in initial_values:
if ":" in iop.name:
smt_conds.add(eq.getEq(iop,initial_values[iop]))
elif (iop.isReg()):
smt_conds.add(eq.getEq(ssa_map[iop.name],initial_values[iop]))
elif (iop.isMem()):
smt_conds.add(eq.getEq(iop,initial_values[iop]))
else:
assert(False)
def detectType(mvars, ins, counter, callstack):
if (len(mvars) == 0):
return None
# dection of parameters of main
name = "s."+hex(callstack.callstack[1])+".1"
# argv
argv_bytes = []
for i in range(12,16):
argv_bytes.append(Operand(name+"@"+str(i),"BYTE"))
argv_bytes = set(argv_bytes)
if argv_bytes.issubset(mvars):
return "argv[]"
## argc
#argc_bytes = []
#
#for i in range(8,12):
# argc_bytes.append(Operand(name+"@"+str(i),"BYTE"))
#
#argc_bytes = set(argv_bytes)
#
#if argc_bytes.issubset(mvars):
# return "argc"
# argv[0], argv[1], ... argv[10]
for i in range(0,40,4):
op = Operand("argv[]@"+str(i),"BYTE")
if op in mvars:
return "arg["+str(i / 4)+"]"
if ins.instruction == "call" and ins.called_function == "malloc":
# heap pointers
if set([Operand("eax","DWORD")]).issubset(mvars):
return "h."+"0x"+ins.address+"."+str(counter)
elif ins.instruction == "call" and ins.called_function == None:
# stack pointers
if mvars.issubset(set([Operand("esp", "DWORD"), Operand("ebp", "DWORD")])):
return "s."+hex(callstack.currentCall())+"."+str(callstack.currentCounter())
# No type deduced
return None
def mkVal(val_type,val):
if val_type == "imm":
return Operand(str(val), "")
elif "s." in val_type or "h." in val_type or "arg" in val_type:
return Operand(val_type+"@"+str(val), "", mem_source = val_type, mem_offset=val)
else:
assert(0)
def addAditionalConditions(mvars, ins, ssa, callstack, smt_conds):
if len(mvars) == 0:
return mvars
# auxiliary eq condition
eq = Eq(None, None)
# if the instruction was a call
if ins.instruction == "call" and ins.called_function == "malloc":
if (Operand("eax","DWORD") in mvars):
initial_values_at_alloc = getInitialConditionsAlloc()
setInitialConditions(ssa, initial_values_at_alloc, smt_conds)
mvars.remove(Operand("eax","DWORD"))
elif ins.instruction == "call" and ins.called_function == None:
initial_values_at_call = getInitialConditionsCall(callstack)
for iop in initial_values_at_call.keys():
#print "iop:",iop
if not (iop in mvars):
del initial_values_at_call[iop]
setInitialConditions(ssa, initial_values_at_call, smt_conds)
mvars = set(filter(lambda o: not (o in initial_values_at_call.keys()), mvars))
new_mvars = set()
for v in mvars:
# we convert stack memory variables from one frame to the previous one
if callstack.currentCounter()>1 and v.isStackMem() and v.mem_offset >= 4:
eop = callstack.convertStackMemOp(v)
smt_conds.add(eq.getEq(v,eop))
new_mvars.add(eop)
else:
new_mvars.add(v)
mvars = set(filter(lambda o: not (o.isStackMem() and o.mem_offset >= 4), mvars))
mvars = mvars.union(new_mvars)
return mvars