#7 — Ethernaut Challenge 7— Force

Understanding the code:

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?

  1. 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.
  2. Open up Remix IDE, and let’s write some simple code:

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.

--

--

I am a student in a university in India, I talk about web3 tech and blockchain because I am a web3 enthusiast!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rahul Pujari

I am a student in a university in India, I talk about web3 tech and blockchain because I am a web3 enthusiast!