-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memcpy.py
More file actions
176 lines (133 loc) · 5.64 KB
/
test_memcpy.py
File metadata and controls
176 lines (133 loc) · 5.64 KB
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
import pytest
from shellblocks.shellcode_step import ShellcodeStep
from shellblocks.primitives.memcpy import ShellcodePrimitiveMemcpy
SECTOR_SIZE = 0x2000
class UcMemcpyHelper:
def __init__(self,
get_mu,
shellcode_address,
first_copy_addr,
second_copy_addr):
self.shellcode_address = shellcode_address
self.first_copy_addr = first_copy_addr
self.first_sector = int(self.first_copy_addr/SECTOR_SIZE) * SECTOR_SIZE
self.second_copy_addr = second_copy_addr
self.second_sector = int(self.second_copy_addr/SECTOR_SIZE) * SECTOR_SIZE
self.mu = get_mu()
self.mu.mem_map(self.shellcode_address, SECTOR_SIZE)
self.mu.mem_map(self.first_sector, SECTOR_SIZE)
self.mu.mem_map(self.second_sector, SECTOR_SIZE)
def write_shellcode(self, shellcode):
self.mu.mem_write(self.shellcode_address, shellcode)
@pytest.fixture(scope='function')
def default_memcpy_helper(get_mu):
return UcMemcpyHelper(
get_mu,
0xbfc00000,
0x81000010,
0x82000010
)
def memcpy_get_shellcode(temp_dir_path, compiler_arch, memcpy_helper, copy_len):
helper = memcpy_helper
step = ShellcodeStep(
"first_step",
[
ShellcodePrimitiveMemcpy(
"copy_next_stage",
helper.first_copy_addr,
helper.second_copy_addr,
copy_len
),
],
0x1000,
base_address=helper.shellcode_address,
)
out_file = step.generate(temp_dir_path / step.nickname, compiler_arch)
shellcode = out_file.read_bytes()
return shellcode
@pytest.mark.parametrize('copy_len', [
100,
200,
0x1000,
2,
3,
100
])
def test_memcpy_sanity(temp_dir_path, compiler_arch, default_memcpy_helper, copy_len):
helper = default_memcpy_helper
shellcode = memcpy_get_shellcode(temp_dir_path, compiler_arch, helper, copy_len)
# Try to run shellcode
# --------------------
# write machine code to be emulated to memory
helper.mu.mem_write(helper.shellcode_address, shellcode)
helper.mu.mem_write(helper.first_copy_addr, b"\xAA" * copy_len)
helper.mu.mem_write(helper.second_copy_addr, b"\x00" * copy_len)
helper.mu.emu_start(helper.shellcode_address, helper.shellcode_address + len(shellcode))
assert helper.mu.mem_read(helper.second_copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.second_copy_addr, copy_len) == (b"\xAA" * copy_len)
assert helper.mu.mem_read(helper.second_copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('shellcode_run_addr', [
(0x83000010),
(0xbc000010),
(0xbcf00010),
(0x91000118),
])
def test_memcpy_is_pic(temp_dir_path, compiler_arch, shellcode_run_addr, default_memcpy_helper):
copy_len = 0x1000
helper = default_memcpy_helper
shellcode = memcpy_get_shellcode(temp_dir_path, compiler_arch, helper, copy_len)
shellcode_run_sector = int(shellcode_run_addr/SECTOR_SIZE) * SECTOR_SIZE
helper.mu.mem_map(shellcode_run_sector, SECTOR_SIZE)
# Try to run shellcode
# --------------------
# write machine code to be emulated to memory
helper.mu.mem_write(shellcode_run_addr, shellcode)
helper.mu.mem_write(helper.first_copy_addr, b"\xAA" * copy_len)
helper.mu.mem_write(helper.second_copy_addr, b"\x00" * copy_len)
helper.mu.emu_start(shellcode_run_addr, shellcode_run_addr + len(shellcode))
assert helper.mu.mem_read(helper.second_copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.second_copy_addr, copy_len) == (b"\xAA" * copy_len)
assert helper.mu.mem_read(helper.second_copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('copy_len', [
100,
200,
0x600,
2,
3,
100
])
def test_memcpy_short(temp_dir_path, compiler_arch, default_memcpy_helper, copy_len):
helper = default_memcpy_helper
shellcode = memcpy_get_shellcode(temp_dir_path, compiler_arch, helper, copy_len)
# Try to run shellcode
# --------------------
# write machine code to be emulated to memory
helper.mu.mem_write(helper.shellcode_address, shellcode)
helper.mu.mem_write(helper.first_copy_addr, b"\xAA" * 2 * copy_len)
helper.mu.mem_write(helper.second_copy_addr, b"\x00" * 2 * copy_len)
helper.mu.emu_start(helper.shellcode_address, helper.shellcode_address + len(shellcode))
assert helper.mu.mem_read(helper.second_copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.second_copy_addr, copy_len) == (b"\xAA" * copy_len)
assert helper.mu.mem_read(helper.second_copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('copy_len', [
100,
200,
0x600,
2,
4,
100
])
def test_memcpy_two_halves(temp_dir_path, compiler_arch, default_memcpy_helper, copy_len):
half_copy_len = int(copy_len/2)
helper = default_memcpy_helper
shellcode = memcpy_get_shellcode(temp_dir_path, compiler_arch, helper, copy_len)
# Try to run shellcode
# --------------------
# write machine code to be emulated to memory
helper.mu.mem_write(helper.shellcode_address, shellcode)
helper.mu.mem_write(helper.first_copy_addr, b"\xAA" * half_copy_len + b"\xBB" * half_copy_len)
helper.mu.mem_write(helper.second_copy_addr, b"\x00" * 2 * copy_len)
helper.mu.emu_start(helper.shellcode_address, helper.shellcode_address + len(shellcode))
assert helper.mu.mem_read(helper.second_copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.second_copy_addr, copy_len) == (b"\xAA" * half_copy_len + b"\xBB" * half_copy_len)
assert helper.mu.mem_read(helper.second_copy_addr + copy_len, 1) == b"\x00"