DEV Community

Erhan Tezcan
Erhan Tezcan

Posted on

Ethernaut: 8. Vault

Play the level

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

contract Vault {
  bool public locked;
  bytes32 private password;

  constructor(bytes32 _password) public {
    locked = true;
    password = _password;
  }

  function unlock(bytes32 _password) public {
    if (password == _password) {
      locked = false;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Here we recall the golden rule: everything on the blockchain is there for everyone to see, and it will always be there. As such, we can find out the password that was used in construction of the contract. There are actually two ways to do it:

Find the password by looking at the contract creation transaction: The contract creation code is composed of contract ABI encoded and the parameters. Therefore some of the digits we see there at the end are constructor arguments. We know that there is only one argument: password (32 bytes). In hexadecimals it is 64 characters, so we can check the final 64 characters of the contract creation code and that is the password. Note that while calling the unlock function you should add 0x at the beginning of the string.

Find the password by looking at the storage variables: Looking at the Vault contract, the storage is read from top to bottom. EVM has 2 ^ 256} slots with a 32-byte value stored in each. At the top, we have bool public locked and after that we have bytes32 private password;. These variable are at index 0 and 1 in the storage respectively. We can therefore read the password via:

await web3.eth.getStorageAt(contract.address, 1)
Enter fullscreen mode Exit fullscreen mode

and simply give the result as the parameter to contract.unlock(...).

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️