#7 — Ethernaut Challenge 7— Force
Understanding the code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Force {/*
MEOW ?
/\_/\ /
____/ o o \
/~____ =ø= /
(______)__m_m)
*/}
Well…empty contract… hmm what do we do?
Objectives:
- Send ether to the contract even though it is an empty contract
How to hack the contract?
- Fire up the console, and let’s check the current balance of the contract by typing in await getBalance(contract.address). It shows the balance as 0. Cool.
- Open up Remix IDE, and let’s write some simple code:
contract Attack{
constructor(address payable _target) payable {
selfdestruct(_target);
}
}
This contract Attack has only one function: the constructor that executes as soon as the contract is deployed. The constructor takes in a parameter _target which is the target address we will be attacking. We marked the function payable to be able to send ether to the contract.
The method selfdestruct takes in the target address as a parameter, and the contract destroys itself after forcefully sending the ether to the specified address. That’s how this works.
3. On Remix IDE, select Injected Provider — Metamask under environment. Under value, change it to 2 Wei and deploy the contract with the target address as the instance address from Ethernaut and paste it in and hit deploy!
4. To check the balance, go back to Ethernaut and on the console type in await getBalance(contract.address) and you should see ‘0.000000000000000002’ as the balance of the contract. Well done.
5. Congratulations! Your smart contract has forcefully sent some ether to an empty contract using the selfdestruct method.
If you found this blog helpful, please follow and clap for more similar content!
Thank you for reading this far.