# RedKeysCoin: predictable RNG in playGame drains funds

RedKeysCoin game used predictable on-chain RNG, letting attacker compute winning bet and drain $12K via repeated plays.

- Payout: Real-world hack
- Severity: high
- Program: RedKeysCoin
- Analysis by: DeFiHackLabs
- Disclosed: 2024-05-27
- Write-up quality: 6/10
- Original: https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2024-05/RedKeysCoin_exp.sol
- Library page: https://bugbountylibrary.com/r/dhl%3A20240527-redkeyscoin
- Tags: domain:web3 impact:funds-loss kind:incident platform:defihacklabs sev:high tech:evm technique:source-review vuln:oracle-manipulation

**Target:** BSC smart contract game (playGame)
**Root cause:** The random number generator used block.timestamp, prevrandao, coinbase, gaslimit, block.number, and counter, all of which are predictable or controllable within the same transaction.
**Steps:**
1. Fork BSC at block 39079951.
2. Deploy attack contract and fund with REDKEYS tokens.
3. Approve game contract to spend tokens.
4. Loop 50 times: read `game.counter()`, compute `randomNumber(counter+1) % 2` using same formula as contract, call `game.playGame(betResultExpectation, 2, 1e9)` with the predicted winning choice.
5. Collect winnings.
**How found:** Likely identified that the RNG relied on block variables and a counter, which are predictable within the same block/transaction, allowing outcome prediction.
**Exploitation:** Attacker replicated the contract's RNG logic off-chain, predicted the outcome of each bet, and placed winning bets repeatedly to drain the game's funds.
**Insight:** On-chain randomness using block attributes and a counter is predictable; replicate the exact RNG formula and simulate before betting to guarantee wins.
