DLL Injectionç
DLL injection is a method that involves inserting a piece of code, structured as a Dynamic Link Library (DLL), into a running process.
There are several different methods for actually executing a DLL injection.
LoadLibrary
The LoadLibrary
API is a function provided by the Windows operating system that loads a Dynamic Link Library (DLL) into the current process’s memory and returns a handle that can be used to get the addresses of functions within the DLL.
This example shows how LoadLibrary
can be used to load a DLL into the current process legitimately:
This other example illustrates the use of LoadLibrary for DLL injection. This process involves allocating memory within the target process for the DLL path and then initiating a remote thread that begins at LoadLibrary and directs towards the DLL path:
LoadLibraryA
is a Windows API function from kernel32.dll that loads a DLL (Dynamic Link Library) into the memory of a process. It takes a path to a.dll
file as a string argument and returns a handle to the loaded module. The"A"
stands for ANSI version — there's alsoLoadLibraryW
for wide-character (Unicode) strings. It comes fromkernel32.dll
, which is a core Windows system library that provides essential functions like memory management, threading, file I/O, and — in this case — DLL loading.
Manual Mapping
Manual Mapping
is an incredibly complex and advanced method of DLL injection. It involves the manual loading of a DLL into a process's memory and resolves its imports and relocations. However, it avoids easy detection by not using the LoadLibrary
function, whose usage is monitored by security and anti-cheat systems.
A simplified outline of the process can be represented as follows:
- Load the DLL as raw data into the injecting process.
- Map the DLL sections into the targeted process.
- Inject shellcode into the target process and execute it. This shellcode relocates the DLL, rectifies the imports, executes the Thread Local Storage (TLS) callbacks, and finally calls the DLL main.
Reflective DLL Injection
Reflective DLL injection is a technique that utilizes reflective programming to load a library from memory into a host process. The library itself is responsible for its loading process by implementing a minimal Portable Executable (PE) file loader. This allows it to decide how it will load and interact with the host, minimising interaction with the host system and process.
Source: https://github.com/stephenfewer/ReflectiveDLLInjection
The procedure of remotely injecting a library into a process is two-fold. First, the library you aim to inject must be written into the target process’s address space (hereafter referred to as the 'host process'). Second, the library must be loaded into the host process to meet the library's runtime expectations, such as resolving its imports or relocating it to an appropriate location in memory.
Assuming we have code execution in the host process and the library we aim to inject has been written into an arbitrary memory location in the host process, Reflective DLL Injection functions as follows.
- Execution control is transferred to the library's
ReflectiveLoader
function, an exported function found in the library's export table. This can happen either viaCreateRemoteThread()
or a minimal bootstrap shellcode. - As the library's image currently resides in an arbitrary memory location, the
ReflectiveLoader
initially calculates its own image's current memory location to parse its own headers for later use. - The
ReflectiveLoader
then parses the host process'skernel32.dll
export table to calculate the addresses of three functions needed by the loader, namelyLoadLibraryA
,GetProcAddress
, andVirtualAlloc
. - The
ReflectiveLoader
now allocates a continuous memory region where it will proceed to load its own image. The location isn't crucial; the loader will correctly relocate the image later. - The library's headers and sections are loaded into their new memory locations.
- The
ReflectiveLoader
then processes the newly loaded copy of its image's import table, loading any additional libraries and resolving their respective imported function addresses. - The
ReflectiveLoader
then processes the newly loaded copy of its image's relocation table. - The
ReflectiveLoader
then calls its newly loaded image's entry point function,DllMain,
withDLL_PROCESS_ATTACH
. The library has now been successfully loaded into memory. - Finally, the
ReflectiveLoader
returns execution to the initial bootstrap shellcode that called it, or if it were called viaCreateRemoteThread
, the thread would terminate.
DLL Hijacking
DLL Hijacking
is an exploitation technique where an attacker capitalizes on the Windows DLL loading process. These DLLs can be loaded during runtime, creating a hijacking opportunity if an application doesn't specify the full path to a required DLL, hence rendering it susceptible to such attacks.
The default DLL search order used by the system depends on whether Safe DLL Search Mode
is activated.
When enabled (which is the default setting), Safe DLL Search Mode repositions the user's current directory further down in the search order. It’s easy to either enable or disable the setting by editing the registry.
- Press
Windows key + R
to open the Run dialog box. - Type in
Regedit
and pressEnter
. This will open the Registry Editor. - Navigate to
HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager
. - In the right pane, look for the
SafeDllSearchMode
value. If it does not exist, right-click the blank space of the folder or right-click theSession Manager
folder, selectNew
and thenDWORD (32-bit) Value
. Name this new value asSafeDllSearchMode
. - Double-click
SafeDllSearchMode
. In the Value data field, enter1
to enable and0
to disable Safe DLL Search Mode. - Click
OK
, close the Registry Editor and Reboot the system for the changes to take effect.
With this mode enabled, applications search for necessary DLL files in the following sequence:
- The directory from which the application is loaded.
- The system directory.
- The 16-bit system directory.
- The Windows directory.
- The current directory.
- The directories that are listed in the PATH environment variable.
However, if 'Safe DLL Search Mode' is deactivated, the search order changes to:
- The directory from which the application is loaded.
- The current directory.
- The system directory.
- The 16-bit system directory.
- The Windows directory
- The directories that are listed in the PATH environment variable
DLL Hijacking involves a few more steps. First, you need to pinpoint a DLL the target is attempting to locate. Specific tools can simplify this task:
Process Explorer
: Part of Microsoft's Sysinternals suite, this tool offers detailed information on running processes, including their loaded DLLs. By selecting a process and inspecting its properties, you can view its DLLs.PE Explorer
: This Portable Executable (PE) Explorer can open and examine a PE file (such as a .exe or .dll). Among other features, it reveals the DLLs from which the file imports functionality.
After identifying a DLL, the next step is determining which functions you want to modify, which necessitates reverse engineering tools, such as disassemblers and debuggers.
Another way would be....
DLL path with status NAME NOT FOUND
Let's say there is a binary main.exe
running as SYSTEM admin, and we set the procmon filter to focus on entries whose path ends in .dll and has a status of NAME NOT FOUND.
If the dll with NAME NOT found is located at a folder where we have write permissions, then we can load our malicious dll within that path.
Last update: 2025-04-13 Created: April 13, 2025 19:25:48