Reflective DLL Injection — DLLs That Load Themselves

Origin and Concept

Reflective DLL Injection was published by Stephen Fewer in 2008 and represents one of the most elegant code injection techniques: a DLL capable of mapping itself into memory without the help of the Windows loader — without LoadLibrary, without disk dependency, without records in the host process PEB.

The fundamental premise is that the Windows DLL loading mechanism (ntdll!LdrLoadDll, invoked by LoadLibrary) performs predictable and well-documented operations. A minimal DLL loader can be implemented inside the DLL itself, executed when it is injected as a raw buffer into the target process's memory.

This self-loader (called ReflectiveLoader) is the DLL's exported function that implements:

  1. Locating its own base in memory

  2. Parsing its own PE header

  3. Mapping its own sections

  4. Resolving imports (IAT)

  5. Applying relocations

  6. Executing DllMain

┌──────────────────────────────────────────────────────────────────────┐
│               Comparison: LoadLibrary vs. Reflective Loading         │
│                                                                      │
│  LoadLibrary (traditional):                                          │
│    Disk → ntdll loader → Mapping → Import resolution                 │
│    ↳ File on disk required                                           │
│    ↳ DLL registered in PEB (InLoadOrderModuleList)                  │
│    ↳ Detectable by module enumeration                                │
│                                                                      │
│  Reflective Loading:                                                 │
│    Raw buffer in memory → ReflectiveLoader (inside DLL)              │
│       → Self mapping → Import resolution → DllMain                  │
│    ↳ No file on disk                                                 │
│    ↳ DLL NOT registered in PEB                                       │
│    ↳ Invisible to EnumProcessModules and GetModuleHandle             │
└──────────────────────────────────────────────────────────────────────┘

Reflective DLL Structure

A reflective DLL has the following structure:


Implementing the ReflectiveLoader

The heart of the technique. The loader must function without depending on any absolute addresses — it is position-independent code (PIC).

Step 1: Locate Its Own Base

The loader does not know at which address it was injected. It must find the beginning of the PE header by walking memory backwards from its own position:

Step 2: Resolve kernel32 Addresses Without an Import Table

Since the DLL was manually loaded (without the Windows loader), the Import Address Table (IAT) has not yet been populated. The loader needs to resolve required functions by manually walking PEB structures:

Step 3: Resolve GetProcAddress and LoadLibraryA by Hash

To reduce detectable strings and simplify the code, the ReflectiveLoader resolves functions by name hash:

Step 4: Allocate Memory and Map Sections


The Injector Side: Injecting the Reflective DLL

The process that injects the reflective DLL into the victim does not need to do anything sophisticated:


Practical Applications

Widely-used offensive frameworks implement Reflective DLL Injection as their primary mechanism:

  • Cobalt Strike: The Beacon is a reflective DLL. The stager injects the raw beacon into memory.

  • Metasploit: meterpreter/reverse_tcp uses reflective loading.

  • Havoc C2: DLL-based payloads using reflective loading.

  • Sliver C2: Windows implants implement reflective loading.


Detection


References

  • Stephen Fewer, "Reflective DLL Injection" — harmonysecurity.com (2008)

  • github.com/stephenfewer/ReflectiveDLLInjection — original implementation

  • ired.team, "Reflective DLL Injection" — ired.team/offensive-security/code-injection-process-injection/

  • Jared Atkinson, "Understanding and Detecting Reflective Code Loading" — SpecterOps (2021)

  • MITRE ATT&CK, "T1055.001 — Dynamic-link Library Injection" — attack.mitre.org

  • Elastic Security, "Hunting for Reflective DLL Injection" — elastic.co/security-labs (2022)

  • Craig Rowland, "Detecting Reflective DLL Injection" — sandflysecurity.com

  • Kyle Hanslovan, "Detecting Reflective Injection" — huntress.com (2020)

Last updated