Modern binaries mark the stack as non-executable (the NX/DEP protection you saw in checksec). That kills the classic attack of injecting your own machine code and jumping to it — the CPU refuses to run instructions from the stack. Return-oriented programming (ROP) is the answer to NX: instead of bringing our own code, we reuse the code already in the binary, stitching together tiny fragments that are already marked executable.
A gadget is a short run of instructions that ends in a ret — for example pop rdi ; ret. The ret is what makes gadgets composable: it pops the next address off the stack and jumps to it. After a buffer overflow we control the saved return address and everything stacked above it, so we can lay down a whole sequence of gadget addresses. Each gadget does a little work and then rets straight into the next one. That sequence is a ROP chain.
The simplest useful chain loads a register and calls a function. We point the overwritten return address at a pop rdi ; ret gadget, place the value we want below it, and place the function to call below that:
Three stack slots, read top to bottom, become: set rdi, then call win() with it. Build longer chains the same way and you can call several functions in a row, each with its own arguments.
On x86, function arguments are passed on the stack. That is extraordinarily convenient for an attacker: to call system("/bin/sh") you just lay down the address of system, a return address, and then the argument — all values on the stack, which is exactly what an overflow gives you. You often need barely any gadgets at all.
On x64, the System V calling convention passes the first six integer arguments in registers — rdi, rsi, rdx, rcx, r8, r9 — not on the stack. You cannot put a value in rdi just by writing it to the stack; you need an instruction that moves it there. That is why x64 exploitation leans on gadget hunting: every argument you want to set means finding a pop reg; ret (or similar) to load it first. The stack slots are still how you supply the values, but a gadget is what routes each value into its register.
This is the whole reason the x64 challenges in this section revolve around finding the right pop gadgets, while the x86 ones can lean on the stack directly.
You do not scan for gadgets by hand. Two tools do it for you: ROPgadget and ropper. Point one at a binary and it lists every gadget it can find; you then filter for the ones you need. Here we ask only for gadgets built from pop and ret:
ROPgadget --binary chall --only "pop|ret"Gadgets information============================================================0x00000000004007bc : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret0x00000000004007be : pop r13 ; pop r14 ; pop r15 ; ret0x00000000004007c0 : pop r14 ; pop r15 ; ret0x00000000004007c2 : pop r15 ; ret0x00000000004007bb : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret0x00000000004007bf : pop rbp ; pop r14 ; pop r15 ; ret0x0000000000400618 : pop rbp ; ret0x00000000004007c3 : pop rdi ; ret0x00000000004007c1 : pop rsi ; pop r15 ; ret0x00000000004007bd : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret0x000000000040053e : retThe skill is not running the tool — it is reading that list and picking gadgets that compose into the effect you want. A pop rdi ; ret sets one argument; a pop rdi ; pop rsi ; pop rdx ; ret sets three at once.
The rest of this section is ROP in practice: Natural Jumps and Artificial Jumps build single-argument chains on both architectures, and Chaining Calls strings three full function calls together. The same idea returns later, scaled up, when we borrow functions straight out of libc.