---
title: "Weak randomness"
url: https://dontgetrekt.today/dev/weak-randomness
section: "Can the platform be trusted?"
---

# Weak randomness

A lottery, mint or game uses block values as a random number. A validator or another contract can predict it and win every time.

## How the attack works

1. The contract uses `block.timestamp`, `blockhash`, `block.prevrandao` or `msg.sender` to pick a winner or a rare item.
2. An attacker contract computes the same value in the same transaction, and calls only when it wins.
3. A validator can also choose to publish or skip a block to change the result.

## Vulnerable code

```solidity
function play() external payable {
    uint256 r = uint256(keccak256(abi.encode(block.timestamp, msg.sender))) % 10;
    if (r == 0) payable(msg.sender).transfer(address(this).balance);
}
```

## Fixed code

```solidity
// Chainlink VRF v2.5: request, then receive the random value in a later transaction.
function play() external payable returns (uint256 id) {
    id = s_vrfCoordinator.requestRandomWords(req);
    player[id] = msg.sender;
}

function fulfillRandomWords(uint256 id, uint256[] calldata words) internal override {
    if (words[0] % 10 == 0) _pay(player[id]);
}
```

## Review checklist

- Never use block values or caller data as randomness for anything of value.
- Use a verifiable random function (Chainlink VRF, Pyth Entropy, Gelato VRF) or a commit-reveal scheme.
- Make the result arrive in a later transaction than the bet, so a contract cannot revert when it loses.

## How to test for it

- Write an attacker contract that computes the result first and calls only when it wins.
- Slither detector `weak-prng`.

## Real exploits (5)

- 2026-03-19 ATM BlindBox ($99K): ATM BlindBox on BNB Chain lost about 13M ATM (~$99K) because its bet settlement fell back to a predictable hash of prevrandao, betId and timestamp once the blockhash expired. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2026-03/ATMBlindBox_exp.sol)
- 2025-03-14 H2O ($22K): H2O token on BNB Chain lost about $22,470 because a random mint/transfer outcome was derived from predictable block values, letting an attacker only attack when the result was favorable. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2025-03/H2O_exp.sol)
- 2024-05-27 RedKeysCoin ($12K): RedKeysCoin's betting game on BNB Chain lost about $12K because its random number was computed from predictable block values that an attacker contract could replicate. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2024-05/RedKeysCoin_exp.sol)
- 2022-12-05 RFB: RFB token on BNB Chain lost about 12 BNB because its buy/sell logic depended on predictable random numbers that the attacker could test and retry. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-12/RFB_exp.sol)
- 2022-08-24 LuckyTiger NFT: LuckyTiger NFT (loss amount not stated) was attacked because its publicMint() randomness came from block.difficulty and block.timestamp, so an attacker could predict the result. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-08/LuckyTiger_exp.sol)

## References

- [SWC-120: Weak sources of randomness from chain attributes](https://swcregistry.io/docs/SWC-120/)
- [Chainlink VRF](https://docs.chain.link/vrf)
