Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: high
Valid

Weak Randomness Used in Battles

Summary

Using block.timestamp, block.prevrandao, msg.sender or block.number as a source of randomness is commonly advised against, as the outcome can be manipulated.

The _battle() function in contract RapBattle is using block.timestamp, block.prevrandao and msg.sender as source of randomness.

Vulnerability Details

The code below shows how the, supposedly, random value is being calculated:

function _battle(uint256 _tokenId, uint256 _credBet) internal {
address _defender = defender;
require(defenderBet == _credBet, "RapBattle: Bet amounts do not match");
uint256 defenderRapperSkill = getRapperSkill(defenderTokenId);
uint256 challengerRapperSkill = getRapperSkill(_tokenId);
uint256 totalBattleSkill = defenderRapperSkill + challengerRapperSkill;
uint256 totalPrize = defenderBet + _credBet;
@> uint256 random =
@> uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender))) % totalBattleSkill;
// Reset the defender

Impact

An attacker can precompute valid "random" numbers to win every battle.

Tools Used

Manual analysis.

Recommendations

Consider using a decentralized oracle for the generation of random numbers, such as Chainlinks VRF.

Updates

Lead Judging Commences

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