DEV Community

hamzairshad02
hamzairshad02

Posted on

Ethernaut Level 1 Walkthrough - Fallback

As indicated by this level’s name, it utilizes a Fallback function. Now what a fallback function is defined below:

A Fallback Function is triggered whenever a method is called which is not present in the contract.

Now in our contract we can see the Fallback Function here.

receive() external payable {
    require(msg.value > 0 && contributions[msg.sender] > 0);
    owner = msg.sender;
  }
Enter fullscreen mode Exit fullscreen mode

How do we indicate this is the fallback function? Because it is a function with no ‘function’ keyword attached to it. If you see all the other functions you can see the ‘function’ keyword attached to them.

Now how many functions do we have in this contract. Each is explained below:

  • constructor(): This is the constructor of the contract which tells us that the contributions should be 1000 ETH in order to be the owner of the contract.
  • contribute(): This function indicates that you can contribute an amount less than 0.001 ETH. This also indicates that if your contributions are more than the current owner (which is 1000 ETH) then you can become the owner of the contract.
  • getContribution(): This function simply returns your contributed amount.
  • withdraw(): With this function you can withdraw all the available balance but it also uses an ‘OnlyOwner’ modifier which requires you to be the owner of the contract in order to withdraw.
  • recieve(): This is the Fallback function which indicates that your contributions should be more than zero in order to become the owner of the contract.

Now you get the clear picture of the contract. You can see that the contract requires you to contribute 1000 ETH in order to be its owner but its Fallback function says that you can become the contract owner if your contributions are more than zero. This is where the conflict arises and this is what we take advantage of so we can unlock the withdraw() function and take out all the balance from the contract.

So lets start by using the console and checking the address of the contract owner and our own player address. Note that you can use the help() to see all the options available in the console.

Image description

As you can see both the addresses are different and we have to be the owner of this contract.

You can also see the contributions of both by the following commands.

Image description

As you can see the owner’s contribution is 1000 ETH which is written in Wei and our contribution is 0 ETH so obviously we can’t be the owner.

Now let’s contribute something to the contract by the following command:

Image description

Now since we have contributed to the contract we can finally take advantage of the Fallback function by triggering it and become the owner of this contract since all it requires is to have a contribution more than zero and that’s what we did just now.

So in order to trigger it. Let’s use the same command as above but without any function present in the contract.

Image description

Notice how we didn’t use ‘contribute’ method this time which triggered the Fallback function and in this function it is only required to have a contribute more than zero to become the owner of the contract. So finally we have become the owner of this contract. Now this can be verified by the following command.

Image description

As you can see both the contract owner and player are the same so we have finally unlocked the withdraw function. So let’s utilize it by the following command.

Image description

Now just click the Submit Instance button and see your winning message.

Top comments (0)