Skip to main content
Be Bitwise

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.

Pattern Strings

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:

python
from pwn import *
payload = cyclic(200) # b'aaaabaaacaaadaaaeaaa...'
# send it, let the target crash, and read the faulting PC in GDB: 0x6161616c
cyclic_find(0x6161616c) # 44: the bytes 'laaa' sit at offset 44

On 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:

  1. Padding - fill bytes up to the return address offset with safe values (often 'A' characters)
  2. Return address - at the exact offset, place the address you want execution to jump to
  3. 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

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded

Chaining It All Together

Against a modern system, a single redirect is rarely enough. A realistic chain looks like:

  1. Use a format string leak (Integer Overflows & Type Confusion) to break ASLR
  2. Calculate library addresses from the leaked pointer
  3. Build a ROP chain (using gadgets at those now-known addresses) to call system("/bin/sh")
  4. 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.