Beanstalk exploit demo

This is a demo to replay the beanstalk exploit happened on Apr-17-2022

https://etherscan.io/tx/0xcd314668aaa9bbfebaf1a0bd2b6553d01dd58899c508d4729fa7311dc5d33ad7

Installation & Run

npm install
npx hardhat run scripts/execute.js

Explanation

From the analysis of the following transactions

https://etherscan.io/tx/0xfdd9acbc3fae083d572a2b178c8ca74a63915841a8af572a10d0055dbe91d219

https://etherscan.io/tx/0xf5a698984485d01e09744e8d7b8ca15cd29aa430a0137349c8c9e19e60c0bb9d

https://etherscan.io/tx/0x68cdec0ac76454c3b0f7af0b8a3895db00adf6daaf3b50a99716858c4fa54c6f

https://etherscan.io/tx/0xcd314668aaa9bbfebaf1a0bd2b6553d01dd58899c508d4729fa7311dc5d33ad7

The following steps were executed

  1. swap 73 ETH worth of beans and deposit the beans to Beanstalk in order to create a proposal.
  2. submit proposal to Beanstalk (at this time the malicious proposal contract is not created yet the exploiter use create2 to control the future created contract so that nobody will find the what the contract actually does after he submit the proposal)
  3. After 1 day of goverance period, create malicious proposal contract in step 2
  4. flash loan 350,000,000 DAI , 500,000,000 USDC, 150,000,000 USDT from AAVE
  5. flash loan 32,100,950 from Uni Beans pool
  6. flash loan 11,643,065 LUSD from Sushi LUSD-OHM pair
  7. add 350,000,000 DAI , 500,000,000 USDC, 150,000,000 USDT to 3CRV pool
  8. exchange 15,000,000 3crv token for 15,251,318 LUSD
  9. add 964,691,328 3crv to Beans3Crv
  10. add 32,100,950 Beans & 26,894,383 LUSd to BeansLusd Curve pool
  11. deposit 795,425,740 Beans3Crv token to Beanstalk protocol for voting power
  12. deposit 58,924,887 BeanLusd token Beanstalk protocol for voting power
  13. vote for the proposal created on step 4
  14. execute emergencyCommit
  15. remove 874,663,982 Beans3Crv liqudity for 1,007,734,729 3Crv
  16. remove 60,562,844 BeansLusd liquidity for 28,149,504 LUSD
  17. return 11,678,100 to Sushi Lusd-Ohm Pair
  18. return 32,197,543 to Uni beans pool
  19. exchange 16,184,690 LUSD to 16,184,690 3crv
  20. remove 511,959,710 3Crv for 522,487,380 USDC
  21. remove 358,371,797 3Crv for 365,758,059 DAI
  22. remove 153,587,913 3Crv for 156,732,232 USDT
  23. return AAVE DAI, USDC & USDT flashloan
  24. remove Beans-WETH 540,716,100,968 liquidity for 10,883 ETH and 32,511,085 Beans
  25. swap 15,443,059 DAI for 15,441,256 USDC
  26. swap 37,228,637 USDC for 11,822 ETH
  27. swap 6,597,232 USDT for 2,124 ETH

There are several reasons that make this exploit possible.

  1. This is a governance and flashloan attack. Beanstalk allow emergency commit to a proposal that has a majority of voting (2/3) after a one day of period.
  2. Majority of voting power can be accquired by flashloan stable assets and stake on Beans Curve pool.

Disclaimer

This codebase is for demonstration purposes only

License

MIT