-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-bytes
More file actions
executable file
·37 lines (32 loc) · 3.62 KB
/
run-bytes
File metadata and controls
executable file
·37 lines (32 loc) · 3.62 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
#!/bin/python3
ORIGINAL_PROGRAM = """
SECTION .text
GLOBAL _start
_start:
TIMES 10000 nop
mov rax,60 ; 'exit' system call = 60
mov rdi,0 ; exit with error code 0
syscall
"""
COMPILED_BYTES1 = b"\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00>\x00\x01\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\xf87\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x008\x00\x02\x00@\x00\x05\x00\x04\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\xb0\x00\x00\x00\x00\x00\x00\x00\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x1c'\x00\x00\x00\x00\x00\x00\x1c'\x00\x00\x00\x00\x00\x00\x00\x10"
COMPILED_BYTES3 = b'\xb8<\x00\x00\x00\xbf\x00\x00\x00\x00\x0f\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\xf1\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x10\x00\x01\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x10\x00\x01\x00\x00@@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00\x00\x10\x00\x01\x00\x00@@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1d\x00\x00\x00\x10\x00\x01\x00\x00@@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00nop1.asm\x00__bss_start\x00_edata\x00_end\x00\x00.symtab\x00.strtab\x00.shstrtab\x00.text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x1c\'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 7\x00\x00\x00\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x02\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb07\x00\x00\x00\x00\x00\x00"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd27\x00\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
COMPILED_BYTES = COMPILED_BYTES1 + b'\x00'*(4096-len(COMPILED_BYTES1)) + b'\x90'*10000 + COMPILED_BYTES3
import subprocess
import sys
def assembler(machine_code: bytes) -> bytes:
insert_point = 4100
return COMPILED_BYTES[:insert_point] + machine_code + COMPILED_BYTES[insert_point+len(machine_code):]
def run_code(elf_binary: bytes):
path="/tmp/dangerous_exe"
run = subprocess.call
with open(path, "wb") as f:
f.write(elf_binary)
run(["chmod", "+x", path])
return run([path])
# Example usage: program that exits with code 42 (aka 0x2A)
# echo "\xB8\x3C\x00\x00\x00\xBF\x2A\x00\x00\x00\x0F\x05" | python run_bytes.py ; echo $?
# Note: limited to 10KB of machine code
if __name__ == '__main__':
code = sys.stdin.buffer.read()
res = run_code(assembler(code))
sys.exit(res)