November 17, 2026
Your Heap Is Betraying You
Welcome to this new Medium post. In this one we will see why heap memory matters for implant development, what kind of artifacts it leavesβ¦

By S12 - 0x12Dark Development
5 min read
Welcome to this new Medium post. In this one we will see why heap memory matters for implant development, what kind of artifacts it leaves behind, and how that affects detection.
Want to go deeper into Windows offensive development?
Video-based courses from beginner to advanced, and text-based modules (mini courses) with new releases constantly. Plus a technique database with 100+ real techniques updated weekly, and custom C2 agents and consulting for teams.
0x12 Dark Development Skip to content Join our offensive development courses and modules, and explore the techniques database with 100+ realβ¦
Introduction
Most implant developers focus on what their code does: syscalls, injection paths, evasion techniques. The heap rarely gets attention until something goes wrong
THis post covers what the heap holds and why that matters.
What ends up in the heap
Before listing what lands there, it helps to know which heap we're talking about, every Windows process starts with a default process heap, accessible via GetProcessHeap(). On top of that, the CRT creates its own private heap to service malloc/new calls, so your implant is likely using at least two heaps without you explicitly managing either
The rule is simple: anything your implant allocates dynamically at runtime ends up there. In practice, that means more than you might expect
The obvious cases are explicit calls: malloc, new, HeapAlloc, RtlAllocateHeap.
But in C++ you rarely write those directly. The heap fills up through the standard library instead:
std::stringwith more than 15 characters allocates its buffer on the heap (most implementations use Small String Optimization, for short strings, keeping them on the stack, but anything longer spills to the heap)std::vector,std::map,std::unordered_mapall their internal buffers are allocated in the heapstd::unique_ptrandstd::shared_ptrstore the managed object on the heap
So if your implant builds a C2 URL as a std::string, stores received commands in a std::vector<Task>, or keeps a config struct in a std::unique_ptr<Config>, all of that is on the heap without you ever writing a single malloc call
C2 Agents: Cobalt Strike as a case study
Cobalt Strike's beacon is a good reference point because its heap behavior has been reversed and the detection tooling built around it is public
The beacon ships with its configuration embedded inside the binary as a packed TLV blob: type, length, value for each setting, XOR'd with a single byte key. One of the first things CobaltStrike does after starting is loading its embedded config and set it into heap memory. The packed config is then overwritten with zeroes
The runtime configuration is a C/C++ array found on the heap, created by the beacon code by parsing the embedded configuration, the memory for the binary data is dynamically allocated using malloc
So in memory, at runtime, you have:
- The runtime config array:
malloc'd on the heap, one entry per config item - Binary values (like the RSA public key): separately
malloc, with pointers inside that array - A pointer to the array stored in
.data
BeaconEye only scans heap memory in search of Cobalt Strike configuration structures which are dynamically allocated at initialization. The allocation block size is 2048 bytes, a known signature that makes finding the config block straightforward when walking heap entries.
Cobalt Strike addressed this by adding a new idea: encrypt the heap before Sleep(), decrypt it after waking. Using HeapWalk(), you can iterate over every allocation in the heap and encrypt it, suspending all threads before encrypting and resuming them post encryption
HeapExplorer
https://github.com/S12cybersecurity/HeapExplorer
For this purpose I just used AI to create a quick tool, is a realtime heap analysis tool built specifically for the kind of research this post is about.
Features
- Browse all active heap blocks in a target process: address, size, and content preview
- See every allocation and free in realtime as they happen
- Spot encrypted or compressed buffers by entropy score
- Extract strings directly from heap memory: C2 domains, pipe names, paths (std::string)
- Compare two snapshots to see exactly what changed between them
- Flag executable heap regions and allocations with no backing module on disk
- Attach to a running process or launch one directly from the tool
Proof of Concept
Let's start analyzing this code:
#include <windows.h>
#include <wincrypt.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#pragma comment(lib, "advapi32.lib")
// Magic tags, first 4 bytes of each block, visible in HeapExplorer's hex preview
#define TAG_CONFIG 0x47464342 // "BCFG"
#define TAG_TEMPBUF 0x44504D54 // "TMPD"
struct BeaconConfig {
uint32_t magic;
uint8_t aes_key[32];
uint8_t c2_encrypted[64];
uint32_t sleep_ms;
uint32_t jitter;
uint32_t version;
};
static void XorBuffer(uint8_t* buf, size_t len, uint8_t key) {
for (size_t i = 0; i < len; i++) buf[i] ^= key;
}
static void FillHighEntropy(uint8_t* buf, size_t len) {
HCRYPTPROV hProv = 0;
if (CryptAcquireContextA(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
CryptGenRandom(hProv, (DWORD)len, buf);
CryptReleaseContext(hProv, 0);
}
}
int main() {
HANDLE hSecureHeap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
if (!hSecureHeap) {
std::cerr << "HeapCreate failed\n";
return 1;
}
BeaconConfig* config = (BeaconConfig*)HeapAlloc(hSecureHeap, HEAP_ZERO_MEMORY, sizeof(BeaconConfig));
if (!config) {
HeapDestroy(hSecureHeap);
return 1;
}
config->magic = TAG_CONFIG;
config->sleep_ms = 15000;
config->jitter = 500;
config->version = 1;
FillHighEntropy(config->aes_key, sizeof(config->aes_key));
FillHighEntropy(config->c2_encrypted, sizeof(config->c2_encrypted));
// These exceed SSO threshold β heap-allocated in the default process heap
std::string c2_domain = "192.168.1.100:4444";
std::string user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)";
std::string pipe_name = "\\\\.\\pipe\\svchost_comms";
std::cout << "Beacon initialized. C2: " << c2_domain << "\n";
std::cout << "Take initial snapshot in HeapExplorer now...\n";
Sleep(3000);
for (int i = 0; i < 5; i++) {
std::cout << "Cycle " << i + 1 << " β rotating AES key...\n";
// Short-lived decryption buffer β TAG_TEMPBUF in first 4 bytes
size_t alloc_size = sizeof(uint32_t) + sizeof(config->c2_encrypted);
uint8_t* temp_buf = (uint8_t*)HeapAlloc(hSecureHeap, 0, alloc_size);
if (temp_buf) {
*(uint32_t*)temp_buf = TAG_TEMPBUF;
uint8_t* payload = temp_buf + sizeof(uint32_t);
memcpy(payload, config->c2_encrypted, sizeof(config->c2_encrypted));
XorBuffer(payload, sizeof(config->c2_encrypted), 0xAB);
HeapFree(hSecureHeap, 0, temp_buf);
}
// Rotate key: same block address, different content β Snapshot Diff shows Modified
FillHighEntropy(config->aes_key, sizeof(config->aes_key));
// C2 response: vector buffer on the default process heap, high entropy
std::vector<uint8_t> c2_response(256);
FillHighEntropy(c2_response.data(), c2_response.size());
std::cout << "C2 response received (" << c2_response.size() << " bytes)\n";
std::cout << "Sleep " << config->sleep_ms << "ms...\n";
Sleep(config->sleep_ms);
}
SecureZeroMemory(config, sizeof(BeaconConfig));
HeapFree(hSecureHeap, 0, config);
HeapDestroy(hSecureHeap);
std::cout << "Beacon done. Custom heap destroyed.\n";
return 0;
}#include <windows.h>
#include <wincrypt.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#pragma comment(lib, "advapi32.lib")
// Magic tags, first 4 bytes of each block, visible in HeapExplorer's hex preview
#define TAG_CONFIG 0x47464342 // "BCFG"
#define TAG_TEMPBUF 0x44504D54 // "TMPD"
struct BeaconConfig {
uint32_t magic;
uint8_t aes_key[32];
uint8_t c2_encrypted[64];
uint32_t sleep_ms;
uint32_t jitter;
uint32_t version;
};
static void XorBuffer(uint8_t* buf, size_t len, uint8_t key) {
for (size_t i = 0; i < len; i++) buf[i] ^= key;
}
static void FillHighEntropy(uint8_t* buf, size_t len) {
HCRYPTPROV hProv = 0;
if (CryptAcquireContextA(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
CryptGenRandom(hProv, (DWORD)len, buf);
CryptReleaseContext(hProv, 0);
}
}
int main() {
HANDLE hSecureHeap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
if (!hSecureHeap) {
std::cerr << "HeapCreate failed\n";
return 1;
}
BeaconConfig* config = (BeaconConfig*)HeapAlloc(hSecureHeap, HEAP_ZERO_MEMORY, sizeof(BeaconConfig));
if (!config) {
HeapDestroy(hSecureHeap);
return 1;
}
config->magic = TAG_CONFIG;
config->sleep_ms = 15000;
config->jitter = 500;
config->version = 1;
FillHighEntropy(config->aes_key, sizeof(config->aes_key));
FillHighEntropy(config->c2_encrypted, sizeof(config->c2_encrypted));
// These exceed SSO threshold β heap-allocated in the default process heap
std::string c2_domain = "192.168.1.100:4444";
std::string user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)";
std::string pipe_name = "\\\\.\\pipe\\svchost_comms";
std::cout << "Beacon initialized. C2: " << c2_domain << "\n";
std::cout << "Take initial snapshot in HeapExplorer now...\n";
Sleep(3000);
for (int i = 0; i < 5; i++) {
std::cout << "Cycle " << i + 1 << " β rotating AES key...\n";
// Short-lived decryption buffer β TAG_TEMPBUF in first 4 bytes
size_t alloc_size = sizeof(uint32_t) + sizeof(config->c2_encrypted);
uint8_t* temp_buf = (uint8_t*)HeapAlloc(hSecureHeap, 0, alloc_size);
if (temp_buf) {
*(uint32_t*)temp_buf = TAG_TEMPBUF;
uint8_t* payload = temp_buf + sizeof(uint32_t);
memcpy(payload, config->c2_encrypted, sizeof(config->c2_encrypted));
XorBuffer(payload, sizeof(config->c2_encrypted), 0xAB);
HeapFree(hSecureHeap, 0, temp_buf);
}
// Rotate key: same block address, different content β Snapshot Diff shows Modified
FillHighEntropy(config->aes_key, sizeof(config->aes_key));
// C2 response: vector buffer on the default process heap, high entropy
std::vector<uint8_t> c2_response(256);
FillHighEntropy(c2_response.data(), c2_response.size());
std::cout << "C2 response received (" << c2_response.size() << " bytes)\n";
std::cout << "Sleep " << config->sleep_ms << "ms...\n";
Sleep(config->sleep_ms);
}
SecureZeroMemory(config, sizeof(BeaconConfig));
HeapFree(hSecureHeap, 0, config);
HeapDestroy(hSecureHeap);
std::cout << "Beacon done. Custom heap destroyed.\n";
return 0;
}The first thing we found in the HeapExplorer are the IOC Strings:
Then we search for the other two allocated memory buffers for configuration, the first one, represents the BeaconConfig:
struct BeaconConfig {
uint32_t magic;
uint8_t aes_key[32];
uint8_t c2_encrypted[64];
uint32_t sleep_ms;
uint32_t jitter;
uint32_t version;
};struct BeaconConfig {
uint32_t magic;
uint8_t aes_key[32];
uint8_t c2_encrypted[64];
uint32_t sleep_ms;
uint32_t jitter;
uint32_t version;
};
The BCFG prefix represents our configuration buffer. This snapshot is just in the initialize phase, after the first key rotation:
If you compare the bytes (4β35) just changed, the key rotation operation.
In this case the data is encrypted so there is no way (without reversing) to read the plain text data.
Conclusions
The heap holds more than you put there explicitly. Standard library types, runtime config structs, and temporary decryption buffers all leave artifacts that persist until explicitly freed, and in some cases longer.
π Follow me: π¦ X | π¬ Discord Server | πΈ Instagram | YouTube
S12.