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

  • Nikkei Hits Record High, Up 37.87% Year-to-Date

    On June 16, the Nikkei 225 index briefly rose by 1% to 70,020.68 points, marking the first time it surpassed the 70,000-point threshold. It ultimately closed up 0.13% at 69,404.50 points, setting a new all-time closing high with a year-to-date increase of 37.87%. The Bank of Japan announced today that it will raise the policy interest rate by 25 basis points to 1%, the highest level in 31 years.

  • Bank of Japan Deputy Governor Shinichi Uchida: Japan's Economy Has Moderately Recovered

    Bank of Japan Deputy Governor Shinichi Uchida stated that Japan's economy has moderately recovered, although some sectors have shown signs of weakness. The central bank will continue to raise policy interest rates based on developments in economic activity, prices, and financial conditions.

  • Bank of Japan Deputy Governor Shinichi Uchida to Hold Monetary Policy Press Conference in Ten Minutes

    Bank of Japan Deputy Governor Shinichi Uchida will hold a monetary policy press conference in ten minutes.

  • WGC Survey: More Central Banks Plan to Increase Gold Reserves

    On June 16, the World Gold Council (WGC) announced that 45% of the central banks surveyed expect to increase their gold holdings in the next 12 months, up 2 percentage points from a year ago. In the annual survey conducted by the WGC from February 5 to May 19, 54% of the 74 central banks indicated that their gold holdings would remain unchanged, while 1% expected a decrease. Most responses were received after the outbreak of conflict in the Middle East in late February, which led to rising oil prices and a decline in gold prices. The WGC's global central bank director stated that central banks remain enthusiastic about gold, and the recent drop in gold prices has not changed their views. Furthermore, the WGC reported that 93% of respondents indicated they already hold gold, up from 81% a year ago. Among the various reasons for holding gold, as many as 90% of respondents cited gold's strong performance during times of crisis. Other key reasons include long-term value storage and portfolio diversification. Respondents from emerging markets and developing economies (85%) place greater emphasis on gold as a hedge against geopolitical risks. As some central banks continue to shift their gold reserves, 9% of respondents reported increasing their domestic gold reserves in the past 12 months, up from 5% last year; 10% indicated they have diversified their overseas gold reserve locations, up from 2% last year. In the next 12 months, 7% of central banks plan to increase domestic storage, and 9% plan to diversify overseas storage locations.

  • SpaceX's Market Value Surpasses $2.5 Trillion, Exceeding Total Cryptocurrency Market Value

    On June 16, SpaceX showed strong performance after its listing on Nasdaq, with its stock price rising significantly, increasing by 19.6% on the second trading day. Its market value has surpassed $2.5 trillion. According to Coingecko data, the total market value of the cryptocurrency market is approximately $2.34 trillion. SpaceX's market value now exceeds that of the entire cryptocurrency market.

  • AI Version of Alipay Officially Launched, Ant Group Concept Stocks Strengthen

    On June 16, Alipay officially launched its AI version, becoming the world's first super app to complete a full-scale AI transformation. On the day of the product launch, Ant Group concept stocks collectively strengthened, with rapid capital inflow and significant profit effects in the sector. As of now, several component stocks have risen over 3%. Among them, Nanwei Software and Hejing Technology have increased by over 5%, Jinshi Technology by over 4%, and Xiexin Energy by over 3%. From a strategic perspective, Alipay has already completed its positioning at the data level. Currently, the number of AI payment users has surpassed 100 million, and the total number of intelligent payment transactions has exceeded 300 million, making it the only large-scale commercial AI-native payment infrastructure globally. Industry insiders compare this event to the mobile payment revolution 12 years ago, when the industry landscape was rewritten due to WeChat's strategic positioning during the Spring Festival. Now, with Alipay taking the lead in fully establishing its presence in the AI era, it may signify a new phase in the competition for super app AI entry.

  • HYPE Surges Over 10% in a Day, Currently Priced at $71.569

    On June 16, market data showed that HYPE surged over 10% in a single day, currently priced at $71.569. Previously, there were reports that Arthur Hayes allegedly bought back 47,000 HYPE tokens.

  • DeepSeek Reportedly Completes Over $7 Billion Financing

    On June 16, according to The Information, DeepSeek has completed over $7 billion in financing, with this round valuing the company at over $50 billion.

  • Reserve Bank of Australia Holds Rates Steady

    On June 16, the Reserve Bank of Australia kept the interest rate unchanged at 4.35%, in line with market expectations, following three consecutive meetings of rate hikes.

  • Nikkei 225 Index Surpasses 70,000 Points for the First Time

    The Nikkei 225 Index has surpassed 70,000 points for the first time, reaching a historic high with a cumulative increase of 39% this year.