DEAL20% OFF
Pre Black FridaySALE
--
Days
--
Hours
--
Minutes
--
Seconds
Book a call
DEAL20% OFF
Pre Black FridaySALE
--
Days
--
Hours
--
Minutes
--
Seconds
Book a call
GLITCH40% OFF
Price Glitch40% OFF
00
Days
00
Hours
05
Minutes
00
Seconds
Claim 40% Off
GLITCH40% OFF
Price Glitch40% OFF
00
Days
00
Hours
05
Minutes
00
Seconds
Claim 40% Off
All articles

Ethernaut Level 5 Walkthrough: Token

Shiran Sukumar 4 min read
Ethernaut Level 5 Walkthrough: Token

Hello Everyone! My name is Shiran Sukumar, I am a student at Metana. I wanted to share with you all a walkthrough on how to approach and solve an Ethernaut challenge. 

Ethernaut is a fantastic learning resource for Solidity and smart contract security. In Ethernaut Level 5, the Token challenge, we're introduced to a deceptively simple token contract. Our mission? Exploit a vulnerability to acquire more than the 20 tokens we’ve been initially allocated.

Understanding the Vulnerability

The Token contract seems straightforward:

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.0;

contract Token {

    mapping(address => uint256) balances;

    uint256 public totalSupply;

    constructor(uint256 _initialSupply) public {

        balances[msg.sender] = totalSupply = _initialSupply;

    }

    function transfer(address _to, uint256 _value) public returns (bool) {

        require(balances[msg.sender] - _value >= 0);

        balances[msg.sender] -= _value;

        balances[_to] += _value;

        return true;

    }

    function balanceOf(address _owner) public view returns (uint256 balance) {

        return balances[_owner];

    }

}

As we walk through this code let's make note of several key lines that will help us establish a vulnerability. 

  1. We check the solidity version: 0.6.0
  2. The transfer function allows us to move tokens around
  3. We can check our balance using the balanceOf function.  

In Solidity version 0.6.0  there are no automatic under/over flow checks and this specific contract has not used any safeguards (like SafeMath) against such attack. The critical flaw lies in the transfer  function. Notice how it only checks if the sender has sufficient balance (balances[msg.sender] - _value >= 0) before performing the transfer. This creates a potential for an integer underflow. What is an integer underflow? 

The Underflow Exploit

  1. Our player account starts with 20 tokens.
  2. Imagine we try to transfer 21 tokens. Mathematically, our balance should become negative.
  3. However, unsigned integers in Solidity cannot store negative values. If a subtraction results in a negative number, it wraps around to a huge positive value! (Think of a buffer to visual why this happens) 

The Attack

Cool, so now you get the approach. How do we actually execute this exploit? This guide assumes you are already familiar with Ethernaut challenges. Otherwise you will need to learn how to set up a wallet, get test ether, and connect to Ethernaut. 

  1. Connect to Ethernaut: using your wallet (MetaMask, Rainbow, etc.) , connect to the Ethernaut Level 5 instance and select Token (challenge 05) 
ethernaut level 5
walkthrough
token
  1. Get the Contract Instance: Use the provided interface to obtain a reference to the vulnerable Token contract.
get the contract instance
  1. Check your initial balance:  Call balanceOf()  function with your address, you can verify you have 20 tokens (you’ll see an object with words containing the value 20) 
check your initial balance
  1. Call the transfer function:  Call transfer with a receiver address and a value exceeding your current balance (e.g., transfer 21 tokens to a random address).
  • Construct the function call
construct the function call
  • Verify your request and sign your transaction.
Verify your request and sign your transaction.
  • Wait for the transaction to complete. You can take the tx Hash and look at it on Etherscan. 
Wait for the transaction to complete. You can take the tx Hash and look at it on Etherscan. 
  1. Massive Balance: Check your balance. You'll have a massively inflated token amount due to the underflow!
massive balance
  1. Submit your Instance: Click that Subit button on the interface and…..Celebrate and look at all those rainbows! You just completed Etherenaut Level 5!
submit your instance


Key Takeaways: Ethernaut Level 5

  • Integer Underflow/Overflow: Always be mindful of potential underflows and overflows when dealing with unsigned integers. Implement safeguards to prevent them. If you must use version 0.6.0, explore using tools like SafeMath. Now outdated, these concepts are important to know. 
  • Security Mindset: Even seemingly basic contracts can have hidden vulnerabilities. A security mindset during development is paramount. You can always do more research, consult experts, and practice writing secure code. Staying up-to-date on best practices and ensuring your own security mindset is the best preventive measure. 

Ready for the next Ethernaut challenge? Click to check out the previous ethernaut challenge and see what's next in our series!

faq

If you want to build and audit smart contracts professionally, Metana's Web3 Solidity bootcamp takes you from Solidity fundamentals to advanced security, gas optimization and Yul.

FAQs

What is Ethernaut Level 5: Token about?

  • It is a blockchain-based puzzle involving smart contract manipulation to progress through levels in the Ethernaut game.

How can I solve Ethernaut Challenge 5: Token?

  • Solving Level 5 requires understanding Ethereum smart contracts, specifically how token balances are managed and manipulated.

What skills are necessary to complete Ethernaut Level 5?

  • Players need a basic understanding of Ethereum, smart contracts, and potentially some knowledge of Solidity programming.

Are there any tools that help in solving Ethernaut Level 5?

  • Tools like Remix, MetaMask, and Solidity compilers are essential for interacting with and testing Ethereum smart contracts.

What common mistakes should you avoid in Ethernaut Level 5?

  • Common mistakes include not verifying contract details thoroughly and misunderstanding the contract's functions and permissions.
Share
Shiran Sukumar

Written by

Shiran Sukumar

Shiran Sukumar is a software engineer and blockchain contributor who writes on smart contract arithmetic safety and token standards. He provides actionable guides on preventing numerical underflows and protecting contract balances, reinforcing best security practices for Solidity developers.

You may also like

Ethernaut Walkthrough

Ethernaut Level 20 Walkthrough: Denial

Ethernaut Level 20: Denial focuses on a vulnerability related to denial-of-service (DoS) attacks . In this article, we'll break down the Denial contract, identify its vulnerabilities, and explain how the Hack contract…

Georgi PlamenovJuly 31, 2024Read
Ethernaut Walkthrough

Ethernaut Level 10 Walkthrough: Re-entrancy

Understanding re-entrancy is crucial for anyone developing or auditing Ethereum smart contracts. Failure to properly handle re-entrant calls can result in the loss of funds or the manipulation of contract state by…

Georgi PlamenovJune 7, 2024Read
Ethernaut Walkthrough

Ethernaut Level 3 Walkthrough: Coin Flip

The Ethernaut Level 3 , called "Coin Flip," is a lesson on the weak spots in how smart contracts handle chance and prediction. This article walks you through the Coin Flip game, showing where the contract has a weak…

Sven DaneelApril 2, 2024Read
Ethernaut Walkthrough

Ethernaut Level 7 Walkthrough: Force

Being relatively new to the Web3 space and having a keen interest in Security has naturally led to me participating the Ethernaut Challenge hosted by OpenZeppelin. For those unfamiliar with these challenges, each…

Jarrod PyneMay 8, 2024Read
Ethernaut Walkthrough

Ethernaut Level 14 Walkthrough: Gatekeeper Two

Ethernaut challenges , comparable to a Web3-themed hacking Capture The Flag (CTF) competition, offer a dynamic environment for diving into Ethereum and Solidity programming. Each level, including Ethernaut Level 14 ,…

Janos BarnaJune 22, 2024Read
Ethernaut-Level-8-Walkthrough-Vault
Ethernaut Walkthrough

Ethernaut Level 8 Walkthrough: Vault

Ethernaut challenges, akin to a hacking Capture The Flag (CTF) for Web3 enthusiasts, provide an immersive platform to explore Ethereum and Solidity programming. Each challenge presents a unique smart contract puzzle,…

Rupam RoychoudhuryMay 19, 2024Read
More Ethernaut Walkthrough articles →

Talk to an advisor

Book a call with a real human before the next cohort fills.

Speak with an advisor. We'll map the right program to your goals - and lock in your enrollment offer.

20% off enrollment offer

20% OFF - limited-time enrollment offer.

Applied automatically when you book a call before the timer hits zero.

--
Days
--
Hours
--
Minutes
--
Seconds
  • Guarantee: Job or 100% money back
  • Expert-curated curriculum
  • On-demand mentor support
Advisor Elena RodriguezAdvisor Sarah JenningsAdvisor David Chen
Real advisors · reply within 2 hours

Book your call

Free