-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathbasic.py
executable file
·72 lines (52 loc) · 1.6 KB
/
basic.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
#!/usr/bin/env python3
import os
import struct
import sys
from manticore.native import Manticore
# Examples:
# printf "\x41\x00\x00\x00" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "++\x00\x00" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "++++" | PYTHONPATH=. ./examples/script/aarch64/basic.py
# printf "ffffff" | PYTHONPATH=. ./examples/script/aarch64/basic.py
DIR = os.path.dirname(__file__)
FILE = os.path.join(DIR, 'basic')
STDIN = sys.stdin.readline()
# Avoid writing anything to 'STDIN' here. Do it in the 'init' hook as that's
# more flexible.
m = Manticore(FILE, concrete_start='', stdin_size=0)
@m.init
def init(m, ready_states):
for state in ready_states:
state.platform.input.write(state.symbolicate_buffer(STDIN, label='STDIN'))
# Hook the 'if' case.
@m.hook(0x4006bc)
def hook_if(state):
print('hook if')
state.abandon()
# Hook the 'else' case.
@m.hook(0x4006cc)
def hook_else(state):
print('hook else')
# See how the constraints are affected by input.
print_constraints(state, 6)
w0 = state.cpu.W0
if isinstance(w0, int): # concrete
print(hex(w0))
else:
print(w0) # symbolic
solved = state.solve_one(w0)
print(struct.pack("<I", solved))
# Hook 'puts' in the 'else' case.
@m.hook(0x4006d4)
def hook_puts(state):
print('hook puts')
cpu = state.cpu
print(cpu.read_string(cpu.X0))
def print_constraints(state, nlines):
i = 0
for c in str(state.constraints).split('\n'):
if i >= nlines:
break
print(c)
i += 1
m.run()