Mystery Box

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

Predictable Randomness in Mystery Box Rewards

Summary

A vulnerability exists in the openBox function of the contract where the randomValue used to determine rewards is predictable. The random number is generated using block.timestamp and msg.sender, both of which are public information that can be accessed by an attacker. This allows an attacker to front-run the contract and manipulate the outcome of the reward generation.

Vulnerability Details

@> uint256 randomValue = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;
  • An attacker can observe the current block.timestamp.

  • By pre-calculating the keccak256 hash using the known msg.sender and block.timestamp, the attacker can determine the randomValue that will be generated.

  • Based on this, the attacker can choose the optimal time to submit a transaction to receive the most valuable reward (such as the "Gold Coin").

POC

The following test demonstrates how the random value in the openBox function can be predicted:

function testRandomValueInOpenBoxCanBePredicted() public {
uint256 randomValue1 = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;
assertEq(randomValue1, 91);
console2.log("It can be predicted, Bronze Coin: ", randomValue1);
vm.warp(block.timestamp + 2);
uint256 randomValue2 = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;
assertEq(randomValue2, 26);
console2.log("It can be predicted, Coal: ", randomValue2);
}

Test Logs:

Logs:
Reward Pool Length: 4
It can be predicted, Bronze Coin: 91
It can be predicted, Coal: 26

Impact

This vulnerability allows an attacker to manipulate the rewards in the openBox function. An attacker can repeatedly call the function at specific times to increase their chances of receiving higher-value rewards (e.g., the "Gold Coin"). This breaks the intended fairness of the reward system and could lead to a significant financial loss for the contract and its users.

Tools Used

Manual Review

Foundry

Recommendations

Use a more secure source of randomness like, Verifiable Random Function (VRF)

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.