1. The Anatomy of a Stack-Based Buffer Overflow
Standard C and C++ programs do not perform automatic memory management or bounds checking. Consider a vulnerable function allocating a 64-byte local buffer on the stack:
void vulnerable_function(char *user_input) {
char buffer[64];
strcpy(buffer, user_input); // Fatal flaw: no length verification!
}When strcpy() copies an input exceeding 64 bytes, it continuously writes upwards through the stack frame, overwriting the Saved Frame Pointer (Saved EBP) and eventually overwriting the Saved Return Address (Saved EIP):
Normal Stack Frame:
[ Local Buffer: 64 bytes ] | [ Saved EBP: 4 bytes ] | [ Saved EIP: 4 bytes ]
|
v (Normal Return Address)
Corrupted Stack Frame during Exploit:
[ 'A' * 64 bytes buffer ] | [ 'A' * 4 bytes EBP ] | [ 'B' * 4 bytes EIP (0x42424242) ]
|
v
Crash! CPU attempts to jump to 0x42424242!2. Precise Offset Calculation: The De Bruijn Pattern
Instead of guessing how many padding bytes are required to reach the Saved EIP register, exploit developers utilize Cyclic De Bruijn Sequences: mathematically generated alphanumeric strings where every 4-byte substring is completely unique (e.g., Aa0Aa1Aa2Aa3...):
Instead of guessing where the Instruction Pointer (EIP) sits in memory, we fill the buffer with an unrepeating 4-character mathematical pattern. When the application crashes, the 4 bytes inside EIP tell us the exact byte offset required to hijack code execution.
OffsetExact EIP DistanceByte count required to fill the buffer before reaching the stored return address
EIPInstruction Pointer Register32-bit CPU register holding the next instruction address to execute
S_(DeBruijn)Cyclic PatternUnique mathematical sequence where every 4-byte substring appears exactly once
When the program crashes in a debugger (like Immunity Debugger or x64dbg), EIP contains a distinct 4-byte value (e.g., 0x39694138, which decodes in Little-Endian to "8Ai9"). We query the exact index of this sequence to determine the precise buffer length down to the single byte!
3. Isolating Bad Characters in Exploit Payloads
Because string copy operations treat certain byte values as delimiters or terminators, any byte that truncates or alters the payload is a Bad Character:
Common Shellcode Bad Characters:
| Byte Value | Canonical Name | Functional Conflict | Required Remediation |
|---|---|---|---|
\x00 | Null Byte | String terminator in C (strcpy halts) | Universal exclusion across all exploits |
\x0a | Line Feed (\n) | Stream end delimiter (fgets halts) | Exclude in network / terminal daemons |
\x0d | Carriage Return (\r) | Terminal delimiter (HTTP / FTP buffers) | Exclude in web / text protocol exploits |
\x20 | Space Character | Parameter separator in CLI tools | Exclude in shell argument injectors |
Python Bad Character Array Generator:
#!/usr/bin/env python3
"""
Bad Character Array Generator for Exploit Developers
"""
# Generate array of all 256 byte values from 0x01 to 0xFF (excluding null byte 0x00)
badchars = (
b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
b"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
b"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
b"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
b"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
b"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
b"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
b"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80"
b"\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
b"\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0"
b"\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0"
b"\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0"
b"\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0"
b"\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0"
b"\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0"
b"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
)
print(f"[*] Generated {len(badchars)} test bytes for memory comparison.")