Anton1636/ai-contract-audit
GitHub: Anton1636/ai-contract-audit
基于 Gemini 大模型的 Solidity 智能合约 AI 安全审计平台,融合多家顶尖安全公司方法论,提供漏洞检测、攻击复现、Gas 优化建议和 PDF 报告导出等一站式审计能力。
Stars: 0 | Forks: 0
## ✨ 特性
- **AI 安全分析** — 结合 OpenZeppelin、CertiK 和 Trail of Bits 的方法检测漏洞
- **9 种漏洞类别** — 访问控制、重入攻击、数学错误、逻辑错误、状态问题、预言机攻击、事件缺失、集成风险、代码质量
- **概念验证** — 针对每个漏洞提供逐步的攻击场景
- **真实世界参考** — 将每个发现与真实的黑客攻击事件(The DAO、Ronin Bridge 等)联系起来
- **Gas 优化** — 附带预估节省量的独立分析
- **PDF 导出** — 下载专业的审计报告
- **审计历史** — 所有先前的审计均在本地保存
- **AI 聊天助手** — 在完整的上下文中询问有关您合约的问题
- **Monaco Editor** — 提供 VS Code 级别的代码编辑体验
## 🛠️ 技术栈
| Category | Technology |
| ----------- | ------------------------------ |
| Framework | Next.js 15 (App Router) |
| Language | TypeScript |
| AI Model | Google Gemini 3 Flash |
| Code Editor | Monaco Editor (VS Code engine) |
| Styling | Tailwind CSS |
| PDF Export | @react-pdf/renderer |
| Testing | Jest + React Testing Library |
| CI/CD | GitHub Actions |
| Deployment | Vercel |
## 🚀 快速开始
### 前置条件
- Node.js 22+
- Google Gemini API 密钥(可在 [aistudio.google.com](https://aistudio.google.com) 免费获取)
### 安装说明
```
# 克隆 repository
git clone https://github.com/Anton1636/ai-contract-audit.git
cd ai-contract-audit
# 安装 dependencies
npm install
# 设置 environment variables
cp .env.example .env.local
# 将你的 GEMINI_API_KEY 添加到 .env.local
# 启动 development server
npm run dev
```
在浏览器中打开 [http://localhost:3000](http://localhost:3000)。
## 🔍 工作原理
1. **粘贴** 将您的 Solidity 合约粘贴到 Monaco 编辑器中
2. **点击** “Audit Smart Contract”
3. **等待** AI 分析您的代码
4. **审查** 详细的安全报告,包含:
- 安全评分(0-100)和等级(A-F)
- 按严重程度分类的漏洞
- 概念验证攻击场景
- 修复后的代码示例
- Gas 优化建议
5. **聊天** 与 AI 助手讨论具体发现
6. **导出** 将完整报告导出为 PDF
## 🏗️ 架构
```
ai-contract-audit/
├── src/
│ ├── app/
│ │ ├── api/
│ │ │ ├── audit/route.ts # Gemini API + caching + rate limiting
│ │ │ └── chat/route.ts # AI chat with contract context
│ │ ├── page.tsx # Main application page
│ │ └── layout.tsx
│ ├── components/
│ │ ├── AuditReport.tsx # Full audit report display
│ │ ├── VulnerabilityCard.tsx # Expandable vulnerability cards
│ │ ├── ScoreRing.tsx # Animated score visualization
│ │ ├── SeverityBadge.tsx # Severity level badges
│ │ ├── CodeEditor.tsx # Monaco editor wrapper
│ │ ├── AuditButton.tsx # Audit trigger button
│ │ ├── ChatPanel.tsx # Floating AI chat
│ │ ├── HistoryPanel.tsx # Audit history
│ │ ├── LoadingOverlay.tsx # Loading screen with progress
│ │ └── AuditPdfDocument.tsx # PDF report generator
│ ├── lib/
│ │ ├── prompts.ts # AI prompts (OpenZeppelin/CertiK methodology)
│ │ ├── utils.ts # Utility functions
│ │ ├── constants.ts # App constants
│ │ ├── historyStorage.ts # LocalStorage management
│ │ └── exportPdf.ts # PDF export logic
│ └── types/
│ └── audit.ts # TypeScript interfaces
├── tests/
│ └── unit/ # Jest unit tests
└── .github/
└── workflows/ci.yml # CI/CD pipeline
```
## 🔐 安全方法学
本审计工具结合了三家顶尖公司的方法:
| Firm | Contribution |
| ----------------- | ----------------------------------------------------------- |
| **OpenZeppelin** | 报告结构:摘要 → 范围 → 发现 |
| **CertiK** | 发现格式:描述 → 影响 → PoC → 建议 |
| **Trail of Bits** | 漏洞类别和严重性定义 |
| **Rekt.news** | 基于真实损失的影响评估 |
### 漏洞类别
| Category | Examples |
| ---------------- | ----------------------------------------- |
| Access Control | Missing onlyOwner, incorrect roles |
| Reentrancy | Cross-function, cross-contract attacks |
| Math Errors | Overflow, underflow, precision loss |
| Logic Errors | Incorrect business logic, validation gaps |
| State Issues | Race conditions, double updates |
| Oracle Attacks | Price manipulation, flash loan exploits |
| Missing Events | No emission after state changes |
| Integration Risk | Unsafe external calls, dependency issues |
| Code Quality | Documentation, best practice violations |
## 🧪 测试
```
# 运行所有 tests
npm test
# Watch 模式
npm run test:watch
# Coverage 报告
npm run test:coverage
```
**25 个测试** 覆盖了:
- 实用工具函数(分数颜色、等级、严重性计数)
- localStorage 历史记录管理
- React 组件渲染
## 📡 API 参考
### `POST /api/audit`
分析 Solidity 智能合约。
**请求:**
```
{
"code": "pragma solidity ^0.8.0; contract MyContract { ... }"
}
```
**响应:**
```
{
"success": true,
"data": {
"score": 75,
"grade": "B",
"contractName": "MyContract",
"summary": "...",
"vulnerabilities": [...],
"gasOptimizations": [...],
"overallRecommendation": "..."
}
}
```
**速率限制:** 每个 IP 每小时 10 次请求
### `POST /api/chat`
与 AI 讨论已审计的合约。
**请求:**
```
{
"contractCode": "...",
"auditReport": {...},
"message": "Explain the reentrancy vulnerability",
"history": [...]
}
```
**响应:**
```
{
"success": true,
"message": "The reentrancy vulnerability occurs when..."
}
```
## 📄 许可证
MIT License — 详见 [LICENSE](LICENSE)。
标签:AI审计, AI对话助手, CertiK, CISA项目, DApp安全, DLL 劫持, Gas优化, Gemini, Monaco Editor, OpenZeppelin, PDF报告, Solidity, Streamlit, Trail of Bits, Web3安全, Web3开发, 云安全监控, 加密货币安全, 区块链安全, 去中心化应用安全, 大语言模型, 智能合约安全, 自动化攻击, 访问控制, 重入攻击, 静态分析