DLL Injection
Apr 21, 2023
Last updated
Apr 21, 2023
Last updated
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.
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.
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
DLL_PROCESS_DETACH
0
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
Let's compile our DLL using gcc
As we can see from the above diagram here are the calls we will do to the Win32 API
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.
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
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?
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 .
The DLL is being unloaded from the calling process because it was loaded unsuccessfully or the processes has either terminated or called .
And congrats! You have your own DLL
According to the If we are successful we should get our handle, otherwise, the hProcess
variable will be set to NULL
.
Now in order to load our DLL we need to get the address of the function which is located inside the module (a module used by the Windows kernel!). To achieve this we will make use of the function as well as the function. Basically, we get a handle on the module that contains the function we want and request its address of it using .
My buddy 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