本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2024/3/29

Deep Dive: Flash Loan Vulnerability and Analysis of the 2023 Euler Finance Flash Loan Attack

n the fast-evolving domain of cryptocurrency and blockchain technology, opportunities and risks coexist, shaping the landscape of innovation. The  2023 Euler Finance flash loan attack  stands as a critical event, shedding light on the complex dynamics and vulnerabilities within the Decentralized Finance (DeFi) ecosystem. Delving into this incident provides essential insights for both seasoned participants and newcomers in the crypto space.

 

Unveiling the Incident

 

March 13, 2023, marked a significant moment in the crypto world as  Euler Finance , a leading DeFi lending protocol, fell victim to a catastrophic  flash loan attack , resulting in substantial losses totaling  $197 million . This event not only underscored the vulnerability of DeFi platforms but also highlighted the importance of understanding the intricacies of this evolving domain.

Understanding Flash Loans

 

At the core of this exploit lies the concept of  flash loans , a unique feature of DeFi. These loans, devoid of collateral requirements, allow borrowers to access capital without upfront investments. However, the caveat lies in the repayment, which must occur within the same transaction, failing which the entire process is nullified.

 

The Flash Loan Attack Scenario ( Example )

 

Flash loan attacks, while seemingly advantageous, harbor malicious potential. Bad actors leverage vulnerabilities within DeFi protocols to orchestrate manipulative schemes, inflate token valuations, or abscond with funds. The typical modus operandi includes:

 

1.  Loan Acquisition : The attacker secures a substantial sum through a flash loan.

2.  Exploitation of Weakness : Leveraging the borrowed funds, the attacker exploits vulnerabilities within the targeted protocol.

3.  Execution of Exploitative Maneuvers : Manipulation of markets, token prices, or outright fund embezzlement ensues.

4.  Prompt Loan Repayment : The entire sequence unfolds within a single transaction, culminating in the timely repayment of the flash loan, leaving minimal traces of foul play.

 

Amplifying Vulnerability in Euler Finance

 

1.  The vulnerability exploited in Euler Finance arises from how the platform allows donations to be performed without a proper account health check.

2. The flawed code introduced in eIP-14¹ brought about multiple changes throughout the Euler Ecosystem. The flaw lies in the first change performed to the EToken implementation (EToken::donateToReserves feature²).

3. The logic within the Liquidation module attempts to repay the full debt of the violator. However, if the collateral they possess cannot satisfy the expected repayment yield, the system defaults to whatever collateral the user has³.

4.  The assumption of this code block states that a borrower’s available collateral will be insufficient only when:

- This can happen when a borrower has multiple collaterals, and seizing all of this won’t bring the violator back to solvency.

5. This security guarantee is not upheld by the donation mechanism, which permits the user to create “bad debt” in the form of leverage that is uncollateralized by donating their EToken units without affecting their DToken balance⁴.

 

Core Problem

The core problem with the Euler Finance protocol is that it allows users to create artificial leverage by minting tokens and depositing them in the same transaction. This means users can mint more tokens than the protocol's collateral can cover. Additionally, a flaw in the donation mechanism allows users to donate their balance without proper checks, resulting in their debt remaining unchanged while their equity decreases. During liquidation, this can lead to bad debt for the protocol. Attackers exploit these flaws to create over-leveraged positions and then liquidate them within the same block, taking advantage of discounts applied during liquidation. This results in the attacker holding bad debt while the liquidator ends up over-collateralized.

Detailed Proof of Concept (POC)

1.  Loan Acquisition and Initial Deposit :

- The attacker borrows 50 million USDC from AAVE V2 and places it into their       account.

 - The deposit 30 million USDC into Euler Finance and receive approximately 29.4 million eUSDC tokens.

2.  Creation of Artificial Leverage :

 - The attacker generates artificial leverage by executing the following steps:

 - They create a synthetic leverage of 300 million eUSDC, resulting in the minting of around 294.3 million eUSDC and 300 million dUSDC.

     - After repaying 15 million USDC, they are left with 285 million dUSDC.

     - They repeat the process, creating another synthetic leverage of 300 million eUSDC, resulting in similar minting outcomes.

 

3.  Donation to Reserve Mechanism :

   - The attacker donates 150 million eUSDC to Euler's reserve, further complicating the platform's balance dynamics

At this stage, the exploiter's account shows the following balances:

- eUSDC: Approximately 588.6 million

- dUSDC: 585 million

4.  Exploitation of Vulnerabilities :

   - Leveraging vulnerabilities in Euler Finance's code, specifically the EToken::donateToReserves feature, the attacker manipulates the system to create uncollateralized leverage.

   - They exploit the flawed mechanism, creating an over-leveraged position and artificially causing it to go “under-water”.

5.  Self-Liquidation :

   - The attacker orchestrates the liquidation of their own position within the same block, taking advantage of the percentage-based discount applied during liquidation.

   - The liquidator incurs a significant portion of EToken units at a discount, while the attacker retains only the debt that matches the collateral acquired.

 6.  Outcome :

   - The attacker retains approximately 16,852,371 USDC, equivalent to roughly 16,010,752.95 USD, post-liquidation.

How to Prevent Flash Loan Attacks?

Considering the earlier examples, we can suggest a list of key methods to safeguard against flash loan attacks.

As these attacks originate from external smart contracts, it’s prudent to validate the addresses allowed to utilize flash loan functionalities. Ensure that external contract calls are authentic and secure and limit interactions exclusively to trusted contracts.

Employing thorough third-party smart contract auditing and validation is always a valuable security measure. Conduct comprehensive testing to identify potential vulnerabilities and collaborate with reputable auditing firms to review your contracts.

Utilize a variety of oracles to obtain the most precise and safe price information, considering that some oracles might be vulnerable. Implement time-weighted average price (TWAP) mechanisms to minimize the risks tied to price manipulation.

Regularly review and stay current with DeFi security best practices and vulnerability reports.

 

Biggest DeFi Hacks Due to Flash Loan Attacks:

 

1.  Cream Finance

   - Blockchain Protocol: Ethereum

   - Date: October 27, 2021

   - Exploit Amount: ~$130M

   - Platform Type: DeFi

 

2.  Fei rari

   - Blockchain Protocol: Arbitrum

   - Date: May 1, 2022

   - Exploit Amount: $80M

   - Platform Type: DeFi

 

3.  Pancake Bunny

   - Blockchain Protocol: BSC

   - Date: May 20, 2021

   - Exploit Amount: $45M

   - Platform Type: DeFi

 

4.  Alpha Finance

   - Blockchain Protocol: Ethereum

   - Date: February 13, 2021

   - Exploit Amount: $37.5M

   - Platform Type: DeFi

 

5.  Spartan Protocol

   - Blockchain Protocol: BNB Chain

   - Date: May 2, 2021

   - Exploit Amount: $30.5M

   - Platform Type: DeFi

 

6.  Xtoken

   - Blockchain Protocol: Ethereum

   - Date: May 12, 2021

   - Exploit Amount: $24M

   - Platform Type: Decentralized passive investing protocol

 

7.  Elephant Money

   - Blockchain Protocol: BSC

   - Date: April 13, 2022

   - Exploit Amount: $22.2M

   - Platform Type: DeFi

 

8.  bEarn

   - Blockchain Protocol: BSC

   - Date: May 16, 2021

   - Exploit Amount: $18M

   - Platform Type: DeFi

 

9.  Saddle Finance

   - Blockchain Protocol: Ethereum

   - Date: May 1, 2022

   - Exploit Amount: $11M

   - Platform Type: DeFi

 

Conclusion

The detailed POC underscores the systematic exploitation of vulnerabilities within Euler Finance's protocol, leading to substantial financial gains for the attacker. Understanding these intricacies is crucial for mitigating similar attacks in the future and reinforcing the security posture of DeFi platforms.

 

Author: Piyush Shukla

 

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More