CTF Writeups

justOne

PWNhard270 pts

The binary reads a line into a stack buffer and passes it straight to one printf, and since RELRO leaves only the lazily-bound GOT writable while win sits unreferenced in .text, three sequential %n writes inside that single call are enough: one points __stack_chk_fail at win, one retargets a later argument at the stack canary, and the third writes a byte through it, so the failed canary check becomes the call to win.

Non-positional specifiers are the reason it fits in one call — arguments are consumed in order and printf re-reads each stack argument from memory, so a %n early in the format changes what a later specifier sees, which turns argv (an argument that holds the address of a later argument) into a write-anything-anywhere primitive aimed back into the frame.

The part that actually costs time is knowing which argument index lives at the address argv holds, and it must be measured rather than derived from the shape of the argv/envp/auxv block: write a distinctive mark through the pointer, dump the following arguments in the same format, and the one that comes back holding the mark fixes every index at once, because the overflow argument area is contiguous. Getting this wrong by two slots aims the write at an auxv word, which fails silently.

Only the low 16 bits of the canary address are unknown, and the frame is 16-byte aligned, so 12 bits are guessed one connection at a time — but the stack is re-randomised on every connection, so the tries are independent draws rather than a sweep, and a full pass over the 4096 candidates covers only 63%.

%c × 29  %4601c %hn   ← count 0x1216 into __stack_chk_fail's GOT entry
%c × 13  %21814c %hn  ← count = low 16 bits of the canary address, into argv's target
%c × 29  %hhn         ← one nonzero byte over the canary's zero LSB