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
|
from miasm2.jitter.jitload import jitter
from miasm2.core import asmbloc
from miasm2.core.utils import *
from miasm2.arch.x86.sem import ir_x86_16, ir_x86_32, ir_x86_64
import logging
log = logging.getLogger('jit_x86')
hnd = logging.StreamHandler()
hnd.setFormatter(logging.Formatter("[%(levelname)s]: %(message)s"))
log.addHandler(hnd)
log.setLevel(logging.CRITICAL)
class jitter_x86_16(jitter):
def __init__(self, *args, **kwargs):
sp = asmbloc.asm_symbol_pool()
jitter.__init__(self, ir_x86_16(sp), *args, **kwargs)
self.vm.set_little_endian()
self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
self.ir_arch.do_stk_segm = False
self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
def push_uint16_t(self, v):
self.cpu.SP -= self.ir_arch.sp.size / 8
self.vm.set_mem(self.cpu.SP, pck16(v))
def pop_uint16_t(self):
x = upck16(self.vm.get_mem(self.cpu.SP, self.ir_arch.sp.size / 8))
self.cpu.SP += self.ir_arch.sp.size / 8
return x
def get_stack_arg(self, n):
x = upck16(self.vm.get_mem(self.cpu.SP + 4 * n, 4))
return x
def init_run(self, *args, **kwargs):
jitter.init_run(self, *args, **kwargs)
self.cpu.IP = self.pc
class jitter_x86_32(jitter):
def __init__(self, *args, **kwargs):
sp = asmbloc.asm_symbol_pool()
jitter.__init__(self, ir_x86_32(sp), *args, **kwargs)
self.vm.set_little_endian()
self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
self.ir_arch.do_stk_segm = False
self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
def push_uint32_t(self, v):
self.cpu.ESP -= self.ir_arch.sp.size / 8
self.vm.set_mem(self.cpu.ESP, pck32(v))
def pop_uint32_t(self):
x = upck32(self.vm.get_mem(self.cpu.ESP, self.ir_arch.sp.size / 8))
self.cpu.ESP += self.ir_arch.sp.size / 8
return x
def get_stack_arg(self, n):
x = upck32(self.vm.get_mem(self.cpu.ESP + 4 * n, 4))
return x
# calling conventions
# stdcall
def func_args_stdcall(self, n_args):
ret_ad = self.pop_uint32_t()
args = []
for _ in xrange(n_args):
args.append(self.pop_uint32_t())
if log.level <= logging.DEBUG:
log.debug('%s %s %s' % (whoami(), hex(ret_ad), [hex(x) for x in args]))
return ret_ad, args
def func_ret_stdcall(self, ret_addr, ret_value1=None, ret_value2=None):
self.cpu.EIP = ret_addr
if ret_value1 is not None:
self.cpu.EAX = ret_value1
if ret_value2 is not None:
self.cpu.EDX = ret_value
# cdecl
def func_args_cdecl(self, n_args, dolog=True):
ret_ad = self.pop_uint32_t()
args = []
for i in xrange(n_args):
args.append(self.get_stack_arg(i))
if dolog and log.level <= logging.DEBUG:
log.debug('%s %s %s' %
(whoami(), hex(ret_ad), [hex(x) for x in args]))
return ret_ad, args
def func_ret_cdecl(self, ret_addr, ret_value):
self.cpu.EIP = ret_addr
self.cpu.EAX = ret_value
def add_lib_handler(self, libs, user_globals=None):
"""Add a function to handle libs call with breakpoints
@libs: libimp instance
@user_globals: dictionnary for defined user function
"""
if user_globals is None:
user_globals = {}
from miasm2.os_dep import win_api_x86_32
def handle_lib(jitter):
fname = libs.fad2cname[jitter.pc]
if fname in user_globals:
f = user_globals[fname]
elif fname in win_api_x86_32.__dict__:
f = win_api_x86_32.__dict__[fname]
else:
log.debug('%s' % repr(fname))
raise ValueError('unknown api', hex(jitter.pop_uint32_t()), repr(fname))
f(jitter)
jitter.pc = getattr(jitter.cpu, jitter.ir_arch.pc.name)
return True
for f_addr in libs.fad2cname:
self.add_breakpoint(f_addr, handle_lib)
def init_run(self, *args, **kwargs):
jitter.init_run(self, *args, **kwargs)
self.cpu.EIP = self.pc
class jitter_x86_64(jitter):
def __init__(self, *args, **kwargs):
sp = asmbloc.asm_symbol_pool()
jitter.__init__(self, ir_x86_64(sp), *args, **kwargs)
self.vm.set_little_endian()
self.ir_arch.jit_pc = self.ir_arch.arch.regs.RIP
self.ir_arch.do_stk_segm = False
self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode
self.ir_arch.irbloc_fix_regs_for_mode = self.ir_archbloc_fix_regs_for_mode
def ir_archbloc_fix_regs_for_mode(self, irbloc, attrib=64):
self.orig_irbloc_fix_regs_for_mode(irbloc, 64)
def push_uint64_t(self, v):
self.cpu.RSP -= self.ir_arch.sp.size / 8
self.vm.set_mem(self.cpu.RSP, pck64(v))
def pop_uint64_t(self):
x = upck64(self.vm.get_mem(self.cpu.RSP, self.ir_arch.sp.size / 8))
self.cpu.RSP += self.ir_arch.sp.size / 8
return x
def get_stack_arg(self, n):
x = upck64(self.vm.get_mem(self.cpu.RSP + 8 * n, 8))
return x
def init_run(self, *args, **kwargs):
jitter.init_run(self, *args, **kwargs)
self.cpu.RIP = self.pc
def func_args_stdcall(self, n_args):
args_regs = ['RCX', 'RDX', 'R8', 'R9']
ret_ad = self.pop_uint64_t()
args = []
for i in xrange(min(n_args, 4)):
args.append(self.cpu.get_gpreg()[args_regs[i]])
for i in xrange(max(0, n_args - 4)):
args.append(self.get_stack_arg(i))
log.debug('%s %s %s' % (whoami(), hex(ret_ad), [hex(x) for x in args]))
return ret_ad, args
def func_ret_stdcall(self, ret_addr, ret_value=None):
self.pc = self.cpu.RIP = ret_addr
if ret_value is not None:
self.cpu.RAX = ret_value
return True
def func_args_cdecl(self, n_args):
args_regs = ['RCX', 'RDX', 'R8', 'R9']
ret_ad = self.pop_uint64_t()
args = []
for i in xrange(min(n_args, 4)):
args.append(self.cpu.get_gpreg()[args_regs[i]])
for i in xrange(max(0, n_args - 4)):
args.append(self.get_stack_arg(i))
log.debug('%s %s %s' % (whoami(), hex(ret_ad), [hex(x) for x in args]))
return ret_ad, args
def func_ret_cdecl(self, ret_addr, ret_value=None):
self.pc = self.cpu.RIP = ret_addr
if ret_value is not None:
self.cpu.RAX = ret_value
return True
def add_lib_handler(self, libs, user_globals=None):
"""Add a function to handle libs call with breakpoints
@libs: libimp instance
@user_globals: dictionnary for defined user function
"""
if user_globals is None:
user_globals = {}
from miasm2.os_dep import win_api_x86_32
def handle_lib(jitter):
fname = libs.fad2cname[jitter.pc]
if fname in user_globals:
f = user_globals[fname]
elif fname in win_api_x86_32.__dict__:
f = win_api_x86_32.__dict__[fname]
else:
log.debug('%s' % repr(fname))
raise ValueError('unknown api', hex(jitter.pop_uint64_t()), repr(fname))
f(jitter)
jitter.pc = getattr(jitter.cpu, jitter.ir_arch.pc.name)
return True
for f_addr in libs.fad2cname:
self.add_breakpoint(f_addr, handle_lib)
|