DLL Injection
Apr 21, 2023
Oh hello fellow malware developers :D. Yes, I am back and if you have been taking a break from malware development (like me), GET BACK HERE and I promise you won't regret it.
What is a DLL
A DLL is a Dynamic Linked Library is Microsoft's implementation of the shared library concept. Basically, a DLL contains reusable code that is used by many applications.
So you might guess that we will inject our own DLL into a process, and that's exactly what we will do!
We will achieve that using the LoadLibraryA();
function that loads a module into the address space of the calling process.
LoadLibraryA();
will not execute a DLL if it's already loaded into a process which means that we won't be able to rerun our exploit.
Let's make a DLL
Since we now know what a DLL is, why not make one? In the end, it's a required thing to have when performing a DLL Injection right?
To make a DLL we will use the DllMain
as the DLL's entry point which is similar to main
of C files.
We can see the fwdReason
parameter that indicates why the DLL entry-point function is being called. This parameter can be one of the following values.
DLL_PROCESS_ATTACH
1
The DLL is being loaded into the current process as a result of the process starting up or as a result of a call to LoadLibrary.
DLL_PROCESS_DETACH
0
The DLL is being unloaded from the calling process because it was loaded unsuccessfully or the processes has either terminated or called FreeLibrary.
DLL_THREAD_ATTACH
2
The current process is creating a new thread. When this occurs, the entry-point function of all DLLs currently attached to the process is called
DLL_THREAD_DETACH 3
A thread is exiting cleanly.
In our case, we only need the DLL_PROCESS_ATTACH case. The code we will write for that specific case will be executed the moment our DLL gets loaded into a process.
Here is the DLL that we will use
Try playing yourself with the available cases. For example, leave a final message using the DLL_PROCESS_DETACH case.
Compiling
Let's compile our DLL using gcc
And congrats! You have your own DLL ๐
The injection
Win32 API Calls
As we can see from the above diagram here are the calls we will do to the Win32 API
Documentation for the following API calls can be found by clicking the functions header.
We can see that the DLL Injection is pretty similar to the shellcode injection we covered in the past article. Let's start by implementing a simple CLI (Command Like Interface)
Now we will attempt to get a handle on the process provided by the user.
According to the documentation If we are successful we should get our handle, otherwise, the hProcess
variable will be set to NULL
.
From now on we will work inside the (hProcess != NULL)
block.
As we did with the shellcode injection we need to allocate some memory within the process.
We will now write the DLL path to the allocated memory
Now in order to load our DLL we need to get the address of the LoadLibraryA function which is located inside the Kernel32 module (a module used by the Windows kernel!). To achieve this we will make use of the GetModuleHandle function as well as the GetProcAddress function. Basically, we get a handle on the module that contains the function we want and request its address of it using GetProcAddress.
Great! Now the only thing that we have to do is to start the remote thread and close the handle to the process.
And that's pretty much it.
Here is a little PoC I put together using verbose logging. I also played a bit with the DLL and also added a message box on DLL_PROCESS_DETACH.
Congratulations! You just performed your first DLL Injection? Isn't that amazing?
My buddy @crow recently reached 10K subs on YouTube.
Please make sure to check out his channel.
P.S Crow if you are watching this, give me Headmaster
:D
Last updated