Photo by AAkash Dhage on Unsplash

Smart contracts vulnerability explained: Insecure randomness

Simon Busch

--

As the world becomes more digitized, blockchain technology is increasingly being used to store and manage sensitive data and assets. Smart contracts, which are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code, are an important aspect of blockchain technology.

One critical aspect of smart contract security is the use of random numbers. Smart contracts use random numbers to make decisions, such as selecting a winner in a lottery or determining which player goes first in a game. However, if the random number generation in a smart contract is not secure, it can lead to serious vulnerabilities that can be exploited by malicious actors.

Insecure randomness occurs when a smart contract uses a source of randomness that is predictable or can be manipulated. This can happen when a smart contract relies on a deterministic function, such as the block hash or timestamp, to generate random numbers. Since these values are publicly visible on the blockchain, an attacker can predict them and use this knowledge to exploit the smart contract.

To demonstrate this vulnerability, let’s look at a simple smart contract that uses the block hash to generate a random number between 1 and 100:

pragma solidity ^0.8.0…

--

--

Simon Busch

Full Stack JS/TS @code4rena building the future of smart contract auditing. Solidity/blockchain security learner 🚀