-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrecover_x86.py
290 lines (238 loc) · 6.74 KB
/
recover_x86.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
from config import *
import pickle
from vmi_top_common import *
ID_EFLAGS = 7
REG_EFLAGS = REG_DICT[ID_EFLAGS]
ID_ESP = 7
#FIXME: autogen these
LOAD_PTR_REG = 0x000
STORE_ADDR = 0x001
STORE_DWORD_ADDR = 0x018
MOVE_ADDR_STACK = 0x14e
LOAD_ADDR = 0x009
LOAD_DWORD = 0x004
LOAD_REG = 0x007
ADD_NO_FLAGS = 0x006
MOVE_STACK_STACK = 0x208
LOAD_DWORD_ADDR = 0x00c
LOAD_WORD = 0x003
STORE_DWORD_REG = 0x01c
INC_DWORD = 0x055
ADD_DWORD = 0x024
XOR_DWORD = 0x03d
ADD_REG_TO_ADDR = 0x211
PUSH_RET_ADDR = 0x15d
UNCOND_JMP = 0x154
GTFO = 0x212
STACK = "stack"
ADDR = "addr"
T_INVALID = "t_invalid"
T_REG_PTR = "t_reg_ptr"
T_CONST = "t_const"
T_REG = "t_reg"
T_BINOP = "t_binop"
N = 2**32
XOR = "OP_XOR"
ADD = "OP_ADD"
class Thing:
def __init__(self, typ, thing):
self.typ = typ
self.thing = thing
def __repr__(self):
typ = self.typ
thing = self.thing
if typ == T_REG:
return thing
elif typ == T_CONST:
return hex(thing)
elif typ == T_REG_PTR:
return "ptr "+thing
else:
assert False, "bad type: "+typ
def __str__(self): return self.__repr__()
class BinOp:
def __init__(self, op, left, right):
self.op = op
self.l = left
self.r = right
def __repr__(self):
if self.op == XOR:
st = "^"
elif self.op == ADD:
st = "+"
else:
assert False
sl = str(self.l)
sr = str(self.r)
s = "%s %s %s"%(sl, st, sr)
return s
def t_type(tu): return tu[0]
def t_thing(tu): return tu[1]
def t_size(tu): return tu[2]
def t_reg_ptr(reg_id):
reg = REG_DICT[reg_id]
return (T_REG_PTR, reg, 4)
def symb_eval(state, vmi):
def stack_size(stack):
l = map(lambda t: t_size(t), stack)
return sum(l)
def pop4(stack):
(typ, thing, size) = stack.pop()
if size == 4:
return (typ, thing, size)
elif size == 2:
assert typ == T_CONST
(typ2, thing2, size2) = stack.pop()
if typ2 != typ or size != size2:
s= "mismatch: (%s, %s), (%d, %d)"%(typ, typ2, size, size2)
assert False, s
x = (thing<<16)+thing2
return (typ, x, 4)
else:
assert False, "bad size: "+size
stack = state[STACK]
addr = state[ADDR]
vid = int(vmi.vid, 16)
native = None
if vid == LOAD_PTR_REG:
reg_id = vmi.param
t = t_reg_ptr(reg_id)
stack.append(t)
elif vid == STORE_ADDR:
addr = pop4(stack)
elif vid == STORE_DWORD_ADDR:
(addr_typ, addr_thing, _) = addr
if addr_typ == T_REG_PTR and stack == []:
native = "vm_%s <- %s"%(addr_thing, addr_thing)
else:
if addr_typ in [T_REG_PTR]:
dst = "%s"
elif addr_typ in [T_REG, T_BINOP]:
dst = "[%s]"
elif addr_typ == T_CONST:
dst = "[%x]"
else:
assert False
(typ, thing, sz) = stack.pop()
fmt = dst + " <- %s"
native = fmt%(addr_thing, thing)
elif vid == MOVE_ADDR_STACK:
size = stack_size(stack)
addr = (T_CONST, size, 4)
elif vid == LOAD_ADDR:
stack.append(addr)
elif vid == LOAD_DWORD:
assert vmi.param_size == 4
t = (T_CONST, vmi.param, 4)
stack.append(t)
elif vid == LOAD_WORD:
assert vmi.param_size == 2
t = (T_CONST, vmi.param, 2)
stack.append(t)
elif vid == LOAD_REG:
assert vmi.param_size == 1
reg = REG_DICT[vmi.param]
t = (T_REG, reg, 4)
stack.append(t)
elif vid == ADD_NO_FLAGS:
(typ1, x1, s1) = stack.pop()
(typ2, x2, s2) = stack.pop()
assert s1 == s2
if typ1 == typ2 == T_CONST:
t = (T_CONST, (x1+x2)%N, s1)
stack.append(t)
else:
th1 = Thing(typ1, x1)
th2 = Thing(typ2, x2)
th = BinOp(ADD, th1, th2)
t = (T_BINOP, th, s1)
stack.append(t)
elif vid == ADD_DWORD:
(typ1, x1, s1) = stack.pop()
(typ2, x2, s2) = stack.pop()
assert s1 == s2
if typ1 == typ2 == T_CONST:
assert False
th1 = Thing(typ1, x1)
th2 = Thing(typ2, x2)
th = BinOp(ADD, th1, th2)
t = (T_BINOP, th, s1)
stack.append(t)
t = (T_REG, REG_EFLAGS, 4)
stack.append(t)
elif vid == ADD_REG_TO_ADDR:
(typ, thing, sz) = addr
assert vmi.param_size == 1
assert sz == 4
reg_id = vmi.param
if reg_id == ID_ESP:
reg = REG_ESP
else:
reg = REG_DICT[reg_id]
th1 = Thing(typ, thing)
th2 = Thing(T_REG, reg)
th = BinOp(ADD, th1, th2)
t = (T_BINOP, th, 4)
addr = t
#nop
elif vid == MOVE_STACK_STACK:
(typ, x, sz) = stack.pop()
cur_size = stack_size(stack)
assert x == cur_size+4
elif vid == LOAD_DWORD_ADDR:
(typ, thing, sz) = addr
if typ == T_REG_PTR:
t = (T_REG, thing, sz)
stack.append(t)
else:
assert False
elif vid == XOR_DWORD:
(typ1, thing1, sz1) = stack.pop()
(typ2, thing2, sz2) = stack.pop()
assert sz1 == sz2
assert typ1 in [T_REG, T_CONST]
assert typ2 in [T_REG, T_CONST]
th1 = Thing(typ1, thing1)
th2 = Thing(typ2, thing2)
tt = BinOp(XOR, th1, th2)
t = (T_BINOP, tt, sz1)
stack.append(t)
#push flags after arith ops
t = (T_REG, REG_EFLAGS, 4)
stack.append(t)
elif vid == STORE_DWORD_REG:
(typ, thing, sz) = stack.pop()
if typ == T_REG:
if vmi.param == ID_EFLAGS:
if thing == REG_EFLAGS:
pass #nothing to do
else:
assert False
else:
assert False
else:
assert False
elif vid == UNCOND_JMP:
assert vmi.param == 1
native = "jmp $+1"
elif vid == PUSH_RET_ADDR:
(typ, ret_addr, sz) = stack.pop()
assert typ == T_CONST
native = "push_return_address: 0x%08x"%ret_addr
elif vid == GTFO:
native = "gtfo"
elif vid == 0x157:
native = "unk_157: mov byte [edi+28h], %d"%vmi.param
else:
assert False, "unkown instr: "+hex(vid)
state[STACK] = stack
state[ADDR] = addr
return state, native
def recover(vmis):
state = {STACK: [], ADDR: (T_INVALID, 0, 0)}
for vmi in vmis:
print vmi.all_disasm()
state, native = symb_eval(state, vmi)
#print state
if native != None:
print native