Skip to content

Instantly share code, notes, and snippets.

@Cerebro92
Created August 25, 2022 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cerebro92/88a3f19da5ce4643f4baedbc03d5f965 to your computer and use it in GitHub Desktop.
Save Cerebro92/88a3f19da5ce4643f4baedbc03d5f965 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract EtherStore {
mapping(address => uint) balances;
bool internal locked;
// modifier noReentrant(){
// require(!locked, "No re-entrancy");
// locked = true;
// _;
// locked = false;
// }
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount, "Insufficient balance");
(bool sent, ) = msg.sender.call{value: _amount}("");
balances[msg.sender] -= _amount;
require(sent, "Failed to send Ether to recepient");
}
function getBalance() public view returns (uint){
return address(this).balance;
}
}
contract Attack{
event Log();
EtherStore public etherStore;
constructor(address _etherStoreAdddress) {
etherStore = EtherStore(_etherStoreAdddress);
}
fallback() external payable{
emit Log();
// if (address(etherStore).balance >= 1 ether){
// etherStore.withdraw(1 ether);
// }
}
// receive() external payable{}
function attack() external payable {
require(msg.value >= 1 ether, "Require atleast 1 ether");
etherStore.deposit{value: 1 ether}();
etherStore.withdraw(1 ether);
}
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