防止 Solidty 程式碼中代價昂貴的漏洞。本技能提供經過驗證的安全模式、漏洞偵測檢查清單,以及專業審計人員用於保護 DeFi 協議和智慧合約的 Gas 優化技術。
下载技能 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。修復:應用 Checks-Effects-Interactions 模式:首先設定 balances[msg.sender] = 0,然後進行外部呼叫。或者,使用 OpenZeppelin 的 ReentrancyGuard 搭配 nonReentrant 修飾符。
正在使用“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 安全
了解常見攻擊向量和安全開發的經過驗證的緩解模式。
试试这些提示
審查我的 Solidity 合約中的常見漏洞,包括重入、溢位和存取控制問題。指出任何不安全模式並建議修復方法。
幫助我在我的提款函數中添加重入保護。向我展示 Checks-Effects-Interactions 模式和 OpenZeppelin ReentrancyGuard 方法。
分析我的合約的 Gas 優化機會。專注於儲存緊湊化、calldata 使用和減少冗餘操作,同時保持安全性。
審查我的合約是否準備好接受審計。檢查 NatSpec 文件、輸入驗證、緊急停止機制和正確的事件發射。建立安全檢查清單。
最佳实践
- 始終應用 Checks-Effects-Interactions 模式:驗證輸入、更新狀態,然後進行外部呼叫
- 使用 OpenZeppelin 經過審計的合約進行 ReentrancyGuard、Ownable 和 Pausable,而非自訂實作
- 啟用 Solidity 0.8+ 以獲得內建的溢位/下溢保護,或對較舊版本使用 SafeMath 函式庫
避免
- 切勿使用 tx.origin 進行身份驗證 - 它可能會被惡意合約進行網路釣魚
- 避免在更新合約狀態之前進行外部呼叫,因為這會啟用重入攻擊
- 不要使用浮動 pragma - 始終固定到特定的 Solidity 版本以確保確定的編譯