Introduction to Binary Exploitation
Binary exploitation (“pwn”) consists of leveraging bugs in compiled programs —typically C/C++— to alter their execution and, ultimately, run your own code or take control of the process. Unlike the web, here you work at the level of memory, registers, and assembly: a mistake handling a buffer, a pointer, or an integer becomes control of the execution flow. It’s the flagship discipline of CTFs and the basis of low-level software exploits.
Threat model
Section titled “Threat model”In pwn the attacker controls an input (stdin, an argument, a socket, a file) that the program processes insecurely. The goal isn’t to exfiltrate data but to corrupt the process’s memory state until execution can be redirected. Everything that follows assumes an authorized lab: CTF challenges, your own binaries, or HTB/pwn.college machines where you have explicit permission.
- Asset to protect: the integrity of the execution flow (
RIP/PC) and of the process memory. - Surface: any function that copies, formats, or sizes input without validation (
gets,strcpy,printf(user),malloc(n*k)). - Attacker capability: from writing N extra bytes up to a write-what-where (writing an arbitrary value to an arbitrary address).
Why these bugs exist
Section titled “Why these bugs exist”C and C++ give full control over memory but with no safety net: they don’t check array bounds, they trust the programmer to handle pointers and sizes correctly. A mistake —writing past a buffer, using freed memory, trusting an attacker-controlled size— corrupts memory structures the attacker can manipulate to redirect execution.
Theory: the process memory
Section titled “Theory: the process memory”A Linux process organizes its memory into regions with distinct permissions:
- Stack: local variables and return addresses. Grows toward low addresses. Target of the stack overflow.
- Heap: dynamic memory from
malloc/free. Target of heap exploitation. .text: the program code (executable, usually non-writable)..data/.bss: global variables (writable).- Library mappings:
libcand other.sofiles, source of gadgets and ofsystem.
Key x86-64 registers:
RIP— instruction pointer: which instruction runs. It’s the target.RSP— stack pointer (top of the stack);RBP— base pointer (current frame).RAX–R15— general purpose;RDI, RSI, RDX, RCX, R8, R9carry the arguments (see x86-64 Assembly).
The goal is almost always to control RIP: if you decide what runs next, you control the program.
The vulnerability classes
Section titled “The vulnerability classes”- Stack buffer overflow — write past a stack buffer and overwrite the return address (see Stack Buffer Overflow).
- Format string —
printf(user)lets you read/write arbitrary memory (see Format String). - Heap exploitation — corrupt allocator metadata via use-after-free, overflow, or double-free (see Heap Exploitation).
- Integer bugs — overflow/underflow/signedness producing wrong sizes or indices (see Integer Overflow and Integer Bugs).
Modern protections (mitigations)
Section titled “Modern protections (mitigations)”Current binaries ship defenses you must understand and bypass:
- ASLR — randomizes the addresses of regions; you need a leak to learn them.
- NX/DEP — the stack and heap aren’t executable; you can’t inject shellcode there, hence ROP.
- Stack canary — sentinel value before the return address; detects the overflow unless you leak it.
- PIE — the executable itself loads at a random address; you need a binary leak.
- RELRO — protects the GOT (see GOT/PLT Overwrite); with Full RELRO the GOT is read-only.
Check what a binary ships with via checksec:
checksec --file=./binary# Arch, RELRO, Stack canary, NX, PIE, Fortify...The flow of a pwn challenge
Section titled “The flow of a pwn challenge”flowchart TD
A["checksec + reversing"] --> B{"Which mitigations?"}
B --> C["Identify the bug: overflow / fmt / heap"]
C --> D{"ASLR/PIE?"}
D -->|Yes| E["LEAK an address (libc/PIE/canary)"]
D -->|No| F["Fixed addresses"]
E --> G{"NX enabled?"}
F --> G
G -->|Yes| H["ROP / ret2libc"]
G -->|No| I["Shellcode on stack"]
H --> J["Control RIP → shell / flag"]
I --> J
J --> K["Run against remote"]
The modern exploit usually is: leak an address (bypass ASLR/PIE), include the canary intact, then redirect the flow with ROP (because NX blocks shellcode).
Exploitation (lab): a pwntools skeleton
Section titled “Exploitation (lab): a pwntools skeleton”A CTF exploit almost always starts the same way. pwntools normalizes local and remote:
from pwn import *
context.binary = elf = ELF('./vuln') # sets arch/bits/endian automaticallylibc = ELF('./libc.so.6')
def conn(): if args.REMOTE: return remote('challenge.ctf', 1337) return process('./vuln')
io = conn()# 1) interact up to the vulnerable point# 2) (if needed) leak -> compute bases# 3) build payload (ret2win / ROP / fmt)# 4) send and p.interactive()io.interactive()Run the script with python3 exploit.py REMOTE to hit the server, or with no argument to test locally first.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- Memory-safe languages (Rust, Go) where feasible; in C/C++, avoid unsafe functions (
gets,strcpy,sprintfwithout a bound). - Enable every mitigation: ASLR (system), NX, stack canaries (
-fstack-protector-strong), PIE (-fPIE -pie), Full RELRO (-Wl,-z,relro,-z,now),-D_FORTIFY_SOURCE=2. - Static/dynamic analysis: sanitizers (ASan, UBSan) in development and fuzzing (AFL++, libFuzzer) to find the bugs before the attacker.
- Validate sizes and bounds: never trust controlled input to size buffers or index arrays.
Checklist
Section titled “Checklist”- Understand process memory and the goal (control
RIP) -
checksec: review the binary’s mitigations - Identify the vulnerability class
- Reverse the binary (Ghidra/GDB) to understand the flow
- Plan the exploit based on protections (leak/ROP/shellcode)
- Develop with pwntools locally
- Bypass ASLR/canary/NX/PIE
- Control
RIP→ shell/flag → remote