Cover photo

Mr Steal Yo Crypto - Safu Vault

Disclaimer

This is not a walkthrough of every contract or code of the challenge. I am sharing my notes and resources I have used to complete this challenge, as well as some lessons I think are useful to take away after completing the challenge. I highly recommend you finish the challenge yourself first and only use this as additional content.

Notes

  • Since we start with 10_000 USDC I tried to figure out a way to manipulate the calculation uint256 r = (balance() * _shares) / (totalSupply()) in withdraw by transferring some USDC directly, but could not find a way.

  • deposit seems like it is susceptible to first deposit attack / inflation attack since it just mints shares when totalSupply() == 0 and does not mint them to address(0) like Uniswap does

    • We can’t use this however since in the test file somebody already deposited 10_000 USDC in the vault

  • depositFor takes in an arbitrary token address as an input parameter

    • Allowing a user to specify which token to use with IERC20(token) is never a good idea, because there is always a possibility for an attacker to create their own token and use it in a malicious way.

    • As soon as a user can specify which external function a contract will execute, it can be used in a malicious way

Attack Contract

  • We can create a new contract where we call depositFor and input token as that contract where we have created a custom transferFrom function

    • In transferFrom we will transfer USDC to SafuVault and call depositFor again to reenter the contract

  • Checkout my comments on depositFor function in SafuVault for a better understanding of how funds increase with every reenter - here

  • Also I highly suggest upping the verbosity when executing the test file to see all function calls

Good Takeaways

  • If there are more than one way to deposit / withdraw funds make sure all of them do it in the same way, and make sure that way is not exploitable

  • Compare opposite functions like deposit and withdraw - do they do the inverse of each other ?

Resources