Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Spot the bug (20 minutes)
A function has char name[16]; strcpy(name, input);. How many bytes can input safely have, counting the final zero byte?
Done when: 15 characters plus the zero byte, 16 bytes in all.
Hint
the zero byte uses one of the 16 bytes.
Easy 2: Which layer (20 minutes)
Match each defence to what it stops: canary, NX, ASLR. The choices are: injected code runs, guessing addresses, an overflowing return address.
Done when: canary stops a changed return address from being used, NX stops injected code, ASLR stops guessing addresses.
Hint
each defence answers one question.
Easy 3: Space of positions (20 minutes)
With 28 random bits, how many positions are there, and what is the chance of one blind guess?
Done when: 2^28 = 268,435,456 and 1 in 268,435,456.
Hint
2^28 is 2^10 x 2^10 x 2^8.
Medium
Medium 1: Check a binary (45 minutes)
Compile the program of section 1 three ways (default, -fstack-protector-all, -fno-stack-protector -z execstack). For each, report nm | grep stack_chk and the GNU_STACK flags.
Done when: a table of three rows with the canary symbol present or not and RW or RWE.
Hint
readelf -lW prog | grep GNU_STACK.
Medium 2: Watch ASLR (45 minutes)
Run the program of section 4 ten times, and count how many different code addresses and stack addresses appear. Then run it with setarch -R ten times.
Done when: ten different values with ASLR on, one value with it off.
Hint
use a shell loop and sort | uniq.
Medium 3: Odds (45 minutes)
An attacker has a 1 in 2^28 chance per try and can try 1,000 times per second before the program is noticed. How long to try the whole space, and how long on average to succeed?
Done when: 268,435,456 / 1,000 = 268,435 s (about 3.1 days), and about half of that, 1.55 days, on average.
Hint
divide the space by the rate.
Hard
Hard 1: Fix the bug (90 minutes)
Rewrite copy from section 1 so that it never overflows, in two ways: with snprintf and with a length check that refuses long input. Run both with the 48-character input.
Done when: both print a safe result with no crash, and a note on which option keeps the data and which rejects it.
Hint
snprintf(buf, sizeof buf, "%s", s) always ends with a zero byte.
Hard 2: Sanitizer (90 minutes)
Compile the original program with -fsanitize=address -g and run it with a long input. Explain each part of the first lines of the report (error kind, size, function, line).
Done when: a paragraph that names stack-buffer-overflow, the size written, and the source line of strcpy.
Hint
read the report from the top: kind, then access, then stack trace.
Hard 3: Which defence failed (90 minutes)
A report says: "the attacker read a pointer from a log file, then used it to place a jump". Which layer did the leak defeat, and which layers still stood? Suggest one fix for the leak.
Done when: ASLR (and PIE) was defeated, the canary and NX still apply, and the fix is to stop printing pointers into logs.
Hint
think about which fact the attacker was missing.