Cointime

Download App
iOS & Android

Hacking Private Data in Smart contracts | QuillAudits

Validated Project

In solidity, “Private” variables can only be read by the contracts and is not accessible to the outside world. But, it is possible to access these data from outside the blockchain. Let’s see how we can read private data from smart contracts.

Table of Contents:

1. State Variable Visibility2. Storage layout in EVM3. Accessing private data using Ethers4. A small challenge5. Remediation6. Reference

State Variable Visibility

According to Solidity Docs, There are three visibility modifiers that users can use: public, internal, or private.

  1. Public: Public means that the variable can be accessed by the contract and by other smart contracts.
  2. Internal: Internal means that the variable can only be used with the contract it is defined in and its subclasses.
  3. Private: Private means that the variable can only be accessed within the contract it is defined. Trying to access it outside this contract gives a compilation error.

In Solidity, “Private” variables are not accessible to the outside world. But wait, if private data can only be accessed within the contract then how can we access these data from outside of the blockchain?We will come to it but let’s first understand storage layout in solidity.

Storage Layout in EVM:

The EVM (Ethereum Virtual Machine) stores smart contract data in a large array with a length of 2**256 in slots on the blockchain. Each memory slot can hold up to 32 bytes of data. The EVM stores smart contract state variables in the order that they were declared in slots on the blockchain. The default value of each slot is always 0, so we do not need to assign a value to 0 when the new declaration is.

Smart contract storage is optimized to minimize space. If two or more variables fit into a single 32-byte slot, they are packed into the same slot, beginning on the right.

Look at the following example for a better understanding of how storage works in EVM.

Storage for Dynamic data types:

Mappings and dynamically-sized array types cannot be stored “in-between” the state variables before and after. Instead, they are considered to occupy only 32 bytes and a different storage slot that is computed using a Keccak-256 hash of the elements they contain.

Accessing Private data from Blockchain:

To access Solidity smart contract private data we can follow the following steps. Here we will be using ethers.js for extracting data.

  1. First, we need to read the contract and understand the order in which the state variables are declared. Let’s suppose we want to access slot0.
  2. We can use ethers.js to read the memory slots of the contract on the blockchain. Use the below function:await ethers.provider.getStorageAt(contract_address, 0);
  3. It will return hex encoded value, we can simply decode it or use ethers utils functions or some sort of hex decoder to decode it.

Ethers.js Script:

You can also use the following js script to fetch private data from the blockchain.

Guide for running script:

  1. Install ethers from npm: npm install ethers
  2. Get a rpc_url of the required network from alchemy and add it to the above script.
  3. Change the contract address and slot in the below script and simply run the js code. Decode the returned hex value.

A small challenge:

Goerli link: 0x4c77de8a0ec0390826f87f227bb8762ab2189194

Objective: Change the value of pwned to true. You can use the above script to extract private data.

Remediation:

In the current state of the blockchain, do not keep sensitive private data in smart contracts. The safest method to keep your private information safe is to avoid storing it on a public blockchain.

Further Reads:

Comments

All Comments

Recommended for you

  • Spot Gold Breaks $4,440/Ounce, Reaching New High Since June 5

    Spot gold has surpassed $4,440 per ounce, marking a new high since June 5, with an intraday increase of 1.66%.

  • Nasdaq 100 Futures Rise by 1%

    Nasdaq 100 futures rose by 1%; S&P 500 futures increased by 0.5%.

  • Bank of America Plans $250 Billion Investment in U.S. Digital and Infrastructure Projects

    On August 12, Bank of America announced the launch of a critical infrastructure financing initiative, planning to invest $250 billion in U.S. digital and infrastructure projects to celebrate the 250th anniversary of the founding of the United States. The initiative aims to strengthen and modernize the nation's infrastructure, support energy security, and enhance job opportunities and economic competitiveness.

  • Ploymarket: Market Probability of Fed Rate Hike in September Drops to 34% After CPI Data Release

    According to prediction market Ploymarket, the probability of a Federal Reserve rate hike in September has dropped to 34% following the release of CPI data. This is the lowest probability for a September rate hike since July 17, and is only half of what it was on that date.

  • Goldman Sachs: Fed May Keep Rates Unchanged Throughout 2026 as Inflation Eases

    Matheus Dibo, head of investment strategy for Europe, the Middle East, and Africa at Goldman Sachs, stated that the Federal Reserve is likely to maintain interest rates unchanged throughout 2026, with inflation risks expected to ease in the second half of the year. "Clearly, the market is still digesting the expectations for interest rate hikes, but we actually disagree with this view and believe that the Fed will keep rates unchanged for the foreseeable future," Dibo said in an interview on Wednesday. He noted that the inflation data released earlier this year was influenced by oil prices, the World Cup, and tariffs, but there are currently few signs that inflation will spread throughout the remainder of 2026. Dibo added that, given trends in the housing market, housing inflation should also ease. (Bloomberg)

  • BTC Falls Below $64,000

    Market data shows that BTC has fallen below $64,000, currently priced at $63,996.44, with a 24-hour decline of 0.52%. The market is experiencing significant volatility, so please ensure proper risk management.

  • Goldman Sachs: U.S. Consumer Price Index Performance in July is 'Encouraging'

    On August 12, Goldman Sachs stated that the performance of the U.S. Consumer Price Index in July is 'encouraging'.

  • US July CPI Year-on-Year Meets Expectations

    On August 12, it was reported that the US unadjusted CPI year-on-year for July recorded a 3.4% increase, the smallest rise since March, which aligns with market expectations. (Jin Shi)

  • Spot Gold Drops $50 in Short Term, Currently at $4,399 per Ounce

    Spot gold has dropped $50 in the short term and is currently reported at $4,399 per ounce.

  • Analyst: July Inflation Data Allows Waller to Breathe Easy

    On August 12, market analyst Chris Anstey stated that the overall and core CPI data for the U.S. in July fully met expectations. In simple terms, these monthly figures do not trigger an inflation alarm and are generally in line with the Federal Reserve's targets. Waller can breathe a sigh of relief for now. However, what he truly needs is for the August inflation report to deliver similar numbers, but it is still 'too early for that.' (Jin Shi)