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

  • The total open interest of Bitcoin contracts on the entire network reached 31.41 billion US dollars

    According to Coinglass data, the total open position of Bitcoin futures contracts on the entire network is 487,500 BTC (approximately 31.41 billion US dollars).Among them, the open position of CME Bitcoin contracts is 143,600 BTC (approximately 9.23 billion US dollars), ranking first;The open position of Binance Bitcoin contracts is 109,400 BTC (approximately 7.07 billion US dollars), ranking second.

  • Bitcoin mining difficulty increased by 1.99% to 88.1T yesterday, a record high

    According to BTC.com data reported by Jinse Finance, the mining difficulty of Bitcoin has increased by 1.99% to 88.1T at block height 840,672 (22:51:52 on April 24), reaching a new historical high. Currently, the average network computing power is 642.78EH/s.

  • US Stablecoin Bill Could Be Ready Soon, Says Top Democrat on House Financial Services Committee

    The top Democrat on the U.S. House Financial Services Committee, Maxine Waters, has stated that a stablecoin bill may be ready soon, indicating progress towards a new stablecoin law in the U.S. before the elections. Waters has previously criticized a version of the stablecoin bill, but emphasized the importance of protecting investors and ensuring that stablecoins are backed by assets. Congressional movement on stablecoin legislation has recently picked up pace, with input from the U.S. Federal Reserve, Treasury Department, and White House in crafting the bill. The stablecoin bill could potentially be tied to a must-pass Federal Aviation Administration reauthorization due next month, and may also be paired with a marijuana banking bill.

  • Crypto mining company Argo mined 1,760 bitcoins last year and earned $50.6 million

    Crypto mining company Argo Blockchain has released its 2023 financial year performance report, which includes:

  • Crypto VC market hits 12-month high in March, with total investment exceeding $1 billion

    According to data from Cointelegraph, the cryptocurrency venture capital market continued to recover in March and April 2024. In March, 161 individual transactions were completed, setting a record in the past 12 months, with a total investment of more than $1 billion, an increase of 52% from the previous month. Although April has not yet ended, as of now, 90 transactions have been completed, attracting more than $820 million in investment.

  • Ethereum Layer 2 TVL has reached $39 billion

    L2BEAT data shows that Ethereum Layer2 TVL has reached $39 billion, with a 7-day increase of 6.66%.

  • Caixin: Mainland investors are currently not allowed to participate in the trading of Hong Kong virtual asset spot ETFs

    According to Caixin, the first batch of six virtual asset spot ETFs issued by Boshi International, Huaxia Fund (Hong Kong), and Jiashi International has been officially approved by the Hong Kong Securities Regulatory Commission. The goal is to be listed on April 30, 2024. It should be noted that mainland Chinese investors are currently not able to participate in the trading of these ETFs, despite the fact that they are first issued by Hong Kong companies under the umbrella of Chinese public funds.According to the product list on the Hong Kong Securities Regulatory Commission website, these six virtual asset spot ETFs were officially approved on April 23, 2024. The products are as follows: Jiashi Bitcoin Spot ETF (03439.HK), Jiashi Ethereum Spot ETF (03179.HK), Huaxia Bitcoin ETF (03042.HK), Huaxia Ethereum ETF (03046.HK), Boshi HashKey Bitcoin ETF (03008.HK), and Boshi HashKey Ethereum ETF (03009.HK).

  • Another person involved in the OneCoin scheme was arrested and the US prosecutors have filed a lawsuit against him

    According to court documents submitted by the Southern District of New York, William Morro, a person involved in OneCoin, has been arrested. Prosecutors said Morro lied to banks about the source of funds to conceal the source of funds related to OneCoin. He was involved in transferring $35 million related to OneCoin to an account in Hong Kong and about $6 million to an account in the United States.

  • Samourai Wallet crypto-currency mixing service co-founder arrested for money laundering

    According to The Block, the co-founders of the encrypted coin-mixing service, Samourai Wallet, have been arrested. Prosecutors allege that they laundered $100 million from Silk Road and other illegal markets. On Wednesday, Samourai CEO Keonne Rodriguez and CTO William Lonergan Hill were charged with operating the Samourai wallet.Prosecutors claim that Samourai is an unlicensed money transfer company that participated in "over $2 billion in illegal transactions and provided over $100 million in money laundering transactions for illegal dark web markets, including Silk Road." Rodriguez was arrested on Wednesday morning and will face trial in Pennsylvania.Hill was reportedly arrested in Portugal, and the US is seeking extradition. Prosecutors say that Samourai's network servers and domain name have also been seized, and the app can no longer be downloaded from the US Google Play store. Rodriguez and Hill are charged with money laundering and unlicensed money transmission, with maximum sentences of 20 years and 5 years, respectively.

  • Rune token DOG's transaction volume exceeded 100 BTC within 4 hours of launch

    According to data from Ordinal News forwarded by Runestone founder Leonidas, the Bitcoin symbol token DOG broke through a trading volume of 118.72 BTC (approximately $7,685,101 USD) within 4 hours of trading. The trading volume on three platforms was: Magic Eden on Bitcoin: 45.21 BTC; OKX Wallet: 20.37 BTC; UniSat: 53.14 BTC.