-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathop.py
125 lines (100 loc) · 2.79 KB
/
op.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
import idc
import re
from common import *
from op_classes import *
def convert_graph(root):
_, root = dfs(root, f_convert)
return root
def f_convert(acc, bb, children):
if acc == None:
acc = dict(), None
bb_to_dbb, root = acc
try:
dbb = bb_to_dbb[bb]
except:
dbb = convert_bb(bb)
bb_to_dbb[bb] = dbb
#print "bb:", bb.disasm()
for i,child in enumerate(children):
try:
child_dbb = bb_to_dbb[child]
except:
child_dbb = convert_bb(child)
bb_to_dbb[child] = child_dbb
#print "child:", i, child.disasm()
if i==0:
dbb.child1 = child_dbb
elif i==1:
dbb.child2 = child_dbb
if root==None:
root = dbb
return bb_to_dbb, root
def convert_bb(bb):
dbb = DBB()
last = bb.body[-1]
for n in bb.body:
# don't collect uncond. jmps from inside
if is_jmp(n) and n!=last:
continue
m = convert_inst(n)
dbb.add(m)
return dbb
def convert_inst(addr):
mnem = idc.GetMnem(addr)
op1 = idc.GetOpnd(addr, 0)
op2 = idc.GetOpnd(addr, 1)
if not op1: op1 = None
if not op2: op2 = None
if is_jxx(addr):
op1 = jxx_target(addr)
op1 = str(op1)
if op1:
op1 = Opnd(op1)
if op2:
op2 = Opnd(op2)
if is_jmp(addr):
ni = Jmp(addr, mnem, op1)
elif is_jxx(addr):
ni = Jxx(addr, mnem, op1)
elif mnem == "add":
ni = Add(addr, mnem, op1, op2)
elif mnem == "sub":
ni = Sub(addr, mnem, op1, op2)
elif mnem == "and":
ni = And(addr, mnem, op1, op2)
elif mnem == "or":
ni = Or(addr, mnem, op1, op2)
elif mnem == "xor":
ni = Xor(addr, mnem, op1, op2)
elif mnem == "dec":
ni = Dec(addr, mnem, op1, op2)
elif mnem == "inc":
ni = Inc(addr, mnem, op1, op2)
elif mnem == "not":
ni = Not(addr, mnem, op1, op2)
elif mnem == "neg":
ni = Neg(addr, mnem, op1, op2)
elif mnem == "shl":
ni = Shl(addr, mnem, op1, op2)
elif mnem == "shr":
ni = Shr(addr, mnem, op1, op2)
elif mnem == "test":
ni = Test(addr, mnem, op1, op2)
elif mnem == "xchg":
ni = Xchg(addr, mnem, op1, op2)
elif mnem == "mov":
ni = Mov(addr, mnem, op1, op2)
elif mnem == "push":
ni = Push(addr, mnem, op1, op2)
elif mnem == "pop":
ni = Pop(addr, mnem, op1, op2)
elif mnem == "pushf":
ni = Pushf(addr, mnem, op1, op2)
elif mnem == "popf":
ni = Popf(addr, mnem, op1, op2)
elif mnem in NOT_IMPLEMENTED:
ni = Inst(addr, mnem, op1, op2)
else:
print "Unsupported addrruction @%s: %s"%(hex(addr), mnem)
assert(False)
return ni