Gadgets & the Gadget Catalog
Return-Oriented Programming · Exploitation Fundamentals track
8 min read
In this module
What Makes a Useful Gadget
A gadget is any instruction sequence that ends with a RET. Not all are equally useful. The most valuable ones do one clean operation and return — giving the attacker precise, surgical control over a single step of their chain.
Examples of Common Gadgets
Here are some gadgets an attacker might find by scanning a program's executable code:
- MOV R0, #5; RET - loads the value 5 into R0, then returns. Useful for setting up register values.
- ADD R0, R1; RET - adds R1 to R0, then returns. Useful for arithmetic.
- STORE [R1], R0; RET - writes R0 to the address in R1, then returns. Useful for writing to arbitrary memory.
- POP R0; RET - pops a value from the stack into R0, then returns. Extremely useful because it lets the attacker load any value from the stack into a register.
Notice that each gadget does exactly one thing and then hands control back via RET. The RET at the end is what makes the chaining mechanism work.
A gadget catalogue is the set of all usable gadgets found in a particular binary. Tools like ROPgadget, ropper, and rp++ automatically scan executable files and list every instruction sequence ending in RET. Attackers search the catalogue for the specific operations they need to build their chain. Larger binaries and statically linked libraries provide richer catalogues.
Why POP Gadgets Are So Valuable
The POP Rn; RET pattern is the most important gadget type because it bridges the stack and the registers. Remember that the attacker controls the stack contents (through the buffer overflow). A POP gadget lets the attacker transfer a value from the stack - where they have full control - into a register, where the program can use it.
For example, to set R0 to an arbitrary value, the attacker places the address of a "POP R0; RET" gadget on the stack, followed immediately by the desired value. When the gadget executes, POP loads the value into R0, and RET advances to the next gadget address on the stack.
Finding Gadgets in Real Binaries
In practice, gadgets aren't neatly aligned with function boundaries. A useful sequence might start in the middle of another instruction. On variable-length encodings like x86, different byte alignments produce completely different instructions from the same bytes — a quirk that makes the gadget catalogue far richer than the function count alone would suggest.
The attacker doesn't need every possible operation. Just enough gadgets to load registers, write to memory, and invoke a system call. Even a modest binary usually provides all the raw material needed.
The Same Gadgets in x86-64
The teaching CPU's POP R0; RET has a direct x86-64 equivalent, and because of the System V calling convention you'll reach for it constantly: rdi holds the first argument, rsi the second, rdx the third, and rax the syscall number. Here are the gadgets that turn up in nearly every Linux chain, with the bytes that encode them:
| Gadget | Bytes | Typical job |
|---|---|---|
ret | c3 | Do nothing but advance the chain; fixes 16-byte stack alignment |
pop rdi; ret | 5f c3 | First argument |
pop rsi; ret | 5e c3 | Second argument |
pop rdx; ret | 5a c3 | Third argument |
pop rax; ret | 58 c3 | Syscall number |
pop r15; ret | 41 5f c3 | Rarely wanted for itself (see below) |
leave; ret | c9 c3 | Moves rsp to rbp; used deliberately for stack pivots, dangerous by accident |
syscall | 0f 05 | Enters the kernel with the registers you've set |
Most of these are only two bytes long, which is why they're so easy to find. The ret row looks useless, and for computation it is. You'll meet its real job in the next lesson, where system crashes without it.
Gadgets Nobody Wrote
The previous section's point about x86 alignment deserves a concrete case. Suppose a function contains the perfectly innocent instruction mov eax, 0xc35f. It encodes as five bytes:
offset +0 +1 +2 +3 +4bytes b8 5f c3 00 00decode at +0 [ mov eax, 0xc35f ]decode at +1 [pop rdi] [ret]Start disassembling one byte later and the CPU sees 5f c3: pop rdi; ret. The compiler never emitted that gadget. It's an accident of the constant. Gadget finders try every starting offset inside every executable section for exactly this reason.
The most famous accidental gadget on Linux came from __libc_csu_init, a startup helper that older glibc versions linked into every dynamically linked program. Its epilogue popped six registers and ended pop r15; ret, which is 41 5f c3. Skip the 41 prefix byte and 5f c3 is pop rdi; ret. For years that meant every non-PIE binary had a pop rdi; ret at a fixed address. glibc 2.34 moved that startup code into libc itself, so binaries built against it often have no pop rdi; ret of their own. When the binary's catalogue comes up short, exploit writers take the gadget from libc instead, which works once you know where libc is loaded.
Reading ROPgadget Output
ROPgadget is the usual first stop. Point it at a binary and filter to the instructions you care about:
$ ROPgadget --binary ./vuln --only "pop|ret"Gadgets information============================================================0x000000000040115d : pop rbp ; ret0x00000000004011fb : pop rdi ; ret0x00000000004011f9 : pop rsi ; pop r15 ; ret0x000000000040101a : ret
Unique gadgets found: 4Each line is an address, a colon, and the instructions that run from that address up to and including the ret, separated by ;. The addresses here are illustrative but realistically shaped: a non-PIE x86-64 binary loads at 0x400000, so its gadgets have small fixed addresses you can paste straight into a payload. On a full listing, pipe through grep rather than scrolling: ROPgadget --binary ./vuln | grep 'pop rdi'.
Run the same command on libc and the numbers change character:
$ ROPgadget --binary /lib/x86_64-linux-gnu/libc.so.6 --only "pop|ret" | grep ': pop rdi ; ret$'0x000000000002a3e5 : pop rdi ; retThat's an offset, not an address. libc is position-independent and is mapped at a different base on every run once ASLR is on, so the real address is the base plus 0x2a3e5. Offsets are specific to one libc build; the one shown is illustrative, and yours will differ. The same applies to PIE executables, whose gadget listings also start near zero.
ropper reads the same binaries with a different layout. ropper --file ./vuln --search "pop rdi" prints a few status lines and then matches such as 0x00000000004011fb: pop rdi; ret;. Same address, same instructions, slightly different punctuation. Both tools can exclude addresses containing bad bytes: ROPgadget with --badbytes "0a", ropper with -b 0a. That matters when the overflow goes through gets, which stops at a newline.
From inside a pwntools script you don't need either tool. ROP(elf).find_gadget(['pop rdi', 'ret']) searches the loaded binary and returns a gadget object whose .address is ready to pack.
One gadget breaks the "ends in ret" rule and is still welcome: a bare syscall. In an execve chain it's the last link, and if execve succeeds the process image is replaced, so there's nothing to return to. ROPgadget lists it with --only "syscall" or by grepping its full output. If you need the chain to carry on after the system call (calling read to pull in a second stage, say), you need a syscall ; ret instead, which is much rarer in ordinary binaries. glibc's thin syscall wrappers are the usual place to find one.
Side Effects and Filler Slots
The listing above has pop rsi ; pop r15 ; ret, and no clean pop rsi ; ret. That's common, and it's fine, but it changes the stack layout. The gadget pops two values before returning, so the chain needs the value for rsi, then a junk value for r15, then the next gadget's address. Forget the filler and the next ret pops your next gadget into r15 and jumps to whatever comes after it. Look at its address again, too: 0x4011f9 is two bytes before 0x4011fb. It's the same __libc_csu_init epilogue entered at a different offset.
Every gadget has a stack cost (how many slots it consumes, including the one its ret pops) and side effects (registers it clobbers). Good chain-building is mostly bookkeeping of both.
Predict: you want rdi = 1 and rsi = 2, using pop rdi ; ret at 0x4011fb and pop rsi ; pop r15 ; ret at 0x4011f9. How many 8-byte slots does that take, before the next gadget's address?
Five. 0x4011fb, then 1 (popped into rdi), then 0x4011f9, then 2 (popped into rsi), then any 8 bytes for r15. The ret at the end of the second gadget pops the sixth slot, which is where the next gadget's address goes.
Try It
In the ROP Chain lab, run ROPgadget --binary vuln --only "pop|ret" before you write any payload and work out which gadgets you'd need if win took an argument. The ROP Chain: Syscall lab ships helper gadgets for rax, rdi, rsi and rdx; find each one, note its stack cost, and check whether any of them pop more than one register.
Try the Simulations
Gadget Identification
Three small instruction sequences (gadgets) each ending in RET. Call them individually to see each one perform its single operation.
ROP Chain
Place gadget addresses on the stack and let RET chain through them - no CALL instructions needed. This is the core ROP mechanism.