Ethernaut: King

Ethernaut: King

My take on CTFs

CTFs constitute a crucial part in the process of becoming a successful security researcher as they require from you an eye for details, a good understanding of solidity and great technical skills to perform the attack and capture the flag. The most talented security researchers are great at CTFs, at solving them of course and sometimes at designing them. CTFs will not turn you into a great web3 security researcher over night, but it will surely arm you with good enough technical skills to be able to write decent coded PoCs to have your finding validated and maybe get selected for report.

What is Ethernaut?

Ethernaut is a CTF(Capture The Flag) developed by the openzeppelin team that you most propbably already heard of. If you haven't, consider taking a look at this roadmap https://www.0xjarix.com/if-i-had-to-start-again/
This CTF gathers 31 challenges for the moment, this number keeps increasing so check their website every now and then: https://ethernaut.openzeppelin.com/
Maybe you'll design your challenge one day and send it to the openzeppelin team.

Also why you here?

Of all the kinds of articles I publish, CTF writeups are those I wish you read the least. I am a big advocate of giving everything the time it needs, if you cannot solve a challenge that you know for a contains an intentional bug in such a small codebase, do not expect to do really well in the contests. There are 2 reasons why someone can fail at solving a challenge, and when I say 'fail' I mean giving up and looking at the writeups, knowing damn well these CTFs are not time-bounded. So if you failed you either:

  • aren't ready for this challenge yet and that is most probably due to the fact that you skipped some steps in the roadmap
  • are lazy, you read the challenge, read the codebase, maybe not enough times, you had some assumptions maybe, you might have identified some entrypoints or some conditions to bypass or break, but you did not give it enough time, you did not allow yourself to succeed and that's a shame
💡
StErMi provided us with a github repo to solve ethernaut challenges in foundry, you can fork it, remove his solutions and try solving the challenges just like I'm doing
GitHub - 0xjarix/foundry-ethernaut: My solutions to the ethernaut CTF
My solutions to the ethernaut CTF. Contribute to 0xjarix/foundry-ethernaut development by creating an account on GitHub.

King

The contract below represents a very simple game: whoever sends it an amount of ether that is larger than the current prize becomes the new king. On such an event, the overthrown king gets paid the new prize, making a bit of ether in the process! As ponzi as it gets xD

Such a fun game. Your goal is to break it.

When you submit the instance back to the level, the level is going to reclaim kingship. You will beat the level if you can avoid such a self proclamation.

Goal

Remain the king by preventing anyone from being the king

Reasoning

  1. In order for us to become the king we have to send a certain amount of ETH greater or equal to that sent by the current king in order to become one, there's no other way, the attack is costly, we know the value to send, it's: prize
  2. Now that we are king, we submit the instance, the level will try to reclaim kingship by sending to it a certain amount of ETH >= prize, we have to prevent them from happening, we're only involved when we get sent the prize that the wannabe king is trying to set, this call can revert if the king declines the payout by reverting upon receipt of the new prize that will never be one since we're going to revert
💡
Key Takeaway
Implement the pull over push pattern (withdraw() function that can only be called by the owner so that the king appoint does not interfere with the king intentions)