DEV Community

hamzairshad02
hamzairshad02

Posted on

Ethernaut Level 7 Walkthrough - Force

This level seems like a cruel joke. There is literally no code at all inside the contract and just a cat meowing at us wtf. Stuff like this makes me suicidal and what does this type of behavior called? Self Destruct. And there exists a concept of self destructing a contract in Ethereum.

How do I got to know that I have to self destruct the contract? I honestly had to google a lot since the only helpful hint was “Sometimes the best way to attack a contract is with another contract” so i learned that selfdestruct() method lets you destroy your smart contract and move all remaining Ethers to another address so i will just be needing another contract to move all the ether from that in this given level contract to win. This justifies the tier 3 difficulty this level has.

Now the name of this level makes sense. The contract itself has no method of receiving any ether so we will selfdestruct() our contract and Force this level’s contract to ultimately accept our remaining ether since it will have no choice but to accept it.

So let’s start by coding up our contract as follows

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Enforcer {
    constructor () payable { 
    }

    function getBalance() public view returns (uint256) {
      return address(this).balance;
    }

     function suicidesquad() public {
      selfdestruct(payable(address(0x25205608cb59bf58c1D2440388480c9B8d69DB11)));
    }
}
Enter fullscreen mode Exit fullscreen mode

In this contract, three functions are coded. First is a payable constructor() which lets you send some ether into this contract while deploying it. Second is getBalance() which will show you the balance present in your contract. Last is the suicidesquad() function which calls in the selfdestruct() method to destroy this contract and force our given level contract to accept the ether since we mentioned its address in the method. To know the address of the given level contract you can simply do it by the following command.

Image description

Now deploy your contract with an initial value of 10000000000000 wei which equals to 0.00001 eth. I am using Remix IDE so this is my configuration while deploying the contract.

Image description

Then use the getBalance() function to check that you have 0.00001 eth in your contract. Then simply use suicidesquad() function to destroy the contract and force the given level contract to accept the 0.00001 eth and win the level.

You can verify that the level contract has accepted and received the eth by the following command

Image description

Now just click on “Submit Instance” button and see your goody good winning banner.

Top comments (0)