Bad Randomness
伪随机数
以太坊上的许多应用程序,比如 NFT 随机 tokenId 抽奖、盲盒开启和 GameFi 战斗结果,都依赖于随机数。 然而,由于以太坊上的所有数据都是公开和确定性的,传统的随机生成方法如 random()
并不可用。
相反,项目通常使用伪随机数生成器,blockhash()
和keccak256()
。 This approach, known as the Bad Randomness Vulnerability, allows attackers to predict outcomes, enabling them to manipulate results like minting specific rare NFTs.
这种漏洞在NFT和GameFi项目中很常见,包括Meebits, Loots, and Wolf Game。 它已经造成了重大的财务损失,比如 SmartBillions 彩票漏洞,攻击者利用可预测的结果赢得了超过 400 ETH。 更多信息请参阅文章, 区块链彩票被黑客攻击,损失12万美元。
Example of a Bad Randomness Vulnerability
我们来探讨一个存在漏洞的NFT合约,FlawedRandomizer.sol
。
contract FlawedRandomizer is ERC721 {
uint256 public totalMints;
// Constructor initializes the NFT collection's name and symbol.
constructor() ERC721("", ""){}
// Mint function: mints only when the input luckyNumber equals the random number.
function mintIfLucky(uint256 guessedNumber) public {
uint256 pseudoRandomNumber = uint256(keccak256(abi.encodePacked(blockhash(block.number - 1), block.timestamp))) % 100; // Get pseudo-random number
require(pseudoRandomNumber == guessedNumber, "Try again next time!");
_mint(msg.sender, totalMints);
totalMints++;
}
}