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
|
import os
from argparse import ArgumentParser
from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
from miasm2.analysis.machine import Machine
from pdb import pm
parser = ArgumentParser(description="x86 32 basic Jitter")
parser.add_argument("-j", "--jitter",
help="Jitter engine. Possible values are : tcc (default), llvm",
default="tcc")
args = parser.parse_args()
# Shellcode
# main:
# MOV EAX, 0x1
# loop_main:
# CMP EAX, 0x10
# JZ loop_end
# loop_inc:
# INC EAX
# JMP loop_main
# loop_end:
# RET
data = "b80100000083f810740340ebf8c3".decode("hex")
run_addr = 0x40000000
def code_sentinelle(jitter):
jitter.run = False
jitter.pc = 0
return True
def init_jitter():
global data, run_addr
# Create jitter
myjit = Machine("x86_32").jitter(args.jitter)
myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data)
# Init jitter
myjit.init_stack()
myjit.jit.log_regs = True
myjit.jit.log_mn = True
myjit.push_uint32_t(0x1337beef)
myjit.add_breakpoint(0x1337beef, code_sentinelle)
return myjit
# Test 'max_exec_per_call'
print "[+] First run, to jit blocks"
myjit = init_jitter()
myjit.init_run(run_addr)
myjit.continue_run()
assert myjit.run is False
assert myjit.cpu.EAX == 0x10
## Let's specify a max_exec_per_call
## 5: main, loop_main, loop_inc, loop_main, loop_inc
myjit.jit.options["max_exec_per_call"] = 5
first_call = True
def cb(jitter):
global first_call
if first_call:
# Avoid breaking on the first pass (before any execution)
first_call = False
return True
return False
## Second run
print "[+] Second run"
myjit.push_uint32_t(0x1337beef)
myjit.cpu.EAX = 0
myjit.init_run(run_addr)
myjit.exec_cb = cb
myjit.continue_run()
assert myjit.run is True
# Use a '<=' because it's a 'max_...'
assert myjit.cpu.EAX <= 3
|