Ethernaut Level 9— King
The Ethernaut is a Web3/Solidity based wargame inspired on overthewire.org. Here’s the solution to the Level 9— 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 xDSuch 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.
So my task is prevent the next player from becoming the next king. Let’s look at the target contract:
As can be seen, the
receive function calls
transfer to give the prize to the “abdicating” king. On Ethereum blockchain every contract can refuse to receive the funds (with the exception of funds coming from the selfdestructing contracts). So, if I’ll create a contract that rejects any payments, it’ll always make the
receive function to fail and therefore will be a king forever. An example attacking contract may look like the following:
The attack can be run using a JS script:
That’s it, after the transactions go through, there is no way to become a next king, since the prize payment will always fail. I can submit the challenge.