Smart contract bugs / Can the platform be trusted?
Weak randomness
A lottery, mint or game uses block values as a random number. A validator or another contract can predict it and win every time.
5 real exploits · $133K lost
How the attack works
- The contract uses
block.timestamp,blockhash,block.prevrandaoormsg.senderto pick a winner or a rare item. - An attacker contract computes the same value in the same transaction, and calls only when it wins.
- A validator can also choose to publish or skip a block to change the result.
Vulnerable and fixed code
function play() external payable {
uint256 r = uint256(keccak256(abi.encode(block.timestamp, msg.sender))) % 10;
if (r == 0) payable(msg.sender).transfer(address(this).balance);
}// Chainlink VRF v2.5: request, then receive the random value in a later transaction.
function play() external payable returns (uint256 id) {
id = s_vrfCoordinator.requestRandomWords(req);
player[id] = msg.sender;
}
function fulfillRandomWords(uint256 id, uint256[] calldata words) internal override {
if (words[0] % 10 == 0) _pay(player[id]);
}Review checklist
- Never use block values or caller data as randomness for anything of value.
- Use a verifiable random function (Chainlink VRF, Pyth Entropy, Gelato VRF) or a commit-reveal scheme.
- Make the result arrive in a later transaction than the bet, so a contract cannot revert when it loses.
How to test for it
- Write an attacker contract that computes the result first and calls only when it wins.
- Slither detector
weak-prng.
Real exploits (5)
-
ATM BlindBox: ATM BlindBox on BNB Chain lost about 13M ATM (~$99K) because its bet settlement fell back to a predictable hash of prevrandao, betId and timestamp once the blockhash expired.
Root cause and fix
Vulnerable code:
0x1F8336aEF584795E282FECe8DE356BaBD7734c59 · settle()Bets are settled by a separate settle(betId) call that the user chooses when to send. If blockhash(betBlock + 2) is no longer available (older than 256 blocks), settle() falls back to keccak256(block.prevrandao, betId, block.timestamp). The attacker can compute this value before sending, and can wait until a block where it gives a winning digit. The attacker placed a large even-parity bet, waited about 818 blocks, then settled only when the fallback hash won. The attacker received 1.95x the stake from the payout pool.
What would have stopped it: Do not use block values or an expired blockhash as a randomness source. Use a verifiable randomness oracle such as Chainlink VRF, or a commit-reveal scheme. At the least, reject settlement of bets whose target blockhash has expired (refund or void them), and cap the bet size against the payout pool.
-
H2O: H2O token on BNB Chain lost about $22,470 because a random mint/transfer outcome was derived from predictable block values, letting an attacker only attack when the result was favorable.
Root cause and fix
Vulnerable code:
H2O token (0xe9c4D4f095C7943a9ef5EC01AfD1385D011855A1)The H2O token decides a random outcome on transfers using keccak256 of block.timestamp, the pair address and blockhash(block.number-1), taken modulo 2. These values are known to the caller, so an attacker can predict the result and only send the attack transaction when it is favorable (failed attempts revert). The attacker flash-loaned 100,000 BUSD from a PancakeV3 pool, bought H2O, then looped transfer-to-pair and skim() 50 times to inflate its H2O balance through the token's random behavior, then sold all H2O for BUSD, repaid the loan and kept the profit. The exact on-chain token logic is inferred from the PoC, not shown.
What would have stopped it: Do not use block values (timestamp, blockhash) as randomness in token logic; use a verifiable randomness source such as Chainlink VRF. Also avoid token transfer hooks that mint or change balances based on chance, and test the token against pair skim/transfer loops.
-
RedKeysCoin: RedKeysCoin's betting game on BNB Chain lost about $12K because its random number was computed from predictable block values that an attacker contract could replicate.
Root cause and fix
Vulnerable code:
RedKeysGame (0x71e3056aa4985de9f5441f079e6c74454a3c95f0) · playGame()The game's randomNumber function builds its seed from the game counter, block.timestamp, block.prevrandao, block.coinbase, block.gaslimit, block.number and the caller's address. All of these are known to a calling contract in the same transaction. The attacker contract ran the same formula, worked out the winning choice, and called playGame with that choice 50 times, so it won every bet and drained the payout pool.
What would have stopped it: Don't use on-chain block data or caller-known values as a randomness source for bets. Use a verifiable randomness oracle such as Chainlink VRF, or a commit-reveal scheme. Also block contract callers, or settle bets in a later block than the one where they are placed.
-
RFB: RFB token on BNB Chain lost about 12 BNB because its buy/sell logic depended on predictable random numbers that the attacker could test and retry.
Root cause and fix
Vulnerable code:
RFB token (0x26f1457f067bF26881F311833391b52cA871a4b5)The RFB token used a random number that could be predicted on-chain, and that number affected the outcome of buys and sells. The write-up only gives the title "Predicting Random Numbers" and a PoC, so the exact mechanism is not shown. The attacker flash-loaned 20 WBNB from DODO and unwrapped it to BNB. It then looped up to 50 times, using a slightly different amount each time. Each try swapped BNB for RFB and back inside a try/catch, and the try kept its result only if the BNB balance ended higher. Failed tries reverted, so only profitable ones stayed. The attacker then repaid the loan and kept the profit.
What would have stopped it: Do not derive value-affecting outcomes from block data or other on-chain values that a caller can predict or test in the same transaction. Use a secure randomness source such as a VRF, or commit-reveal. Also block contracts from calling the function, so an attacker cannot revert the bad outcomes.
-
LuckyTiger NFT: LuckyTiger NFT (loss amount not stated) was attacked because its publicMint() randomness came from block.difficulty and block.timestamp, so an attacker could predict the result.
Root cause and fix
Vulnerable code:
0x9c87A5726e98F2f404cdd8ac8968E9b2C80C0967 · publicMint()The NFT's publicMint() decides the "lucky" outcome using a pseudo-random value derived from block.difficulty and block.timestamp. The caller can compute the same hash inside their own contract in the same transaction, so they know the result in advance. The attacker's contract only calls publicMint() (10 times in the PoC) when the computed value is the winning one, and reverts otherwise. This lets it mint only favourable NFTs. The PoC's source for publicMint() is not shown, so this is inferred from the PoC's getRandom logic.
What would have stopped it: Do not use block values such as timestamp or difficulty/prevrandao as a randomness source. Use a verifiable randomness oracle such as Chainlink VRF, or a commit-reveal scheme. Also block contract callers, or make the outcome unknowable at the time of the call.