FAUSTCTF 2020 - marscasino

Marscasino is a Python Flask service, which allows users to play on Mars’ casino! Users can register, to then (theoretically) have an access token sent to their IP address. Once logged in, they can sell articles, play roulette, and get vouchers. Finally, there is the option for the organizers to donate money to an account.

Step 1: Registration

This bugged me at first; the CTF was run over IPv6. But, using a socket with AF_INET means we can only use IPv4; so that code could never work to send the registration token to another team. However, paying a bit more attention, we can see that in line 19 of the snippet, we see that the variable error is assigned the code; and the variable error ends up being rendered in the page later on. Hence, instead of trying to leak the token via IPv4, we can just extract it from the page.

So, the first part of our “marscasino client” does just that

Interlude: Flag storage

We found the second bug (the XOR one) before the start of the CTF, but were left guessing where flags might be. It turned out that the gameserver would sign up for an account, and then have an item for sale, giving it a random price well above the 50 coins a user got for initial signup. Hence, we needed a way to get more coins, which is how all the bugs actually worked.

POST /home HTTP/1.1
Host: [fd66:666:85::2]:7777
Content-Length: 58
Content-Type: application/x-www-form-urlencoded
Connection: close

item=FAUST_XwjnsABVA33habMAAAAATOoAlfrubapx&item_cost=5122


Bug 1: Game 1 (Roulette)

We exploited this bug as the second bug, but let’s start with it in either case.

This is obviously roulette, but with a twist. Essentially, as highlighted in the abbreviated code, if the user sends as the field the value 0, we always win 36x of our bet. Hence, it’s very easy to make money ;-)

Given that we cannot go over 100000 coins and also given that we start with 50 coins, it suffices to just win this game two times:

The actual exploit is a bit more involved, as we stored previously bought flags in a Redis instance to avoid unnecessary overhead, and also filled up the coins when we had too little money. But you get the idea…

Bug 2: Game 2 (Voucher)

We actually exploited this bug first, also giving us first blood for the service in the CTF.

So, looking at the core functionality, we provide some bet to the service, where the value is then generated using a random number between 0 and 1.3 to calculate the worth. What is more interesting, of course, if how the voucher is then generated. Using the key we get from the database in line 6, it’s just the code that is begin XORed with the key, which yields the final voucher code. Checking create_key, we can see that the key generated by the application is always exactly 12 bytes long. Hence, given a known part of the XORed code that is at least 12 chars long, we can simply extract the XOR key, and use it to generate our own vouchers.

Bug 3: Donations (not exploited by us in the CTF)

The (last?) bug is the in the donations feature.

Without trying to understand if the VerifingKey is somewhat weak, there is another bug in the service here. Assuming only the gameserver knows the SigningKey, we can leverage any donation we receive ourselves.

The check in line 13 attempts to ensure that the requested hostname is the same as the IP address contained in the donation. However, the service itself does not check if the provided Host header is actually the IP address of our vulnbox or not. Hence, we can simply use a donation that was made to us, register the corresponding username with other teams, and use the donation code. Since old users are also deleted after 5 minutes, we could have just used the same user over and over again.

Since the donation method has no checks for double-spending, we could have easily called this over and over again to get sufficient funds.

Fixing the bugs (properly, or in CTF style)

For game1, we simply disabled the use of the number 0 for betting, by redirecting the user to / whenever they attempted to bet on 0. For game2, we deployed an even dirtier fix: instead of using 12 bytes for the XOR key, we used 11. Since the assumption in the CTF is that nobody attempts to manually attack others, this was sufficient to throw off any exploits. The properly solution would have been to use some form of signature on the voucher, but this fix took about 1s :-)

Finally, for the donation (which we did not exploit, but saw in our traffic towards the end of the CTF), it would have been sufficient to ensure that the Host header sent to the service actually matched our IP address. Interestingly, we could only see legitimate use of the feature about 10 times, stopping at 17:37. So, turning the feature off would have also done the trick.

Summary

Overall nice service, which unfortunately had a small bug in it, which lead to the orgas pushing an update to all teams at some point. It was a bit sad to see the donation feature only used so sparsely, as my initial check in the first hour or so of the CTF revealed it was never used.

In total, we managed to get 4,778 flags on this service, the most of any team in the CTF on the service (incl. first blood)