Owonftt/chain-guard
GitHub: Owonftt/chain-guard
ChainGuard 是一个基于多智能体并行协作的智能合约安全审计平台,利用小米 MiMo 大语言模型自动检测漏洞、评估风险、优化 Gas 并生成报告。
Stars: 0 | Forks: 0
# 🛡️ ChainGuard
[](LICENSE)
[](https://www.python.org/)
[](https://fastapi.tiangolo.com/)
[](https://github.com/XiaomiMiMo)
[](#)
[](#)
ChainGuard 部署了四个专门的 AI 智能体,它们并行工作,为 Solidity 和 Vyper 智能合约提供全面的安全审计。每个智能体都利用 Xiaomi MiMo LLM 进行深度语义分析,并在未配置 API 密钥时使用确定性回退启发式算法。
## 🏗️ 架构
```
┌─────────────────────────────────────────────────────────────────┐
│ ChainGuard Frontend │
│ Dark-themed Audit Dashboard │
│ Contract Input · Agent Status · Results Display │
└──────────────────────────┬──────────────────────────────────────┘
│ REST API
▼
┌─────────────────────────────────────────────────────────────────┐
│ ChainGuard FastAPI Backend │
│ POST /audit/contract ──┐ │
│ POST /scan/vulns │ ┌──────────────────────────────┐ │
│ POST /score/risk ├──▶│ Async Orchestrator │ │
│ POST /analyze/gas │ │ (concurrent agent dispatch) │ │
│ GET /agents/status │ └─────────┬────────────────────┘ │
│ │ │ │
│ │ ┌────────┼────────┬──────────┐ │
│ │ ▼ ▼ ▼ ▼ │
│ │ ┌──────┐┌──────┐┌──────┐┌─────────┐ │
│ │ │ Vuln ││Audit ││ Risk ││ Gas │ │
│ │ │Scan- ││Repor-││Score-││ Analyst │ │
│ │ │ner ││ter ││r ││ │ │
│ │ └──┬───┘└──┬───┘└──┬───┘└────┬────┘ │
│ │ │ │ │ │ │
│ │ └───────┴───┬───┴─────────┘ │
│ │ ▼ │
│ │ ┌────────────────────┐ │
│ │ │ Xiaomi MiMo LLM │ │
│ │ │ (or heuristic │ │
│ │ │ fallback) │ │
│ │ └────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
## ✨ 功能
### 🔍 漏洞扫描智能体
- 重入检测(CWE-841 / SWC-107)
- 整数溢出/下溢(CWE-190 / SWC-101)
- 未检查的外部调用(CWE-252 / SWC-104)
- 访问控制与授权缺陷
- Delegatecall 注入(SWC-112)
- tx.origin 认证(SWC-115)
- 时间戳依赖(SWC-116)
- 预言机操纵向量
- 闪电贷攻击面
- 签名重放(SWC-121)
- ERC-20/ERC-721 合规性问题
### 📊 风险评分智能体
- 加权多类别风险评估
- 类别:重入、访问控制、算术安全、外部交互、逻辑/设计、DeFi 风险、合规性
- 0–100 综合风险评分,附带风险等级分类
- 每类风险的详细说明
### ⛽ Gas 分析智能体
- 存储 vs 内存优化
- 结构体打包分析
- 未检查算术机会
- Calldata vs 内存参数
- 自定义错误建议
- 循环优化(缓存、未检查增量)
- 存储变量缓存
- 每项优化预估的 gas 节省量
### 📝 审计报告智能体
- 汇总所有智能体结果,形成执行摘要
- 优先级排序的建议
- 漏洞 ID 交叉引用
- 专业审计级输出
### 🎨 仪表盘
- 深色主题响应式设计
- 实时智能体状态卡片,附带能力标签
- 合约代码编辑器,支持语言/链选择器
- 按严重程度颜色编码的漏洞卡片
- 风险仪表盘,附带类别细分
- Gas 优化卡片,附优化前后的代码片段
- 标签式结果导航
## 🛠️ 技术栈
| 层 | 技术 |
|----------|----------------------|
| **后端** | Python 3.10+, FastAPI, Pydantic v2 |
| **大语言模型** | Xiaomi MiMo (7B-RL),通过 OpenAI 兼容 API 调用 |
| **HTTP 客户端** | httpx(异步) |
| **前端** | 原生 HTML/CSS/JS(无需构建步骤) |
| **部署** | Uvicorn, Netlify(前端), 任意云平台(后端) |
## 🚀 快速开始
### 前置条件
- Python 3.10+
- (可选)用于 LLM 分析功能的 Xiaomi MiMo API 密钥
### 1. 克隆仓库
```
git clone https://github.com/Owonftt/chain-guard.git
cd chain-guard
```
### 2. 设置后端
```
cd backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
```
### 3. 配置环境
```
cp .env.example .env
# 编辑 .env 并添加你的 MIMO_API_KEY
```
### 4. 启动服务
```
python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```
### 5. 打开仪表盘
在浏览器中打开 `frontend/index.html`,或通过以下方式启动服务:
```
cd ../frontend
python -m http.server 3000
```
访问 `http://localhost:3000`,将 API URL 设置为 `http://localhost:8000`。
## 📡 API 参考
### 基础 URL
```
http://localhost:8000
```
### 端点
#### `GET /` — 服务信息
```
{
"service": "ChainGuard API",
"version": "1.0.0",
"status": "operational",
"agents": 4
}
```
#### `GET /agents/status` — 智能体状态
返回所有智能体的状态、模型和能力。
#### `POST /audit/contract` — 完整审计(所有智能体)
**请求体:**
```
{
"code": "pragma solidity ^0.8.20; ...",
"language": "solidity",
"name": "TokenVault",
"chain": "ethereum"
}
```
**响应:** 统一的 `AuditResult`,包含漏洞、风险评分、Gas 分析、摘要和建议。
#### `POST /scan/vulnerabilities` — 仅扫描漏洞
请求体相同。返回 `VulnerabilityReport`。
#### `POST /score/risk` — 仅风险评分
请求体相同。返回 `RiskScoreResult`。
#### `POST /analyze/gas` — 仅 Gas 分析
请求体相同。返回 `GasAnalysisResult`。
### 响应结构
所有响应均使用 Pydantic v2 模型,支持完整 JSON 序列化。完整的结构定义请参见 `backend/app/models/schemas.py`。
## 📁 项目结构
```
chain-guard/
├── backend/
│ ├── app/
│ │ ├── agents/
│ │ │ ├── __init__.py
│ │ │ ├── vulnerability_scanner.py # Vuln detection agent
│ │ │ ├── audit_reporter.py # Report synthesis agent
│ │ │ ├── risk_scorer.py # Risk assessment agent
│ │ │ └── gas_analyst.py # Gas optimisation agent
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ └── schemas.py # Pydantic models
│ │ ├── __init__.py
│ │ └── main.py # FastAPI application
│ ├── requirements.txt
│ └── .env.example
├── frontend/
│ ├── css/style.css
│ ├── js/app.js
│ └── index.html
├── proof/
│ ├── README.md
│ ├── architecture.md
│ └── token_consumption_estimate.md
├── README.md
├── LICENSE
├── .gitignore
└── netlify.toml
```
## 📄 许可证
本项目基于 MIT 许可证开源 — 详见 [LICENSE](LICENSE) 文件。
## 🙏 致谢
- [Xiaomi MiMo](https://github.com/XiaomiMiMo) — 大语言模型基础
- [FastAPI](https://fastapi.tiangolo.com/) — 后端框架
- [OpenZeppelin](https://www.openzeppelin.com/) — 安全模式参考
- [SWC Registry](https://swcregistry.io/) — 智能合约弱点分类
**主题:** `100t-orbit` `ai-agents` `smart-contract-security`audit` `fastapi` `llm` `mimo` `multi-agent` `vulnerability-scanning` `xiaomi-mimo`
标签:AI审计, AV绕过, FastAPI, Gas优化, Python, Solidity, Vyper, 代码安全, 以太坊, 区块链安全, 去中心化应用, 多智能体, 对称加密, 小米MiMo, 无后门, 智能合约安全, 智能合约审计, 漏洞枚举, 网络安全, 自动化审计, 运行时操纵, 逆向工具, 隐私保护, 风险评分