Solidity智能合约常见漏洞(一)

智能合约安全漏洞及修复建议

1. 重入攻击(Reentrancy Attack)

漏洞描述:

攻击者利用合约调用其他合约的函数,在调用栈的不同位置多次调用发起攻击的合约,从而执行未预期的操作。

修改前的例子:

contract Vulnerable {
    mapping(address => uint) public balances;

    function withdraw(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        (bool success, ) = msg.sender.call{value: _amount}("");
        require(success);
        balances[msg.sender] -= _amount;
    }
}

修改建议:

使用 reentrancyGuard 或将状态修改放在函数的最后,以确保在修改状态之前不再调用其他合约。

修改后的例子:

solidityCopy codecontract Secure {
    mapping(address => uint) public balances;
    bool private locked;

    function withdraw(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        
        // 添加重入性锁
        require(!locked, "Reentrancy protection");
        locked = true;
        
        (bool success, ) = msg.sender.call{value: _amount}("");
        require(success);
        
        balances[msg.sender] -= _amount;
        
        // 释放锁
        locked = false;
    }
}

2. 整数溢出和下溢(Integer Overflow and Underflow)

漏洞描述:

由于整数溢出或下溢而导致计算错误,通常发生在加减法操作中。

修改前的例子:

solidityCopy codecontract Vulnerable {
    uint public balance = 100;

    function transfer(uint _amount) public {
        balance -= _amount;
    }
}

修改建议:

使用 SafeMath 库(在 Solidity 0.8.0 及更高版本中不再需要)或在进行整数计算之前检查溢出和下溢。

修改后的例子:

solidityCopy codecontract Secure {
    uint public balance = 100;

    function transfer(uint _amount) public {
        // 检查溢出
        require(_amount <= balance);
        balance -= _amount;
    }
}

3. 未授权访问

漏洞描述:

合约中的敏感函数没有适当的权限检查,可能被未经授权的用户或合约调用。

修改前的例子:

solidityCopy codecontract Vulnerable {
    address public owner;

    function transferOwnership(address _newOwner) public {
        owner = _newOwner;
    }
}

修改建议:

添加适当的权限检查,使用 require 确保只有授权用户能够调用敏感函数。

修改后的例子:

solidityCopy codecontract Secure {
    address public owner;

    function transferOwnership(address _newOwner) public {
        // 添加权限检查
        require(msg.sender == owner, "Unauthorized");
        owner = _newOwner;
    }
}

4. 交易顺序依赖(Front-Running)

漏洞描述:

攻击者通过在确认前查看和复制用户的交易,然后以高价优先执行相同的交易,以获利。

修改前的例子:

solidityCopy codecontract Vulnerable {
    mapping(address => uint) public balances;

    function buyTokens(uint _amount) public {
        require(balances[msg.sender] >= _amount);
        // 发放代币
        // ...
        balances[msg.sender] -= _amount;
    }
}

修改建议:

使用一些技术手段,如使用随机数生成器或提交交易时使用密钥来减缓攻击者的优势。

修改后的例子:

solidityCopy codecontract Secure {
    mapping(address => uint) public balances;

    function buyTokens(uint _amount) public {
        // 添加随机数生成或其他防御措施
        require(balances[msg.sender] >= _amount);
        // 发放代币
        // ...
        balances[msg.sender] -= _amount;
    }
}
  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值