Hello Ethernaut - Level 00
Tags: 100daystooffload, ctf, ethereum, ethernaut
Published: 2023-07-26 04:57:56

## Problem statement

The Ethernaut is a Web3/Solidity based wargame inspired by [overthewire.org](https://overthewire.org/), played in the Ethereum Virtual Machine.

This level, [Hello Ethernaut](https://ethernaut.openzeppelin.com/level/0), walks you through the very basics of how to play the game.

## Solution

Since this level is all about to learn the platform it's pretty straight forward. Here is the gist of it:

* Install the [MetaMask](https://metamask.io/) extension
* Get some Ether, this [faucet](https://sepoliafaucet.com/) (I'm using the Sepolia network) worked for me, which is provided by [Alchemy](https://www.alchemy.com/)

At the ethernaut page connect the MetaMask wallet and open up the developer tool in your browser (F12). Call the help method in the console window:

```javascript
help()
```

__Output:__

| (index) | Value |
|---------|-------|
|player | 'current player address' |
|ethernaut | 'main game contract' |
|level | 'current level contract address' |
|contract | 'current level contract instance (if created)' |
|instance | 'current level instance contract address (if created)' |
|version | 'current game version' |
|getBalance(address) | 'gets balance of address in ether' |
|getBlockNumber() | 'gets current network block number' |
|sendTransaction({options}) | 'send transaction util' |
|getNetworkId() | 'get ethereum network id' |
|toWei(ether) | 'convert ether units to wei' |
|fromWei(wei) | 'convert wei units to ether' |
|deployAllContracts() | 'Deploy all the remaining contracts on the current network.' |

Looks good, everything seems to be working so far. Next step is to create a new contract instance by clicking on the button, __Get new instance__. This will result in some feedback in the console window. Wait until an instance address has been returned.

Lets do a quick inspection of the contract object by just typing in the variable name in the console and push enter:

```javascript
contract

/*
Expand the n {methods: {…}, abi: .....} row to see all
available methods/properties on the contract object:

* abi
* address
* allEvents
* authenticate
* call
* constructor
* contract
* estimateGas
* getCleared
* getPastEvents
* info
* info1
* info2
* info42
* infoNum
* method7123949
* methods
* password
* send
* sendTransaction
* theMethodName
* transactionHash
* [[Prototype]]
*/
```

Read through the list of methods/properties. Now you can start to interact with the contract object as the given instructions ask you to do.

```javascript
await contract.info()

// Output: 'You will find what you need in info1().'

await contract.info1()

// Output: 'Try info2(), but with "hello" as a parameter.'

await contract.info2('hello')

// Output: 'The property infoNum holds the number of the next info method to call.'

await contract.infoNum()

// Output: { length: 1, negative: 0, red: null, words: [42, ] }
// Since the object has the number 42 in one of its properties,
// I guess that we should call the method info42 because 
// it was also listed when we inspected the contract object earlier

await contract.info42()

// Output: 'theMethodName is the name of the next method.'

await contract.theMethodName()

// Output: 'The method name is method7123949.'

await contract.method7123949()

// Output: 'If you know the password, submit it to authenticate().'
// The contract object has a password method on it, lets
// call that one to see if it will return the password

await contract.password()

// Output: 'ethernaut0'
// Could be the password we are looking for

await contract.authenticate('ethernaut0')

// Output: ...sending transaction
// Nice
```

The final step is to click on the __Submit__ button on the page to submit the solution back to ehternaut. This will result in log messages of your success in the console window.

## Explanation

This is a web3 site which means in this case that the user will need a wallet to enable all features on the site. For example the button, __Get new instance__, will only be available if the wallet is connected.

The developers of the page has provided us with some objects and help functions which are mentioned in the description, however by using following code in the console you can list all objects available on the page:

```javascript
console.table(Object.keys(window))
```

From the output you can see the objects that are important for this level, _contract_ and _help_.

Before the creation of a new instance of the contract it will be initialized to the string __No contract set, go to a level and click 'Get new instance'__. 

When clicking on the button, __Get new instance__, we will start to interact with  the [Ethernaut contract](https://sepolia.etherscan.io/address/0xa3e7317e591d5a0f1c605be1b3ac4d2ae56104d6) on Ethereum (Sepolia network in this case) and call the function _createLevelInstance_. The call will deploy a new contract and therefore the need of Eth.

The address of the Ethernaut contract can be found in the console:

```javascript
ethernaut

// Output: An Ethernaut contract object which has
// a property with the contract address on the blockchain
```

The input data to _createLevelInstance_ is an address, [0x7E0f53981657345B31C59aC44e9c21631Ce710c7](https://sepolia.etherscan.io/address/0x7e0f53981657345b31c59ac44e9c21631ce710c7) which we also can look up on the etherscan. That input tells which contract to deploy for this level and the creator of the contract will be the wallet that is connected to the site.

By looking at the _Internal Transactions_ for the contract 0x7e0f...10c7, we can see details about the contract creation. Here it is possible to see what is given to the contract constructor.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa3hcebin2jgvp8u5522.png)

Looks like the password is given when creating the contract.

There will be some feedback when the deployment of the contract is done in the console window, and the contract object will be initialized with the deployed contract. This contract will also have an address that can be used to look up the contract on etherscan. However the code given when passing the challenge looks better than the decompiled version at etherscan.

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

contract Instance {

  string public password;
  uint8 public infoNum = 42;
  string public theMethodName = 'The method name is method7123949.';
  bool private cleared = false;

  // constructor
  constructor(string memory _password) {
    password = _password;
  }

  function info() public pure returns (string memory) {
    return 'You will find what you need in info1().';
  }

  function info1() public pure returns (string memory) {
    return 'Try info2(), but with "hello" as a parameter.';
  }

  function info2(string memory param) public pure returns (string memory) {
    if(keccak256(abi.encodePacked(param)) == keccak256(abi.encodePacked('hello'))) {
      return 'The property infoNum holds the number of the next info method to call.';
    }
    return 'Wrong parameter.';
  }

  function info42() public pure returns (string memory) {
    return 'theMethodName is the name of the next method.';
  }

  function method7123949() public pure returns (string memory) {
    return 'If you know the password, submit it to authenticate().';
  }

  function authenticate(string memory passkey) public {
    if(keccak256(abi.encodePacked(passkey)) == keccak256(abi.encodePacked(password))) {
      cleared = true;
    }
  }

  function getCleared() public view returns (bool) {
    return cleared;
  }
}
```

As you can see the query methods are _pure_ which means that we don't need to pay any fees.

When we finally call the _authenticate_ function we are updating the state of the contract (update of the _cleared_ variable if the given password is correct) and therefore a new transaction is done to the network.

Last action is the interaction with the Ethernaut contract again which submits the instance so it can keep track of the progress. Also this can be tracked in Etherscan.

At the end it should be three activities registered in your wallet.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i3sah1dzxxlexieg8w0.png)


## Resources

* [OpenZeppelin](https://www.openzeppelin.com/) - The creator of the CTF
* [The Ethernaut](https://ethernaut.openzeppelin.com/) - Main page for the Ethernaut CTF
* [Hello Ethernaut](https://ethernaut.openzeppelin.com/level/0) - This challenge
* [MetaMask](https://metamask.io/) - Crypto wallet extension
* [Sepolia faucet](https://sepoliafaucet.com/) - Faucet to retrieve Eth
* [Alchemy](https://www.alchemy.com/) - Provider of the Sepolia faucet
* [Ethernaut contract](https://sepolia.etherscan.io/address/0xa3e7317e591d5a0f1c605be1b3ac4d2ae56104d6) - The Ethernaut contract on etherscan
* [Etherscan](https://etherscan.io/) - is your friend