Faycal Lalidji
    • 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
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
  • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    On May 8th [an attacker](https://etherscan.io/address/0xcb36b1ee0af68dce5578a487ff2da81282512233) was able to drain approximately 2600 ETH from the Rari Capital Ethereum Pool by leveraging the functions of the ibETH pool (`Bank` contract) of Alpha Homora V1, which is integrated in Rari Capital V2. The attack was performed over a series of approximately 200 transactions. This write-up provides a description of the technical details that made the attack possible and provides a step-by-step description of the attack. # Why the Attack Was Possible The issue that caused the hack of the Rari Capital protocol was known by Alpha Finance since it was raised during the PeckShield audit published on October 5th, 2020 (see issue 2.3, [link](https://github.com/AlphaFinanceLab/alphahomora/blob/master/audit_report/PeckShield-Security-Review-Alpha-Homora-v1.0.pdf)). According to the report, this issue was not fixed. The `fallback()` function implemented in the Alpha Finance `Bank` contract (also known as `ibETH`) allows any address to deposit Ether into the contract, which modifies the return value of the `Bank.totalETH()` function. However, any ETH sent to the contract directly will be lost (distributed between the LPs) since there will be no way to withdraw it back, meaning that attackers cannot use it normally to exploit the `Bank` contract, except for a single scenario, which is described below. The aforementioned issue in the PeckShield audit was not assessed correctly, the way that `Bank.work()` was implemented allowed the attacker to modify the `ibETH` state by depositing ETH through the `fallback()` function and still withdrawing it from the Alpha Finance `Bank` contract. To summarize the attacker exploited the following functions in Alpha Finance: - `Bank.fallback()` - `Bank.totalETH()` - `Bank.work()` By doing so the attacker was able to manipulate the Alpha Finance contract state in a way that impacted the Rari Capital Ethereum Pool contracts. # How Was the Rari Capital Ethereum Pool Manipulated? ​ Alpha Finance LP earnings rely on the [`Bank.totalETH()`](https://github.com/AlphaFinanceLab/alphahomora/blob/5017311b56acf64020cd834e2e82a9ad556fea68/contracts/5/Bank.sol#L99#L101) function to estimate the total interest accrued, anyone able to manipulate its return value will technically be able to attack any third party relying on it. Alpha Finance themselves relies on the `totalETH()` function to calculate ETH holdings of their users. When a user calls [`Bank.withdraw()`](https://github.com/AlphaFinanceLab/alphahomora/blob/5017311b56acf64020cd834e2e82a9ad556fea68/contracts/5/Bank.sol#L111#115), `share.mul(totalETH()).div(totalSupply())` is used to estimate the user share from the total ETH holdings of the `Bank`. Rari Capital relied on the same computation in [`AlphaPoolController.getBalance()`](https://github.com/Rari-Capital/rari-ethereum-pool-contracts/blob/75fb2562b8258a214256a5e938738d98cbd6ad05/contracts/lib/pools/AlphaPoolController.sol#L39#L41) and [`AlphaPoolController.withdraw()`](https://github.com/Rari-Capital/rari-ethereum-pool-contracts/blob/75fb2562b8258a214256a5e938738d98cbd6ad05/contracts/lib/pools/AlphaPoolController.sol#L56#L63). ​ The attacker was able to manipulate `Bank.totalETH()` by sending a significant amount of ETH to the Alpha Finance `Bank` contract using a direct transfer and still being able to withdraw it due to a flaw/feature in the [`Bank.work()`](https://github.com/AlphaFinanceLab/alphahomora/blob/5017311b56acf64020cd834e2e82a9ad556fea68/contracts/5/Bank.sol#L123#L163) function. It is important to note here that for the majority of contracts in DeFi, sending ETH directly to the contract without calling the designated deposit function will almost always lead to the sender losing their funds because they will not be able to withdraw it. ​ # Exploit Description The description below only includes the main steps that the attacker went through to execute the exploit. We deem the attack extremely complex, the attacker needed a high degree of knowledge over multiple DeFi protocols (i.e. Alpha Finance, SushiSwap, Rari Capital) especially Alpha Finance and SushiSwap. 1. The attacker deploys a malicious token contract to handle most of the attack logic. 2. The attacker creates a fake SushiSwap token pair (ETH/Malicous Token) and add liquidity to it. 3. The attacker calls the `Bank.work()` function using the [`SushiswapGoblin` contract address](https://etherscan.io/address/0x9eed7274ea4b614acc217e46727d377f7e6f9b24) and a special payload that contains the [`StrategyAddTwoSidesOptimal` contract address](https://etherscan.io/address/0x81796c4602b82054a727527cd16119807b8c7608) and the malicious token contract address. **Note** that both of the `SushiswapGoblin` and `StrategyAddTwoSidesOptimal` contracts: * were deployed by AlphaFinance on November 27th, 2020 and * were never used until the Rari Ethereum Pool hack on May 8th, 2021. 4. The `SushiswapGoblin` contract calls `execute()` on the `StrategyAddTwoSidesOptimal` contract of Alpha Finance. 5. `StrategyAddTwoSidesOptimal` contract calls the malicious token `approve()` function that is the main trigger of the attack. 6. The malicious `approve()` function in the token contract deployed by the attacker makes a flash loan from dYdX, deposits it in the Rari Ethereum Pool contract and receives REPT in exchange for that deposit. This is done by calling [`RariFundManager.deposit()`](https://github.com/Rari-Capital/rari-ethereum-pool-contracts/blob/75fb2562b8258a214256a5e938738d98cbd6ad05/contracts/RariFundManager.sol#L464), which at one point down the call chain calls `Bank.totalETH()` from Alpha Finance to compute the amount of REPT minted, as shown in the following screenshot of the transaction trace: ![](https://i.imgur.com/6PFRqv9.png) 7. Inside the same `approve()` function in the malicious contract, ETH is sent to the Alpha Finance `Bank` through the [fallback()](https://github.com/AlphaFinanceLab/alphahomora/blob/5017311b56acf64020cd834e2e82a9ad556fea68/contracts/5/Bank.sol#L243) function. The key to the attack resides here, the funds sent will allow the attacker to modify the return value of `Bank.totalETH()` in a way that will allow them to withdraw more ETH than they initially deposited in the Rari Ethereum Pool contract. 8. The malicious contract calls `RariFundManager.withdraw()` to redeem the previously minted REPT tokens using the dYdX flash loan. Since the attacker deposited Ether in step 7, the Rari Ethereum Pool will calculate a higher total balance due to the Alpha Finance `Bank.totalETH()` returning a higher balance than the same call returned in step 6, when REPT was minted. This means that significantly less REPT is burned than the amount that was minted in step 6, which leaves the attacker with the difference in REPT while getting back the same amount of ETH. 9. All the remaining logic is executed, the goblin call returns and `Bank.work()` continues its execution. The amount sent in step 7 is sent back to the `msg.sender` (attacker) due to [`Bank.work()#L162`](https://github.com/AlphaFinanceLab/alphahomora/blob/5017311b56acf64020cd834e2e82a9ad556fea68/contracts/5/Bank.sol#L162). 10. The attacker calls the `donate()` function in the malicious contract which: - Burns the extra REPT gained in step 8 in exchange for ETH. - Deposits the received ETH in step 9 back to the malicious token contract. 11. The attacker re-executes steps 3-10 multiple times, withdrawing more ETH every time from the victim contract (the Rari Ethereum Pool contract). # Conclusion Approximately 2600 ETH was stolen from Rari Capital on May 8th 2021. This was due to an exploit of a known bug reported on October 5th, 2020 in a public audit report and which was not fixed. In response to the report, Alpha Finance stated: > "The randomly received ether would not greatly interfere with the calculation. We leave it as is.” It is debatable whose responsibility it is to check for such an `ibETH` pool exploit. We have considered several `ibETH` integration implementations and we have yet to find one where this kind of attack is prohibited. This means that any Dapp that integrates with the Alpha Finance V1 (`ibETH` pool) or is currently using it, runs the risk of a similar exploit.

    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