-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memset.py
More file actions
195 lines (151 loc) · 5.49 KB
/
test_memset.py
File metadata and controls
195 lines (151 loc) · 5.49 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import pytest
import os
from shellblocks.shellcode_step import ShellcodeStep
from shellblocks.primitives.memset import ShellcodePrimitiveMemset
SECTOR_SIZE = 0x2000
class UcMemsetHelper:
def __init__(self,
get_mu,
arch_helper,
shellcode_address,
copy_addr,
stack_address):
self.shellcode_address = shellcode_address
self.arch_helper = arch_helper
self.copy_addr = copy_addr
self.copy_sector = int(self.copy_addr/SECTOR_SIZE) * SECTOR_SIZE
self.mu = get_mu()
self.mu.mem_map(self.shellcode_address, SECTOR_SIZE)
self.mu.mem_map(self.copy_sector, SECTOR_SIZE)
self.mu.mem_map(stack_address, 0x2000)
self.arch_helper.set_curr_sp(self.mu, stack_address + 0x2000)
def write_shellcode(self, shellcode):
self.mu.mem_write(self.shellcode_address, shellcode)
@pytest.fixture()
def stack_address():
return 0x80001000
@pytest.fixture(scope='function')
def default_memset_helper(get_mu, arch_helper, stack_address):
return UcMemsetHelper(
get_mu,
arch_helper,
0xbfc00000,
0x82000010,
stack_address
)
def memset_get_shellcode(temp_dir_path, compiler_arch, memset_helper, copy_bytes):
helper = memset_helper
step = ShellcodeStep(
"first_step",
[
ShellcodePrimitiveMemset(
"copy_next_stage",
helper.copy_addr,
copy_bytes
),
],
0x1000 * len(copy_bytes),
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,
2,
3,
8,
16,
32,
])
def test_memset_sanity(temp_dir_path, compiler_arch, default_memset_helper, copy_len):
helper = default_memset_helper
to_write = b"\xAA" * copy_len
shellcode = memset_get_shellcode(temp_dir_path, compiler_arch, helper, to_write)
# 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.copy_addr, b"\x00" * copy_len)
helper.mu.emu_start(helper.shellcode_address, helper.shellcode_address + len(shellcode))
assert helper.mu.mem_read(helper.copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.copy_addr, copy_len) == to_write
assert helper.mu.mem_read(helper.copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('shellcode_run_addr', [
(0x83000010),
(0xbc000010),
(0xbcf00010),
(0x91000118),
])
def test_memset_is_pic(temp_dir_path, compiler_arch, shellcode_run_addr, default_memset_helper):
copy_len = 200
helper = default_memset_helper
to_write = b"\xAA" * copy_len
shellcode = memset_get_shellcode(temp_dir_path, compiler_arch, helper, to_write)
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.copy_addr, b"\x00" * copy_len)
helper.mu.emu_start(shellcode_run_addr, shellcode_run_addr + len(shellcode))
assert helper.mu.mem_read(helper.copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.copy_addr, copy_len) == (b"\xAA" * copy_len)
assert helper.mu.mem_read(helper.copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('copy_len', [
100,
200,
2,
4,
8,
12,
16,
20,
24,
28,
32,
100
])
def test_memset_two_halves(temp_dir_path, compiler_arch, default_memset_helper, copy_len):
half_copy_len = int(copy_len/2)
helper = default_memset_helper
to_write = b"\xAA" * half_copy_len + b"\xBB" * half_copy_len
shellcode = memset_get_shellcode(temp_dir_path, compiler_arch, helper, to_write)
# 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.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.copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.copy_addr, copy_len) == to_write
assert helper.mu.mem_read(helper.copy_addr + copy_len, 1) == b"\x00"
@pytest.mark.parametrize('copy_len', [
100,
200,
2,
4,
8,
12,
16,
20,
24,
28,
32,
100
])
def test_memset_random(temp_dir_path, compiler_arch, default_memset_helper, copy_len):
helper = default_memset_helper
to_write = os.urandom(copy_len)
shellcode = memset_get_shellcode(temp_dir_path, compiler_arch, helper, to_write)
# 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.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.copy_addr - 1, 1) == b"\x00"
assert helper.mu.mem_read(helper.copy_addr, copy_len) == to_write
assert helper.mu.mem_read(helper.copy_addr + copy_len, 1) == b"\x00"