Athena Wiki

Binary Exploitation

pwnbeginner

Index of all pwn/binary exploitation pages - from beginner stack overflows to advanced kernel exploitation.

pwnbinary-exploitationindexoverview

Binary Exploitation (Pwn)

In CTFs, pwn challenges give you a compiled program and ask you to break it. The goal is almost always the same, trick the program into giving you a shell or printing the flag. You do this by finding bugs in how the program handles memory, then crafting input that takes control of what the program does.

This section starts from scratch. No prior pwn experience needed — just basic C knowledge and comfort with a terminal.

First step: Read Linux Protections. Before you can exploit a binary, you need to understand what protections are active and what they mean.

Prerequisites

Before diving in, make sure you're comfortable with:

  • C basics: pointers, arrays, strings, functions, the main() entry point
  • Terminal: running commands, navigating directories, basic shell usage
  • Hex and binary: reading hex addresses, understanding what a byte is

If you've never seen a buffer overflow before, start with Stack Buffer Overflow - it's the foundation everything else builds on.


What's in This Section

PageDifficultyWhat You'll Learn
Linux ProtectionsBeginnerWhat ASLR, NX, PIE, Canaries, RELRO are, and how to read checksec output
Stack Buffer OverflowBeginnerOverflow a buffer to overwrite the return address and redirect execution
ROP ChainsIntermediateChain small code gadgets to bypass NX (non-executable stack)
ret2libcIntermediateCall system("/bin/sh") from libc when you can't inject shellcode
ShellcodeIntermediateWrite and inject your own machine code (when NX is off)
Format StringIntermediateUse printf bugs to read and write arbitrary memory
GOT OverwriteIntermediateRedirect calls to libc functions by overwriting the GOT
Heap ExploitationAdvancedHow glibc's malloc/free work, and how to abuse them
Use-After-FreeAdvancedExploit dangling pointers for leaks and arbitrary writes
Tcache PoisoningAdvancedHijack the tcache freelist to get arbitrary allocation
House of SeriesAdvancedNamed heap exploitation techniques (Force, Spirit, Apple, etc.)
SROPAdvancedUse sigreturn to set every register at once with minimal gadgets
Seccomp BypassAdvancedBypass syscall filters when execve is blocked
Kernel PwnExpertExploit Linux kernel modules for privilege escalation

Learning Path

Beginner — Foundations

Start here. These three pages give you the mental model for everything that follows.

  1. Linux Protections — Learn what checksec tells you. Every exploit decision depends on this.
  2. Stack Buffer Overflow — The simplest exploit: overflow a buffer, overwrite the return address, jump to a win() function.
  3. ROP Chains — The stack is non-executable? Chain existing code snippets to do what you want.

Intermediate — More Attack Surface

These techniques expand what you can exploit beyond simple stack overflows.

  1. ret2libc — Call system("/bin/sh") from libc. You'll need this when there's no win() function.
  2. Shellcode — When NX is off, write your own machine code and inject it.
  3. Format String — When printf(buf) lets you control the format string, you can read and write memory freely.
  4. GOT Overwrite — Turn an arbitrary write into code execution by redirecting function calls.

Advanced — Heap & Specialized Techniques

Heap exploitation is a different world from stack-based attacks. These pages build on each other.

  1. Heap Exploitation — Understand chunks, bins, and how malloc/free actually work.
  2. Use-After-Free — The most common heap bug in CTFs. Free a chunk, then use it again.
  3. Tcache Poisoning — The go-to technique for turning a UAF into arbitrary allocation.
  4. SROP — When you have very few gadgets, sigreturn sets everything at once.
  5. Seccomp Bypass — When execve is blocked, use open-read-write shellcode.

Expert — Kernel & Named Techniques

  1. House of Series — A reference of named heap techniques for different glibc versions.
  2. Kernel Pwn — Exploit kernel modules for root. Different tooling, different mindset.

Your First Exploit in 60 Seconds

Here's what a typical pwn challenge looks like end-to-end:

# 1. Receive the binary
file ./vuln          # ELF 64-bit, dynamically linked
checksec --file=./vuln  # check protections

# 2. Run it to understand behavior
./vuln               # "Enter your name: " → type something → "Hello, <input>"

# 3. Find the vulnerability
# (reverse engineer it, or notice it crashes with long input)

# 4. Write the exploit in Python with pwntools
from pwn import *

p = process('./vuln')
payload = b'A' * 72 + p64(0x401234)  # overflow + address of win()
p.sendline(payload)
p.interactive()  # get a shell

That's the pattern. Every page in this section is a variation on this — find the bug, craft the payload, get control.


How to Decide What Technique to Use

Run checksec first. Then:

Mermaid diagram
checksec showsWhat to try
No NXShellcode injection
NX + No PIE + No ASLRret2win or ret2libc with fixed addresses
NX + PIE + ASLRLeak addresses first → ROP chain
Canary foundLeak canary via format string before overflow
Full RELROTarget hooks or FSOP, not GOT

Essential Tools

pwntools is the Python library you'll use for almost every exploit.

pip3 install pwntools

Boilerplate:

from pwn import *

context.binary = elf = ELF('./vuln')
context.log_level = 'info'

def start():
    if args.REMOTE:
        return remote('challenge.ctf.com', 1337)
    elif args.GDB:
        return gdb.debug('./vuln', gdbscript='break main\ncontinue\n')
    else:
        return process('./vuln')

p = start()
# ... your exploit ...
p.interactive()

Key functions: process(), remote(), sendline(), p64()/u64(), flat(), cyclic(), cyclic_find(), ROP(), SigreturnFrame(), fmtstr_payload().


Last updated on

On this page