Mystery Box

First Flight #25
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: medium
Valid

Insecure Randomness Generation in `openBox` Function

Summary

The openBox function uses an insecure method to generate randomness, making it susceptible to manipulation and prediction by malicious actors. This vulnerability can be exploited to influence the outcome of rewards.

Vulnerability Details

The openBox function uses block.timestamp and msg.sender to generate a pseudo-random number. Both of these values can be influenced or predicted by miners or users, leading to predictable outcomes.

function openBox() public {
require(boxesOwned[msg.sender] > 0, "No boxes to open");
// Generate a random number between 0 and 99
>> uint256 randomValue = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;
// Determine the reward based on probability
if (randomValue < 75) {
// 75% chance to get Coal (0-74)
rewardsOwned[msg.sender].push(Reward("Coal", 0 ether));
} else if (randomValue < 95) {
// 20% chance to get Bronze Coin (75-94)
rewardsOwned[msg.sender].push(Reward("Bronze Coin", 0.1 ether));
} else if (randomValue < 99) {
// 4% chance to get Silver Coin (95-98)
rewardsOwned[msg.sender].push(Reward("Silver Coin", 0.5 ether));
} else {
// 1% chance to get Gold Coin (99)
rewardsOwned[msg.sender].push(Reward("Gold Coin", 1 ether));
}
boxesOwned[msg.sender] -= 1;
}

The issue with this approach is that block timestamps and sender addresses are predictable or controllable by miners or users. Here’s how the vulnerability works:
Miners can slightly influence the block timestamp. Although they cannot fully control it, they can adjust the timestamp within a small window to tilt the outcome in their favor.
The sender’s address is known ahead of time, making it a poor source of randomness. Attackers can use this knowledge to calculate the resulting random number and manipulate their actions accordingly (e.g., deciding when to open a mystery box).

Impact

Malicious users or miners can predict or manipulate the random number generation to consistently receive more valuable rewards, undermining the fairness of the system.

Tools Used

Manual Review

Recommendations

Implement a more secure method for randomness, such as Chainlink VRF. Utilize Chainlink's Verifiable Random Function (VRF) to generate provably fair and tamper-proof randomness.

Updates

Appeal created

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Weak Randomness

Support

FAQs

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

Give us feedback!