Daniil Ogurtsov
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Foundry for studying hacks ## Intro This article aims to explain how the Foundry smart contract development framework can be used in studying hacks. It can be a useful workflow for beginners who prefer studying hacks as a step in mastering Solidity skills. ## Setting up Foundry is a smart contract development framework, like Hardhat or Brownie. Like others, it allows the compilation and deploying smart contracts and projects, writing all the variety of necessary tests. The key advantages are: - it is fast - you write in Solidity, both smart contracts, tests, and script (no need to switch between Python/JS and Solidity) Follow this guide to install Foundry: https://book.getfoundry.sh/getting-started/installation The simplest way to build a project is running: ``` forge init ``` As a result, you will have this project structure. ``` ├── lib │   └── forge-std ... ├── script │   └── Counter.s.sol ├── src │   └── Counter.sol └── test │ └── Counter.t.sol ├── foundry.toml ├── README.md ``` This project has a basic smart contract and a simple unit test. We can run the test: ``` forge test ``` ## Forking By default, Foundry launches a network on a local machine. But you can easily customize to use any real network - Foundry allows forking networks, including specifying the exact block. In practice it is widely used for real-world tests - some projects prefer testing their project in the environment of real tokens and other projects. Here we should introduce the concept of **Cheatcodes**. Foundry has built-in precompiles - it treats some smart contract calls as reserved commands, allowing some magic not allowed in real networks. There is a huge list of available Cheatcode commands: https://book.getfoundry.sh/forge/cheatcodes?highlight=cheatc#cheatcodes Manipulating cheatcodes is the key to mastering Foundry. Now let's write a test: ``` pragma solidity ^0.8.13; import {Test, console} from "forge-std/Test.sol"; contract ForkTest is Test { function setUp() public { vm.createFork(MAINNET_RPC_URL); } function test_PrintBalanceCETH { address cETH = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; uint256 balanceOnCETH = cETH.balance; console.log("Balance on cETH: ", balanceOnCETH); } ``` We created `ForkTest` file, which inherits from Test (imported from `forge-std/Test.sol`). This is how we connect our Cheatcodes. It has `function setUp()` - this is a reserve function name. It will be run before every other test function. So here we can indicate operations that are the same for every test function in the file. Forking perfectly fits here: ``` vm.createFork("MAINNET_RPC_URL") ``` This cheatcode tries to find an RPC named "MAINNET_RPC_URL" in your `.env` file in the project directory. So we should configure an env file - you should add ".env" file to the project directory. Fill it with this line: ``` MAINNET_RPC_URL=https://eth.llamarpc.com ``` You can register on an RPC provider website to take the key (like Alchemy). Or you can google any publicly available RPCs (but remember that not all of them will allow forking). If your tests require multiple RPCs, you can indicate any necessary amount of them in your `.env` file. Technically it is possible to run multiple forks and switch between them in one test file, everything using just Cheatcode commands. So, having multiple RPCs is the case. When `function setUp()` sets a fork, it is time for tests. They must be named as `test_YourTestName`. ``` function test_PrintBalanceCETH { address cETH = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; uint256 balanceOnCETH = cETH.balance; console.log("Balance on cETH: ", balanceOnCETH); } ``` This function introduces a new Cheacode - `console.log()`. It is very handy and allows printing on your console. So, this test takes the mainnet cETH address and prints its ETH balance. To execute tests, run the following forge terminal command: ``` forge test ``` You will notice that you see nothing printed. That's because Foundry has multiple levels of script detailization. As described in Foundry docs: Level 1 (`forge test`): Only displays a summary of passing and failing tests. Level 2 (`forge test -vv`): Logs emitted during tests are also displayed. That includes assertion errors from tests, showing information such as expected vs actual. Level 3 (`forge test -vvv`): Stack traces for failing tests are also displayed. Level 4 (`forge test -vvvv`): Stack traces for all tests are displayed, and setup traces for failing tests are displayed. Level 5 (`forge test -vvvvv`): Stack traces and setup traces are always displayed So, you will see your `console.log()` if you run tests as: ``` forge test -vv ``` ## Forking to study hacks We will use this repository by SunWeb3Sec to study the library of hacks. https://github.com/SunWeb3Sec/DeFiHackLabs It is a large Forge repository with a long list of hack demonstrations. You can find all the tests in `src/test`. To the day of writing this article, the repository consists of more than 300 hacks. First of all, follow the setup instruction in the README of the repo, as cloning a repo has a different script. When you have it locally, take a look at the repo structure - this repo has many tests, but don't run everything with `forge test` - it will take a lot of time to run them all. Instead, use this: ``` forge test --contracts src/test/NAME_OF_THE_FILE.t.sol -vv ``` Or this one: ``` forge test --match-path src/test/NAME_OF_THE_FILE.t.sol -vv ``` ## Simple hack In this section we will go through one of the hacks - CowSwap exploit. https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/CowSwap_exp.sol This is the transaction of the hack. https://etherscan.io/tx/0x90b468608fbcc7faef46502b198471311baca3baab49242a4a85b73d4924379b As you can see an attacker managed to build a malicious calldata. So this hack will be very simple to demonstrate additional Foundry possibilities. First of all, look at the setUp() function: ``` function setUp() public { cheats.createSelectFork("mainnet", 16_574_048); vm.label(address(DAI), "DAI"); vm.label(address(swapGuard), "SwapGuard"); vm.label(address(GPv2Settlement), "GPv2Settlement"); } ``` This type of forking is almost the same. There is a small difference between: ``` vm.createSelectFork() and vm.createFork() ``` You can learn in deep here: https://book.getfoundry.sh/cheatcodes/forking `cheats` and `vm` have no difference for our purposes. But both `createSelectFork()` and `createFork()` can accept the second argument - the block number. As you can see for this attack it is `16_574_048`. Then you have this cheatcode: ``` vm.label(address(DAI), "DAI"); ``` It helps to read traces better. If traces in your console print address `0x6B175474E89094C44Da98b954EedeAC495271d0F` it will be displayed with the "DAI" label. It is handy in analyzing complicated traces. Then, you have the attack flow in the function `testExploit()`. Here is the question - who is the attacker in this test? For all tests, Foundry has a default `msg.sender` and `tx.origin`. For this simple attack, they are not changed, because everyone can make this attack, even some default address. ## More complex hack Here we will study this Hundred Finance hack. https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/HundredFinance_2_exp.sol The official post-mortem: https://blog.hundred.finance/15-04-23-hundred-finance-hack-post-mortem-d895b618cf33 It utilizes the inflation attack, a well-known attack vector. If you are not familiar, we recommend our article explaining the topic in deep. https://mixbytes.io/blog/overview-of-the-inflation-attack The exploit test is messy. First of all, it has three smart contracts in it. - contractTest - test script itself - ETHDrain - code of one of the attacker contracts - tokenDrain - same thing, but for tokens This is done to have all smart contract codes in one file. As the repo reserves one file for an attack. If the call is made from a deployed smart-contract it will be treated as usual - that this deployed smart-contract makes a call (`msg.sender` is changed according to EVM rules). In the attack script, we have these lines: ``` ... cheats.startPrank(HundredFinanceExploiter); hWBTC.transfer(address(this), 1_503_167_295); cheats.stopPrank(); ... ``` `startPrank()` is the extremely important cheatcode. It allows changing a `msg.sender` for the next calls. It ends with the `cheats.stopPrank()` which changes `msg.sender` back to the default address. The widely used alternative is `prank()`. It changes a `msg.sender` only for the next call. The script ends with taking a flashloan. But it is not the end, because it continues in the flashloan callback - at the function `executeOperation()`. This function runs a few attacks - one `ETHDrains()` and multiple `tokenDrains()`. You can find these functions below. Each of them follows the same pattern where some of the attack steps are placed either in `tokenDrains()` function or in tokenDrain smart contract constructor. Each step is followed by `console.log()` - as a result, the attack is very well documented. ## Further steps Now you are familiar with Foundry. You can use it in development or keep studying hacks in the same way as we did here.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully