-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86.py
204 lines (170 loc) · 5.41 KB
/
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
import sys
#sys.path.append("/cs/radoca/libs/")
from target import *
import struct
import pydasm
import colors
def get_ascii_string(target, addr, pid=0, length=100):
ret = ""
if target.running:
ret = target.read(addr, length, pid=pid)
ret = ret[:ret.find("\x00")]
if ret.count("\xff") == len(ret):
return ""
else:
try:
byte = target.mem[addr]
except IndexError:
return ''
while byte != "\x00" and length:
ret += byte
length -= 1
addr += 1
try:
byte = target.mem[addr]
except IndexError:
return ''
return ret
def find_functions(target):
addrs = target.entry_points
for addr in addrs:
if not target.has_func(addr):
target.add_func(addr)
for segment in target.mem.segments:
next = segment.start
while next < segment.end:
next = linear_sweep(target, next) + 1
print "[*] Found %d functions"%len(target.functions)
#TODO: add intelligent parent-detection
def add_func(kip, pc):
f = func(pc); f.module="target"
kip.add_func(f)
def linear_sweep(kip, addr):
pc = addr
mnemonic = ""
while 1:
try:
instr = pydasm.get_instruction(kip.mem[pc:pc+15], pydasm.MODE_32)
except IndexError:
break
if not instr:
pc += 1
parent = None
continue #invalid instruction
#if push %ebp; mov %esp, %ebp -> this is a function: add it
if kip.mem[pc:pc+3] == "\x55\x89\xe5":
parent = pc
if not kip.has_func(pc):
#print "Found func %x with prologue"%pc
add_func(kip,pc)
elif instr.type == pydasm.INSTRUCTION_TYPE_CALL:
if instr.op1.type == pydasm.OPERAND_TYPE_IMMEDIATE:
dest = instr.op1.immediate+pc+instr.length
if not kip.has_func(dest):
#print "Found func %x with call"%pc
add_func(kip,dest)
pc += instr.length
kip.functions.sort(func_cmp)
#second pass, add parents and set up function endings
pc = addr
while 1:
try:
instr = pydasm.get_instruction(kip.mem[pc:pc+15], pydasm.MODE_32)
except IndexError:
break
if not instr:
pc += 1
parent = None
continue #invalid instruction
#if push %ebp; mov %esp, %ebp -> this is a function: add it
if instr.type == pydasm.INSTRUCTION_TYPE_CALL:
if instr.op1.type == pydasm.OPERAND_TYPE_IMMEDIATE:
dest = instr.op1.immediate+pc+instr.length
x = kip.find_func(dest)
parent = kip.which_parent(pc)
if parent:
try:
x.parents.append(parent.start_addr)
except:
print "missing func @ %x"%dest
else:
print "!!!@@@ unknown parent start addr",hex(pc)
pc += instr.length
#add the ends now
prev = kip.functions[0]
for x in kip.functions[1:]:
prev.end_addr = x.start_addr - 1
prev = x
return pc
#def deref(bin, addr):
# try:
# x = bin.mem[addr:addr+4]
# return struct.unpack("<L",x)[0]
# except IndexError:
# return "fail"
####################################################
#display routines
def x86_disas_func(bin, addr, stop=0, running=0):
pc = addr
mnemonic = ""
while pc < stop or (stop == 0 and "hlt" not in mnemonic and "ret" not in mnemonic):
try:
if not running:
instr = pydasm.get_instruction(bin.mem[pc:pc+15], pydasm.MODE_32)
else:
instr = pydasm.get_instruction(str(bin.read(pc,15)), pydasm.MODE_32)
except IndexError: #bad memory address
break
if not instr: break
mnemonic = pydasm.get_instruction_string(instr, pydasm.FORMAT_ATT, pc)
head = mid = tail = reset = ""
if colors.COLORS:
reset = colors.RESET
head += "0x%.8x:\t"%pc
#print "0x%.8x:\t%s"%(pc,mnemonic),
if int(instr.type) in [pydasm.INSTRUCTION_TYPE_CMP, pydasm.INSTRUCTION_TYPE_CMPS, pydasm.INSTRUCTION_TYPE_TEST]:
if colors.COLORS:
mid = colors.PURPLEfgB
elif instr.type in [pydasm.INSTRUCTION_TYPE_PUSH, pydasm.INSTRUCTION_TYPE_POP]:
if colors.COLORS:
mid = colors.GREENfg
elif instr.type in [pydasm.INSTRUCTION_TYPE_RET]:
if colors.COLORS:
mid = colors.YELLOWfgB
elif instr.type in [pydasm.INSTRUCTION_TYPE_CALL, pydasm.INSTRUCTION_TYPE_JMPC, pydasm.INSTRUCTION_TYPE_JMP]:
#hilite calls
if colors.COLORS:
mid = colors.YELLOWfgB
#show function name/ annotations
if instr.immbytes:
dest = instr.op1.immediate + instr.length + pc
f = bin.find_func( dest )
if f and f.name:
tail = "\t###\t%s()"%f.name
elif instr.op1.reg == pydasm.REGISTER_ESP or instr.op2.reg == pydasm.REGISTER_ESP or instr.op3.reg == pydasm.REGISTER_ESP:
if colors.COLORS:
mid = colors.CYANfg
if instr.immbytes:
x = ""
if instr.op2.immediate in bin.mem:
x = instr.op2.immediate
elif instr.op1.immediate in bin.mem:
x = instr.op1.immediate
if x != "":
if colors.COLORS:
tail = "\t@@@\t%s%r%s"%(colors.REDfg, get_ascii_string(bin, x), reset)
else:
tail = "\t@@@\t%r%s"%(get_ascii_string(bin, x), reset)
print head + mid + mnemonic +reset + tail
pc += instr.length
#print "--"
return pc
def dump_code(bin):
bin.functions.sort(func_cmp)
prev = 0
for func in bin.functions:
#mode 32
#print "go",hex(func.start_addr),func.name
if func.end_addr == 0: continue
x86_disas_func(bin, func.start_addr, stop=func.end_addr)
print ""