test/stomper.cc.
What is Module Stomping?
Module stomping is a shellcode execution technique that:- Loads a legitimate DLL without resolving imports
- Finds the executable
.textsection - Changes memory protection to writable
- Overwrites the section with shellcode
- Restores memory protection
- Executes the shellcode from the legitimate module’s memory
Benefits
Memory Protection Evasion
- Shellcode executes from a legitimate module’s memory space
- Module appears legitimate to memory scanners
- Backed by a file on disk (appears less suspicious)
Execution Flow
- No RWX memory allocation needed
- Uses existing executable memory
- Execution starts from a legitimate module’s address space
Implementation Walkthrough
Let’s examine howtest/stomper.cc implements module stomping.
1. Load Target DLL
Load the target DLL withDONT_RESOLVE_DLL_REFERENCES flag:
test/stomper.cc
DONT_RESOLVE_DLL_REFERENCES flag:
- Maps the DLL into memory
- Does NOT resolve imports
- Does NOT call DllMain
- Prevents dependency loading
2. Find the .text Section
Locate the executable code section:test/stomper.cc
- Gets the NT headers from the DOS header
- Gets the first section header
- Iterates through sections looking for
.text
3. Calculate Addresses
Calculate the entry point and section address:test/stomper.cc
entry: DLL entry point (where execution will start)image_base: Address of the.textsectionsec_header->SizeOfRawData: Size of the section
4. Change Memory Protection
Make the section writable:test/stomper.cc
- Changes protection to
PAGE_READWRITE - Stores old protection in
protectionvariable - Covers the entire
.textsection
5. Copy Shellcode
Overwrite the section with shellcode:test/stomper.cc
6. Restore Protection
Restore original memory protection:test/stomper.cc
PAGE_EXECUTE_READ).
7. Execute Shellcode
Execute the shellcode:test/stomper.cc
entry function pointer now points to your shellcode!
Using the Stomper
Build the Stomper
Compile both x64 and x86 versions:test/stomper.x64.exe- 64-bit loadertest/stomper.x86.exe- 32-bit loader
Run Your Shellcode
Example Output
Choosing Target DLLs
Good Candidates
Look for DLLs with:- Large .text sections: More space for shellcode
- Common presence: Less suspicious
- Minimal dependencies: Faster loading with
DONT_RESOLVE_DLL_REFERENCES
Avoid
- System-critical DLLs (ntdll.dll, kernel32.dll)
- DLLs with extensive dependencies
- DLLs with strong code signing requirements
Advanced Techniques
Dynamic Target Selection
Choose target DLL at runtime:Section Size Validation
Ensure the section is large enough:Finding Alternative Sections
Look for other executable sections:Clean Memory
Zero out remaining section space:File Reading
The stomper includes a helper function to read shellcode:test/stomper.cc
Detection Considerations
EDR/AV May Detect
- Suspicious API sequence: LoadLibraryEx → VirtualProtect → memcpy → Execute
- Memory modifications: Changes to legitimate DLL code
- Execution from modified memory: Code doesn’t match disk image
- Behavioral patterns: Entry point execution without DllMain
Evasion Techniques
- Add delays: Between stomping stages
- Randomize targets: Choose different DLLs each run
- Indirect calls: Use function pointers
- Clean up: Restore original code after execution
Troubleshooting
LoadLibraryEx Fails
- DLL not found in system directories
- Architecture mismatch (x64 vs x86)
- DLL requires dependencies
VirtualProtect Fails
- Insufficient permissions
- Protected process
- Code integrity enforcement
Shellcode Crashes
- Check architecture matches (x64 stomper with x64 shellcode)
- Verify shellcode is position-independent
- Enable debug build:
make debug - Check section is large enough
Section Not Found
If.text section isn’t found:
- Target DLL may use different section name
- Look for any executable section
- Check section characteristics
Security Implications
Module stomping is commonly used in:- Red team operations
- Malware execution
- AV/EDR evasion
