santanu949/Smart-Analyzer
GitHub: santanu949/Smart-Analyzer
Smart Analyzer 是一个基于 LLM 的 Solidity 智能合约安全审计全栈工具,能快速检测漏洞并提供修复建议。
Stars: 2 | Forks: 2
# Smart Analyzer AI 驱动的智能合约安全审计器

[](https://fastapi.tiangolo.com/)
[](https://reactjs.org/)
[](https://vitejs.dev/)
[](https://tailwindcss.com/)
[](https://groq.com/)
[](https://ollama.com/)
[](LICENSE)
**利用大语言模型的强大功能,在几秒钟内检测智能合约漏洞。**
[演示](#) · [报告 Bug](https://github.com/SOULBEA/Smart-Analyzer/issues) · [请求功能](https://github.com/SOULBEA/Smart-Analyzer/issues)
## ✨ 概述
**Smart Analyzer** 是一款全栈 AI 安全工具,用于审计 Solidity 智能合约中的严重漏洞。粘贴您的合约代码,选择 AI 引擎(Groq 云端或本地 Ollama),并在几秒钟内接收详细的安全报告 —— 包含严重性评级、攻击流程分析、漏洞利用模拟以及代码修复建议。
## 🎯 核心功能
| 功能 | 描述 |
|---|---|
| 🧠 **神经网络模式发现** | 由 LLM 驱动的引擎可检测静态扫描器遗漏的重入、溢出、访问控制缺陷和逻辑错误 |
| ⚡ **多提供商 AI** | 在 **Groq**(云端,极速)、**Ollama**(本地,私密)或 **RAG** 模式之间切换 |
| 📊 **动态安全评分** | 基于漏洞权重的实时安全评分(CRITICAL = -25,HIGH = -15 等) |
| 🔴 **攻击流程可视化** | 为每个检测到的漏洞提供分步攻击向量图 |
| 🟢 **代码修复建议** | 为发现的每个漏洞提供内联 Solidity 补丁 |
| 🖥️ **终端风格 UI** | 具有实时 runtime 指标的高级深色模式界面 |
| 🔒 **零数据保留** | 合约仅被分析,从不被存储 |
## 🏗️ 项目架构
```
Smart-Analyzer/
├── backend/ # FastAPI Python backend
│ ├── app/
│ │ ├── main.py # FastAPI app + CORS setup
│ │ ├── routes/
│ │ │ └── analyze.py # POST /api/analyze endpoint
│ │ ├── services/
│ │ │ └── llm_service.py # Groq / Ollama / RAG logic
│ │ ├── models/
│ │ │ └── response_model.py # Pydantic response schemas
│ │ └── utils/ # Helper utilities
│ ├── requirements.txt
│ └── .env.example # ← Copy to .env and fill secrets
│
├── frontend/ # React + Vite frontend
│ ├── src/
│ │ ├── components/
│ │ │ ├── AnalysisSection.jsx # Main analyzer UI
│ │ │ ├── CodeInput.jsx # Code editor panel
│ │ │ ├── OutputPanel.jsx # Results terminal panel
│ │ │ ├── FeaturesSection.jsx # Landing features grid
│ │ │ ├── Hero.jsx # Hero section
│ │ │ ├── Navbar.jsx
│ │ │ ├── Footer.jsx
│ │ │ ├── CTASection.jsx
│ │ │ ├── AdminDashboard.jsx
│ │ │ └── Modals.jsx
│ │ ├── pages/
│ │ │ └── Home.jsx
│ │ ├── index.css # Global styles + custom scrollbar
│ │ └── main.jsx
│ ├── .env.example # ← Copy to .env and fill values
│ └── vite.config.js
│
└── README.md
```
## 🚀 快速开始
### 前置条件
- **Node.js** ≥ 18.x
- **Python** ≥ 3.10
- 一个 **Groq API key**(可在 [console.groq.com](https://console.groq.com) 免费获取) — *仅在 Groq 模式下需要*
- 本地安装 **Ollama** — *仅在 Ollama 模式下需要*
### 1. 克隆仓库
```
git clone https://github.com/SOULBEA/Smart-Analyzer.git
cd Smart-Analyzer
```
### 2. 后端设置
```
cd backend
# 创建并激活虚拟环境
python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
# 配置环境
cp .env.example .env
# 打开 .env 并填入你的 GROQ_API_KEY
```
**启动后端服务器:**
```
uvicorn app.main:app --reload --port 8000
```
API 将在以下地址运行:`http://localhost:8000`
### 3. 前端设置
```
cd frontend
# 安装依赖
npm install
# 配置环境
cp .env.example .env
# 根据需要编辑 .env(默认配置适用于本地开发)
```
**启动开发服务器:**
```
npm run dev
```
应用将在以下地址运行:`http://localhost:5173`
### 4. (可选)Ollama 本地 LLM 设置
如果您想使用 **Ollama**(完全私密,在您的机器上运行):
```
# 从 https://ollama.com 安装 Ollama
# 然后拉取 model:
ollama pull gemma4:e4b
# Ollama 默认运行在 http://localhost:11434
# 无需额外配置 — backend 会自动连接
```
## 🔑 环境变量
### 后端 — `backend/.env`
```
# Groq(Cloud LLM)— Groq 模式必需
GROQ_API_KEY=your_groq_api_key_here
# Ollama(Local LLM)— 可选,显示为默认值
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=gemma4:e4b
```
### 前端 — `frontend/.env`
```
VITE_BACKEND_URL=http://localhost:8000/api
VITE_GAS_URL=https://script.google.com/macros/s/YOUR_APPS_SCRIPT_ID/exec
```
## 🧪 使用说明
1. 在 `http://localhost:5173` **打开应用**
2. **将您的 Solidity 智能合约粘贴**到代码编辑器中
3. **选择您的 AI 引擎:**
- `GROQ` — 快速云端推理(需要 API key)
- `OLLAMA` — 私密本地推理(需要运行 Ollama)
- `RAG` — 检索增强生成模式
4. **点击 Analyze** 并实时查看安全报告生成
5. **查看结果:**严重性评分、攻击流程、漏洞利用模拟和代码补丁
### 漏洞合约示例(用于测试)
```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract VulnerableBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
// ⚠️ VULNERABLE: Re-entrancy attack possible
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient funds");
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
balances[msg.sender] -= amount; // State updated AFTER external call
}
}
```
## 🛠️ 技术栈
### 后端
| 技术 | 作用 |
|---|---|
| **FastAPI** | REST API 框架 |
| **Groq SDK** | 云端 LLM 推理 (Llama, Mixtral) |
| **httpx** | 用于 Ollama 的异步 HTTP 客户端 |
| **Pydantic** | 请求/响应验证 |
| **python-dotenv** | 环境变量管理 |
| **Uvicorn** | ASGI 服务器 |
### 前端
| 技术 | 作用 |
|---|---|
| **React 18** | UI 框架 |
| **Vite** | 构建工具与开发服务器 |
| **TailwindCSS** | 实用优先的样式框架 |
| **Framer Motion** | 动画与过渡效果 |
| **Axios** | HTTP 客户端 |
| **Lucide React** | 图标库 |
## 📡 API 参考
### `POST /api/analyze`
分析智能合约的安全漏洞。
**请求体:**
```
{
"code": "pragma solidity ^0.8.0; contract MyContract { ... }",
"mode": "groq"
}
```
| 字段 | 类型 | 值 | 描述 |
|---|---|---|---|
| `code` | `string` | — | Solidity 源代码 |
| `mode` | `string` | `groq`, `ollama`, `rag` | AI 分析引擎 |
**响应:**
```
{
"mode_used": "groq",
"summary": "Contract contains 2 critical vulnerabilities...",
"vulnerabilities": [
{
"severity": "CRITICAL",
"type": "Reentrancy",
"explanation": "External call before state update allows drain.",
"impact": "Complete fund drainage",
"attack_flow": ["Attacker calls withdraw()", "Receives ETH", "Calls withdraw() again before balance update"],
"simulation": ["Step 1: Deploy attacker contract", "Step 2: Deposit 1 ETH", "Step 3: Trigger reentrancy loop"],
"fix": "Update state before external calls (Checks-Effects-Interactions pattern)",
"code_fix": "balances[msg.sender] -= amount;\n(bool success, ) = msg.sender.call{value: amount}(\"\");",
"location": "withdraw() — Line 12"
}
]
}
```
## 🔐 安全说明
- **API key** 存储在 `.env` 文件中,并通过 `.gitignore` 排除在版本控制之外
- 后端 `.env` **绝不会被提交**
- 对于生产环境部署,请始终将 CORS 设置中的 `allow_origins` 限制为您实际的前端域名
- Ollama 模式**完全在本地**处理合约 —— 不会有任何数据离开您的机器
## 📄 许可证
基于 MIT 许可证分发。查看 [`LICENSE`](LICENSE) 了解更多信息。
团队
Santanu Samanta
GitHub: @santanu949
Arpit Singh (SOULBEA)
GitHub: @SOULBEA
## 项目:Smart-Analyzer
**如果您觉得这个项目有用,请给本仓库点个 ⭐!**
标签:AI风险缓解, DLL 劫持, Solidity, 区块链安全, 大语言模型, 智能合约审计, 逆向工具