Euler Finance’s 196 Million Flash Loan Exploit | Learn how to re-do the hack

alt

On 13th March 2023, Euler Finance suffered a significant hack resulting in the loss of around $196 million in assets. The attacker carried out multiple flash loan attacks, and the breakdown of stolen assets includes USDC, stETH, WBTC, and DAI.

Tokens Stolen

 

Analysis

The attacker executed several transactions as follows.

Transactions involved in the hack.

 

One of the hacker transactions (related to the DAI token). https://etherscan.io/tx/0xc310a0affe2169d1f6feec1c63dbc7f7c62a887fa48795d327d4d2da2d6b111d

For a deeper understanding, let’s recreate this hack.

Step 1: Developing the smart contract used by the hacker based on the on-chain transactions.

1.1 The first step involved borrowing 30 million DAI through a flash loan from Aave. This can be done by using the following contract where excuteHack is used to request the flash loan and the excuteOperation is called by the flash loan contract of Aave.

1.2 Then, deployed a smart contract and transferred 20 million DAI to it. Let’s add this operation in the executeOperation function as follows.

1.3 The smart contract was then used to deposit 20 million DAI and mint 195 million eDAI / 200 million dDAI. After repaying 10 million DAI, the borrower minted 195 million eDAI / 200 million dDAI and donated 100 million eDAI to make the position liquidatable and changed the conversion rate, also added the liquidation smart contract deployment script to the function.

1.4 Then called the liquidate function, initiating the liquidation process and obtaining 310 million dDAI and 250 million eDAI. Finally, the attacker called the withdraw function, obtaining 38.9 million DAI, which was used to repay the 30 million DAI borrowed through the flash loan. They profited 8.87 million.

1.5 Putting everything together. You can also find the code on GitHub.

Final smart contract

 

Step 2: We will use BuildBear to fork the Ethereum Mainnet to perform the Hack.

2.1 To make our lives easier we have BuildBear.io , just head out to BuildBear.io and create a private Testnet by forking Ethereum from 16817993. which was 1 block before the hack was performed.

Forking Ethereum Mainnet

 

2.2 You can add the private Testnet to your MetaMask wallet by clicking on ‘Add to MetaMask’ on the BuildBear.io website. So that we can sign and send transactions from Wallet.

Dashboard

 

2.3 After that, you can visit the faucet on the Testnet, connect your wallet, enter the amount of BB Eth you want, and click on ‘Get BB Eth’. This will give you the necessary Testnet ether to perform the simulation.

Faucet

Step 3: Deploying the Smart Contract

3.1. Navigate to the Remix Online IDE website.

3.2. Create a new contract and paste the Smart Contract we have developed, you can also find it over here 👉 Github

3.3. Compile the Smart Contract, by using the following details:

Compile smart contract details

 

Please note that the ABI of the Contract is available only once the Contract is compiled. You can see in the above image the ABI which can be copied. Please keep this handy. We will need it momentarily.

3.4. Select the “Deploy and Run Transactions” tab: and deploy the contract as follows:

Deployment of smart contract

 

  • Make sure that you update the Environment to “Injected provider — MetaMask” | CRITICAL,

Click on the “transact” button to deploy the contract; once done, you will see something similar to the following:

Deployed contract

 

3.6. Copy the address of the Contract and visit the Blockchain Explorer for your personal forked Testnet (link available on the dashboard page at home.buildbear.io) and locate your contract.

3.6.1. Submit the ABI of the contract for ease of interaction.

You can visit your contract page on BuildBear’s Blockchain Explorer and then visit the Contract tab. You should see something similar to the following:

Explorer contract page

 

Submit the ABI that we copied from Step 3.3; once done, you should see the Read and Write Contract buttons made available to you on the Contract Page:

Contract page one’s ABI is submitted.

 

Without any future due click on connect to web3 and click on the write of excuteHack function to perform the hack. ( MetaMask may take 10secs to process and show transaction confirmation screen).

Transaction details

 

Call the getdai() function and transfer the remaining DAI in the contract to our address.

getdai function

 

We have received 8.8 million DAI.

We have successfully recreated the hack 🎉🎉.

Now you know exactly how the hack was performed !

The vulnerabilities that allowed this hack to happen.

1. Lack of liquidity checks on the donateToReserves function:

The donateToReserves function enables Euler users to deposit funds into a reserved address using both Debt Tokens (DToken) and Equity Tokens (EToken). Unfortunately, the function does not check or confirm the liquidity status of borrowers, which can result in under-collateralized leverage. Essentially, users can donate their Equity Tokens to the reserve while their Debt Tokens remain unchanged, leading to technical bad debt. Hackers exploited this vulnerability by using a liquidation contract to withdraw funds from the protocol.

donatetoReserve function of Euler Finance

 

2. The Healthscore Flaw:

Euler Finance assigns health scores to accounts, with insolvent accounts being allowed to keep collateral without repaying outstanding debt. This was done through the computeLiqOpp function. However, this logic can be exploited by attackers to conduct under-collateralized leverage.

computeLiqOpp function of Euler Finance

 

We share developer's resources every single day, follow us on Twitter, and LinkedIn and Join the Telegram group if you haven’t done yet.

And please give us a clap 👏 if you like our work.

This is for educational purposes only.

About BuildBear:

BuildBear is a platform for testing dApps at scale, for teams. It provides users with their own private Testnet to test their smart contracts and dApps, which can be forked from any EVM chain. It also provides a Faucet, Explorer, and RPC for testing purposes.

BuildBear aims to build an ecosystem of tools for testing dApps at scale for the teams.

Authors: Chandan

Let’s get started then, Shall we?