DEAL20% OFF
Pre Black FridaySALE
--
Days
--
Hours
--
Minutes
--
Seconds
Book a call
DEAL20% OFF
Pre Black FridaySALE
--
Days
--
Hours
--
Minutes
--
Seconds
Book a call
GLITCH40% OFF
Price Glitch40% OFF
00
Days
00
Hours
05
Minutes
00
Seconds
Claim 40% Off
GLITCH40% OFF
Price Glitch40% OFF
00
Days
00
Hours
05
Minutes
00
Seconds
Claim 40% Off
All articles

Ethernaut Level 12 Walkthrough: Privacy

Alexis Amoi 3 min read
Ethernaut Level 12 Walkthrough: Privacy

Ethernaut Level 12 titled "Privacy" challenges the concept of "private" data on the blockchain. In this level, you'll encounter a locked contract, and your objective is to unlock it. The key, however, isn't so private after all! It lies in the way the Ethereum Virtual Machine (EVM) stores data. This walkthrough will guide you through the process.

Understanding Storage Slots

Imagine a shoebox with compartments. The EVM stores variables similarly, allocating 32 bytes (256 bits) to each compartment called a storage slot. The EVM is space-efficient; smaller variables like booleans (1 byte) or uint8s (1 byte) can share a slot with other variables as long as the total size doesn't exceed 32 bytes. This level serves as a valuable lesson in understanding how the EVM stores data.

Analyze the Contract

First, take a look at the Privacy.sol contract. You'll notice a variable named data declared as a bytes32 array. There's also a function called unlock that takes a bytes16 argument as input.

Ethernaut Privacy level console walkthrough with tips and contract code

EVM Storage Shenanigans

The EVM packs variables efficiently to save storage space. Smaller variables like locked (bool) and denomination (uint8) share the same storage slot as the first element of the data array (data[0]).

Unlocking the Secret

To unlock the contract, we need the value stored in data[2]. That value is located at slot 5. However, the EVM stores it as bytes32. We need to cast it to bytes16 to match the unlock function's input.

Here's a step-by-step guide to unlocking the contract:

  1. Use the terminal in the console:
  2. Get the Hex string of the data[2] at slot 5: await web3.eth.getStorageAt(contract.address, 5). This will give us a string of bytes32. We want to convert it to bytes16.
  3. Put that hex string in a constant: const b32 = '0x5d6655fba48ea25651c1cad326e3a62ded205cb4464b71e0cf64951ecfe605d2'.
  4. Find the length: b32.length (This will return 66).
  5. Convert it to bytes16 (half the length, excluding 0x): const b16 = b32.slice(0, 34).
  6. Use that value to call the unlock function: contract.unlock('0x5d6655fba48ea25651c1cad326e3a62d').
Ethernaut Level 12 Privacy contract unlock steps with MetaMask confirmation
  1. Check the status of the locked boolean to see if it changed to false: await contract.locked().
  2. Finally, you can submit the instance. Congrats on completing this level!
Ethernaut challenge 12 Privacy walkthrough showing successful contract unlock steps and completion message

Conclusion: Ethernaut level 12 (Privacy)

This challenge highlights that nothing on the blockchain is truly private, not even data declared with the private keyword. The EVM's storage layout can reveal information you might think is hidden. So, when building smart contracts, be mindful of how data is stored and accessed. Rely on encryption or other secure mechanisms for truly sensitive information.

Bonus Tip: Security best practices involve keeping user data off-chain whenever possible. Consider using decentralized storage solutions for sensitive information that doesn't require constant on-chain interaction.

To practise this hands-on with senior engineers, explore Metana's Ethereum developer course, a project-based Solidity bootcamp backed by a job guarantee.

faq

FAQs:

  1. Can this exploit be used to steal funds from real contracts?

No, directly exploiting storage packing like this wouldn't be effective for stealing funds from real contracts. This technique relies on specific contract vulnerabilities and wouldn't work universally.

  1. What are some best practices to avoid similar vulnerabilities?

When writing smart contracts, be mindful of storage slot packing. Avoid storing sensitive data directly on-chain, and consider using encryption or alternative storage solutions for truly private information.

  1. Is there another way to solve this challenge without the Truffle console?

Yes, it's possible to solve this challenge using other tools or platforms that allow interaction with deployed contracts and storage slots.

  1. What if I don't know the storage slot locations beforehand?

Analyzing the contract code and understanding Solidity variable sizes can help you predict storage slot usage. Alternatively, some tools offer functionalities to explore storage slots on deployed contracts.

  1. What are the limitations of the "private" keyword in Solidity?

The private keyword in Solidity only affects code visibility within the contract itself. It doesn't guarantee data privacy on the blockchain due to the EVM's storage layout and potential for analysis.

Share
Alexis Amoi

Written by

Alexis Amoi

Alexis Amoi is a Solidity engineer and security contributor who explores the nuances of token standards, byte packing, and access control. Through practical CTF teardowns, Alexis helps aspiring smart contract engineers transition from basic Solidity syntax to defensive smart contract development.

You may also like

Ethernaut Walkthrough

Ethernaut Level 3 Walkthrough: Coin Flip

The Ethernaut Level 3 , called "Coin Flip," is a lesson on the weak spots in how smart contracts handle chance and prediction. This article walks you through the Coin Flip game, showing where the contract has a weak…

Sven DaneelApril 2, 2024Read
Ethernaut Walkthrough

Ethernaut Level 7 Walkthrough: Force

Being relatively new to the Web3 space and having a keen interest in Security has naturally led to me participating the Ethernaut Challenge hosted by OpenZeppelin. For those unfamiliar with these challenges, each…

Jarrod PyneMay 8, 2024Read
Ethernaut Walkthrough

Ethernaut Level 6 Walkthrough: Delegation

In this level, we will familiarize ourselves with delegatecall and fallback . This serves as a preliminary step towards understanding more advanced concepts, like the proxy pattern.This article will clarify delegatecall…

Vinh TranMay 3, 2024Read
Ethernaut Walkthrough

Ethernaut Level 18 Walkthrough: Magic Number

As you may already know, The Ethernaut , from OpenZeppelin, is a series of challenges designed to teach and test your understanding of smart contract security. Well, today was the time for Ethernaut Level 18 , titled…

Ana RojoJuly 15, 2024Read
Ethernaut Walkthrough

Ethernaut Level 9 Walkthrough: King

In this challenge , we'll dive into smart contracts' payment vulnerability and essential security practices to prevent such issues. By the end of ethernaut level 9 , you'll be equipped to both exploit and safeguard the…

Vinh TranMay 20, 2024Read
Ethernaut Walkthrough

Ethernaut Challenge 13 Walkthrough: Gatekeeper One

TL;DR: Ethernaut Challenge 13, Gatekeeper One, tests knowledge of Ethereum transactions and gas mechanics. The GatekeeperOne contract includes three modifiers: gateOne (ensures caller is a contract), gateTwo (requires…

Georgi PlamenovJune 19, 2024Read
More Ethernaut Walkthrough articles →

Talk to an advisor

Book a call with a real human before the next cohort fills.

Speak with an advisor. We'll map the right program to your goals - and lock in your enrollment offer.

20% off enrollment offer

20% OFF - limited-time enrollment offer.

Applied automatically when you book a call before the timer hits zero.

--
Days
--
Hours
--
Minutes
--
Seconds
  • Guarantee: Job or 100% money back
  • Expert-curated curriculum
  • On-demand mentor support
Advisor Elena RodriguezAdvisor Sarah JenningsAdvisor David Chen
Real advisors · reply within 2 hours

Book your call

Free