Ethernaut Puzzle 07 Force
This question provides an empty smart contract called Force
, which doesn’t have a receive
or fallback
function. As a result, if I try to transfer or send ether to this smart contract, it will be reverted. However, the question requires that the balance of this smart contract’s address be greater than 0, which means we still need to find a way to transfer some ether into it.
We are going to introduce a very tricky function in Solidity called selfdestruct
.
The selfdestruct
function in Solidity is a pre-defined function that allows a contract to destroy itself and transfer its remaining ether balance to a specified address. When a contract is destroyed using selfdestruct
, all of its code, data, and storage are removed from the blockchain.
The selfdestruct
function takes a single argument, which is the address to which the contract’s remaining ether balance will be transferred. This address is specified as a parameter to the function call. The selfdestruct
function can only be called by the contract itself, and only once. Once the function is called, the contract will be immediately destroyed and all remaining ether will be transferred to the specified address.
It’s worth noting that once a contract is destroyed using selfdestruct
, it can no longer receive any transactions or send any messages. The contract’s address will still exist on the blockchain, but all attempts to interact with it will fail. Therefore, it’s important to ensure that the specified address in the selfdestruct
call is a valid and active address.
The selfdestruct
function can be useful in a number of scenarios, such as when a contract is no longer needed and its resources can be reclaimed, or when a contract needs to be upgraded and its functionality transferred to a new contract. However, it should be used with caution, as any ether remaining in the contract after the selfdestruct
call cannot be recovered.
After this introduction of selfdestruct
function, we know all we need to do is to create our own smart contract and invoke our smart contract’s function to destroy it and transfer the value to the target address.
1 | // SPDX-License-Identifier: MIT |
Remember when you deploy this smart contract, the value
should be greater than 0 so that it can deployed successfully and be transferred to the target address.