Akhilesh-kolli/Enterprise-Purple-Team-Validation-Platform
GitHub: Akhilesh-kolli/Enterprise-Purple-Team-Validation-Platform
一个基于 FastAPI 构建的企业级紫队平台,通过对手模拟和 MITRE ATT&CK 覆盖率评估来自动化验证安全检测能力并生成检测差距报告。
Stars: 0 | Forks: 0
# 企业级紫队验证与检测覆盖平台
一个企业级的紫队平台,用于自动化对手模拟、SIEM 检测验证、MITRE ATT&CK 覆盖率评估和检测差距分析。
## 架构概述
基于 **Clean Architecture** 原则构建:
- **API 层**:带有类型安全 Pydantic schema 的 FastAPI 路由
- **服务层**:不依赖于框架的业务逻辑
- **仓库层**:数据访问抽象
- **领域模型**:带有类型提示的 SQLAlchemy ORM
- **集成**:隔离的、可插拔的工具集成
- **Worker**:通过 Celery 处理异步任务
**技术栈**:
- 后端:Python 3.12, FastAPI, SQLAlchemy 2.0, Celery
- 数据库:支持异步的 PostgreSQL
- 缓存:Redis
- 基础设施:Docker Compose
## 快速开始
### 前置条件
- Docker & Docker Compose
- Python 3.12+(用于本地开发)
- Git
### 1. 克隆仓库
```
git clone https://github.com/Akhilesh-kolli/Enterprise-Purple-Team-Validation-Platform.git
cd Enterprise-Purple-Team-Validation-Platform
```
### 2. 环境设置
```
# 复制示例环境
cp .env.example .env
# 对于开发,.env 中的默认值应该可以工作
# 对于生产环境,更改 JWT_SECRET_KEY 并更新凭据
```
### 3. 启动服务(Docker Compose)
```
# 启动所有服务(PostgreSQL、Redis、FastAPI、Celery)
docker-compose up -d
# 验证所有服务是否正在运行
docker-compose ps
```
**运行中的服务**:
- FastAPI 后端:http://localhost:8000
- PostgreSQL:localhost:5432
- Redis:localhost:6379
- pgAdmin:http://localhost:5050(可选)
### 4. 初始化数据库并填充数据
```
# 选项 A:使用 Docker Compose
docker-compose exec backend python scripts/seed_data.py
# 选项 B:本地 Python(需要 venv 设置)
python scripts/seed_data.py
```
**创建的默认用户**:
- **admin** / AdminPassword123(ADMIN 角色)
- **purple_team_user** / TestPassword123(PURPLE_TEAM 角色)
- **soc_analyst** / TestPassword123(SOC_ANALYST 角色)
- **detection_eng** / TestPassword123(DETECTION_ENGINEER 角色)
- **read_only_user** / TestPassword123(READ_ONLY 角色)
### 5. 测试 API
```
# 健康检查
curl http://localhost:8000/api/v1/health
# 登录
curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "AdminPassword123"}'
# 响应
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"token_type": "bearer",
"expires_in": 1800
}
# Refresh token
curl -X POST http://localhost:8000/api/v1/auth/refresh \
-H "Content-Type: application/json" \
-d '{"refresh_token": ""}'
```
### 6. 查看 API 文档
- **Swagger UI**:http://localhost:8000/docs
- **ReDoc**:http://localhost:8000/redoc
## 本地开发(不使用 Docker)
### 1. 设置 Python 虚拟环境
```
cd backend
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```
### 2. 设置数据库
```
# 确保 PostgreSQL 在本地 localhost:5432 上运行
# 手动创建数据库或更新 .env 中的 DATABASE_URL
# 复制 .env.example 到 .env 并在需要时更新
cp ../.env.example .env
```
### 3. 运行数据库迁移(准备就绪后 - 第一阶段)
```
# 设置 Alembic(目前为占位符)
# alembic upgrade head
```
### 4. 填充初始数据
```
python scripts/seed_data.py
```
### 5. 启动 FastAPI 服务器
```
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```
### 6. 启动 Celery Worker(可选)
```
# 需要 Redis 在 localhost:6379 上运行
celery -A app.celery_app worker --loglevel=info
```
## 项目结构
```
.
├── backend/
│ ├── app/
│ │ ├── main.py # FastAPI application
│ │ ├── config.py # Configuration
│ │ ├── database.py # Database connection
│ │ ├── dependencies.py # Dependency injection
│ │ ├── logging.py # Structured logging
│ │ └── celery_app.py # Celery configuration
│ ├── api/
│ │ └── v1/
│ │ ├── routes/ # API endpoint handlers
│ │ │ ├── auth.py # Authentication
│ │ │ └── health.py # Health checks
│ │ └── schemas/ # Pydantic request/response models
│ ├── domain/
│ │ ├── models/ # SQLAlchemy ORM models
│ │ ├── repositories/ # Data access layer
│ │ └── services/ # Business logic
│ ├── integrations/ # External tool integrations (Phase 3+)
│ ├── workers/ # Celery async tasks (Phase 3+)
│ ├── utils/
│ │ ├── exceptions.py # Custom exceptions
│ │ └── security.py # JWT, password hashing
│ ├── tests/ # Unit & integration tests
│ └── requirements.txt # Python dependencies
├── frontend/ # React/TypeScript (Phase 9)
├── docker/
│ ├── Dockerfile.backend # Backend container image
│ └── Dockerfile.frontend # Frontend container image (Phase 9)
├── docker-compose.yml # Multi-container orchestration
├── scripts/
│ ├── seed_data.py # Initial data seeding
│ └── ... # Additional scripts
├── docs/ # Documentation
├── configs/ # Configuration files
├── .gitignore # Git ignore rules
├── .env.example # Environment variables template
└── README.md # This file
```
## 认证与授权
### JWT Token
- **Access Token**:短期有效(30 分钟),用于 API 请求
- **Refresh Token**:长期有效(7 天),用于获取新的 access token
- **Token 类型**:Bearer
### 角色与权限
| 角色 | 权限 |
|------|-------------|
| **ADMIN** | 完全的系统访问权限、用户管理、设置 |
| **PURPLE_TEAM** | 执行攻击、查看所有结果 |
| **SOC_ANALYST** | 查看告警、事件、仪表板 |
| **DETECTION_ENGINEER** | 管理检测规则、查看覆盖率 |
| **READ_ONLY** | 对仪表板的只读访问权限 |
### Header 格式
```
Authorization: Bearer
```
## API 端点(第一阶段)
### 认证
- `POST /api/v1/auth/login` - 登录并获取 token
- `POST /api/v1/auth/register` - 注册新用户(READ_ONLY 角色)
- `POST /api/v1/auth/refresh` - 刷新 access token
### 健康与状态
- `GET /api/v1/health` - 系统健康状态(数据库、Redis、版本)
## 数据库 Schema(第一阶段)
### users
- id (PK)
- username (unique, indexed)
- email (unique, indexed)
- full_name
- hashed_password
- role (enum: admin, purple_team, soc_analyst, detection_engineer, read_only)
- is_active
- is_superuser
- created_at, updated_at
### audit_logs
- id (PK)
- user_id (FK)
- action
- resource_type
- resource_id
- details
- ip_address
- user_agent
- status
- created_at, updated_at
## 测试
### 运行测试
```
# 单元测试
pytest backend/tests/unit -v --cov=backend
# 集成测试
pytest backend/tests/integration -v
# 带覆盖率的所有测试
pytest backend/tests -v --cov=backend --cov-report=html
```
### 测试覆盖率
- 目标:70%+ 覆盖率
- 重点领域:服务层、仓库层、实用工具
## 日志记录
使用 `structlog` 的结构化 JSON 日志:
```
from app.logging import get_logger
logger = get_logger(__name__)
logger.info("event_name", key1="value1", key2="value2")
```
**日志输出**(生产环境):
```
{"event": "event_name", "key1": "value1", "key2": "value2", "timestamp": "2024-01-09T10:00:00Z"}
```
## 安全注意事项
### 密钥管理
- **切勿将 `.env` 提交**到版本控制系统
- 始终使用 `.env.example` 作为模板
- 在生产环境中更改 `JWT_SECRET_KEY`
- 使用高强度的数据库和 Redis 密码
### 密码哈希
- 使用调整过参数的 Argon2
- 加盐:通过 Passlib 自动进行
### HTTPS
- 生产环境:通过 Nginx 反向代理启用 HTTPS
- 开发环境:可接受 HTTP
## 第一阶段完成清单
- [x] 项目文件夹结构
- [x] Docker Compose 环境
- [x] PostgreSQL 数据库设置
- [x] FastAPI 骨架
- [x] 带有 RBAC 的用户模型
- [x] JWT 认证(登录、刷新)
- [x] 结构化日志
- [x] 健康检查端点
- [x] 管理员用户种子脚本
- [x] 环境变量
- [x] Requirements.txt
- [x] 包含设置说明的 README
## 下一步(第二阶段)
**资产管理**:
- 资产的 CRUD API
- 资产类型(服务器、工作站、DC、防火墙、云)
- 资产搜索与过滤
- 批量 CSV 导入
- Agent 跟踪
## 许可证
企业许可证 - 仅限内部使用
## 支持
如有问题、疑问或功能请求,请联系安全架构团队。
**最后更新**:2026-07-09
**阶段**:1 - 已完成
**状态**:已准备好进行第二阶段
标签:ATT&CK评估, AV绕过, FastAPI, Python, 搜索引擎查询, 无后门, 测试用例, 紫队, 请求拦截, 逆向工具, 防御验证