Ethernaut Lvl 0 Walkthrough: ABIs, Web3, and how to abuse them

Detailed Walkthrough

This level requires you to guess a secret password in order to “get cleared” to move on.

  1. Notice that Ethernaut passed a secret _password into the constructor, when it created your contract instance:
function Instance(string _password) public { 
password = _password;
}

2. This password is stored as a public string variable

string public password;

3. All public, basic variable types in Solidity have an auto-generated getter function. This means you can directly read this not-so-secret password by typing into the console:

await contract.password()

You can use async/await to work with Web3 promises with more ease

4. To pass this level, simply call the final authenticate function and pass in the retrieved password, via the console:

await contract.authenticate("[password here]");

You’ll be modifying storage in the authenticate function, so expect to pay some gas when calling this transaction.

5. Finally, you should be able to double check if you’ve passed this level:

await contract.getCleared();

Key Security Takeaways

  • All functions and variables stored on the blockchain are viewable by the public
  • Never store passwords directly inside a smart contract, (not even as private variables, as we’ll learn shortly)

read original article here