【问题标题】:solidity re-entrancy attack explanationsolidity 重入攻击解释
【发布时间】:2022-12-05 12:39:47
【问题描述】:

嘿,我有一个关于重入的问题,我明白了逻辑,但考虑到交易时间以及 A 和 B 中的两个函数都在下一行,攻击者如何在余额设置为 0 之前设法再次退出

re-entrancy

【问题讨论】:

    标签: security ethereum blockchain solidity smartcontracts


    【解决方案1】:

    假设我们有 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这可能会导致 控制流到攻击者合约,因为这些函数为回退函数委托了足够的气体。一旦控制权传递给攻击者的合约,目标合约的状态将不完整。目标合约失去控制权。

    Prevent reentrancy attacks

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 2011-06-01
      • 2013-04-13
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      相关资源
      最近更新 更多