技能 solidity-security
🔒

solidity-security

安全

安全智能合约开发

也可從以下取得: wshobson

防止 Solidity 代码中出现代价高昂的漏洞。此技能提供专业审计人员使用的成熟安全模式、漏洞检测清单和 Gas 优化技术,以保护 DeFi 协议和智能合约。

支援: Claude Codex Code(CC)
🥉 75 青銅
1

下載技能 ZIP

2

在 Claude 中上傳

前往 設定 → 功能 → 技能 → 上傳技能

3

開啟並開始使用

測試它

正在使用「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 已获授权。

安全審計

安全
v1 • 2/25/2026

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.

2
已掃描檔案
562
分析行數
0
發現項
1
審計總數
未發現安全問題
審計者: claude

品質評分

38
架構
100
可維護性
87
內容
50
社群
100
安全
100
規範符合性

你能建構什麼

构建 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.
Gas 优化
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 版本以确保确定性编译

常見問題

最常见的智能合约漏洞是什么?
重入攻击是最常见且最关键的漏洞。它发生在外部调用在状态更新之前执行时,允许攻击者递归调用函数并窃取资金。始终使用检查 - 效果 - 交互模式或 OpenZeppelin 的 ReentrancyGuard。
Solidity 0.8+ 需要 SafeMath 吗?
不需要。Solidity 0.8 及更高版本包含内置的溢出和下溢检查,会在算术错误时自动回滚。SafeMath 仅对于使用 0.8 之前 Solidity 版本的合约是必需的。
我应该使用 msg.sender 还是 tx.origin 进行身份验证?
始终使用 msg.sender 进行身份验证。tx.origin 代表交易的原始发送者,可能被钓鱼攻击利用。切勿使用 tx.origin 进行授权检查。
拉取支付和推送支付模式有什么区别?
推送支付涉及合约向用户发送资金,如果用户有回退问题可能会失败。拉取支付让用户自行提取资金,更安全,因为用户控制时机。对于批量分发,优先选择拉取而非推送模式。
如何让我的合约为专业审计做好准备?
添加全面的 NatSpec 文档,实现输入验证,使用访问控制修饰符,为状态变化发出事件,编写涵盖边界情况的单元测试,并创建安全检查清单。确保没有编译器警告并使用固定的 pragma 版本。
此技能可以替代专业安全审计吗?
不能。此技能帮助识别常见漏洞并实施最佳实践,但不能替代专业审计。高价值合约、DeFi 协议和生产系统需要形式化验证和安全专家的人工审查。