-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellcode_step.py
More file actions
61 lines (48 loc) · 1.63 KB
/
shellcode_step.py
File metadata and controls
61 lines (48 loc) · 1.63 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
import shutil
from pathlib import Path
from shellblocks.compiler_arch import CompilerArch
from shellblocks.shellcode_primitive import ShellcodePrimitive
from shellblocks.utils import check_call_print
class ShellcodeStep:
def __init__(
self,
nickname: str,
primitives: [ShellcodePrimitive],
max_len: int,
base_address: int = 0
):
self.nickname = nickname
self.base_address = base_address
self.primitives = primitives
self.max_len = max_len
def generate(self, build_dir: Path, compiler: CompilerArch):
# Create build dir
try:
shutil.rmtree(build_dir.as_posix())
except FileNotFoundError:
pass
build_dir.mkdir(parents=True, exist_ok=True)
# Generate primitives elf files
out_files = []
for p in self.primitives:
p_build_dir = build_dir / p.nickname
p_build_dir.mkdir()
out_file = p.generate(p_build_dir, compiler)
out_files.append(out_file.as_posix())
# Join all primitives to final shellcode
check_call_print(
compiler.compile_step(out_files, self.base_address),
cwd=build_dir.as_posix()
)
check_call_print([
"objcopy",
"-O", "binary",
"-j", ".text",
"-j", ".rodata",
"shellcode.elf",
"final_shellcode.bin",
], cwd=build_dir.as_posix())
# Verify outfile is valid
outfile_path = build_dir / "final_shellcode.bin"
assert outfile_path.stat().st_size < self.max_len
return outfile_path