The MysteryBox::openBox function is used to open a user's box, which could contain one of several items: Coal, Bronze, Silver, or Gold. The function uses a randomValue to determine the contents of the box. However, this randomValue is derived from block.timestamp and msg.sender, making it susceptible to manipulation by malicious users. By deploying a contract that strategically calls this function, an attacker can influence the randomValue and predict or manipulate the box's outcome.
Proof of Concept:
A malicious user deploys a smart contract that purchases and opens boxes under certain conditions.
The contract repeatedly calls the Hacker::manipulateOpenBox function until a desired outcome is achieved, allowing the attacker to influence the randomness.
Proof of Code:
Vulnerable Code in MysteryBox Contract
Test Contract to Demonstrate Randomness Manipulation
A malicious user can influence the outcome of the MysteryBox by controlling the randomValue generated in the MysteryBox::openBox function, thereby eliminating the intended randomness. The test results show that the maximum gas cost for 100 attempts is approximately 128,404.
Foundry
Manual Review
To mitigate this issue, it's recommended to use a more secure source of randomness. The best solution is to implement a Verifiable Random Function (VRF), such as Chainlink VRF, to ensure the integrity of random number generation and prevent manipulation.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.