forked from 36hours/idaemu
/
idaemu.py
305 lines (272 loc) · 12.4 KB
/
idaemu.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from __future__ import print_function
from unicorn import *
from unicorn.x86_const import *
from unicorn.arm_const import *
from unicorn.arm64_const import *
from struct import pack, unpack_from, calcsize
from idaapi import get_func
from idc import Qword, GetManyBytes, SelStart, SelEnd, here, ItemSize
from idautils import XrefsTo
PAGE_ALIGN = 0x1000 # 4k
COMPILE_GCC = 1
COMPILE_MSVC = 2
TRACE_OFF = 0
TRACE_DATA_READ = 1
TRACE_DATA_WRITE = 2
TRACE_CODE = 4
class Emu(object):
def __init__(self, arch, mode, compiler=COMPILE_GCC, stack=0xf000000, \
ssize=3, RA=0xdeadbeaf):
assert(arch in [UC_ARCH_X86, UC_ARCH_ARM, UC_ARCH_ARM64])
if mode in [UC_MODE_16, UC_MODE_THUMB]: RA &= 0xffff
self.arch = arch
self.mode = mode
self.compiler = compiler
self.stack = self._alignAddr(stack)
self.ssize = ssize
self.RA = RA # return address, for stop emulate
self.data = []
self.regs = []
self.curUC = None
self.traceOption = TRACE_OFF
self.logBuffer = []
def _addTrace(self, logInfo):
self.logBuffer.append(logInfo)
# callback for tracing invalid memory access (READ or WRITE, FETCH)
def _hook_mem_invalid(self, uc, access, address, size, value, user_data):
addr = self._alignAddr(address)
uc.mem_map(addr, PAGE_ALIGN)
data = self._getOriginData(addr, PAGE_ALIGN)
uc.mem_write(addr, data)
return True
def _hook_mem_access(self, uc, access, address, size, value, user_data):
if access == UC_MEM_WRITE and self.traceOption & TRACE_DATA_WRITE:
self._addTrace("### Memory WRITE at 0x%x, data size = %u, data value = 0x%x" \
%(address, size, value))
elif access == UC_MEM_READ and self.traceOption & TRACE_DATA_READ:
self._addTrace("### Memory READ at 0x%x, data size = %u" \
%(address, size))
def _hook_code(self, uc, address, size, user_data):
self._addTrace("### Trace Instruction at 0x%x, size = %u" %(address, size))
def _alignAddr(self, addr):
return addr // PAGE_ALIGN * PAGE_ALIGN
def _getOriginData(self, address, size):
res = []
for offset in xrange(0, size, 64):
tmp = GetManyBytes(address + offset, 64)
if tmp == None:
res.extend([pack("<Q", Qword(address + offset + i)) for i in range(0, 64, 8)])
else:
res.append(tmp)
res = "".join(res)
return res[:size]
def _initStackAndArgs(self, uc, RA, *args):
uc.mem_map(self.stack, (self.ssize+1) * PAGE_ALIGN)
sp = self.stack + self.ssize * PAGE_ALIGN
regs = []
if self.arch == UC_ARCH_X86:
if self.mode == UC_MODE_16:
step = 2
uc.reg_write(UC_X86_REG_SP, sp)
uc.mem_write(sp, pack('<H', RA))
self.RES_REG = UC_X86_REG_AX
elif self.mode == UC_MODE_32:
step = 4
uc.reg_write(UC_X86_REG_ESP, sp)
uc.mem_write(sp, pack('<I', RA))
self.RES_REG = UC_X86_REG_EAX
elif self.mode == UC_MODE_64:
step = 8
uc.reg_write(UC_X86_REG_RSP, sp)
uc.mem_write(sp, pack('<Q', RA))
self.RES_REG = UC_X86_REG_RAX
if self.compiler == COMPILE_GCC:
regs = [UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX, UC_X86_REG_RCX,
UC_X86_REG_R8, UC_X86_REG_R9]
elif self.compiler == COMPILE_MSVC:
regs = [UC_X86_REG_RCX, UC_X86_REG_RDX, UC_X86_REG_R8, UC_X86_REG_R9]
elif self.arch == UC_ARCH_ARM:
if self.mode == UC_MODE_ARM:
step = 4
elif self.mode == UC_MODE_THUMB:
step = 2
uc.reg_write(UC_ARM_REG_SP, sp)
uc.reg_write(UC_ARM_REG_LR, RA)
self.RES_REG = UC_ARM_REG_R0
regs = [UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3]
elif self.arch == UC_ARCH_ARM64:
step = 8
uc.reg_write(UC_ARM64_REG_SP, sp)
uc.reg_write(UC_ARM64_REG_X30, RA)
self.RES_REG = UC_ARM64_REG_X0
regs = [UC_ARM64_REG_X0, UC_ARM64_REG_X1, UC_ARM64_REG_X2, UC_ARM64_REG_X3,
UC_ARM64_REG_X4, UC_ARM64_REG_X5, UC_ARM64_REG_X6, UC_ARM64_REG_X7]
## init the arguments
i = 0
while i < len(regs) and i < len(args):
uc.reg_write(regs[i], args[i])
i += 1
while i < len(args):
sp += step
uc.mem_write(sp, args[i])
i += 1
def _getBit(self, value, offset):
mask = 1 << offset
return 1 if (value & mask) > 0 else 0
def _showRegs(self, uc):
print(">>> regs:")
try:
if self.mode == UC_MODE_16:
ax = uc.reg_read(UC_X86_REG_AX)
bx = uc.reg_read(UC_X86_REG_BX)
cx = uc.reg_read(UC_X86_REG_CX)
dx = uc.reg_read(UC_X86_REG_DX)
di = uc.reg_read(UC_X86_REG_SI)
si = uc.reg_read(UC_X86_REG_DI)
bp = uc.reg_read(UC_X86_REG_BP)
sp = uc.reg_read(UC_X86_REG_SP)
ip = uc.reg_read(UC_X86_REG_IP)
eflags = uc.reg_read(UC_X86_REG_EFLAGS)
print(" AX = 0x%x BX = 0x%x CX = 0x%x DX = 0x%x" % (ax, bx, cx, dx))
print(" DI = 0x%x SI = 0x%x BP = 0x%x SP = 0x%x" % (di, si, bp, sp))
print(" IP = 0x%x" % eip)
elif self.mode == UC_MODE_32:
eax = uc.reg_read(UC_X86_REG_EAX)
ebx = uc.reg_read(UC_X86_REG_EBX)
ecx = uc.reg_read(UC_X86_REG_ECX)
edx = uc.reg_read(UC_X86_REG_EDX)
edi = uc.reg_read(UC_X86_REG_ESI)
esi = uc.reg_read(UC_X86_REG_EDI)
ebp = uc.reg_read(UC_X86_REG_EBP)
esp = uc.reg_read(UC_X86_REG_ESP)
eip = uc.reg_read(UC_X86_REG_EIP)
eflags = uc.reg_read(UC_X86_REG_EFLAGS)
print(" EAX = 0x%x EBX = 0x%x ECX = 0x%x EDX = 0x%x" % (eax, ebx, ecx, edx))
print(" EDI = 0x%x ESI = 0x%x EBP = 0x%x ESP = 0x%x" % (edi, esi, ebp, esp))
print(" EIP = 0x%x" % eip)
elif self.mode == UC_MODE_64:
rax = uc.reg_read(UC_X86_REG_RAX)
rbx = uc.reg_read(UC_X86_REG_RBX)
rcx = uc.reg_read(UC_X86_REG_RCX)
rdx = uc.reg_read(UC_X86_REG_RDX)
rdi = uc.reg_read(UC_X86_REG_RSI)
rsi = uc.reg_read(UC_X86_REG_RDI)
rbp = uc.reg_read(UC_X86_REG_RBP)
rsp = uc.reg_read(UC_X86_REG_RSP)
rip = uc.reg_read(UC_X86_REG_RIP)
r8 = uc.reg_read(UC_X86_REG_R8)
r9 = uc.reg_read(UC_X86_REG_R9)
r10 = uc.reg_read(UC_X86_REG_R10)
r11 = uc.reg_read(UC_X86_REG_R11)
r12 = uc.reg_read(UC_X86_REG_R12)
r13 = uc.reg_read(UC_X86_REG_R13)
r14 = uc.reg_read(UC_X86_REG_R14)
r15 = uc.reg_read(UC_X86_REG_R15)
eflags = uc.reg_read(UC_X86_REG_EFLAGS)
print(" RAX = 0x%x RBX = 0x%x RCX = 0x%x RDX = 0x%x" % (rax, rbx, rcx, rdx))
print(" RDI = 0x%x RSI = 0x%x RBP = 0x%x RSP = 0x%x" % (rdi, rsi, rbp, rsp))
print(" R8 = 0x%x R9 = 0x%x R10 = 0x%x R11 = 0x%x R12 = 0x%x " \
"R13 = 0x%x R14 = 0x%x R15 = 0x%x" % (r8, r9, r10, r11, r12, r13, r14, r15))
print(" RIP = 0x%x" % rip)
print(" EFLAGS:")
print(" CF=%d PF=%d AF=%d ZF=%d SF=%d TF=%d IF=%d DF=%d OF=%d IOPL=%d " \
"NT=%d RF=%d VM=%d AC=%d VIF=%d VIP=%d ID=%d"
% (self._getBit(eflags, 0),
self._getBit(eflags, 2),
self._getBit(eflags, 4),
self._getBit(eflags, 6),
self._getBit(eflags, 7),
self._getBit(eflags, 8),
self._getBit(eflags, 9),
self._getBit(eflags, 10),
self._getBit(eflags, 11),
self._getBit(eflags, 12) + self._getBit(eflags, 13) * 2,
self._getBit(eflags, 14),
self._getBit(eflags, 16),
self._getBit(eflags, 17),
self._getBit(eflags, 18),
self._getBit(eflags, 19),
self._getBit(eflags, 20),
self._getBit(eflags, 21)))
except UcError as e:
print("#ERROR: %s" % e)
def _initData(self, uc):
for address, data, init in self.data:
addr = self._alignAddr(address)
size = PAGE_ALIGN
while addr + size < len(data): size += PAGE_ALIGN
uc.mem_map(addr, size)
if init: uc.mem_write(addr, self._getOriginData(addr, size))
uc.mem_write(address, data)
def _initRegs(self, uc):
for reg, value in self.regs:
uc.reg_write(reg, value)
def _emulate(self, startAddr, stopAddr, *args):
try:
self.logBuffer = []
uc = Uc(self.arch, self.mode)
self.curUC = uc
self._initStackAndArgs(uc, stopAddr, *args)
self._initData(uc)
self._initRegs(uc)
# add the invalid memory access hook
uc.hook_add(UC_HOOK_MEM_READ_UNMAPPED | UC_HOOK_MEM_WRITE_UNMAPPED | \
UC_HOOK_MEM_FETCH_UNMAPPED, self._hook_mem_invalid)
# add the trace hook
if self.traceOption & (TRACE_DATA_READ | TRACE_DATA_WRITE) :
uc.hook_add(UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITE, self._hook_mem_access)
if self.traceOption & TRACE_CODE :
uc.hook_add(UC_HOOK_CODE, self._hook_code)
# start emulate
uc.emu_start(startAddr, stopAddr)
except UcError as e:
print("#ERROR: %s" % e)
# set the data before emulation
def setData(self, address, data, init=False):
self.data.append((address, data, init))
def setReg(self, reg, value):
self.regs.append((reg, value))
def showRegs(self, *regs):
if self.curUC == None:
print("current uc is none.")
return
for reg in regs:
print("0x%x" % self.curUC.reg_read(reg))
def showData(self, fmt, addr, count = 1):
if self.curUC == None:
print("current uc is none.")
return
if count > 1: print('[')
for i in range(count):
dataSize = calcsize(fmt)
data = self.curUC.mem_read(addr + i * dataSize, dataSize)
if count > 1: print(' ', end='')
st = unpack_from(fmt, data)
print(st[0], end='') if len(st) == 1 else print(st, end='')
if count > 1: print(',')
print(']') if count > 1 else print('')
def setTrace(self, opt):
if opt != TRACE_OFF:
self.traceOption |= opt
else:
self.traceOption = TRACE_OFF
def showTrace(self):
logs = "\n".join(self.logBuffer)
print(logs)
def eFunc(self, address=None, *args):
if address == None: address = here()
func = get_func(address)
refs = [ref.frm for ref in XrefsTo(func.startEA, 0)]
if len(refs) == 0:
RA = self.RA
else:
RA = refs[0] + ItemSize(refs[0])
self._emulate(func.startEA, RA, *args)
print("Euclation done. Below is the Result:")
res = self.curUC.reg_read(self.RES_REG)
print(">>> function result = %d" % res)
def eBlock(self, codeStart=None, codeEnd=None):
if codeStart == None: codeStart = SelStart()
if codeEnd == None: codeEnd = SelEnd()
self._emulate(codeStart, codeEnd)
self._showRegs(self.curUC)