DLL Injection
Overview
DLL injectionis a method that involves inserting a piece of code, structured as a Dynamic Link Library (DLL), into a running process. It allows the inserted code to run within the process's context, to influence its behavior or access its resources.
DLL injection has legitimate use cases, such has hot patching.
In this section we detail the different DLL injection methods from a theoretical standpoint.
LoadLibrary
The
LoadLibraryAPI is a function provided by the Windows operating system that loads a DLLs into the current process’s memory and returns a handle that can be used to get the addresses of functions within the DLL.
Using this API, we can legitimately load a DLL into the current process:
#include <windows.h>
#include <stdio.h>
int main() {
// Using LoadLibrary to load a DLL into the current process
HMODULE hModule = LoadLibrary("example.dll");
if (hModule == NULL) {
printf("Failed to load example.dll\n");
return -1;
}
printf("Successfully loaded example.dll\n");
return 0;
}We can also use this API for DLL injection into a target proces:
Allocate memory within the target process for the DLL path
Initiate a remote thread that begins at
LoadLibraryand directs towards the DLL path
Manual Mapping
This method is much more complex but provides more stealth because LoadLibrary isn't used.
Simplified overview:
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
It leverages reflective programming, read more about it here => https://github.com/stephenfewer/ReflectiveDLLInjection
DLL Hijacking
We can hijack DLL when their loaded at runtime if an application doesn't specify the full path to a required DLL
Safe DLL Search Mode
The default DLL search order used by the system depends on whether Safe DLL Search Mode is activated.
If actived (default) the system will look for DLLs in this order:
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.
If it's deactivated:
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
We can see "current directory" is pushed up the list.
We can easily deactive the safe search from the registry:
Press
Windows key + Rto open the Run dialog box.Type in
Regeditand pressEnter. This will open the Registry Editor.Navigate to
HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager.In the right pane, look for the
SafeDllSearchModevalue. If it does not exist, right-click the blank space of the folder or right-click theSession Managerfolder, selectNewand thenDWORD (32-bit) Value. Name this new value asSafeDllSearchMode.Double-click
SafeDllSearchMode. In the Value data field, enter1to enable and0to disable Safe DLL Search Mode.Click
OK, close the Registry Editor and Reboot the system for the changes to take effect.
Attack Example
Locate vulnerable DLL
We can use tools like Process Explorer (sysinternal suite) or PE explorer. These tools allow us to see the DLL loaded for each process.
Reverse engineer the program
We need to decompile and debug a target program to decide which function imported from the DLL we will target.
Once we found the function to target we have 2 options:
DLL Proxying
We replace the original DLL by a malicious one
The malicious DLL will load the function from the original library
It will then tamper with it (this is where we put our payload)
Finally it returns the tampered result to the program
Invalid DLL
Replace a valid library the program is attempting to load but cannot find with a crafted library. We will need write access to a location the program attempts to load from.
Each programming language has a specific library function that allows to execute some instructions when the library is loaded. We can put a malicious payload there.
Last updated