[Advanced] — how modern exploitation defeats NX and ASLR: stop injecting code, reuse the code that’s already there.

The kid version first

NX means you can’t run your own code on the stack. So instead of writing new code, you borrow the program’s existing code — it’s full of useful snippets. String enough snippets together and you can make the program do anything, without injecting a single byte of your own.

It’s like writing a ransom note by cutting words out of a newspaper. You didn’t write the words; you chose which existing ones to paste in what order.

ret2libc — the simplest code reuse

The C library (libc) is already loaded and executable, and it contains system(). So don’t inject shellcode — return into system("/bin/sh").

You need three things on the stack when the vulnerable function returns:

[ padding to offset ][ pop_rdi_gadget ][ addr of "/bin/sh" ][ addr of system ]
                        ↑ sets RDI          ↑ the argument      ↑ what runs

system’s first argument goes in RDI (the x86-64 calling convention), so you need a pop rdi; ret gadget to load the address of the string "/bin/sh" — which conveniently already exists inside libc. Then ret into system.

from pwn import *
libc = ELF("./libc.so.6")
payload  = b"A" * offset
payload += p64(pop_rdi)                    # gadget
payload += p64(libc_base + libc.search(b"/bin/sh").__next__())
payload += p64(libc_base + libc.symbols["system"])

This is the whole game on a No-canary, NX-on binary — if you know libc_base. With ASLR, you don’t, which is the next problem.

Defeating ASLR: the info leak

ASLR randomises the base, but offsets within libc are fixed — if you learn the runtime address of any one libc function, you can compute libc_base, and from that every other function.

The standard two-stage exploit:

Stage 1: leak — use the bug to print a real libc address (e.g. from the GOT)
         → libc_base = leaked_addr − known_offset_of_that_function
Stage 2: with libc_base known, build the ret2libc / ROP chain and return to it

A common Stage-1 primitive is calling puts(puts_got) — make the program print the GOT entry for puts, which contains puts’s real address:

payload  = b"A"*offset
payload += p64(pop_rdi) + p64(puts_got)    # RDI = address to leak
payload += p64(puts_plt)                    # call puts(puts_got) → prints the address
payload += p64(main_addr)                   # return to main to send stage 2
leaked = u64(p.recvline().strip().ljust(8, b"\x00"))
libc.address = leaked - libc.symbols["puts"]   # now everything is known

“Leak, then pwn” is the shape of nearly every modern exploit. The bug rarely gives control directly — it gives a read (to defeat ASLR) that enables a write (to take control).

ROP — Return-Oriented Programming

When you need more than a single function call — set several registers, call a syscall, write to memory — you chain gadgets: short instruction sequences ending in ret, scattered throughout existing code.

pop rdi ; ret          ← load RDI
pop rsi ; pop r15 ; ret ← load RSI (and junk into R15)
pop rdx ; ret          ← load RDX
mov [rdi], rsi ; ret   ← WRITE rsi to where rdi points  (a write primitive!)
syscall ; ret          ← make a syscall

The stack becomes a program written in gadget addresses. Each ret pops the next gadget’s address off the stack and jumps to it — so a carefully laid-out stack drives execution through a sequence you assembled:

[ pop_rdi ][ value_for_rdi ][ pop_rsi_r15 ][ value_for_rsi ][ junk ][ target_function ]

Find gadgets with:

ROPgadget --binary ./vuln | grep "pop rdi"
# or in pwntools:
rop = ROP(elf)
rop.call("system", [next(elf.search(b"/bin/sh"))])
print(rop.dump())          # pwntools BUILDS the chain for you

pwntools’ ROP builder is why this is tractable — you describe the calls you want and it finds and orders the gadgets.

ret2syscall and ret2csu

  • ret2syscall — no system available? ROP your way to execve("/bin/sh",0,0) directly: set RAX=59, RDI→string, RSI=RDX=0, then a syscall gadget. Pure syscalls, no libc functions needed
  • ret2csu — need to set RDX (third argument) but there’s no pop rdx gadget? A universal gadget in __libc_csu_init (present in most dynamically-linked binaries) sets RDX, RSI, RDI at once. A classic “there’s no gadget for that register” escape hatch

GOT overwrite

If RELRO is only partial, the GOT is writable. Overwrite the GOT entry for a function the program calls (say free) with the address of system — now the next free(user_controlled) runs system(user_controlled). Full RELRO kills this → RELRO.

The escalation ladder

The techniques form a progression, and which you need is dictated by checksec:

No defences        → inject shellcode, jump to it
NX                 → ret2libc / ROP (reuse code)
NX + ASLR          → leak an address first, THEN ret2libc / ROP
NX + ASLR + PIE    → leak the binary base too, before using its gadgets
+ Full RELRO       → no GOT overwrite; use pure ROP to execve
+ canary           → leak the canary first, or avoid the return address
+ CET/shadow stack → ROP itself is detected; back to memory-write primitives, JOP, or data-only attacks

Key insight

Modern exploitation is code reuse, not code injection — NX forced the entire field to build payloads out of the target’s own instructions, and ASLR forced every exploit to leak an address first. The mental shift is that the stack stops being “where I put shellcode” and becomes “the program I’m writing in gadget addresses.” checksec tells you which rung of the ladder you’re on, and pwntools’ ROP builder does the tedious part.

Source: [reference] — Aug 2026. CTF/authorised-testing material.

↑ Back to Binary Exploitation