Skip to the content ForoHackers
Log inJoin

CTF warm-up: read a stack frame and the vulnerability stops being magic

AssemblyBinary analysisCTF
#1195 · sh3llc0d3_sh3ph3rd · · Report

Before any "smash the stack" challenge, make sure you can draw the frame of a function. Take the classic practice binary, built for the exercise:

void greet(void) {
    char name[16];
    gets(name);               /* the deliberate bug in the challenge */
    printf("hi %s\n", name);
}

On x86-64 the prologue is boring and the picture is what matters:

push rbp
mov  rbp, rsp
sub  rsp, 16
lea  rax, [rbp-16]     ; &name
...
higher addresses
+--------------------+
| return address     |  <- rbp+8
| saved rbp          |  <- rbp
| name[16]           |  <- rbp-16
+--------------------+
lower addresses

gets writes upward, towards higher addresses, with no idea how big name is. Past 16 bytes you are inside the saved rbp; past 24 you are inside the return address, which ret will happily pop and jump to. That is the whole idea. No spells.

The homework for your own lab VM: in gdb, break after the prologue, run x/4gx $rsp and info frame, and match what you see to the diagram. Then recompile with -fstack-protector-strong and look at the extra canary load and compare at the end of the function.

#2085 · rop_gadget_goblin · · Report

The canary sits between the local buffer and the saved rbp, so a linear overflow has to trample it to reach the return address, and the function checks it before ret. CTF boxes usually ship with it off, NX maybe on, ASLR on. It is worth building the same binary with each protection toggled, one at a time, and noting what the disassembly gains.

#2086 · hexdump_hermit · · Report

Add -fno-omit-frame-pointer if you want rbp chains in optimized code. Otherwise offsets from rsp. Same picture, different register.

#2087 · n0p_sl3d · · Report

If you are new: draw the frame on paper for every function you read in the first ten challenges. Sizes, offsets, arrows. You will hate it for a week and then you will read any disassembly twice as fast.

#2088 · sh3llc0d3_sh3ph3rd · · Report

Seconded. The paper matters. Also compile once with -S and read your own source next to its assembly. You know what it was meant to do, so the mapping teaches faster than a mystery binary.

Log in or join to reply.