Modulo bias in winner selection in raffle
The Raffle contract selects a winner by taking a random number modulo the ticket count without correcting for modulo bias, so when the ticket count does not divide the random number space evenly some ticket indices are more likely to win. An attacker could time or manipulate ticket purchases to obtain favourable indices and increase their chance of winning.
- Disclosed after under an hour
49626 sc insight modulo bias in winner selection in raffle
Submitted on Jul 17th 2025 at 19:12:26 UTC by @Opzteam for Attackathon | Plume Network
- Report ID: #49626
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/immunefi-team/attackathon-plume-network/blob/main/plume/src/spin/Raffle.sol
- Impacts:
- Contract fails to deliver promised returns, but doesn't lose value
Description
Vulnerability Description
The Raffle contract contains a modulo bias vulnerability in the winner selection mechanism. The contract uses a simple modulo operation without bias protection, which can favor certain ticket indices when the total number of tickets doesn't evenly divide the VRF random number space (1,000,000), leading to unfair raffle outcomes.
Vulnerable Code Snippet
// Line 247 in handleWinnerSelection function
uint256 winningTicketIndex = (rng[0] % totalTickets[prizeId]) + 1;
When totalTickets[prizeId] doesn't evenly divide 1,000,000, some ticket indices get more chances than others.
Example: If totalTickets = 7, then 1,000,000 % 7 = 6, meaning tickets 1-6 get 142,858 chances each while ticket 7 only gets 142,852 chances.
Proof of Concept
{% stepper %} {% step %} Create a raffle with 100,001 total tickets {% endstep %}
{% step %} Calculate bias: 1,000,000 % 100,001 = 999,999 (significant remainder) {% endstep %}
{% step %} Exploit: Tickets 1-999,999 get 10 chances each, ticket 1,000,000 gets 9 chances, ticket 100,001 gets 1 chance {% endstep %}
{% step %} Result: Users with tickets in positions 1-999,999 have higher winning probability than those in positions 1,000,000 and 100,001 {% endstep %}
{% step %} Manipulation: Attacker can strategically time entries or manipulate ticket purchases to favor advantageous positions {% endstep %} {% endstepper %}
Remediation
{% hint style="info" %} Implement rejection sampling to ensure uniform distribution. {% endhint %}
Recommended code pattern:
uint256 maxValidValue = (type(uint256).max / totalTickets[prizeId]) * totalTickets[prizeId];
uint256 randomValue;
do {
randomValue = rng[0];
} while (randomValue >= maxValidValue);
uint256 winningTicketIndex = (randomValue % totalTickets[prizeId]) + 1;
This ensures all ticket indices have exactly equal probability of being selected.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The winner index is computed with a plain modulo of the random value by the ticket count, without rejection sampling to remove the uneven remainder.
Impact
The raffle may not deliver fair promised odds, and an attacker could gain a higher chance of winning by choosing favourable ticket positions, though the contract does not lose funds directly.
What the attacker needed
Ability to create or enter a raffle; control over ticket purchase timing or positions
The fix
The winner selection should use rejection sampling so that every ticket index has exactly equal probability.
How it was found
- Create a raffle with 100,001 total tickets
- Calculate the remainder of 1,000,000 divided by 100,001
- Observe that some ticket positions receive more chances than others
- Strategically time entries or manipulate ticket purchases to favour advantageous positions
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A raffle picks a winner by turning a random number into a ticket number using division remainder. If the range of random numbers does not split evenly by the number of tickets, some ticket numbers come up slightly more often. This report says that small unfairness could be exploited by choosing when to buy tickets.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.