abhimishra21/AI-smart-contract-auditor-blockchain
GitHub: abhimishra21/AI-smart-contract-auditor-blockchain
AI 驱动的区块链智能合约安全审计平台,集成多种静态分析引擎与大语言模型,提供多链多语言的全流程漏洞扫描、风险评估和报告生成。
Stars: 0 | Forks: 0
# AI 驱动的区块链安全审计器

一款由 AI 驱动的全面区块链智能合约安全审计器。集成了多种静态分析引擎、基于 AI 的漏洞评估和风险评分。
## 🎯 功能
- ✅ **多区块链支持**:Ethereum、Solana、Polkadot、Cosmos、Aptos
- ✅ **语言检测**:Solidity、Rust、Move、Vyper、Python (Algorand)
- ✅ **静态分析**:Mythril、Slither、Semgrep、Solhint、自定义规则
- ✅ **风险评分**:基于 CVSS v3.1 的漏洞评估
- ✅ **AI 分析**:LLM 驱动的威胁建模和建议
- ✅ **报告生成**:PDF、HTML、JSON、DOCX 格式
- ✅ **异步处理**:使用 Redis 队列的 Celery worker
- ✅ **Web 仪表板**:带有实时更新的 React 前端
- ✅ **REST API**:带有 JWT 认证的 FastAPI
- ✅ **向量数据库**:用于知识库 RAG 的 Milvus
## 📋 项目结构
```
.
├── ai-agent/ # AI Agent implementation (LLM integration)
├── backend/ # FastAPI backend service
│ ├── main.py # API endpoints
│ ├── language_detector.py # Language detection & parsing
│ ├── static_analyzer.py # Vulnerability scanning
│ ├── risk_analyzer.py # Risk assessment
│ ├── report_generator.py # Report generation
│ └── requirements.txt
├── database/ # Database models
│ └── models.py # SQLAlchemy ORM
├── docker/ # Docker configurations
│ ├── docker-compose.yml
│ ├── Dockerfile.backend
│ ├── Dockerfile.worker
│ └── nginx.conf
├── frontend/ # React dashboard
│ ├── src/App.js
│ ├── src/App.css
│ ├── package.json
│ └── Dockerfile
├── rag/ # RAG Knowledge Base setup
├── reports/ # Generated audit reports
├── worker/ # Celery background tasks
│ └── tasks.py
└── docs/ # Documentation
```
## 🚀 快速开始
### 前置条件
- Docker & Docker Compose
- Git
- Python 3.11+(用于本地开发)
- Node.js 18+(用于前端开发)
### 选项 1:Docker Compose(推荐)
```
# Clone repository
git clone
cd AI-Blockchain-Security-Auditor
# 创建环境文件
cp backend/.env.example backend/.env
# 启动所有服务
docker-compose -f docker/docker-compose.yml up -d
# 等待服务变为健康状态
docker-compose -f docker/docker-compose.yml ps
# 访问应用程序
# Frontend: http://localhost:3000
# API: http://localhost:8000/api/v1
# Nginx: http://localhost:80
```
默认凭据:
- 用户名:`admin`
- 密码:`admin`
### 选项 2:本地开发
#### 后端设置
```
cd backend
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# 安装依赖
pip install -r requirements.txt
# 运行 API 服务器
uvicorn main:app --reload
```
#### Worker 设置
```
cd worker
# 在另一个终端中
# 激活与 backend 相同的 venv
source ../backend/venv/bin/activate
# 启动 Celery worker
celery -A tasks worker --loglevel=info
# 在另一个终端中启动 Celery beat(定时任务)
celery -A tasks beat --loglevel=info
```
#### 前端设置
```
cd frontend
# 安装依赖
npm install
# 启动开发服务器
npm start
```
## 📚 API 文档
### 认证
```
# 登录
curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin"}'
# 响应
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"token_type": "bearer",
"expires_in": 1800
}
```
### 上传合约
```
curl -X POST http://localhost:8000/api/v1/contract/upload \
-H "Authorization: Bearer " \
-F "file=@contract.sol" \
-F "blockchain=ethereum"
```
### 开始分析
```
curl -X POST http://localhost:8000/api/v1/analysis/start \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{
"contract_name": "MyContract.sol",
"blockchain": "ethereum",
"analysis_type": "full"
}'
```
### 获取分析状态
```
curl -X GET http://localhost:8000/api/v1/analysis/{analysis_id}/status \
-H "Authorization: Bearer "
```
### 获取审计报告
```
curl -X GET http://localhost:8000/api/v1/analysis/{analysis_id}/report \
-H "Authorization: Bearer "
```
### 导出报告
```
curl -X POST http://localhost:8000/api/v1/reports/export \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{"analysis_id": "", "format": "pdf"}'
```
## 🔧 配置
### 后端配置 (.env)
```
# 安全
SECRET_KEY=your-secret-key-change-in-production
DEBUG=True
# 数据库
DATABASE_URL=postgresql://user:password@localhost:5432/auditor_db
# Redis/Cache
REDIS_URL=redis://localhost:6379/0
# 分析工具
USE_MYTHRIL=True
USE_SLITHER=True
USE_SEMGREP=True
USE_SOLHINT=True
# LLM 配置
LLM_MODEL=gpt-4
LLM_API_KEY=sk-your-openai-api-key
LLM_TEMPERATURE=0.7
# Vector Database
MILVUS_HOST=localhost
MILVUS_PORT=19530
# Celery
CELERY_BROKER_URL=redis://localhost:6379/1
CELERY_RESULT_BACKEND=redis://localhost:6379/2
```
## 📊 系统架构
```
┌─────────────────┐
│ Web Browser │
└────────┬────────┘
│
┌────▼────┐
│ Nginx │ (Reverse Proxy)
└────┬────┘
│
┌────┴────────────┐
│ │
┌───▼────┐ ┌──────▼──────┐
│Frontend │ │ Backend │
│ (React) │ │ (FastAPI) │
└────┬────┘ └──────┬──────┘
│ │
│ ┌───────┴───────┐
│ │ │
┌─▼──────────▼─┐ ┌──────────▼────┐
│ PostgreSQL │ │ Redis │
│ Database │ │ Cache/Broker │
└──────────────┘ └───────┬────────┘
│
┌────────┴────────┐
│ │
┌─────▼────┐ ┌─────▼─────┐
│ Workers │ │ Milvus │
│ (Celery) │ │ (Vector DB)
└──────────┘ └───────────┘
Analysis Pipeline:
1. Upload → Language Detection → Static Analysis
2. Risk Scoring → Threat Modeling
3. LLM Analysis → Report Generation
4. Results → Database & Cache
```
## 🧪 测试
```
# 运行 backend 测试
cd backend
pytest
# 运行 frontend 测试
cd frontend
npm test
# 运行集成测试
cd tests
pytest -v
```
## 📈 性能调优
### 数据库优化
```
-- Create indexes
CREATE INDEX idx_audits_user_id ON audits(user_id);
CREATE INDEX idx_audits_status ON audits(status);
CREATE INDEX idx_findings_audit_id ON findings(audit_id);
CREATE INDEX idx_findings_severity ON findings(severity);
-- Analyze query plans
EXPLAIN ANALYZE SELECT * FROM audits WHERE user_id = '...';
```
### Redis 配置
```
# 监控 Redis
redis-cli monitor
# 检查内存使用情况
redis-cli INFO memory
# 如有需要,清除 cache
redis-cli FLUSHDB
```
### Celery 优化
```
# 增加 workers
celery -A worker.tasks worker --concurrency=8
# 对 I/O bound 任务使用 gevent
celery -A worker.tasks worker -p gevent -c 1000
```
## 🐛 故障排除
### 数据库连接错误
```
# 检查 PostgreSQL
docker-compose ps | grep postgres
# 查看日志
docker-compose logs postgres
# 重新连接
docker-compose down && docker-compose up -d
```
### Worker 未处理任务
```
# 检查 Celery 状态
docker-compose logs worker
# 清除失败的任务
docker exec auditor-worker celery -A worker.tasks purge
# 重启 worker
docker-compose restart worker
```
### 前端无法加载
```
# 检查 Node modules
npm install
# 清除 cache
npm cache clean --force
# 重启 frontend
docker-compose restart frontend
```
## 📝 示例:分析 Solidity 合约
```
# 1. 获取认证 token
TOKEN=$(curl -s -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin"}' | jq -r '.access_token')
# 2. 上传 contract
RESPONSE=$(curl -s -X POST http://localhost:8000/api/v1/contract/upload \
-H "Authorization: Bearer $TOKEN" \
-F "file=@MyContract.sol" \
-F "blockchain=ethereum")
# 3. 启动分析
ANALYSIS=$(curl -s -X POST http://localhost:8000/api/v1/analysis/start \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"contract_name\": \"MyContract.sol\", \"blockchain\": \"ethereum\", \"analysis_type\": \"full\"}")
ANALYSIS_ID=$(echo $ANALYSIS | jq -r '.analysis_id')
# 4. 轮询状态
while true; do
STATUS=$(curl -s -X GET http://localhost:8000/api/v1/analysis/$ANALYSIS_ID/status \
-H "Authorization: Bearer $TOKEN")
echo "Status: $STATUS"
sleep 5
done
# 5. 获取报告
curl -s -X GET http://localhost:8000/api/v1/analysis/$ANALYSIS_ID/report \
-H "Authorization: Bearer $TOKEN" | jq .
```
## 🚢 部署
### 生产环境检查清单
- [ ] 更改默认凭据
- [ ] 设置强 SECRET_KEY
- [ ] 启用 SSL/TLS 证书
- [ ] 配置生产环境数据库
- [ ] 设置监控和警报
- [ ] 启用速率限制
- [ ] 正确配置 CORS
- [ ] 启用请求日志记录
- [ ] 设置备份策略
- [ ] 配置自动缩放
### 使用 Docker Swarm 部署
```
docker swarm init
docker stack deploy -c docker/docker-compose.yml auditor
```
### 使用 Kubernetes 部署
```
# 创建 ConfigMap
kubectl create configmap auditor-config --from-file=.env
# 部署服务
kubectl apply -f k8s/
```
## 📚 其他资源
- [FastAPI 文档](https://fastapi.tiangolo.com/)
- [Celery 文档](https://docs.celeryproject.org/)
- [React 文档](https://react.dev/)
- [Mythril 文档](https://mythril-classic.readthedocs.io/)
- [Slither 文档](https://slither.readthedocs.io/)
- [CVSS v3.1 指南](https://www.first.org/cvss/v3.1/specification-document)
## 📄 许可证
本项目基于 MIT 许可证授权 - 详情请参阅 LICENSE 文件。
## 📞 支持
如有问题、疑问或贡献:
- 在 GitHub 上创建 issue
- 电子邮件:support@example.com
- Discord:[加入我们的社区](#)
## 🙏 致谢
- OpenZeppelin 的智能合约安全模式
- Mythril 的 AST 分析
- Slither 的 Solidity 分析
- LangChain 的 LLM 集成
- 所有贡献者和社区成员
**版本**:1.0.0
**最后更新**:2024-07-10
**状态**:生产就绪 ✅
标签:AV绕过, DLL 劫持, FastAPI, React, Syscalls, 人工智能, 区块链安全, 大语言模型, 搜索引擎查询, 智能合约审计, 测试用例, 用户模式Hook绕过, 网络测绘, 请求拦截, 逆向工具, 错误基检测, 静态代码分析