or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
On May 8th an attacker 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). According to the report, this issue was not fixed.
The
fallback()
function implemented in the Alpha FinanceBank
contract (also known asibETH
) allows any address to deposit Ether into the contract, which modifies the return value of theBank.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 theBank
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 theibETH
state by depositing ETH through thefallback()
function and still withdrawing it from the Alpha FinanceBank
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()
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 callsBank.withdraw()
,share.mul(totalETH()).div(totalSupply())
is used to estimate the user share from the total ETH holdings of theBank
. Rari Capital relied on the same computation inAlphaPoolController.getBalance()
andAlphaPoolController.withdraw()
.
The attacker was able to manipulate
Bank.totalETH()
by sending a significant amount of ETH to the Alpha FinanceBank
contract using a direct transfer and still being able to withdraw it due to a flaw/feature in theBank.work()
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.
The attacker deploys a malicious token contract to handle most of the attack logic.
The attacker creates a fake SushiSwap token pair (ETH/Malicous Token) and add liquidity to it.
The attacker calls the
Bank.work()
function using theSushiswapGoblin
contract address and a special payload that contains theStrategyAddTwoSidesOptimal
contract address and the malicious token contract address. Note that both of theSushiswapGoblin
andStrategyAddTwoSidesOptimal
contracts:The
SushiswapGoblin
contract callsexecute()
on theStrategyAddTwoSidesOptimal
contract of Alpha Finance.StrategyAddTwoSidesOptimal
contract calls the malicious tokenapprove()
function that is the main trigger of the attack.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 callingRariFundManager.deposit()
, which at one point down the call chain callsBank.totalETH()
from Alpha Finance to compute the amount of REPT minted, as shown in the following screenshot of the transaction trace:Inside the same
approve()
function in the malicious contract, ETH is sent to the Alpha FinanceBank
through the fallback() function. The key to the attack resides here, the funds sent will allow the attacker to modify the return value ofBank.totalETH()
in a way that will allow them to withdraw more ETH than they initially deposited in the Rari Ethereum Pool contract.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 FinanceBank.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.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 themsg.sender
(attacker) due toBank.work()#L162
.The attacker calls the
donate()
function in the malicious contract which: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:
It is debatable whose responsibility it is to check for such an
ibETH
pool exploit. We have considered severalibETH
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.