Ethernaut level 5— Token

Shahaneri
2 min readJan 15, 2023

--

Objective

  • The goal of this level is for you to hack the basic token contract below.
  • You are given 20 tokens to start with and you will beat the level if you somehow manage to get your hands on any additional tokens. Preferably a very large amount of tokens.

Step 1:

  • Copy the code into a remix. And get a new instance.
  • See the state variable balances and see how we might update these balances to something greater than 20.
  • I see the balances updated inside the constructor and any other place to change the balances is inside the function transfer. First, check the balance and then we will update the balance.
  • The key to solving this challenge that in the solidity 0.6 there is no safe math is enabled so we will be able to perform underflow and overflow without any error.
  • so how can we exploit this in solidity 0.6 and it has no safe math checks.
  • If we call function transfer from another account that does not have any tokens and then for the 2 addresses we set this _to our account and then we will be able to increase tokens.
  • So let’s write the code using solidity 0.8
  • After writing the hack contract deploy on the meta mask with the contact address.
  • And last step is to check the balance of player .
  • You can see that our balance is 21 is greater than 20.
  • Submit the instance.
  • level pass!!!!!!!!!!!!!!!!!!

--

--