ROP chain and payload builder
Assemble an exploit payload from padding, addresses, and raw bytes, with a live hexdump and offset ruler - the pwntools flat() workflow.
Open in ctfpalA ROP chain is a list of return addresses that the ret instruction walks through, each gadget doing a small amount of work before returning into the next. Building one is bookkeeping - the difficulty is keeping the stack layout straight, and a wrong offset by eight bytes produces a crash indistinguishable from a wrong gadget.
Building the payload as a visible structure rather than a concatenated string is what keeps that bookkeeping honest. When the layout is a list of rows with running offsets, an eight-byte error is something you can see; when it is a chain of string concatenations, it is something you discover after an hour of debugging the wrong hypothesis.
The standard 64-bit shape
payload = b’A' * offset # fill to the saved return address
payload += p64(pop_rdi) # gadget: pop rdi ; ret
payload += p64(binsh_addr) # -> rdi = "/bin/sh"
payload += p64(ret_gadget) # stack alignment - see below
payload += p64(system_addr) # -> system("/bin/sh")Two things that break chains silently
- Stack alignment. The x86-64 ABI requires 16-byte alignment at a
call. glibc’ssystemuses SSE instructions that fault if it is not aligned. A bareretgadget inserted before the call shifts the stack by 8 and fixes it - this is the cause of the notorious 'crashes insidesystem' symptom. - Bad bytes. If the input goes through
scanf("%s"), whitespace terminates it; throughgets, newline does; throughstrcpy, null does. A gadget whose address contains a forbidden byte cannot be used, however correct it is.
Part of a module
10. Binary exploitation
Read a binary’s protections, find an overflow offset in one crash, and build a ROP chain when the stack is not executable.
Practise on real challenges
Go deeper
- From crash to shell: stack overflows, offsets, ret2win, and ret2libcA segfault is not an exploit. The path from an unexpected crash to a controlled instruction pointer to a shell, with the mitigation checks that decide which technique you need and the stack-alignment detail that breaks working exploits.
Related tools
ROP gadget finder
Search a binary for return-oriented programming gadgets, filter by the registers they touch, and exclude ones containing bad bytes.
Struct pack and unpack (p32, p64, u32, u64)
Convert integers to little-endian byte strings and back at 8, 16, 32, and 64 bits - the pwntools p32/p64 helpers without the install.
Cyclic pattern generator and offset finder
Generate a de Bruijn sequence and find the exact overflow offset from a crashed register value - the pwntools cyclic workflow, in the browser.
Libc base address calculator
Turn a leaked libc pointer into the library’s base address, then resolve any other symbol - the arithmetic every ret2libc exploit runs on.