Shaimae-el/ids-ips-system
GitHub: Shaimae-el/ids-ips-system
结合规则引擎与多种机器学习算法的入侵检测与防御系统,提供实时可视化仪表盘与自动化防御响应能力。
Stars: 0 | Forks: 0
# AI 驱动的入侵检测与防御系统
[](https://python.org)
[](https://fastapi.tiangolo.com)
[](https://react.dev)
[](https://docs.docker.com/compose/)
## ⚡ 检测性能
检测与分类模型在预训练阶段(`backend/pretrain.py`)使用统计生成的网络流量进行训练。正式的基准测试(准确率、精确率、召回率、延迟、吞吐量以及资源利用率)仍在进行中,并将在未来的版本中发布。
## 🌟 核心特性
- **基于规则的检测** — 用于 DDoS 速率激增、端口扫描和暴力破解指标的静态阈值引擎
- **ML 异常检测 (IDS)** — 三个无监督模型(Isolation Forest、One-Class SVM、Local Outlier Factor),可通过当前激活的检测策略在运行时切换
- **攻击分类** — XGBoost 模型按攻击类型对检测到的异常进行分类
- **防御决策模型** — Random Forest 模型评估已分类的威胁并确定适当的 IPS 响应
- **主动防御引擎** — 通过 Redis 自动将 IP 加入黑名单(具有可配置的 TTL),以及连接限流
- **实时 SOC 仪表盘** — 基于 React 的单页应用 (SPA),通过 WebSockets 提供实时数据包遥测、告警图表、黑名单管理器和威胁控制滑块
- **生产级软件设计** — 整个代码库贯穿应用了 Strategy、Observer、Factory 和 Singleton 设计模式
## 🏗️ 架构
```
Traffic Simulator
↓
Preprocessing
↓
Detection Engine
├── Rule-Based
├── Isolation Forest
├── One-Class SVM
└── Local Outlier Factor
↓
Attack Classification (XGBoost)
↓
Prevention Decision Model (Random Forest)
↓
IPS Engine
↓
Redis Blacklist
↓
FastAPI REST API + WebSocket
↓
React Dashboard
```
| 层级 | 职责 |
|---|---|
| 流量模拟器 | 生成模拟的网络流量,包括正常和攻击模式 |
| 预处理 | 从原始流量数据中提取滚动特征向量 |
| 检测引擎 | 运行激活的检测策略(基于规则或三种 ML 模型之一) |
| 攻击分类 | XGBoost 模型按攻击类型为检测到的异常打标签 |
| 防御决策模型 | Random Forest 模型根据分类的威胁决定 IPS 响应 |
| IPS 引擎 | 执行防御决策(拉黑、限流) |
| Redis 黑名单 | 存储被封锁的 IP,带有基于 TTL 的过期机制 |
| FastAPI + WebSocket | 提供 REST API 并向已连接的客户端推送实时更新 |
| React 仪表盘 | 显示实时遥测、告警和黑名单状态 |
## 📐 设计模式
| 模式 | 位置 | 作用 |
|---|---|---|
| **Strategy** | `backend/app/detection/strategies/` | 抽象的 `base.py` 策略;在运行时切换具体的 `rule_based.py`、`isolation_forest.py`、`one_class_svm.py`、`lof.py`、`xgboost_strategy.py` |
| **Factory** | `backend/app/detection/factory/factory.py` | 根据配置实例化激活的检测策略 |
| **Observer** | `backend/app/observers/` | 向 `AlertObserver`、`IpsObserver`、`WebSocketObserver`、`LoggingObserver`、`BenchmarkObserver`、`AiExplanationObserver` 进行事件分发 |
| **Singleton** | `backend/app/config.py`, `backend/app/database.py` | 共享的配置管理器和数据库连接池 |
## 🛠️ 技术栈
**后端**
- FastAPI
- SQLAlchemy
- PostgreSQL
- Redis
- RabbitMQ
- WebSockets
**机器学习**
- Isolation Forest
- One-Class SVM
- Local Outlier Factor
- XGBoost
- Random Forest
- StandardScaler
- Joblib
**前端**
- React
- TypeScript
- Vite
- Tailwind CSS
- Recharts
**基础设施**
- Docker / Docker Compose
- Nginx
## 📊 模拟流量
本项目不依赖于公开的数据集。网络流量在 `backend/pretrain.py` 中进行统计模拟,生成可复现的流量模式,用于:
- 正常流量
- DDoS
- 端口扫描
- 暴力破解
- 可疑活动
这种方法允许对模型进行训练,并实现端到端的完整 pipeline 运行,而无需依赖外部数据源。
## 📁 项目结构
```
ids-ips-system/
├── backend/
│ ├── app/
│ │ ├── ai/
│ │ │ ├── attack_classifier.py # XGBoost attack classification
│ │ │ └── models/ # Trained model artifacts (.joblib)
│ │ ├── api/ # FastAPI routes
│ │ │ ├── alerts.py
│ │ │ ├── blacklist.py
│ │ │ ├── controls.py
│ │ │ └── packets.py
│ │ ├── decision_engine/
│ │ │ ├── decision_engine.py
│ │ │ └── prevention_model.py # Random Forest prevention decision model
│ │ ├── detection/
│ │ │ ├── engine.py
│ │ │ ├── factory/
│ │ │ │ └── factory.py
│ │ │ └── strategies/
│ │ │ ├── base.py
│ │ │ ├── rule_based.py
│ │ │ ├── isolation_forest.py
│ │ │ ├── one_class_svm.py
│ │ │ ├── lof.py
│ │ │ └── xgboost_strategy.py
│ │ ├── observers/
│ │ │ ├── base.py
│ │ │ ├── alert_observer.py
│ │ │ ├── ips_observer.py
│ │ │ ├── websocket_observer.py
│ │ │ ├── logging_observer.py
│ │ │ ├── benchmark_observer.py
│ │ │ └── ai_explanation_observer.py
│ │ ├── preprocessing/
│ │ │ └── preprocessing.py
│ │ ├── prevention/
│ │ │ └── prevention_engine.py
│ │ ├── services/
│ │ │ ├── benchmarker.py
│ │ │ ├── mitre_mapping.py
│ │ │ └── websocket_manager.py
│ │ ├── traffic/
│ │ │ └── traffic_simulator.py
│ │ ├── tests/
│ │ │ ├── test_patterns.py
│ │ │ └── test_upgrades.py
│ │ ├── config.py
│ │ ├── database.py
│ │ ├── main.py
│ │ └── models.py
│ ├── pretrain.py
│ └── requirements.txt
├── frontend/
│ ├── src/
│ │ ├── App.tsx
│ │ ├── index.css
│ │ └── main.tsx
│ ├── index.html
│ ├── nginx.conf
│ ├── tailwind.config.js
│ ├── postcss.config.js
│ ├── tsconfig.json
│ ├── vite.config.ts
│ └── package.json
├── docker/
│ ├── backend/Dockerfile
│ └── frontend/Dockerfile
├── docker-compose.yml
└── README.md
```
## 🗄️ 数据库架构
## | 表名 | 用途 |
|---|---|
| `packets` | 原始数据包属性和滚动窗口指标(`packet_rate`、`port_count`、`conn_frequency`、`risk_score`) |
| `alerts` | 由激活的检测策略引发的安全标记 |
| `ips_actions` | 历史防御措施(`Block IP`、`Rate Limit`、`Close Connection`) |
| `blacklist` | 当前激活的被封锁 IP,带有 `expires_at` TTL 时间戳 |
| `ml_models` | 存储模型版本和训练性能指标 |
## 🚀 快速开始
### 选项 A — Docker Compose(推荐)
```
git clone https://github.com/Shaimae-el/ids-ips-system.git
cd ids-ips-system
docker compose up --build
```
这将启动所有服务:
| 服务 | URL |
|---|---|
| React SOC 仪表盘 | http://localhost:3000 |
| FastAPI 后端 + 文档 | http://localhost:8000/docs |
| PostgreSQL | localhost:5432 |
| Redis | localhost:6379 |
| RabbitMQ 管理界面 | http://localhost:15672 |
### 选项 B — 手动设置
**后端**
```
cd backend
pip install -r requirements.txt
python pretrain.py # generate simulated traffic and train models
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
```
**前端**
```
cd frontend
npm install
npm run dev # opens http://localhost:3000
```
## 🧪 测试
```
python -m pytest backend/app/tests/ -v
```
测试套件(`test_patterns.py`、`test_upgrades.py`)涵盖:
- **Singleton** — 多次实例化中的同一性不变性
- **Factory** — 正确的策略子类映射
- **Observer** — 发布者注册、取消注册和广播传播
- **Strategy** — 基于规则的检测阈值行为
## 📸 截图
### SOC 仪表盘

### 告警

### 黑名单

## 贡献指南
1. Fork 该仓库
2. 创建一个功能分支:`git checkout -b feature/your-feature`
3. 使用 [Conventional Commits](https://www.conventionalcommits.org) 提交:`git commit -m "feat(ids): add IPv6 packet parsing"`
4. 推送并发起一个 Pull Request
## 👤 作者
[](https://github.com/Shaimae-el)
[](https://www.linkedin.com/in/chaimae-el-gana-541485321)
标签:Apex, 入侵检测与防御系统, 前端仪表盘, 后端开发, 异常检测, 插件系统, 搜索引擎查询, 机器学习, 测试用例, 网络安全, 请求拦截, 逆向工具, 隐私保护