【发布时间】:2022-12-05 12:39:47
【问题描述】:
标签: security ethereum blockchain solidity smartcontracts
标签: security ethereum blockchain solidity smartcontracts
假设我们有 2 份合同。持有一些以太币的目标合约属于攻击者合约。现在攻击者合约想要取钱
攻击者合约有两个功能:fallback和withdraw
// this gets triggered when ether is receive
fallback(){
A.withdraw()
}
attack(){
A.withdraw()
}
假设攻击者合约调用attack函数。这将调用 withdraw 内部目标合约:
目标合约:
withdraw(){
require balance>0
send ether
// before balance gets updated attacker's fallback triggers another withdraw
// send function will give the control to the attacker's contract
set balance of attacker =0
}
现在目标合约会将ether发送给攻击者的合约。当它发生时,攻击者的合约内部fallback函数被触发。每次收到以太币时都会执行fallback。我解释了fallback functions in detail
现在攻击者的合约收到以太币并立即调用目标合约内的withdraw。所以目标合约的withdraw函数在循环中,直到目标合约的余额达到0
如果合约使用call, send or transfer这可能会导致
控制流到攻击者合约,因为这些函数为回退函数委托了足够的气体。一旦控制权传递给攻击者的合约,目标合约的状态将不完整。目标合约失去控制权。
【讨论】: