ChikoDoe/sentinel-waf

GitHub: ChikoDoe/sentinel-waf

基于 Claude AI 的智能 WAF 系统,实时监控 Nginx 日志并自动生成 Cloudflare 和 iptables 双层防护规则。

Stars: 1 | Forks: 0

Sentinel WAF Banner # 🛡️ Sentinel WAF **AI 驱动的 Web Application Firewall,全自动运行。** 监控 nginx 日志,检测攻击,并自动生成 Cloudflare + iptables/nftables 规则 —— 全部由 Claude AI 提供支持。 [![Node.js](https://img.shields.io/badge/Node.js-20+-339933?style=flat-square&logo=node.js&logoColor=white)](https://nodejs.org) [![Cloudflare](https://img.shields.io/badge/Cloudflare-WAF%20%2B%20Rate%20Limit-F38020?style=flat-square&logo=cloudflare&logoColor=white)](https://cloudflare.com) [![Claude AI](https://img.shields.io/badge/AI-Claude%20Sonnet-CC785C?style=flat-square&logo=anthropic&logoColor=white)](https://anthropic.com) [![License](https://img.shields.io/badge/License-MIT-blue?style=flat-square)](LICENSE) [![Ubuntu](https://img.shields.io/badge/Ubuntu-20.04%2B-E95420?style=flat-square&logo=ubuntu&logoColor=white)](https://ubuntu.com) [演示](#demo) · [快速安装](#-instalasi-cepat) · [配置](#-konfigurasi) · [API 文档](#-rest-api) · [贡献](#-kontribusi)
### 实时威胁信息流 Live Threats Dashboard ### AI 规则生成器 AI Rule Generator ### Cloudflare 与 iptables 规则 Firewall Rules
## ✨ 主要功能 | 功能 | 说明 | |-------|-----------| | 🔍 **实时日志监控** | 实时追踪 nginx `access.log`,解析每一个传入请求 | | 🧠 **AI 规则生成** | Claude Sonnet 分析违规行为并生成精确规则 | | ☁️ **Cloudflare WAF** | 自动推送 WAF 规则,HTTPS 速率限制,通过 API 封禁 IP | | 🔥 **iptables / nftables** | 在 L3/L4 层封禁,速率限制,自动封禁子网 | | 📊 **实时仪表盘** | 带 WebSocket 的 React UI,无需刷新即可更新 | | 🎯 **URL 定向** | 添加想要监控的特定 URL/域名 | | ⏰ **规则自动过期** | 规则可设置为自动过期(例如:封禁 1 小时) | | 🛡️ **白名单** | 永远不会被封锁的 IP(你的 IP,监控服务器等) | | 💾 **持久化规则** | 规则自动保存到磁盘,重启后依然有效 | ## 🏗️ 架构 ``` ┌─────────────────────────────────────────────────────────────────┐ │ VPS / Server │ │ │ │ [Nginx] ──access.log──► [Log Reader] │ │ │ │ │ detect threat │ │ │ │ │ ▼ │ │ [Threat Engine] │ │ SQL/XSS/RCE/DDoS │ │ │ │ │ breach detected │ │ │ │ │ ▼ │ │ [Claude AI] 🤖 │ │ analyze → generate rules │ │ │ │ │ ┌────────────────────┼────────────────────┐ │ │ ▼ ▼ ▼ │ │ [Cloudflare API] [iptables/nft] [WebSocket] │ │ - WAF rules (L7) - L3/L4 block Dashboard UI │ │ - Rate limit HTTPS - Rate limiting Live updates │ │ - Geo challenge - Subnet block │ └─────────────────────────────────────────────────────────────────┘ ``` ## 📋 前置条件 在安装之前,请确保你拥有: - **VPS Ubuntu 20.04+** 或 Debian 11+(裸机) - **Nginx** 已运行并正在处理流量 - **Node.js 18+**(如果尚未安装,安装程序会自动安装) - **Cloudflare** 作为你域名的 DNS 代理 - **Anthropic API Key** — 在 [console.anthropic.com](https://console.anthropic.com) 注册 - **Cloudflare API Token** — 具有 `Firewall Services:Edit` 权限 - 对服务器的 **root/sudo** 访问权限 ## 🚀 快速安装 ### 选项 A —— 单行命令(推荐) ``` git clone https://github.com/ChikoDoe/sentinel-waf.git cd sentinel-waf sudo bash install.sh ``` 安装程序将自动执行: - ✅ 如果尚未安装,则安装 Node.js 20 - ✅ 设置 `iptables-persistent` 和 `nftables` - ✅ 安装所有 npm 依赖项 - ✅ 构建 React 仪表盘 - ✅ 创建 systemd 服务(`sentinel-waf`) - ✅ 在 8080 端口配置 nginx 反向代理 - ✅ 设置 sudoers 以便无需密码提示即可执行 iptables 安装程序完成后,请继续阅读 [配置](#-konfigurasi)。 ### 选项 B —— 手动设置
点击展开手动步骤 #### 1. 克隆仓库 ``` git clone https://github.com/ChikoDoe/sentinel-waf.git cd sentinel-waf ``` #### 2. 安装后端依赖 ``` cd backend npm install ``` #### 3. 构建仪表盘 ``` cd ../dashboard npm install npm run build ``` #### 4. 配置 ``` cd .. cp .env.example .env nano .env ``` #### 5. 运行后端 ``` cd backend node server.js ``` 仪表盘地址:`http://localhost:3001`
## ⚙️ 配置 编辑 `.env` 文件(如果使用安装程序,位于 `/opt/sentinel-waf/.env`): ``` sudo nano /opt/sentinel-waf/.env ``` ### 必需变量 ``` # ── AI Engine ──────────────────────────────── ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxx # ── Cloudflare ─────────────────────────────── CF_API_TOKEN=your_cloudflare_api_token CF_ZONE_ID=your_zone_id CF_ACCOUNT_ID=your_account_id # ── 安全 ───────────────────────────────── # 重要:添加你自己的 IP 以免被拦截! ALLOWED_IPS=1.2.3.4,5.6.7.8 ``` ### 获取 API Keys 的方法 #### 🔑 Anthropic API Key 1. 打开 [console.anthropic.com](https://console.anthropic.com) 2. 点击 **API Keys** → **Create Key** 3. 复制到 `ANTHROPIC_API_KEY` #### 🔑 Cloudflare API Token 1. 打开 [dash.cloudflare.com](https://dash.cloudflare.com) → **Profile** → **API Tokens** 2. 点击 **Create Token** → **Custom Token** 3. 设置以下权限: | 权限 | 值 | |-----------|-------| | Zone → Zone | Read | | Zone → Firewall Services | Edit | | Zone → Zone WAF | Edit | 4. **Zone Resources** → Include → 选择你的域名 5. 复制令牌到 `CF_API_TOKEN` #### 🔑 Cloudflare Zone ID 1. 打开 [dash.cloudflare.com](https://dash.cloudflare.com) → 选择域名 2. 在右侧边栏,向下滚动 → **Zone ID** 3. 复制到 `CF_ZONE_ID` ## 🔧 管理服务 ``` # 启动 sudo systemctl start sentinel-waf # 停止 sudo systemctl stop sentinel-waf # 重启(修改 .env 后) sudo systemctl restart sentinel-waf # 检查状态 sudo systemctl status sentinel-waf # 实时查看日志 sudo journalctl -u sentinel-waf -f ``` 访问仪表盘:**`http://YOUR_VPS_IP:8080`** ## 📁 项目结构 ``` sentinel-waf/ ├── install.sh # Auto-installer untuk Ubuntu/Debian ├── .env.example # Template konfigurasi ├── README.md ├── docs/ │ └── assets/ # ← taruh screenshot dashboard di sini │ ├── backend/ │ ├── server.js # Express API + WebSocket server │ ├── log-reader.js # Nginx log parser + threat detection engine │ ├── ai-engine.js # Claude AI rule generator │ ├── cloudflare.js # Cloudflare API integration (WAF + rate limit) │ ├── firewall.js # iptables/nftables manager │ └── package.json │ └── dashboard/ ├── index.html ├── vite.config.js ├── package.json └── src/ ├── main.jsx └── App.jsx # React dashboard (realtime via WebSocket) ``` ## 🔍 威胁检测引擎 Sentinel WAF 自动检测 10 多种类型的攻击: | 模式 | 攻击类型 | 严重程度 | |---------|--------------|----------| | `UNION SELECT`, `' OR 1=1`, `sleep()` | SQL 注入 | 🔴 严重 (CRITICAL) | | `