09. King — Ethernaut

ThongTran
CoinsBench
Published in
2 min readOct 4, 2022

--

Stop King’s Ponzi

In this challenge, our mission is to break the contract such that no one could ever claim the kingship. (this kind of pattern is kinda popular somewhere on CTFs)

The contract logic is straightforward:

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”.

However, the point is on the line king.transfer(msg.value). The is two cases where this execution could get broken:

1. king is a contract that does not receive ether: it neither has fallback() nor receive()

2. king is a contract that can receive ether, using fallback() or receive() (says fallback()) but the logic inside that function consumes more gas than 2300 gas (amount of gas that transfer() forwards)

=> If the current king has one of above attributes, the contract is broken.

To solve this challenge, we first need to claim kingship and then stop everyone from claiming our kingships

Prepare a contract like this (just named is Queen because Queen is King’s wife lol)

pragma solidity 0.8.9;contract Queen{  function stopKing(address payable king) external payable{    (bool s,) = king.call{value: King(king).prize()}(‘’);    if(!s) revert();  }  fallback() external payable{    revert();  }}

Just execute Queen.stopKing(<instance-address>) with value == prize to complete the challenge.

Btw, the result of this contract is that the previous king gets the prize and then contract is in DoS status => We don’t have any profit, just the previous king has lol

Have a fun game!

--

--