Vault | OpenZeppelin’s Ethernaut Level 8 Walk-Through

Unlock the vault to win the level. It’s set to locked and password protected at deployment, this is done using the bool public locked; variable and the bytes32 private password; variable, the problem is that this data is stored on chain, allowing anyone to determine what is being stored.

Private data shouldn’t be stored on-chain or it should be properly encrypted first.
And SWC Registry give’s us some good insight on variables, private doesn’t equate to unreachable.

It is a common misconception that private type variables cannot be read. Even if your contract is not published, attackers can look at contract transactions to determine values stored in the state of the contract. For this reason, it’s important that unencrypted private data is not stored in the contract code or state.

SWC-136, Unencrypted Private Data On-Chain.

Let’s get to it.
Get a new instance, then copy and paste the contract over to Remix.

Get new instance & Copy the contract

New file vault.sol.

vault.sol

Compile, set your provider to MetaMask, copy and paste your instance address in the At Address box.

vault.sol At Address

Now we are going to use web3.eth.getStorageAt(contract.address, 1)
getStorageAt is a built in function in web3js

Remix terminal

This should return the 32bytes password that is stored in slot 1.

Return value

I accomplished the same in the Ethernaut console like so:

await web3.eth.getStorageAt()

Copy that return value and paste it into the unlock box, then transact.

unlock with return value

All that’s left to do is submit our instance.

Complete

Congratulations, we just learned about private data on-chain.

DAVE

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top