← All posts

Detecting code injection in Windows memory: malfind, hollowing and hooks

7 min read

Injected code has one structural weakness: it has to live somewhere. The loader never mapped it from a file, so the kernel's bookkeeping for the process (the VAD tree, the loader lists, the thread entry points) disagrees with what the process looks like from user mode. Every technique below exploits one of those disagreements.

This post walks through the injection and process-tampering checks ramparser runs on a Windows image with kernel symbols, using the Volatility plugin names from The Art of Memory Forensics: what the attacker does, which structure gives it away, what a hit looks like, and where the false positives come from.

malfind: private executable memory

Every committed range of a process's address space is described by a VAD (_MMVAD) node. The node records the protection given at allocation time and whether a file (DLL, EXE, mapped section) backs the range. Legitimate code comes from image mappings. Shellcode and reflectively loaded DLLs come from VirtualAlloc, so they sit in private memory.

ramparser's malfind walks each process's VAD tree and keeps a region when:

  • it is private and not file-backed, and
  • its protection is executable and writable (EXECUTE_READWRITE, EXECUTE_WRITECOPY), or it is executable-only but one of its pages is marked dirty in the page table, meaning code was written there after allocation;
  • and at least one page is non-zero. Reserved or fully paged-out regions are dropped.

Each hit carries the range, the protection and an mz flag set when the region starts with MZ. The findings list rates an MZ region high ("PE injection") and an MZ-less one medium ("suspicious RWX"). An MZ header in private memory is a manually mapped PE; the header-less case is typically shellcode or a PE whose header was wiped.

False positives: .NET, browsers and anything else with a JIT compiler allocate private RWX memory routinely. A malfind hit in chrome.exe means little on its own; the same hit in svchost.exe with an MZ header means a lot.

Hollowed processes

Process hollowing starts a legitimate executable suspended, unmaps its image, writes a payload in its place and resumes the thread. The process keeps its name, path and parent. hollowprocesses checks three things the swap usually breaks:

  1. PEB.ImageBaseAddress (user-writable) differs from _EPROCESS.SectionBaseAddress (recorded by the kernel when the image was mapped).
  2. No VAD starts at the executable's base: the original image was unmapped.
  3. The VAD at the executable's base does not have EXECUTE_WRITECOPY, the protection of a normal image mapping.

Each symptom is a separate row (PEB-mismatch, exe-vad-missing, exe-protection) so you can see which ones agree.

Process ghosting

Ghosting creates the image section from a file that is already marked for deletion, so the file is gone before the process starts. processghosting flags live processes whose _EPROCESS.ImageFilePointer is null, or points to a _FILE_OBJECT with DeletePending set. System, Idle and minimal processes without a PEB (Registry, MemCompression) are skipped, since they never had an image file. One limitation: the DeletePending bit is read at a fixed Windows 10 x64 offset rather than from the PDB.

Threads that start in the wrong place

Injection needs a thread to run the payload (CreateRemoteThread, NtCreateThreadEx, APC queueing). suspicious_threads walks each process's thread list and resolves every user-mode _ETHREAD.Win32StartAddress against the VAD tree. A thread whose start lands in a VAD with no backing file is reported with the reason "start in non-file-backed region". The mapped file name is resolved, so pagefile-backed sections count as non-file-backed. Starts outside every VAD are ignored as bad reads. The findings view groups hits per process at medium severity. The same JIT caveat as malfind applies.

ldrmodules: DLLs unlinked from the PEB

The loader tracks each DLL in three _PEB_LDR_DATA lists: InLoadOrder, InMemoryOrder and InInitializationOrder. Tools that call the Windows API (Toolhelp, EnumProcessModules) read these lists, so malware hides a DLL by unlinking it. The mapping itself stays in the VAD tree.

For each process, ramparser takes the union of the three lists and of the image-mapped VADs that start with an MZ header, then reports every base that is not present everywhere, with in_load / in_mem / in_init / in_vad flags. A DLL mapped in the VAD tree but absent from all three lists is the classic case. The process detail pane lists these under "ldrmodules unlinked", and each one is a high finding.

Expect a few benign gaps: the main executable is never in the initialization-order list.

impscan: what the injected code calls

A manually mapped payload has no import table the loader filled in: its own stub resolved API addresses. impscan rebuilds those imports for every region malfind flagged. It maps every export of the DLLs mapped in the process (address to dll!function), then:

  • reads the region as a table of aligned pointers (8-byte, and 4-byte for WOW64) and keeps every value that is an export address, and
  • decodes call/jmp through [rip+disp32] (x64) or [abs32] (WOW64) and follows the slot they reference.

The findings view turns the rebuilt imports into capabilities. For example, VirtualAllocEx + WriteProcessMemory + CreateRemoteThread becomes "can inject into other processes"; SetWindowsHookEx or GetAsyncKeyState becomes "can log keystrokes". That turns an anonymous RWX blob into a statement about what it does.

Hooks and EDR evasion in ntdll

PluginWhat it looks atA hit means
apihooksEvery module's IAT, resolved against the imported DLL's imageAn import resolves outside the DLL it names (forwarded exports excluded)
iatpatchThe same, restricted to imports from ntdll.dllA patched entry on the path to the kernel
unhooked_system_callsThe first 0x20 bytes of a fixed set of Nt* stubs in every process's ntdllThis process's stub differs from the majority
direct_system_calls0F 05 (syscall) byte pairs in loaded modules other than ntdll / win32uA module that makes its own system calls
etwpatchThe prologue of EtwEventWrite, EtwEventWriteEx, EtwEventWriteFull, EtwNotificationRegisterret, xor eax,eax; ret, mov eax,0; ret or a jmp at entry

unhooked_system_calls needs no reference image. When an EDR hooks ntdll in every process, the hooked stub is the majority. A process that restored clean bytes, or installed its own hook, is the minority, and its differing bytes are shown for review.

Two limits. direct_system_calls counts raw byte pairs over the first 4 MB of each module in the PEB loader list, including non-code data, so a low count needs disassembly at the reported address before it means anything. etwpatch only recognizes the known patch signatures above; a novel patch with a different first instruction is not flagged.

YARA inside process memory

vadyarascan scans each process's VAD regions (up to 4 MB per region) with a small built-in catalog of commodity strings (Cobalt Strike, Metasploit, Mimikatz, Quasar, AsyncRAT, Remcos, Conti, LockBit) and attributes each hit to the process. It is string matching, not full YARA, and a lone family-name match is rated low because it also hits unrelated text.

For real rules, the YARA view runs your own rules (YARA-X, in the browser) over physical memory and over each process's VAD regions, with a per-region size cap and hits attributed to the owning process. By default only regions with no file behind them are scanned, which is where injected code lives; mapped images and files can be included.

Reading the results together

No single check here is a verdict. The strong cases are where independent structures agree: a malfind MZ region, a thread starting inside it, rebuilt imports that spell out injection, and a YARA hit on the same range. One process, four structures, one story. For each suspect PID, pivot to its command line, handles and network connections before concluding.

Further reading