Repository navigation
Expand file tree
/
Copy pathtarget.py
More file actions
113 lines (100 loc) · 5.64 KB
/
Copy pathtarget.py
File metadata and controls
113 lines (100 loc) · 5.64 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""A harmless test subject: a process that does one injection-shaped thing *to itself*, prints "ready"
and sleeps until it is killed. Planted code is not executed: the planted thread stays suspended, the
hooked function is never called, the stomped DLL is mapped without running. The two exceptions are
"sleeper" and "masked": a 32-byte stub whose only instruction of substance is a call to SleepEx(INFINITE),
run from a normal Python thread, so a return address into private memory sits on that thread's stack.
"masked" then XORs the stub and makes it non-executable, as sleep-masking implants do while they wait.
python target.py clean | thread | pe | hook | iat | stomp | rwx | sleeper | masked
"""
import ctypes
import os
import sys
import threading
import time
from ctypes import wintypes as W
sys.path.insert(0, os.path.dirname(__file__))
from pebuild import make_pe # noqa: E402
k32 = ctypes.WinDLL("kernel32", use_last_error=True)
k32.VirtualAlloc.restype = ctypes.c_void_p
k32.VirtualAlloc.argtypes = [ctypes.c_void_p, ctypes.c_size_t, W.DWORD, W.DWORD]
k32.VirtualProtect.argtypes = [ctypes.c_void_p, ctypes.c_size_t, W.DWORD, ctypes.POINTER(W.DWORD)]
k32.CreateThread.restype = W.HANDLE
k32.CreateThread.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p, W.DWORD, ctypes.POINTER(W.DWORD)]
k32.GetModuleHandleW.restype = W.HMODULE
k32.GetProcAddress.restype = ctypes.c_void_p
k32.GetProcAddress.argtypes = [W.HMODULE, ctypes.c_char_p]
k32.LoadLibraryExW.restype = W.HMODULE
k32.LoadLibraryExW.argtypes = [W.LPCWSTR, W.HANDLE, W.DWORD]
MEM = 0x3000 # MEM_COMMIT | MEM_RESERVE
RX, RW, RWX = 0x20, 0x04, 0x40
CREATE_SUSPENDED = 0x4
DONT_RESOLVE_DLL_REFERENCES = 0x1
def alloc(data: bytes, final_protect: int) -> int:
addr = k32.VirtualAlloc(None, max(len(data), 0x1000), MEM, RW)
ctypes.memmove(addr, data, len(data))
old = W.DWORD()
k32.VirtualProtect(addr, max(len(data), 0x1000), final_protect, ctypes.byref(old))
return addr
def patch(addr: int, data: bytes) -> None:
old = W.DWORD()
k32.VirtualProtect(addr, len(data), RWX, ctypes.byref(old))
ctypes.memmove(addr, data, len(data))
k32.VirtualProtect(addr, len(data), old.value, ctypes.byref(old))
def main(mode: str) -> None:
info = {}
if mode == "thread":
code = alloc(b"\xc3" * 64, RX) # ret; the thread never even starts
tid = W.DWORD()
k32.CreateThread(None, 0, code, None, CREATE_SUSPENDED, ctypes.byref(tid))
info = {"addr": code, "tid": tid.value}
elif mode == "pe":
info = {"addr": alloc(make_pe(b"\xc3" * 32, exports={"Run": 0x1000}), RX)}
elif mode == "rwx":
info = {"addr": alloc(b"\x90" * 32 + b"\xc3", RWX)}
elif mode == "hook":
target = alloc(b"\xc3" * 16, RX) # where the hook leads: private memory
beep = k32.GetProcAddress(k32.GetModuleHandleW("kernel32.dll"), b"Beep")
patch(beep, b"\xff\x25\x00\x00\x00\x00" + target.to_bytes(8, "little")) # jmp [rip+0]; never called
info = {"addr": beep, "target": target}
elif mode == "iat":
target = alloc(b"\xc3" * 16, RX)
exe =k32.GetModuleHandleW(None) # python.exe's own import table
lfanew = ctypes.c_uint32.from_address(exe + 0x3C).value
iat_rva, iat_size = ctypes.c_uint32.from_address(exe + lfanew + 24 + 112 + 12 * 8).value, ctypes.c_uint32.from_address(exe + lfanew + 24 + 112 + 12 * 8 + 4).value
slot = exe + iat_rva + iat_size - 16 # the last import, which this process has already used
while not ctypes.c_uint64.from_address(slot).value:
slot -= 8
patch(slot, target.to_bytes(8, "little"))
info = {"addr": slot, "target": target}
elif mode == "stomp":
dll = os.path.join(os.environ["SystemRoot"], "System32", "dbghelp.dll")
base = k32.LoadLibraryExW(dll, None, DONT_RESOLVE_DLL_REFERENCES) # mapped, DllMain not run
patch(base + 0x1000, b"\xcc" * 0x3000) # overwrite 12 KB of its code with int3
info = {"addr": base + 0x1000}
elif mode in ("sleeper", "masked"):
sleep_ex = k32.GetProcAddress(k32.GetModuleHandleW("kernel32.dll"), b"SleepEx")
stub = (b"\x48\x83\xec\x28" # sub rsp, 0x28
b"\xb9\xff\xff\xff\xff" # mov ecx, INFINITE
b"\x31\xd2" # xor edx, edx
b"\x48\xb8" + sleep_ex.to_bytes(8, "little") + # mov rax, SleepEx
b"\xff\xd0" # call rax: the return address below lands on the stack
b"\x48\x83\xc4\x28\xc3") # add rsp, 0x28; ret (never reached)
if mode == "sleeper":
addr = alloc(stub, RX)
else:
addr = k32.VirtualAlloc(None, 0x1000, MEM, RWX)
ctypes.memmove(addr, stub, len(stub))
threading.Thread(target=ctypes.CFUNCTYPE(None)(addr), daemon=True).start()
time.sleep(0.5) # the thread is now inside SleepEx
if mode == "masked":
old = W.DWORD()
k32.VirtualProtect(addr, 0x1000, RW, ctypes.byref(old))
ctypes.memmove(addr, bytes(b ^ 0x5A for b in stub), len(stub)) # "encrypted" while it sleeps
info = {"addr": addr, "ret": addr + 23}
elif mode != "clean":
raise SystemExit(f"unknown mode {mode}")
print("ready", " ".join(f"{k}={v}" for k, v in info.items()), flush=True)
while True:
time.sleep(1)
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "clean")