Appearance
S1C9: Password Protected
1. Problem
The contract has a password-protected function. You need to find the correct password to call mintFlag.
2. Cause
The password is stored in the contract's storage. Since all data on the blockchain is public, you can directly read the storage slot to obtain the password.
3. Solution
- Use
cast storageto read the contract's storage slots - Find the slot where the password is stored
- Use that password to call mintFlag
bash
# Read storage slots (try different slots)
for slot in 0 1 2 3 4 5; do
cast storage <CHALLENGE> $slot --rpc-url $RPC
done
# Use the discovered password
cast send <CHALLENGE> "mintFlag(string)" "found_password"
4. Pitfalls Encountered
Password Format: The password may be stored across multiple slots (if longer than 32 bytes), requiring proper concatenation.
5. Why the Pitfall Occurred
In Solidity, if a string type is <= 31 bytes in length, it is stored in a single slot (short string optimization). If longer, the current slot stores the length, and the data is stored in consecutive slots starting from keccak256(slot).
6. How to Resolve
First check if the rightmost byte of the slot value equals length * 2 (short string encoding). If not, read the consecutive slots starting from keccak256(slot).
7. Technical Takeaways
| Point | Explanation |
|---|---|
| Blockchain Transparency | All storage data is publicly readable |
| Short String Encoding | Length <= 31 bytes stored directly in slot, > 31 bytes uses keccak256 addressing |
| Storage Slots | Read using cast storage or eth_getStorageAt |