Reflective DLL Injection
Loading DLL from memory
Reflective DLL injection is a technique that allows an attacker to inject a DLL's into a victim process from memory rather than disk.
Purpose
The purpose of this lab is to:
Test reflective DLL injection capability in metasploit
Goof around with basic memory forensics
Implement a simple reflective DLL injection POC by myself
Technique Overview
The way the reflective injection works is nicely described by the technique's original author Stephen Fewer here:
Execution is passed, either via CreateRemoteThread() or a tiny bootstrap shellcode, to the library's ReflectiveLoader function which is an exported function found in the library's export table.
As the library's image will currently exists in an arbitrary location in memory the ReflectiveLoader will first calculate its own image's current location in memory so as to be able to parse its own headers for use later on.
The ReflectiveLoader will then parse the host processes kernel32.dll export table in order to calculate the addresses of three functions required by the loader, namely LoadLibraryA, GetProcAddress and VirtualAlloc.
The ReflectiveLoader will now allocate a continuous region of memory into which it will proceed to load its own image. The location is not important as the loader will correctly relocate the image later on.
The library's headers and sections are loaded into their new locations in memory.
The ReflectiveLoader will then process the newly loaded copy of its image's import table, loading any additional library's and resolving their respective imported function addresses.
The ReflectiveLoader will then process the newly loaded copy of its image's relocation table.
The ReflectiveLoader will then call its newly loaded image's entry point function, DllMain with DLL_PROCESS_ATTACH. The library has now been successfully loaded into memory.
Finally the ReflectiveLoader will return execution to the initial bootstrap shellcode which called it, or if it was called via CreateRemoteThread, the thread will terminate.
Execution
This lab assumes that the attacker has already gained a meterpreter shell from the victim system and will now attempt to perform a reflective DLL injection into a remote process on a compromised victim system, more specifically into a notepad.exe
process with PID 6156
Metasploit's post-exploitation module windows/manage/reflective_dll_inject
configured:
Reflective_dll.x64.dll
is the DLL compiled from Steven Fewer's reflective dll injection project on github.
After executing the post exploitation module, the below graphic shows how the notepad.exe executes the malicious payload that came from a reflective DLL that was sent over the wire from the attacker's system:
Observations
Once the metasploit's post-exploitation module is run, the procmon accurately registers that notepad created a new thread:
Let's see if we can locate where the contents of reflective_dll.x64.dll
are injected into the victim process when the metasploit's post-exploitation module executes.
For that, lets debug notepad in WinDBG and set up a breakpoint for MessageBoxA
as shown below and run the post-exploitation module again:
The breakpoint is hit:
At this point, we can inspect the stack with kv
and see the call trace. A couple of points to note here:
return address the code will jump to after the
USER32!MessageBoxA
finishes is00000000031e103e
inspecting assembly instructions around
00000000031e103e
, we see a call instructioncall qword ptr [00000000031e9208]
inspecting bytes stored in
00000000031e9208
, (dd 00000000031e9208 L1
) we can see they look like a memory address0000000077331304
(note this address)inspecting the EIP pointer (
r eip
) where the code execution is paused at the moment, we see that it is the same0000000077331304
address, which means that the earlier mentioned instructioncall qword ptr [00000000031e9208]
is the actual call toUSER32!MessageBoxA
This means that prior to the above mentioned instruction, there must be references to the variables that are passed to the
MessageBoxA
function:
If we inspect the 00000000031e103e
0x30 bytes earlier, we can see some suspect memory addresses and the call instruction almost immediatley after that:
Upon inspecting those two addresses - they are indeed holding the values the MessageBoxA
prints out upon successful DLL injection into the victim process:
Looking at the output of the !address
function and correlating it with the addresses the variables are stored at, it can be derived that the memory region allocated for the evil dll is located in the range 031e0000 - 031f7000
:
Indeed, if we look at the 031e0000
, we can see the executable header (MZ) and the strings fed into the MessageBoxA
API can be also found further into the binary:
Detecting Reflective DLL Injection with Volatility
Malfind
is the Volatility's pluging responsible for finding various types of code injection and reflective DLL injection can usually be detected with the help of this plugin.
The plugin, at a high level will scan through various memory regions described by Virtual Address Descriptors (VADs) and look for any regions with PAGE_EXECUTE_READWRITE
memory protection and then check for the magic bytes 4d5a
(MZ in ASCII) at the very beginning of those regions as those bytes signify the start of a Windows executable (i.e exe, dll):
Note how in our case, volatility discovered the reflective dll injection we inspected manually above with WindDBG:
Implementing Reflective DLL Injection
I wanted to program a simplified Reflective DLL Injection POC to make sure I understood its internals, so this is my attempt and its high level workflow of how I've implemented it:
Read raw DLL bytes into a memory buffer
Parse DLL headers and get the SizeOfImage
Allocate new memory space for the DLL of size
SizeOfImage
Copy over DLL headers and PE sections to the memory space allocated in step 3
Perform image base relocations
Load DLL imported libraries
Resolve Import Address Table (IAT)
Invoke the DLL with
DLL_PROCESS_ATTACH
reason
Steps 1-4 are pretty straight-forward as seen from the code below. For step 5 related to image base relocations, see my notes T1093: Process Hollowing and Portable Executable Relocations
Resolving Import Address Table
Portable Executables (PE) use Import Address Table (IAT) to lookup function names and their memory addresses when they need to be called during runtime.
When dealing with reflective DLLs, we need to load all the dependent libraries of the DLL into the current process and fix up the IAT to make sure that the functions that the DLL imports point to correct function addresses in the current process memory space.
In order to load the depending libraries, we need to parse the DLL headers and:
Get a pointer to the first Import Descriptor
From the descriptor, get a pointer to the imported library name
Load the library into the current process with
LoadLibrary
Repeat process until all Import Descriptos have been walked through and all depending libraries loaded
Before proceeding, note that my test DLL I will be using for this POC is just a simple MessageBox that gets called once the DLL is loaded into the process:
Below shows the first Import Descriptor of my test DLL. The first descriptor suggests that the DLL imports User32.dll and its function MessageBoxA. On the left, we can see a correctly resolved library name that is about to be loaded into the memory process with LoadLibrary
:
Below shows that the user32.dll gets loaded successfully:
After the Import Descriptor is read and its corresponding library is loaded, we need to loop through all the thunks (data structures describing functions the library imports), resolve their addresses using GetProcAddress
and put them into the IAT so that the DLL can reference them when needed:
Once we have looped through all the Import Decriptors and their thunks, the IAT is considered resolved and we can now execute the DLL. Below shows a successfully loaded and executed DLL that pops a message box:
Code
References
Last updated