Skip to main content
Be Bitwise

ROP in Practice

Return-Oriented Programming · Exploitation Fundamentals track

8 min read

In this module

From Theory to Exploitation

In real-world exploits, ROP chains are the standard route to code execution on modern systems with DEP/NX enabled. The typical move: use a ROP chain to call OS functions that change memory permissions, then jump to shellcode in the newly executable region. Use the defence's own rules to undo the defence.

The Common ROP Strategy

The most frequent real-world ROP pattern follows a two-stage approach:

  1. Stage 1 (ROP chain): Use gadgets to set up registers with the right arguments, then call mprotect() (on Linux) or VirtualProtect() (on Windows). These system calls can mark a memory region as executable.
  2. Stage 2 (Shellcode): Now that the attacker has made a region executable, they jump to traditional shellcode stored in that region.

ROP is the bridge that gets past DEP/NX. Once the attacker can mark memory as executable, they return to the simpler shellcode approach for the actual payload.

mprotect

mprotect is a system call that changes the memory protection flags on a region of memory. An attacker who can call mprotect with the right arguments can mark the stack or heap as executable, undoing the protection that DEP/NX provides. This is why ROP chains in practice often target this single system call.

Three Routes to a Shell on x86-64 Linux

The mprotect route is one of three that come up again and again. Which one you pick depends on what the binary and its libc give you.

RouteRegisters to set (System V)Needs
ret2libc: call system("/bin/sh")rdi = pointer to "/bin/sh"libc's address, pop rdi; ret, an alignment ret
Syscall chain: execve("/bin/sh", NULL, NULL)rax = 59, rdi = string pointer, rsi = 0, rdx = 0pop gadgets for four registers, and a syscall gadget
mprotect then shellcoderdi = page-aligned address, rsi = length, rdx = 7 (read, write, execute)Somewhere to put shellcode, gadgets for three registers, mprotect's address

ret2libc is the shortest when libc is reachable, which is why the Ret2Libc lab starts there. The syscall chain needs no libc at all, so it suits statically linked binaries, which carry a huge gadget catalogue of their own; that's the ROP Chain: Syscall lab. The mprotect route takes the most work and gives the most freedom afterwards, since the second stage can be any shellcode you like.

Getting Past ASLR: the Two-Stage Leak

Every chain so far has assumed you know where libc is. With ASLR on, libc's base changes each run, so a hard-coded system address is wrong almost every time. The standard answer on a non-PIE binary is to spend one chain learning the address, then send a second chain that uses it.

Stage one calls puts through the binary's own PLT (fixed address, no ASLR) with the binary's GOT entry for puts as the argument. The GOT entry holds puts's real address in libc, so puts prints its own location. The chain then returns to main, which runs the vulnerable function again and waits for stage two.

python
rop = ROP(elf)
rop.call(elf.plt.puts, [elf.got.puts]) # puts(&puts_got_entry)
rop.call(elf.sym.main)
io.sendline(flat({72: rop.chain()}))
leak = u64(io.recvline().rstrip(b'\n').ljust(8, b'\x00'))
libc.address = leak - libc.sym.puts
log.info('libc base: %#x', libc.address)

The ljust matters. A libc address such as 0x00007f3a1c680e50 has two zero bytes at the top, and puts stops at the first zero, so only six bytes arrive. Padding them back to eight lets u64 unpack them. Once libc.address is set, stage two is the chain from the previous lesson, unchanged. If either stage crashes in a movaps, the alignment fix is the same: one extra ret.

Predict: the leak is 0x7f3a1c680e50 and your libc's puts offset is 0x80e50 (illustrative). What's the base, and how can you tell at a glance whether you used the right libc?

The base is 0x7f3a1c600000. Subtracting gives a value ending in 000, and that's the sanity check: shared libraries are mapped on page boundaries, so a correct base always ends in three hex zeros on x86-64 with 4 KiB pages. If your base ends in anything else, the offset came from a different libc build than the one the target runs, and every address in stage two will be wrong.

One more x86-64 detail when a chain calls variadic functions such as printf: the System V ABI uses al to say how many vector registers carry arguments. If al happens to hold a large leftover value, the function's prologue spills the XMM registers to the stack with aligned stores, which brings back the movaps problem. Clearing rax with a gadget first, or calling puts instead, avoids it.

Variants Beyond ROP

Researchers and attackers have developed several variations on the return-oriented theme:

  • JOP (Jump-Oriented Programming) - uses indirect jump instructions instead of RET. The "dispatcher" gadget reads the next target from a table rather than the stack.
  • SROP (Sigreturn-Oriented Programming) - abuses the Unix signal return mechanism. A single sigreturn system call can set all registers at once from a crafted stack frame, reducing an entire ROP chain to one gadget.
  • COP (Call-Oriented Programming) - chains through CALL instructions and function pointers rather than RET.

Each variant works around specific defences while following the same core principle: reuse existing code rather than injecting new code.

Defences Against ROP

The arms race between attackers and defenders has produced several targeted countermeasures:

  • ASLR (Address Space Layout Randomisation) - randomises where code is loaded in memory. If the attacker does not know where gadgets are, they cannot build a chain. However, information leaks can defeat ASLR by revealing code addresses.
  • CFI (Control-Flow Integrity) - restricts where indirect branches (including RET) can transfer control. Each RET is checked against a set of valid targets, preventing jumps to arbitrary gadgets.
  • Shadow Stacks - the hardware or OS maintains a second, protected copy of return addresses. On every RET, the popped address is compared against the shadow stack. If they differ, execution is halted.
  • CET (Control-flow Enforcement Technology) - Intel's hardware implementation of shadow stacks and indirect branch tracking, built directly into the CPU.

These defences represent a genuine shift in strategy. Earlier mitigations stopped at preventing code injection; CFI and shadow stacks go further and confine the program's control flow to paths that were actually intended. The adversarial pressure between exploits and mitigations is what keeps driving hardware vendors to build features like CET — which tells you how seriously the industry now takes this particular class of attack.

When the Chain Doesn't Live on the Stack

Everything in this module has assumed a stack overflow, so the chain sits right where ret will find it. Many modern bugs aren't stack overflows: a use-after-free or a heap overflow gives the attacker control of some heap memory and, eventually, one hijacked function pointer. That buys a single jump, not a chain.

The bridge is a stack pivot: one gadget that points rsp at memory the attacker controls. xchg rsp, rax ; ret works if rax happens to hold the address of the attacker's buffer when the hijacked pointer fires. leave ; ret works if rbp does, because leave copies rbp into rsp. After the pivot, every following ret reads from the heap buffer, which the attacker filled with an ordinary ROP chain in advance. Browser and kernel exploits use this pattern constantly, and it's why leave ; ret was flagged as dangerous in the gadget table two lessons ago.

How the Mitigations Interact

No single mitigation stops ROP, and attackers rarely face just one. What matters in practice is the combination, and each layer changes what the exploit needs:

MitigationWhat it forces the attacker to do
NXReuse code: ROP instead of shellcode, or ROP to mprotect before shellcode
ASLRLeak a libc address before the real chain, as in the two-stage example
PIELeak an address inside the binary too, since its gadgets move as well
Stack canaryLeak the canary and write it back unchanged, or find a write that skips over it
Full RELROLeave the GOT alone as a write target; reading it to leak addresses still works
Shadow stack (Intel CET)Every ret is checked against a protected copy of the return address, so ret-based chains die at the first gadget

The last row is the one that ends ROP as described in this module. With a hardware shadow stack, call pushes the return address to both the normal stack and a shadow stack that ordinary writes can't touch, and ret compares the two. Your overflow changes only the normal copy, so the first ret into a gadget raises a control-protection fault. The hardware has been in Intel processors since Tiger Lake and AMD since Zen 3, and Linux added user-space shadow stack support in kernel 6.6, with glibc support following. Uptake depends on every library in the process being built for it, so plenty of systems still run without it today.

Each of these has its own lesson in Exploit Mitigations, including ASLR and PIE and CFI and beyond; checksec is how you'll check which ones a binary has before you plan a chain.