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

  • Swiss Crypto Bank Amina: Listing Ethereum as a Security Could Cause Many Crypto Teams to Exit the Space

    Swiss encrypted bank Amina stated in the latest "Cryptocurrency Market Monitoring" report that classifying Ethereum as a security could not only bring risks to the entire cryptocurrency market, but also lead to many cryptocurrency teams exiting the field. This determination could hinder the development of the cryptocurrency market and potentially reverse progress made over the years. In addition, the US SEC is likely to delay its decision on the status of Ethereum, putting the cryptocurrency asset in a "gray area".

  • Ethereum has about $48.05 million in on-chain loan liquidation quota around $2,778

    According to Defi Llama data, there is approximately $48.05 million in on-chain liquidation volume for Ethereum around $2,778.

  • SoftBank Group to invest 10 trillion yen in "AI revolution"

    SoftBank Group (SBG) Chairman and CEO Masayoshi Son's "AI revolution" has begun. SoftBank Group plans to expand its business into industries such as data centers, robots, and power generation using AI semiconductors as a breakthrough. The expected investment amount could reach up to 10 trillion yen (approximately RMB 464.09 billion). American companies such as Microsoft are also making huge investments in the AI field, indicating a trend of global tech giants entering this growing field. (Nikkei News)

  • Ethereum has about $48.05 million in on-chain loan liquidation quota around $2,778

    On May 13th, according to DefiLlama data, there is approximately $48.05 million in on-chain loan liquidation volume for Ethereum around $2778.

  • The Philippine central bank has approved the PHPC, a stablecoin pegged to the peso

    The Central Bank of the Philippines has approved a stablecoin called PHPC, which is pegged to the Philippine peso and aims to promote cost-effective remittances. It is backed by cash and equivalents held by Philippine banks and will initially be launched on the Coins.ph platform, with plans to expand to other platforms. PHPC will be the first retail stablecoin backed by the peso and will offer real-time trading 24/7.

  • GBTC has seen a cumulative net outflow of over US$17.6 billion since the launch of the Bitcoin spot ETF

    On May 12th, according to Farside Investors data, the cumulative net outflow of GBTC since the launch of Bitcoin spot ETF reached 17.6329 billion US dollars. According to Coinglass data, GBTC's asset management scale has decreased to 17.647 billion US dollars.

  • The Ethereum network has currently destroyed more than 4.29 million ETH

    According to Ultrasound data, as of now, the Ethereum network has destroyed a total of 4,290,622.06 ETH. Note: Since the introduction of EIP-1559 in the Ethereum London upgrade, the Ethereum network will dynamically adjust the BaseFee of each transaction based on transaction demand and block size, and this portion of the fee will be directly burned and destroyed.

  • Türkiye prepares new encryption law to align with international standards

    Turkey is preparing to submit a new law to regulate cryptocurrency assets to the parliament. The law aims to align with international standards, reduce risks associated with cryptocurrency transactions, and implement strict regulation of cryptocurrency trading platforms by the Capital Markets Board. It will also ensure the safekeeping of assets, establish transparent platforms and customer relationships, and clearly define sanctions for non-compliant behavior. The legislative proposal is expected to be submitted to the parliament within a week.

  • Ripple CEO: The US government’s next target is Tether

    Ripple CEO Brad Garlinghouse recently stated in an interview that the next target of the US government is Tether, the stablecoin company responsible for the circulation of USDT. Some experts believe that Garlinghouse's statement is a very big insider report, and if it really happens, there will be a huge selling pressure in the USDT stablecoin market. In this case, the value of USDT will experience a significant decline.

  • Cointime May 12 News Express

    1.The number of Bittensor subnets for the AI ​​project will increase to 64, and 1024 subnets will be achieved this year2.Trader predicts Bitcoin price will reach $350,0003.vladilena.eth redeemed 1930 weETH from Zircult, suspected of selling4.Solana’s on-chain DEX transaction volume yesterday exceeded the sum of five chains including Ethereum, BSC, and Arbitrum5.RSS3 VSL locked-in amount surged in the past two days and is close to 200 million US dollars 6.The transaction volume of Club Key on friend.tech platform exceeded 1 million7.Lido has paid out more than 516,000 ETH in staking rewards, equivalent to approximately $1.51 billion8.1,000 BTC transferred from TronDAO to an unknown new wallet9.Report: Justin Sun deposited 120,000 eETH into Swell L2, worth $376 million10.1707.36 BTC have flowed out of Binance in the past 7 days