Beginner's Luck
Last updated
Last updated
Web, 989 points.
Can you help me to win the flag ? I bet you can't ..
We were given a website along with its sourcecode (PHP).
The site greets us with a page in which we can click on a "Generate" button. Everytime we click on it, a random token is generated, and the site says "Better luck next time!". We are given 10 attempts ; after those, the site says "Max Attempts Reached" and our session is reset.
Taking a look at the source, here is what we can read.
Every time we click on the button, our client generates the random 100-character token, and then sends it via POST. Our goal is to find the correct token.
Let's take a look at the source files we are given : index.php, play.php and reset.php.
When we first connect to index.php, our session is initialized and a random 100-character token is created for our session. We are not given the details of the generator function, though, but since they make our client generate random tokens on lowercase letters, we can infer the session token is the same. Right?
A table users
stores (ip, token) couples. Then in play.php, when we send a token via POST :
An SQL query checks if there exists an entry for our (ip, token). If there is, we are invited to input our token in a page called flag.php (which is not present in the sources), so that we can claim our flag.
The following line is prone to a very obvious SQL injection.
We don't know the exact output of the SQL request, so we cannot run a "UNION" type of SQL injection, but we do know whether it returns something or not. Indeed, when there exists such a couple (ip, token), the database returns at least a row and the site displays "True", "Click here and use the token to get your flag". Otherwise, it displays "Better luck next time". We can use this information to run a blind SQL injection.
Let's check by sending a pretty basic injection first in $_POST['val']
:
The site returns "True". It worked! Let's try to run another injection :
It works too: the token is 100 characters long. Good. Now we're able to write a small script that will brute-force each character individually thanks to the SUBSTRING function of MySQL.
There's an issue, though. Our session is reset and destroyed after 10 requests. We can't find a 100-character token with only 10 binary requests...
Our solution was to use two different IPs with a teammate. One person would be running the injection, but looking for the token associated to the other person's IP. Making sure that other person kept their session alive, they could then submit the token without increasing the number of attempts at all.
Our payload looks like this :
Where i is between 1 and 100, and c in the lowercase letters charset. Except it was not the case. We ran the injection once and found many characters in the token that were actually not letters. We then ran the injection again with lowercase letters + digits, and it worked. Those bastards had fooled us with that javascript generator! :D
Here is the exploit in Python.
And we get our friend's token, for instance, qx9tiuvxniog4qfkulftatkvysgiosw4qwppqcevks1f98hujnejxp6w8dvnvsjdfytw0xicha9h9g1it7simo4lrryea99gys2q
.
Input it in flag.php and get the flag. Enjoy!
Note: the exploit could have been much faster if we tested bits in the token instead of looping through a charset. Since there are 36 characters, which is approximately 5 bits, we could have been able to retrieve a character using only 6 requests instead of 36 worst case scenario. The payload would have been more complicated though, running some conversions and stuff.