From Crash to Exploit
Exploit Development & Real-World Impact · Exploit Development track
4 min read
In this module
Starting with a Crash
You have a program that crashes when you feed it certain input. The debugger shows a segmentation fault — the program tried to jump to a nonsense address. That's your starting point.
What Do You Control?
The first question is: what values does the attacker's input control at the moment of the crash? Attach a debugger and examine:
- Registers - does the program counter (PC) contain a value from your input? If so, you control where execution goes.
- Stack contents - are there values from your input on the stack? These could serve as ROP gadget addresses.
- Memory near the crash - what does the corrupted memory look like? How much did you overwrite?
If the PC contains bytes from your input, you've got a direct control-flow hijack. If not, you need an indirect path — tracing how the corrupted data eventually influences something that controls execution.
Finding the Right Offset
A classic technique for finding which bytes in your input overwrite the return address is to send a unique pattern - a string where every sequence of 4 (or 8) bytes is unique. When the program crashes, the value in the PC tells you exactly which position in your input landed on the return address.
For example, if you send a pattern of 256 bytes and the crash shows PC = 0x41366341 (the ASCII bytes "Ac6A"), you search the pattern for that sequence and find it at offset 78. Now you know: bytes 78-81 of your input overwrite the return address.
Tools like Metasploit's pattern_create and pattern_offset automate this process. They generate non-repeating byte sequences and, given the crash value, instantly report the offset. This technique has been a staple of exploit development for decades.
In pwntools the same job is cyclic and cyclic_find. Its default pattern is built from lowercase letters in 4-byte groups:
from pwn import *
payload = cyclic(200) # b'aaaabaaacaaadaaaeaaa...'# send it, let the target crash, and read the faulting PC in GDB: 0x6161616ccyclic_find(0x6161616c) # 44: the bytes 'laaa' sit at offset 44On x86-64 there's a wrinkle. A pattern-filled return address isn't a canonical address, so ret faults before RIP ever holds it. Read the 8 bytes at the top of the stack instead (x/gx $rsp in GDB at the crash) and use cyclic(200, n=8) with cyclic_find(value, n=8).
Building the Payload
With the offset known, you construct your input:
- Padding - fill bytes up to the return address offset with safe values (often 'A' characters)
- Return address - at the exact offset, place the address you want execution to jump to
- Payload - after the return address, place your ROP chain or shellcode (if DEP is disabled)
When the vulnerable function returns, it pops your crafted address off the stack and jumps to it. You now control execution.
See It in Action
The simulator below demonstrates offset finding. A unique pattern of bytes is written to memory, and by examining which value ends up at the return address location, you can determine the exact offset needed.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Chaining It All Together
Against a modern system, a single redirect is rarely enough. A realistic chain looks like:
- Use a format string leak (Integer Overflows & Type Confusion) to break ASLR
- Calculate library addresses from the leaked pointer
- Build a ROP chain (using gadgets at those now-known addresses) to call
system("/bin/sh") - Deliver the whole thing through the overflow, with the canary value sitting in exactly the right place
Each step leans on what came before. Exploit development is applied synthesis — everything from the previous modules, combined.
Defensive Thinking
The complexity of modern exploitation is itself a defence mechanism. Each mitigation adds a step; each step is a chance for the chain to fail. Defence in depth works not because any single layer is impenetrable, but because stacking enough imperfect layers makes the combined problem prohibitive for most attackers.
Try the Simulations
Full Exploit Chain
Complete exploit: overflow a buffer to overwrite the return address, redirecting execution to attacker-controlled code that stores a marker value.
Finding the Offset
Write a unique pattern of bytes to consecutive memory addresses to determine which offset overwrites the return address. The pattern value at the return address location reveals the exact offset.