-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
53 lines (40 loc) · 1.87 KB
/
runner.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
import ctypes
import struct
import sys
from lib.core import Util
"""
runner.py - shellcode runner
Takes a NASM assembly file containing some shellcode, turns it into
opcodes using Keystone Engine, and runs it using VirtualAlloc, RtlMoveMemory,
and CreateThread from the Win32 API via ctypes.
Will dump the resulting opcodes to the console, highlighting NULL bytes.
Usage:
python runner.py shellcode/filename.nasm
"""
if __name__ == '__main__':
SHELLCODE_FILE = sys.argv[1];
SHELLCODE = Util.read_nasm(SHELLCODE_FILE)
encoding, count = Util.read_bytes(SHELLCODE)
print(f"[INFO] Encoded {count} instructions ({len(encoding)} bytes)")
Util.dump_nasm_bytes(encoding)
sh = b""
for e in encoding:
sh += struct.pack("B", e)
shellcode = bytearray(sh)
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),
ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr),
buf,
ctypes.c_int(len(shellcode)))
print(f"\n[+] Shellcode located at address {hex(ptr)}")
input("...PRESS ENTER TO EXECUTE SHELLCODE...")
ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0)))
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht), ctypes.c_int(-1))