memory namespace provides essential memory operations optimized for shellcode environments. These functions avoid external dependencies and implement core memory operations inline for maximum portability.
memory::zero()
Securely zeros a memory region using Windows’RtlSecureZeroMemory to prevent compiler optimizations from removing the zeroing operation.
Pointer to the memory region to zero. The buffer will be modified in place.
Number of bytes to zero, starting from the
memory address.No return value. The function modifies memory directly.
Purpose
Unlike standardmemset, RtlSecureZeroMemory guarantees the zeroing operation will not be optimized away by the compiler, making it suitable for:
- Clearing sensitive data (keys, passwords, tokens)
- Sanitizing buffers before freeing
- Ensuring predictable memory state
Example Usage
Implementation
RtlSecureZeroMemory is available on all Windows versions and does not require explicit linking since it’s typically inlined or available via ntdll.dll.memory::copy()
Copies bytes from a source buffer to a destination buffer using a simple byte-by-byte loop.Pointer to the destination buffer where bytes will be copied. Must be at least
length bytes in size.Pointer to the source buffer to copy from. Must be at least
length bytes in size.Number of bytes to copy from source to destination.
Returns the
destination pointer, allowing for chained operations or assignment.Characteristics
- No External Dependencies: Implements copying via inline loop
- Forward Copy: Copies from index 0 to
length - 1 - No Overlap Safety: Does not handle overlapping source/destination (use
memmoveif overlap is possible) - Simple Implementation: Byte-by-byte copy, not optimized for large buffers
Example Usage
Implementation
memory::compare()
Compares two memory regions byte-by-byte and returns the difference of the first non-matching bytes.Pointer to the first memory region to compare.
Pointer to the second memory region to compare.
Number of bytes to compare between the two regions.
0if all bytes are equal- Positive value if the first differing byte in
memory1is greater - Negative value (when cast to signed) if the first differing byte in
memory1is less
Return Value Semantics
The function returns:compare("abc", "abc", 3) == 0→ Equalcompare("abd", "abc", 3) > 0→ First buffer is “greater” (d > c)compare("abc", "abd", 3) < 0→ First buffer is “less” (c < d)
Example Usage
Implementation
Algorithm Details
- Cast both pointers to
char*for byte-wise comparison - Loop through each byte:
- Compare current bytes
- If different, return the difference
- Advance pointers
- If loop completes, all bytes matched → return 0
Use Cases in Shellcode
Clearing Sensitive Data
Copying PE Headers
Validating Signatures
Duplicating Strings
Design Philosophy
Why Custom Implementations?
- No CRT Dependency: Standard C library functions (memset, memcpy, memcmp) require linking against the CRT, which increases shellcode size and complexity
- Position Independence: These implementations are fully inline and don’t rely on external symbols or import tables
- Size Optimization: Minimal implementation reduces compiled shellcode footprint
- Control: Direct control over memory operations without hidden behavior from library implementations
Inline Functions
All functions use theinline keyword to suggest inlining at call sites:
Performance Considerations
memory::zero()
- Delegates to
RtlSecureZeroMemory, which is optimized by Windows - May use vectorized instructions (SSE/AVX) internally on modern CPUs
- Guaranteed to not be optimized away by compiler
memory::copy()
- Simple byte-by-byte loop
- Not optimized for large buffers (no vectorization)
- Suitable for small data structures in shellcode
- For large copies, consider using platform-specific optimized routines
memory::compare()
- Early exit on first mismatch
- Best case: O(1) for immediate difference
- Worst case: O(n) for identical buffers
- No optimization for word/dword-aligned comparisons
These implementations prioritize simplicity and size over raw performance. For high-performance scenarios, consider using Windows API functions like
RtlCopyMemory or RtlCompareMemory.Safety Considerations
Safe Usage Checklist
- Verify buffer sizes before calling
- Ensure pointers are not null
- Check for potential buffer overruns
- For
copy(), validate non-overlapping regions - For
compare(), ensure both buffers are at leastlengthbytes
