Why does an attacker need a stack overflow?
Buffer overflows aren't just a textbook abstraction. In MITRE ATT&CK, the technique falls under two tactics: Exploitation for Client Execution (T1203, Execution), where a vulnerability in an application allows arbitrary code execution, and Exploitation for Privilege Escalation (T1068), where a vulnerable binary runs with the SUID bit or as root. According to Mandiant M-Trends 2025, exploits are the most common initial access vector: 38% of incidents begin with vulnerability exploitation. For more information, see our review of binary vulnerability analysis .
The attack's business logic is ridiculously simple. The program stores user data (buffer contents) and control information (the function's return address) in a single contiguous block of memory—the stack. If you write more to the buffer than it can hold, the extra bytes will overwrite the return address. When the function executes ret, the processor will load the spoofed address into the EIP (on x86) or RIP (on x86-64) and continue execution from where the attacker pointed. On CTF, this scenario is reduced to a single task: overwriting the return address with the function address. win(), which causes system("/bin/sh")The pattern is called ret2win.
Stack frame: what lies between the buffer and the return address
To exploit buffer overflow, it is enough to understand three registers.
ESP / RSP (Stack Pointer) — the top of the stack. The stack on x86 grows downwards, from higher addresses to lower ones: each pushreduces ESP, each popincreases.
EBP / RBP (Base Pointer) — the start of the current stack frame. The compiler addresses local variables (negative offset) and function arguments (positive offset) relative to this point.
EIP / RIP (Instruction Pointer) is the address of the next instruction for the processor. It cannot be written to directly, but retpops the value from the top of the stack and loads it into the EIP/RIP. Control over the stack = control over program execution. That's all the magic.
What happens when a function is called
When the processor is executing call vuln, it pushes the address of the next one onto the stack callInstructions - Return Address. Next vulnsaves the current EBP ( push ebp), installs a new one ( mov ebp, esp) and shifts ESP down, allocating space for local variables ( sub esp, N).
For a function with char buf[64]The stack frame looks like this (from lowest to highest addresses):
Address (conditional) Content Size (x86)
ESP -> buf[0..63] — local buffer 64 bytes
... padding (compiler dependent) 0-12 bytes
EBP -> saved EBP — saved frame pointer of the calling function 4 bytes
EBP+4 return address — the return address (where ret will return) 4 bytes
Entry in bufgoes from lower addresses to higher ones - up the table. When gets()Reads the input without checking the length, the data sequentially overwrites the padding, saved EBP, and return address. This is where the attacker intercepts EIP.
x86-64 nuance for rewriting RIP
On 64-bit, the principle is the same, but there are two pitfalls. Addresses are 8 bytes, and the processor only accepts canonical ones: 0x0000000000000000to 0x00007FFFFFFFFFFFFor userland. Fill RIP with bytes. 0x41it won't work - 0x4141414141414141is not canonical, the processor will throw a general protection fault. Also, on x86-64, the stack must be aligned to 16 bytes before calling functions; misalignment is a common cause of segfaults on movapsin libc (and a frequent question on the forum). For the first pwn task, we work with 32-bit, which doesn't have these pitfalls.
We prepare the environment and build a vulnerable binary
Preconditions
Parameter Meaning
OS Linux (Ubuntu/Debian/Kali)
Binary architecture x86 (32-bit)
ASLR Disabled (randomize_va_space = 0)
Stack Canary Disabled (-fno-stack-protector)
NX (DEP) Disabled (-z execstack)
ABOUT Disabled (-no-pie)
Debugger GDB + pwndbg или GEF
Python pwntools
We install dependencies: sudo apt install gcc-multilib gdb And pip install pwntoolsFor pwndbg, clone the repository and run it. setup.sh, he will patch .gdbinitAfter this, GDB will automatically display context when a process stops: registers, stack, disassembled code—without any manual commands. Anyone who's used bare GDB will appreciate the difference.
Disable ASLR: echo 0 | sudo tee /proc/sys/kernel/randomize_va_spaceAfter practice, be sure to return back: echo 2 | sudo tee /proc/sys/kernel/randomize_va_space.
Minimal vulnerable binary in ret2win style:
#include <stdio.h>
#include <stdlib.h>
void win() { system("/bin/sh"); }
void vuln() {
char buf[64];
gets(buf); // читает без проверки длины — тут и живёт баг
}
int main() { vuln(); return 0; }
Function win()It is in the code, but is not called anywhere normally - the attacker's task is to "get to" it. gets()Reads input up to a newline character, ignoring the buffer size. GCC will give you a fair warning: the gets function is dangerousIt is precisely this danger that makes the task possible.
Compile: gcc -m32 -fno-stack-protector -z execstack -no-pie -o vuln vuln.cWe check the protections: checksec --file=vulnExpected result: Canary: No, NX: Disabled, PIE: No, RELRO: Partial. If any protection is enabled, the exploit will fail; recompile.
Finding offset using de Bruijn pattern in GDB
Offset is the number of bytes from the start of the buffer to the return address. Manually calculating "64 bytes of buffer + 4 bytes of saved EBP = 68" isn't always accurate: the compiler may add padding for alignment. Therefore, we determine the offset experimentally, using a de Bruijn pattern—a sequence in which each 4-byte substring is unique.
Step by step process
Run the binary in the debugger: gdb ./vulnWe generate a 200-byte pattern in pwndbg directly inside GDB: cyclic 200. Or in the terminal: python3 -c "from pwn import *; print(cyclic(200).decode())".
run, we insert the pattern. The program crashes with SIGSEGV—EIP attempted to jump to an address that is actually a fragment of our pattern. Pwndbg will highlight the EIP value—let's say, 0x6161616c.
We determine the position: cyclic -l 0x6161616cPwntools will return a number—that's the offset. Let's say 76: the first 76 bytes fill the buffer, padding, and saved EBP, and bytes 77–80 fall exactly at the return address.
Verification
Submitting a test payload: 76 bytes Aand four bytes B. B GDB: run < <(python3 -c "import sys; sys.stdout.buffer.write(b'A'*76 + b'BBBB')")If EIP has become 0x42424242 (ASCII B = 0x42) — the offset is correct. If not, we recalculate. The pattern must be at least 50 bytes longer than the buffer, otherwise the unique substring may not reach the return address.
Buffer overflows aren't just a textbook abstraction. In MITRE ATT&CK, the technique falls under two tactics: Exploitation for Client Execution (T1203, Execution), where a vulnerability in an application allows arbitrary code execution, and Exploitation for Privilege Escalation (T1068), where a vulnerable binary runs with the SUID bit or as root. According to Mandiant M-Trends 2025, exploits are the most common initial access vector: 38% of incidents begin with vulnerability exploitation. For more information, see our review of binary vulnerability analysis .
The attack's business logic is ridiculously simple. The program stores user data (buffer contents) and control information (the function's return address) in a single contiguous block of memory—the stack. If you write more to the buffer than it can hold, the extra bytes will overwrite the return address. When the function executes ret, the processor will load the spoofed address into the EIP (on x86) or RIP (on x86-64) and continue execution from where the attacker pointed. On CTF, this scenario is reduced to a single task: overwriting the return address with the function address. win(), which causes system("/bin/sh")The pattern is called ret2win.
Stack frame: what lies between the buffer and the return address
To exploit buffer overflow, it is enough to understand three registers.
ESP / RSP (Stack Pointer) — the top of the stack. The stack on x86 grows downwards, from higher addresses to lower ones: each pushreduces ESP, each popincreases.
EBP / RBP (Base Pointer) — the start of the current stack frame. The compiler addresses local variables (negative offset) and function arguments (positive offset) relative to this point.
EIP / RIP (Instruction Pointer) is the address of the next instruction for the processor. It cannot be written to directly, but retpops the value from the top of the stack and loads it into the EIP/RIP. Control over the stack = control over program execution. That's all the magic.
What happens when a function is called
When the processor is executing call vuln, it pushes the address of the next one onto the stack callInstructions - Return Address. Next vulnsaves the current EBP ( push ebp), installs a new one ( mov ebp, esp) and shifts ESP down, allocating space for local variables ( sub esp, N).
For a function with char buf[64]The stack frame looks like this (from lowest to highest addresses):
Address (conditional) Content Size (x86)
ESP -> buf[0..63] — local buffer 64 bytes
... padding (compiler dependent) 0-12 bytes
EBP -> saved EBP — saved frame pointer of the calling function 4 bytes
EBP+4 return address — the return address (where ret will return) 4 bytes
Entry in bufgoes from lower addresses to higher ones - up the table. When gets()Reads the input without checking the length, the data sequentially overwrites the padding, saved EBP, and return address. This is where the attacker intercepts EIP.
x86-64 nuance for rewriting RIP
On 64-bit, the principle is the same, but there are two pitfalls. Addresses are 8 bytes, and the processor only accepts canonical ones: 0x0000000000000000to 0x00007FFFFFFFFFFFFor userland. Fill RIP with bytes. 0x41it won't work - 0x4141414141414141is not canonical, the processor will throw a general protection fault. Also, on x86-64, the stack must be aligned to 16 bytes before calling functions; misalignment is a common cause of segfaults on movapsin libc (and a frequent question on the forum). For the first pwn task, we work with 32-bit, which doesn't have these pitfalls.
We prepare the environment and build a vulnerable binary
Preconditions
Parameter Meaning
OS Linux (Ubuntu/Debian/Kali)
Binary architecture x86 (32-bit)
ASLR Disabled (randomize_va_space = 0)
Stack Canary Disabled (-fno-stack-protector)
NX (DEP) Disabled (-z execstack)
ABOUT Disabled (-no-pie)
Debugger GDB + pwndbg или GEF
Python pwntools
We install dependencies: sudo apt install gcc-multilib gdb And pip install pwntoolsFor pwndbg, clone the repository and run it. setup.sh, he will patch .gdbinitAfter this, GDB will automatically display context when a process stops: registers, stack, disassembled code—without any manual commands. Anyone who's used bare GDB will appreciate the difference.
Disable ASLR: echo 0 | sudo tee /proc/sys/kernel/randomize_va_spaceAfter practice, be sure to return back: echo 2 | sudo tee /proc/sys/kernel/randomize_va_space.
Minimal vulnerable binary in ret2win style:
#include <stdio.h>
#include <stdlib.h>
void win() { system("/bin/sh"); }
void vuln() {
char buf[64];
gets(buf); // читает без проверки длины — тут и живёт баг
}
int main() { vuln(); return 0; }
Function win()It is in the code, but is not called anywhere normally - the attacker's task is to "get to" it. gets()Reads input up to a newline character, ignoring the buffer size. GCC will give you a fair warning: the gets function is dangerousIt is precisely this danger that makes the task possible.
Compile: gcc -m32 -fno-stack-protector -z execstack -no-pie -o vuln vuln.cWe check the protections: checksec --file=vulnExpected result: Canary: No, NX: Disabled, PIE: No, RELRO: Partial. If any protection is enabled, the exploit will fail; recompile.
Finding offset using de Bruijn pattern in GDB
Offset is the number of bytes from the start of the buffer to the return address. Manually calculating "64 bytes of buffer + 4 bytes of saved EBP = 68" isn't always accurate: the compiler may add padding for alignment. Therefore, we determine the offset experimentally, using a de Bruijn pattern—a sequence in which each 4-byte substring is unique.
Step by step process
Run the binary in the debugger: gdb ./vulnWe generate a 200-byte pattern in pwndbg directly inside GDB: cyclic 200. Or in the terminal: python3 -c "from pwn import *; print(cyclic(200).decode())".
run, we insert the pattern. The program crashes with SIGSEGV—EIP attempted to jump to an address that is actually a fragment of our pattern. Pwndbg will highlight the EIP value—let's say, 0x6161616c.
We determine the position: cyclic -l 0x6161616cPwntools will return a number—that's the offset. Let's say 76: the first 76 bytes fill the buffer, padding, and saved EBP, and bytes 77–80 fall exactly at the return address.
Verification
Submitting a test payload: 76 bytes Aand four bytes B. B GDB: run < <(python3 -c "import sys; sys.stdout.buffer.write(b'A'*76 + b'BBBB')")If EIP has become 0x42424242 (ASCII B = 0x42) — the offset is correct. If not, we recalculate. The pattern must be at least 50 bytes longer than the buffer, otherwise the unique substring may not reach the return address.