Adarsh-Singh-Chauhan/CyberSentric
GitHub: Adarsh-Singh-Chauhan/CyberSentric
一个结合 Isolation Forest 机器学习异常检测与多智能体协同架构的全栈网络安全平台,提供实时威胁检测、自动响应和可视化仪表盘功能。
Stars: 0 | Forks: 0
# 🛡️ CyberSentric
### 具备自愈系统的实时自主网络防御
## 👥 团队成员
| 成员 | 角色 | 职责 |
|--------|------|----------------|
| **Krutika** | AI/ML 与文档负责人 | 设计了 Isolation Forest ML 流水线、特征工程、威胁分类模型以及项目文档 |
| **Kanishka** | 前端开发人员 | 构建了带有实时图表、玻璃拟态设计、WebSocket 实时推送和交互式威胁分析的 React 仪表盘 UI |
| **Monika** | 后端开发人员 | 实现了 FastAPI 多智能体架构、JWT 身份验证、响应自动化和 API 路由 |
| **Adarsh** | 系统架构与集成 | 设计了多智能体编排流水线、WebSocket 基础设施、红队模拟引擎以及系统集成 |
## 📋 目录
- [项目概述](#-project-overview)
- [架构](#-architecture)
- [技术栈](#-tech-stack)
- [文件夹结构](#-folder-structure-explained)
- [ML 流水线 — 工作原理](#-ml-pipeline--how-it-works)
- [多智能体系统](#-multi-agent-system)
- [API 端点](#-api-endpoints)
- [设置与安装](#-setup--installation)
- [运行应用程序](#-running-the-application)
- [截图](#-screenshots)
- [未来展望](#-future-scope)
## 🎯 项目概述
**CyberSentric** 是一个全栈网络安全平台,它结合了**人工智能**、**机器学习**和**多智能体系统**,以创建一个自主的网络防御解决方案。与传统的基于规则的安全工具不同,CyberSentric 使用:
1. **真正的 ML 异常检测**(Isolation Forest)——而不是虚拟/硬编码的逻辑
2. **5 个自主 AI 智能体**在协调的流水线中工作
3. **实时 WebSocket** 仪表盘,用于实时威胁监控
4. **自动化响应系统**,可以阻断 IP、进行速率限制并提醒管理员
5. **红队模拟**,用于测试和验证防御系统
### 核心功能
| 功能 | 描述 |
|---------|-------------|
| 🧠 **ML 异常检测** | 基于 800 多个合成基线样本训练的 Isolation Forest 模型,可在实时流量上自动重新训练 |
| 🤖 **多智能体流水线** | Defender → Analyzer → Response → Monitor 流水线处理每一个请求 |
| 📊 **实时仪表盘** | 通过 WebSocket 展示实时图表、威胁推送和智能体状态面板 |
| 🔐 **JWT 身份验证** | 基于角色的访问权限(管理员/用户),使用 bcrypt 进行密码哈希 |
| 🔴 **红队模拟** | 自动化攻击模拟(SQLi、XSS、暴力破解、提示注入) |
| ⚡ **自动响应** | 自动阻断 IP、速率限制和管理员警报 |
| 📈 **14 维特征向量** | 使用请求频率、熵、错误率等进行行为画像 |
| 🔄 **在线重训练** | 模型通过每 200 个样本重新训练一次,以适应流量漂移 |
## 🏗️ 架构
```
┌──────────────────────────────────────────────────────────────┐
│ REACT FRONTEND │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │Dashboard │ │ Charts │ │ Threat │ │ Red Team │ │
│ │ Panel │ │(Recharts)│ │ Feed │ │ Simulator │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │ │
│ └─────────────┴────────────┴───────────────┘ │
│ │ WebSocket + REST API │
└──────────────────────────┼───────────────────────────────────┘
│
┌──────────────────────────┼───────────────────────────────────┐
│ FASTAPI BACKEND │
│ │ │
│ ┌───────────────────────▼───────────────────────────┐ │
│ │ ORCHESTRATOR │ │
│ │ (Coordinates all agents in sequence) │ │
│ └──┬──────────┬──────────┬──────────────┬───────────┘ │
│ │ │ │ │ │
│ ┌──▼──┐ ┌───▼───┐ ┌──▼───┐ ┌──────▼──────┐ │
│ │DEFE-│ │ANALY- │ │RESP- │ │ MONITOR │ │
│ │NDER │ │ ZER │ │ONSE │ │ AGENT │ │
│ │AGENT│ │ AGENT │ │AGENT │ │ │ │
│ │ │ │ │ │ │ │ (Logging & │ │
│ │(Rule│ │(ML │ │(Auto │ │ WebSocket │ │
│ │Based│ │Isola- │ │Block │ │ Broadcast) │ │
│ │Scan)│ │tion │ │IPs) │ │ │ │
│ │ │ │Forest)│ │ │ │ │ │
│ └─────┘ └───┬───┘ └──────┘ └─────────────┘ │
│ │ │
│ ┌────────────▼────────────────────────────────┐ │
│ │ ML PIPELINE │ │
│ │ ┌──────────────┐ ┌───────────────────┐ │ │
│ │ │ Feature │ │ Isolation Forest │ │ │
│ │ │ Extractor │──│ Anomaly Detector │ │ │
│ │ │ (14 dims) │ │ (scikit-learn) │ │ │
│ │ └──────────────┘ └────────┬──────────┘ │ │
│ │ │ │ │
│ │ ┌──────────────────────────▼──────────┐ │ │
│ │ │ Threat Classifier │ │ │
│ │ │ (ML + Rules → Structured Output) │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ RED TEAM AGENT │ │
│ │ (Simulates attacks to test defenses) │ │
│ │ • Prompt Injection • XSS • SQLi │ │
│ │ • Command Injection • Brute Force │ │
│ └─────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
```
## 🛠️ 技术栈
| 层级 | 技术 | 使用原因 |
|-------|-----------|---------------|
| **前端** | React 18 + Vite | 快速、基于组件的 UI,支持热模块重载 |
| **样式** | Tailwind CSS | 快速的暗色主题样式设计,带有玻璃拟态效果 |
| **图表** | Recharts | 美观、动态的数据可视化 |
| **图标** | Lucide React | 简洁、现代的 SVG 图标集 |
| **后端** | FastAPI (Python) | 高性能异步 API,带有自动生成的文档 |
| **ML 模型** | Scikit-Learn (Isolation Forest) | 无监督异常检测 —— 学习什么是“正常”的 |
| **特征工程** | NumPy | 用于特征向量的快速数值计算 |
| **身份验证** | JWT (python-jose) + bcrypt | 安全的基于 token 的身份验证,带有密码哈希 |
| **实时通信** | WebSocket (FastAPI) | 无需轮询即可将更新即时推送到仪表盘 |
| **数据模型** | Pydantic | 严格的数据验证与序列化 |
## 📁 文件夹结构(说明)
```
CyberSentric/
│
├── start.bat # 🚀 One-click launcher — builds frontend & starts server
├── README.md # 📖 This file — complete project documentation
├── docker-compose.yml # 🐳 Docker setup for containerised deployment
├── .gitignore # 🚫 Files/folders excluded from Git
│
├── backend/ # ⚙️ PYTHON BACKEND (FastAPI)
│ │
│ ├── requirements.txt # 📦 All Python dependencies listed here
│ ├── .env # 🔑 Secret keys (DB URLs, JWT secret) — NEVER commit this
│ ├── .env.example # 📋 Template showing required environment variables
│ ├── Dockerfile # 🐳 Docker config for backend container
│ │
│ └── app/ # 🏠 Main application package
│ │
│ ├── main.py # 🚪 Entry point — creates FastAPI app, registers routes,
│ │ # starts monitor heartbeat, serves frontend build
│ │
│ ├── config.py # ⚙️ Loads settings from .env (DB URLs, JWT secrets,
│ │ # CORS origins, app name/version)
│ │
│ ├── orchestrator.py # 🎯 THE BRAIN — coordinates all 5 agents in sequence:
│ │ # Defender → Analyzer → Response → Monitor
│ │ # Also handles Red Team simulations
│ │
│ ├── websocket_manager.py # 📡 Manages WebSocket connections, broadcasts real-time
│ │ # events (threats, metrics) to all connected clients
│ │
│ ├── agents/ # 🤖 AI AGENT MODULES
│ │ ├── __init__.py
│ │ ├── base_agent.py # 🧱 Abstract base class — defines ThreatResult, AgentEvent,
│ │ │ # Severity enum, status tracking (all agents inherit this)
│ │ │
│ │ ├── defender.py # 🛡️ DEFENDER AGENT — First line of defense
│ │ │ # • Regex pattern matching for prompt injection
│ │ │ # • XSS / SQLi / Command injection detection
│ │ │ # • Shannon entropy analysis for obfuscation
│ │ │ # • Input sanitisation (strips malicious tags)
│ │ │
│ │ ├── analyzer.py # 🧠 ANALYZER AGENT — ML-Powered Intelligence (★ CORE)
│ │ │ # • Calls FeatureExtractor for 14-dim vectors
│ │ │ # • Runs Isolation Forest inference
│ │ │ # • Combines ML + behavioural flags + defender results
│ │ │ # • Returns structured threat classification
│ │ │
│ │ ├── response.py # ⚡ RESPONSE AGENT — Automated mitigation
│ │ │ # • Blocks malicious IPs
│ │ │ # • Applies rate limiting
│ │ │ # • Sends admin alerts
│ │ │ # • Logs incidents at appropriate severity
│ │ │
│ │ ├── monitor.py # 📊 MONITOR AGENT — System health tracking
│ │ │ # • Heartbeat system metrics (CPU, memory, connections)
│ │ │ # • Broadcasts events to WebSocket subscribers
│ │ │ # • Logs all threat responses for audit trail
│ │ │
│ │ └── redteam.py # 🔴 RED TEAM AGENT — Attack simulator
│ │ # • Prompt injection test suite (10 payloads)
│ │ # • XSS test suite (5 payloads)
│ │ # • SQL injection test suite (5 payloads)
│ │ # • Command injection test suite (5 payloads)
│ │ # • Brute force login simulation
│ │ # • Full suite — runs everything + reports detection rate
│ │
│ ├── ml/ # 🧪 MACHINE LEARNING PIPELINE
│ │ ├── __init__.py
│ │ ├── feature_extractor.py # 📐 Extracts 14-dimensional feature vectors:
│ │ │ # request_count, failed_logins, unique_endpoints,
│ │ │ # payload_size, time_delta, session_duration,
│ │ │ # error_rate, requests_per_minute, payload_entropy,
│ │ │ # unique_ips, is_login, is_failed, hour_of_day,
│ │ │ # special_char_ratio
│ │ │
│ │ ├── anomaly_detector.py # 🤖 Isolation Forest model:
│ │ │ # • Trains on 800 synthetic "normal" samples
│ │ │ # • StandardScaler normalisation
│ │ │ # • 150 estimator trees, 8% contamination
│ │ │ # • Auto-retrains every 200 live samples
│ │ │ # • Sigmoid-normalised anomaly scores (0-1)
│ │ │
│ │ └── threat_classifier.py # 🎯 Merges ML scores + rules → final output:
│ │ # { "threat": "brute_force",
│ │ # "severity": "high",
│ │ # "confidence": 0.92 }
│ │
│ └── routes/ # 🌐 API ENDPOINTS
│ ├── auth.py # 🔐 Authentication routes:
│ │ # POST /api/auth/login — JWT token login
│ │ # POST /api/auth/register — new user registration
│ │ # GET /api/auth/me — current user info
│ │
│ └── core.py # 📡 Core API routes:
│ # POST /api/analyze — run ML threat analysis
│ # GET /api/dashboard — aggregated dashboard data
│ # GET /api/agents — all agent statuses
│ # GET /api/threats — recent threat data
│ # GET /api/stats — chart statistics
│ # POST /api/redteam/simulate — run attack simulation
│ # WS /api/ws — WebSocket endpoint
│
├── frontend/ # 🎨 REACT FRONTEND (Vite)
│ │
│ ├── package.json # 📦 Node.js dependencies (React, Recharts, Lucide, Tailwind)
│ ├── vite.config.js # ⚡ Vite dev server config + API proxy to backend
│ ├── tailwind.config.js # 🎨 Tailwind theme (custom cyber colours, fonts, animations)
│ ├── postcss.config.js # CSS processing pipeline
│ ├── index.html # 🏠 HTML entry point (loads Google Fonts, favicon)
│ ├── Dockerfile # 🐳 Docker config for frontend
│ │
│ └── src/ # 📂 Source code
│ │
│ ├── main.jsx # 🚪 React entry — mounts App to DOM
│ ├── index.css # 🎨 Global styles:
│ │ # • Tailwind base/components/utilities
│ │ # • Glassmorphism .glass class
│ │ # • Neon glow effects (.neon-blue, .neon-red)
│ │ # • Cyber grid background
│ │ # • Severity badge classes
│ │ # • Custom scrollbar styling
│ │
│ ├── App.jsx # 🏠 Main app layout:
│ │ # • Sidebar navigation (6 tabs)
│ │ # • Dashboard / Agents / Threats / RedTeam / Logs / Settings
│ │ # • JWT auth flow + token management
│ │ # • WebSocket event tracking
│ │ # • 5-second polling for dashboard data
│ │
│ ├── services/
│ │ └── api.js # 🌐 API client — all fetch calls to backend
│ │ # (login, register, analyze, dashboard, stats, etc.)
│ │
│ ├── hooks/
│ │ └── useWebSocket.js # 📡 Custom React hook for WebSocket connection
│ │ # Auto-reconnects, parses events, tracks status
│ │
│ └── components/ # 🧩 REUSABLE UI COMPONENTS
│ ├── LoginScreen.jsx # 🔐 Beautiful login/register page with demo credentials
│ ├── Sidebar.jsx # 📋 Left navigation panel with 6 tab icons
│ ├── Topbar.jsx # 📊 Top bar showing connection status + quick stats
│ ├── RiskScore.jsx # 🎯 Animated risk gauge based on latest threat severity
│ ├── AgentPanel.jsx # 🤖 Grid of agent status cards (5 agents)
│ ├── ThreatFeed.jsx # 📡 Live scrolling feed of WebSocket threat events
│ ├── Charts.jsx # 📈 Line chart (threats over time) + Pie chart (attack types)
│ ├── ActionHistory.jsx # 📋 Table of all automated response actions taken
│ ├── InputAnalyzer.jsx # ⌨️ Text input to test threat detection with quick-test buttons
│ └── RedTeamPanel.jsx # 🔴 Red Team simulation launcher + results display
```
## 🧠 ML 流水线 — 工作原理
### 问题所在
传统的网络安全工具使用**基于规则的检测** —— 它们只能捕获被明确编程识别的攻击。未知的全新攻击模式则会成为漏网之鱼。
### 我们的解决方案:Isolation Forest(无监督学习)
Isolation Forest 是一种机器学习算法,它首先学习**“正常”流量的特征**,然后将任何偏离正常情况的数据标记为异常 —— 即使是它从未见过的攻击也能识别。
### 流水线步骤
#### 步骤 1:数据预处理与特征提取
每个传入的请求都会被转换为一个 **14 维数值特征向量**:
```
Feature Vector (14 dimensions):
───────────────────────────────────────────────────────────────
[0] request_count — How many requests in the last 10 min
[1] failed_login_count — Failed login attempts in window
[2] unique_endpoints — How many different URLs were accessed
[3] payload_size — Size of the request payload (chars)
[4] time_delta_seconds — Time since previous request
[5] session_duration — Total session length
[6] error_rate — Fraction of failed requests
[7] requests_per_minute — Request frequency
[8] payload_entropy — Shannon entropy (randomness) of input
[9] unique_ips_for_user — How many IPs this user has used
[10] is_login_action — Whether this is a login attempt
[11] is_failed_status — Whether the request failed
[12] hour_of_day — Time of day (normalised 0-1)
[13] special_char_ratio — Fraction of special characters
```
#### 步骤 2:模型训练 (Isolation Forest)
```
# 在启动时,模型在 800 个合成的“正常”样本上进行训练:
model = IsolationForest(
n_estimators=150, # 150 decision trees
contamination=0.08, # expect ~8% anomalies
max_features=1.0, # use all 14 features
)
model.fit(StandardScaler().fit_transform(normal_data))
```
**Isolation Forest 的工作原理:**
- 它构建随机的决策树,试图“隔离”每个数据点
- 正常点很难被隔离(它们与许多其他点相似)
- 异常点很容易被隔离(它们与所有其他点都不同)
- 被快速隔离的点 → **异常**(较短的树路径)
#### 步骤 3:实时推理
```
# 对于每个传入的请求:
features = feature_extractor.extract(event) # 14-dim vector
result = anomaly_detector.predict(features) # ML scoring
# 输出:
{
"anomaly_score": 0.78, # 0.0 = normal, 1.0 = anomalous
"classification": "high_threat", # normal | suspicious | high_threat
"confidence": 0.88, # model confidence
"ml_prediction": "anomaly" # raw model output
}
```
#### 步骤 4:威胁分类
ML 分数将与以下内容结合:
- **行为标记**(登录失败、请求速率、端点扫描)
- **Defender 智能体结果**(模式匹配到的注入/XSS/SQLi)
最终结构化输出:
```
{
"threat": "brute_force",
"severity": "high",
"confidence": 0.92,
"threat_category": "authentication_attack",
"recommended_actions": ["block_ip", "alert_admin", "log_critical"]
}
```
#### 步骤 5:在线重训练
模型使用积累的实时流量数据,**每累积 200 个新样本就会自动重新训练**。这使得它能够在无需人工干预的情况下,适应不断变化的流量模式。
## 🤖 多智能体系统
| 智能体 | 角色 | 工作原理 |
|-------|------|-------------|
| **🛡️ Defender** | 第一道防线 | 对已知攻击(提示注入、XSS、SQLi、命令注入)进行正则表达式模式匹配。计算信息熵以检测混淆操作。净化危险的输入。 |
| **🧠 Analyzer** | ML 大脑 | 运行完整的 ML 流水线:特征提取 → Isolation Forest 推理 → 威胁分类。将 ML 分数与行为分析相结合。 |
| **⚡ Response** | 自动响应器 | 执行建议的操作:阻断 IP、实施速率限制、发送警报、记录事件。维护拦截列表和操作历史。 |
| **📊 Monitor** | 观察者 | 通过 WebSocket 广播所有事件。跟踪系统指标(CPU、内存、连接数)。维护所有威胁-响应对的审计跟踪。 |
| **🔴 Red Team** | 测试员 | 通过完整的流水线模拟 5 种类型的攻击。测量检测率。验证防御系统是否正常工作。 |
## 🌐 API 端点
| 方法 | 端点 | 需要身份验证 | 描述 |
|--------|----------|------|-------------|
| `POST` | `/api/auth/login` | ❌ | 使用用户名/密码登录,返回 JWT |
| `POST` | `/api/auth/register` | ❌ | 注册新用户账号 |
| `GET` | `/api/auth/me` | ✅ | 获取当前用户信息 |
| `POST` | `/api/analyze` | ✅ | 提交输入以进行 ML 威胁分析 |
| `GET` | `/api/dashboard` | ✅ | 获取完整的仪表盘数据(智能体、警报、指标) |
| `GET` | `/api/agents` | ✅ | 获取所有 5 个智能体的状态 |
| `GET` | `/api/agents/{name}` | ✅ | 获取特定智能体的详情及事件 |
| `GET` | `/api/threats` | ✅ | 获取最近的警报、被拦截的 IP、操作历史 |
| `GET` | `/api/stats` | ✅ | 获取图表数据(攻击类型、严重程度分布) |
| `POST` | `/api/redteam/simulate` | 🔒 管理员 | 运行攻击模拟(full/xss/sqli 等) |
| `GET` | `/api/redteam/history` | ✅ | 获取过去的模拟结果 |
| `WS` | `/api/ws` | ❌ | 用于实时事件流的 WebSocket |
**默认凭据:**
- **管理员:** `admin` / `admin123`
- **用户:** `user` / `user123`
## 🚀 设置与安装
### 前置条件
- **Python 3.10+**(带有 pip)
- **Node.js 18+**(带有 npm)
- **Git**(用于版本控制)
### 快速设置
```
# 1. 克隆仓库
git clone https://github.com/YOUR_USERNAME/cybersentric.git
cd cybersentric
# 2. 设置 Backend
cd backend
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux/Mac
pip install -r requirements.txt
# 3. 设置 Frontend
cd ../frontend
npm install
# 4. 返回根目录
cd ..
```
### 环境变量
将 `backend/.env.example` 复制到 `backend/.env` 并进行配置:
```
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
CORS_ORIGINS=http://localhost:5173
DEBUG=true
```
## ▶️ 运行应用程序
### 选项 1:一键启动 (Windows)
```
# 双击 start.bat 或运行:
start.bat
```
这将在 `http://localhost:8000` 构建前端并启动统一服务器
### 选项 2:开发模式(使用两个终端)
```
# 终端 1 — Backend
cd backend
venv\Scripts\activate
uvicorn app.main:app --reload --port 8000
# 终端 2 — Frontend (支持热重载)
cd frontend
npm run dev
```
前端开发服务器:`http://localhost:5173`(将 API 调用代理到后端)
## 🔮 未来展望
- [ ] 集成 **Supabase/PostgreSQL** 以实现持久的威胁日志记录
- [ ] 采用 **LSTM 时间序列**模型进行高级时序异常检测
- [ ] 通过 Twilio 或 SendGrid 发送 **电子邮件/短信警报**,以应对严重威胁
- [ ] 使用 **Docker Compose** 一键部署到云端
- [ ] 集成 **Grafana** 以实现专业的监控仪表盘
- [ ] 接入 **威胁情报源**(VirusTotal、AbuseIPDB)以获取 IP 信誉
- [ ] 提供面向企业级部署的 **多租户** 支持
## 📄 许可证
本项目采用 **MIT 许可证** 授权 —— 可免费使用、修改和分发。
由 CyberSentric 团队用 ❤️ 构建
Krutika • Kanishka • Monika • Adarsh
标签:AMSI绕过, Apex, AV绕过, CISA项目, FastAPI, FTP漏洞扫描, IP 地址批量处理, Isolation Forest, JWT认证, MIT许可, PyRIT, Python, React, Scikit-learn, Syscalls, WebSocket, 人工智能, 仪表盘可视化, 依赖分析, 后端开发, 多智能体系统, 威胁分类, 威胁情报, 威胁检测, 安全智能, 安全编排, 开发者工具, 异常检测, 无后门, 机器学习, 用户模式Hook绕过, 系统架构, 红队模拟, 网络安全, 自主防御, 自动化响应, 自我修复, 请求拦截, 逆向工具, 隐私保护