防止 Solidity 代码中出现代价高昂的漏洞。此技能提供专业审计人员使用的成熟安全模式、漏洞检测清单和 Gas 优化技术,以保护 DeFi 协议和智能合约。
下載技能 ZIP
在 Claude 中上傳
前往 設定 → 功能 → 技能 → 上傳技能
開啟並開始使用
測試它
正在使用「solidity-security」。 Is this withdraw function secure? function withdraw() public { uint256 amount = balances[msg.sender]; payable(msg.sender).transfer(amount); balances[msg.sender] = 0; }
預期結果:
此函数存在重入漏洞。外部调用发生在状态更新之前。攻击者可以在余额设置为零之前递归调用 withdraw。修复:应用检查 - 效果 - 交互模式:首先设置 balances[msg.sender] = 0,然后进行外部调用。或者,使用带有 nonReentrant 修饰符的 OpenZeppelin ReentrancyGuard。
正在使用「solidity-security」。 How do I add access control to my admin-only function?
預期結果:
导入 OpenZeppelin 的 Ownable 合约并使用 onlyOwner 修饰符。或者,使用映射和自定义修饰符实现基于角色的访问控制。确保函数在执行敏感操作之前验证 msg.sender 已获授权。
安全審計
安全All 72 static analysis findings are false positives. The detected patterns (shell backticks, dynamic requires, weak cryptography) exist within markdown code blocks demonstrating Solidity security concepts and JavaScript testing examples. This skill contains only educational documentation with no executable code. The skill legitimately teaches smart contract security best practices including reentrancy prevention, access control, and gas optimization.
品質評分
你能建構什麼
构建 DeFi 协议
使用适当的重入保护和访问控制实现安全的借贷或交易合约。
审计智能合约
在主网部署或集成之前审查现有合约是否存在漏洞。
学习 Solidity 安全
了解常见攻击向量和经过验证的缓解模式,以实现安全开发。
試試這些提示
Review my Solidity contract for common vulnerabilities including reentrancy, overflow, and access control issues. Point out any insecure patterns and suggest fixes.
Help me add reentrancy protection to my withdraw function. Show me both the Checks-Effects-Interactions pattern and the OpenZeppelin ReentrancyGuard approach.
Analyze my contract for gas optimization opportunities. Focus on storage packing, calldata usage, and reducing redundant operations while maintaining security.
Review my contract for audit readiness. Check for NatSpec documentation, input validation, emergency stop mechanisms, and proper event emission. Create a security checklist.
最佳實務
- 始终应用检查 - 效果 - 交互(Checks-Effects-Interactions)模式:验证输入、更新状态,然后进行外部调用
- 使用 OpenZeppelin 经过审计的合约来实现 ReentrancyGuard、Ownable 和 Pausable,而不是自定义实现
- 启用 Solidity 0.8+ 以获得内置溢出/下溢保护,或对旧版本使用 SafeMath 库
避免
- 切勿使用 tx.origin 进行身份验证 - 它可能被恶意合约钓鱼攻击
- 避免在更新合约状态之前进行外部调用,这会引发重入攻击
- 不要使用浮动的 pragma - 始终固定到特定的 Solidity 版本以确保确定性编译