Midnight Sun CTF 2020 Quals - pwn4
An homage to pwny.racing, we present… speedrun pwn challenges. These bite-sized challenges should serve as a nice warm-up for your pwning skills.
Files: repo
Analysis
Binary info:
Exploiting pwn4 required usage of not so well known format string syntax.
Decompiling main provided following output:
Lets analyze what is happening here:
- Secret code is read by using function get_secret that provides random 4 bytes from
/dev/urandom
. - Memory region with secred code is mprotected to make it read only.
- User is required to provide parameters user and code.
- Secret code is compared with code provided by the user and if they are equal
/bin/sh
is spawned.
It looks like we need to guess secret code generated by /dev/urandom
or force comparison to be true by forcing one of the parameters to the known value.
By analyzing log_attempt
function we can discover format string vulnerability in the last line.
It looks that we can abuse vulnerability and copy the secret (4 bytes) to our guess variable and pass the check. In order to do that we can use asterisk ‘*’ that will allows us to choose the value from the stack that will be threated as a number for formatting the output.
Syntax *25$
means to take value “25th” from the stack and put it into the specified place. In this case 25th on the stack is secret value. That will print with %*25$x
total number of bytes equal to secret value. Then we use this secret value to overwite our input code that is located on position 16th - %16$n
.
There will be a lot of bytes that need to be read from the server so we wrap it into nice script