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
|
def prove(f):
'''取自 http://rise4fun.com/Z3Py/tutorialcontent/guide#h26'''
s = Solver()
s.add(Not(f))
if s.check() == unsat:
return True
return False
class SymbolicExecutionEngine(object):
'''符号执行引擎是处理符号执行的类。它将跟踪遇到的不同方程以及程序每个点的CPU上下文。
符号变量必须由用户找到(或使用数据污染)。这不是本类的目的。
我们很幸运,只需要处理这些操作和编码:
. mov:
. mov reg32, reg32
. mov reg32, [mem]
. mov [mem], reg32
. shr:
. shr reg32, cst
. shl:
. shl reg32, cst
. and:
. and reg32, cst
. and reg32, reg32
. xor:
. xor reg32, cst
. or:
. or reg32, reg32
. add:
. add reg32, reg32
我们也不关心:
. EFLAGS
. 分支
. 较小的寄存器(16/8位)
长话短说:它是完美的;这种环境使得玩符号执行非常容易。'''
def __init__(self, start, end):
# 这是每个时间的CPU上下文
# 寄存器的值是方程字典中的索引
self.ctx = {
'eax' : None,
'ebx' : None,
'ecx' : None,
'edx' : None,
'esi' : None,
'edi' : None,
'ebp' : None,
'esp' : None,
'eip' : None
}
# 符号执行开始的地址
self.start = start
# 符号执行停止的地址
self.end = end
# 我们的反汇编器
self.disass = Disassembler(start, end)
# 这是指令可用于保存临时值/结果的内存
self.mem = {}
# 每个方程必须有唯一ID
self.idx = 0
# 符号变量将存储在那里
self.sym_variables = []
# 每个方程将存储在这里
self.equations = {}
def _check_if_reg32(self, r):
'''XXX: 做一个装饰器?'''
return r.lower() in self.ctx
def _push_equation(self, e):
self.equations[self.idx] = e
self.idx += 1
return (self.idx - 1)
def set_reg_with_equation(self, r, e):
if self._check_if_reg32(r) == False:
return
self.ctx[r] = self._push_equation(e)
def get_reg_equation(self, r):
if self._check_if_reg32(r) == False:
return
return self.equations[self.ctx[r]]
def run(self):
'''从开始地址到结束地址运行引擎'''
for mnemonic, dst, src in self.disass.get_next_instruction():
if mnemonic == 'mov':
# mov reg32, reg32
if src in self.ctx and dst in self.ctx:
self.ctx[dst] = self.ctx[src]
# mov reg32, [mem]
elif (src.find('var_') != -1 or src.find('arg') != -1) and dst in self.ctx:
if src not in self.mem:
# 尝试读取未初始化的位置,我们得到了一个符号变量!
sym = BitVec('arg%d' % len(self.sym_variables), 32)
self.sym_variables.append(sym)
print 'Trying to read a non-initialized area, we got a new symbolic variable: %s' % sym
self.mem[src] = self._push_equation(sym)
self.ctx[dst] = self.mem[src]
# mov [mem], reg32
elif dst.find('var_') != -1 and src in self.ctx:
if dst not in self.mem:
self.mem[dst] = None
self.mem[dst] = self.ctx[src]
else:
raise Exception('This encoding of "mov" is not handled.')
elif mnemonic == 'shr':
# shr reg32, cst
if dst in self.ctx and type(src) == int:
self.set_reg_with_equation(dst, LShR(self.get_reg_equation(dst), src))
else:
raise Exception('This encoding of "shr" is not handled.')
elif mnemonic == 'shl':
# shl reg32, cst
if dst in self.ctx and type(src) == int:
self.set_reg_with_equation(dst, self.get_reg_equation(dst) << src)
else:
raise Exception('This encoding of "shl" is not handled.')
elif mnemonic == 'and':
x = None
# and reg32, cst
if type(src) == int:
x = src
# and reg32, reg32
elif src in self.ctx:
x = self.get_reg_equation(src)
else:
raise Exception('This encoding of "and" is not handled.')
self.set_reg_with_equation(dst, self.get_reg_equation(dst) & x)
elif mnemonic == 'xor':
# xor reg32, cst
if dst in self.ctx and type(src) == int:
self.set_reg_with_equation(dst, self.get_reg_equation(dst) ^ src)
else:
raise Exception('This encoding of "xor" is not handled.')
elif mnemonic == 'or':
# or reg32, reg32
if dst in self.ctx and src in self.ctx:
self.set_reg_with_equation(dst, self.get_reg_equation(dst) | self.get_reg_equation(src))
else:
raise Exception('This encoding of "or" is not handled.')
elif mnemonic == 'add':
# add reg32, reg32
if dst in self.ctx and src in self.ctx:
self.set_reg_with_equation(dst, self.get_reg_equation(dst) + self.get_reg_equation(src))
else:
raise Exception('This encoding of "add" is not handled.')
else:
print mnemonic, dst, src
raise Exception('This instruction is not handled.')
def get_reg_equation_simplified(self, reg):
eq = self.get_reg_equation(reg)
eq = simplify(eq)
return eq
|