Process Injection Techniques
Red Team Tooling Internals · Advanced Exploitation track
5 min read
In this module
Running Code Where You Should Not
Process injection is the technique of executing code inside another process's address space. Malware uses it to shelter inside legitimate processes; red teams use it to evade detection; debuggers and security tools use it legitimately. Whatever the purpose, the OS primitives are the same — and understanding them at the API level is what lets you build detection that actually sticks.
CreateRemoteThread Injection
The most straightforward injection technique uses Windows APIs to allocate memory in a target process, write code into it, and create a thread to execute it:
// Classic CreateRemoteThread injection (conceptual)// 1. Open the target processHANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID);
// 2. Allocate executable memory in the targetLPVOID pRemote = VirtualAllocEx(hProcess, NULL, shellcodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// 3. Write shellcode into the allocated memoryWriteProcessMemory(hProcess, pRemote, shellcode, shellcodeSize, NULL);
// 4. Create a thread in the target process that executes the shellcodeCreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pRemote, NULL, 0, NULL);This sequence — OpenProcess, VirtualAllocEx, WriteProcessMemory, CreateRemoteThread — is one of the most-watched call chains in endpoint detection. Every mature EDR hooks all four and treats the combination as probable cause.
APC Injection
Asynchronous Procedure Call (APC) injection avoids creating a new remote thread. Instead, it queues a callback to an existing thread:
// 1. Allocate and write shellcode (same as above)// 2. Queue an APC to a thread in the target processQueueUserAPC((PAPCFUNC)pRemote, hThread, 0);// The APC executes when the target thread enters an alertable wait stateNo new thread is created, so CreateRemoteThread detection doesn't fire. The catch: the target thread must enter an alertable wait state — SleepEx, WaitForSingleObjectEx, or similar — for the APC to be dispatched. If no thread obliges, the payload just sits there.
Early Bird injection is an APC variant that queues the APC to a newly created process's main thread before it starts running. The thread doesn't need to enter an alertable wait. When it's resumed, it starts in the loader (LdrInitializeThunk), which calls NtTestAlert once the process's DLLs are initialised, and that drains the thread's APC queue before the program's entry point ever runs. The injected code therefore fires very early in the process's lifetime, which can be before an EDR's user-mode hooks are fully in place. The cross-process APC queue to a freshly created, suspended process is itself a well-known signal now, and many EDRs watch for it.
Process Hollowing
Process hollowing is a more sophisticated technique that replaces a process's entire executable image:
- Create a legitimate process (e.g., svchost.exe) in a SUSPENDED state
- Use NtUnmapViewOfSection to unmap the legitimate executable's image
- Allocate memory in the target (at the payload's preferred image base if possible) and write the malicious PE: its headers first, then each section at its virtual address, applying relocations if it didn't land at its preferred base
- Update the image base in the target's PEB (
PEB->ImageBaseAddress) so the loader and APIs like GetModuleHandle(NULL) find the new image - Point the thread at the new entry point: with GetThreadContext and SetThreadContext, change the register the suspended thread will start from (RCX on x64)
- Resume the main thread
The result: task manager shows svchost.exe, but the code actually running is your payload. Nothing on disk reveals this — the on-disk binary is untouched.
Detection means comparing the in-memory image against the on-disk executable. Any divergence is a tell.
Thread Hijacking
Thread hijacking takes over an existing thread without creating new threads or queuing APCs:
- Suspend a thread in the target process with SuspendThread
- Get the thread's context (register values) with GetThreadContext
- Modify the instruction pointer (RIP) to point to injected code
- Set the thread's context with SetThreadContext
- Resume the thread with ResumeThread
The hijacked thread now executes the attacker's code. After the injected code finishes, it restores the original context and returns to the hijacked thread's original execution point.
Module Stomping
Module stomping loads a legitimate DLL into the target process, then overwrites its code section with malicious code:
- Use CreateRemoteThread + LoadLibrary to load a benign DLL into the target
- Overwrite the loaded DLL's .text section with malicious code
- Execute the malicious code by calling a function in the "loaded DLL"
The advantage: the malicious code appears to be part of a legitimately loaded DLL. Memory scanners that check whether code belongs to a mapped file see a valid file-backed mapping. The code's address falls within the range of a known, signed DLL.
Detection Principles
Each injection technique has detectable artifacts:
- Cross-process memory operations: Any combination of OpenProcess + VirtualAllocEx + WriteProcessMemory is suspicious
- Private executable memory: Code that is not file-backed (anonymous RWX pages) indicates injection
- Image discrepancies: Differences between the on-disk file and the in-memory image indicate hollowing or stomping
- Thread creation anomalies: Threads with start addresses pointing to unusual locations
- Call stack anomalies: Threads whose call stacks show unexpected transitions from legitimate code to unbacked memory
Line the four main techniques up against what they leave behind and you can see why defenders scan memory as well as watching API calls. Each technique dodges some artefact, but none of them leaves zero.
ETW and kernel callbacks provide the telemetry for all of the above. The thing to hold onto: injection must use OS primitives. There's a finite list, every one of them is monitorable, and attackers can't escape that constraint.
As EDR detection improves, injection techniques become more creative. Researchers develop new techniques like phantom DLL hollowing, transacted hollowing, and mapping injection. Each technique avoids specific detection heuristics. The response is always the same: understand the new technique, identify its invariant artifacts, and build detection for those artifacts.