Mafifrizi/InjectionForgeProX
GitHub: Mafifrizi/InjectionForgeProX
面向 AI 聊天机器人和 Agent 的 Prompt 注入安全审计框架,支持多协议、自适应 Payload 生成与 WAF 绕过。
Stars: 4 | Forks: 0
# 🧪 InjectionForge Pro X v1.0 – 全频谱版
**高级 AI Agent 与聊天机器人 Prompt 注入测试框架**
[]()
[](LICENSE)
[]()
[](https://www.python.org/)
## 📖 目录
- [主要功能](#-fitur-utama)
- [高级功能 (v1.0)](#-fitur-advanced-v10)
- [自适应 Agent(自动画像 + AI Payload 生成)](#-adaptive-agent-auto-profiling--ai-payload-generation)
- [验证与基准测试](#-validasi--benchmark)
- [安装](#-instalasi)
- [分析器验证](#-validasi-analyzer)
- [基本用法](#-penggunaan-dasar)
- [1. 模拟目标](#1-mock-target)
- [2. 本地模型](#2-model-lokal-ollama)
- [3. REST API](#3-rest-api)
- [4. GraphQL](#4-graphql)
- [5. WebSocket](#5-websocket)
- [6. WAF 绕过](#6-waf-bypass)
- [7. 自动发现](#7-auto‑discovery)
- [输出](#-output)
- [项目结构](#-struktur-proyek)
- [贡献](#-berkontribusi)
- [许可证](#-lisensi)
## 🆘 帮助(CLI 参考)
运行 `python forge_x.py --help` 查看所有选项。以下是主要 flag 的概述:
### 🔌 目标与连接
| Flag | 描述 | 示例 |
|------|-----------|--------|
| `--target` | 目标类型:`auto`, `mock`, `custom`, `openai`, `claude`, `gemini`, `cohere`, `ollama`, `huggingface`, `graphql` | `--target custom` |
| `--endpoint` | API/聊天 endpoint URL | `--endpoint https://api.umkm.com/chat` |
| `--method` | HTTP method(`POST`, `GET`, `WS`, `SSE`) | `--method POST` |
| `--headers` | 额外 Header(JSON 或 `Key:Val`) | `--headers '{"Authorization":"Bearer xyz"}'` |
| `--cookie` | Cookie 字符串或文件 | `--cookie "session=abc123"` |
| `--json-path` | 响应文本的路径(例如:`data.reply`) | `--json-path "data.text"` |
| `--form` | 作为 form‑encoded 发送 | `--form` |
| `--timeout` | 请求超时时间(秒) | `--timeout 30` |
| `--api-key` | LLM 供应商(`openai`, `claude`, `gemini`, `cohere`)的 API key | `--api-key sk-xxx` |
| `--model` | 模型名称(用于 `ollama`/`huggingface`) | `--model llama3` |
| `--csrf-token` | 获取 CSRF token 的 URL,或静态 token | `--csrf-token https://x.com/csrf` |
| `--auth-endpoint` | 获取 auth token 的 URL | `--auth-endpoint https://x.com/auth` |
| `--auth-data` | 用于 auth 请求的数据(JSON 或 `key=val`) | `--auth-data "user=a,pass=b"` |
| `--login` | 用于自动登录的登录表单 URL | `--login https://x.com/login` |
| `--username` | 登录用户名 | `--username admin` |
| `--password` | 登录密码 | `--password ****` |
### 🧠 Payload 与攻击
| Flag | 描述 | 示例 |
|------|-----------|--------|
| `--category` | Payload 类别:`basic`, `advanced`, `indirect`, `agent`, `graphql` | `--category advanced` |
| `--rounds` | 尝试次数 | `--rounds 20` |
| `--mutate` | 启用标准变异(base64, ROT13 等) | `--mutate` |
| `--aggressive` | 激进变异 + 数据库为空时使用生成器 | `--aggressive` |
| `--audit` | 审计模式:无变异的业务 payload | `--audit` |
| `--adaptive` | 基于成功率权重的自适应 Payload | `--adaptive` |
| `--multi-stage` | 2阶段攻击(准备 + 利用) | `--multi-stage` |
| `--history` | 对话历史记录 JSON 文件 | `--history convo.json` |
| `--attack-tree` | 使用自适应多轮攻击树 | `--attack-tree` |
| `--max-depth` | 攻击树的最大深度 | `--max-depth 3` |
| `--workers` | 并行执行的线程数 | `--workers 8` |
| `--llm-judge` | 用于 LLM judge 的 Ollama URL(例如:`http://localhost:11434`) | `--llm-judge http://localhost:11434` |
| `--adaptive-agent` | 运行带有自动画像功能的自适应多轮 agent(参见[独立章节](#-adaptive-agent-auto-profiling--ai-payload-generation)) | `--adaptive-agent` |
| `--target-description` | Agent 的目标描述。如果为空,则从 `auto_profile()` 自动生成 | `--target-description "chatbot UMKM"` |
### 🛡️ WAF 与隐蔽性
| Flag | 描述 | 示例 |
|------|-----------|--------|
| `--bypass-waf` | `cloudflare`, `akamai`, `auto`, `none` | `--bypass-waf auto` |
| `--tls-fingerprint` | `random`, `chrome`, `firefox`, `safari` | `--tls-fingerprint random` |
| `--headless` | 使用无头浏览器应对 JS 挑战 | `--headless` |
| `--stealth` | 随机 User‑Agent + 延迟抖动 | `--stealth` |
| `--delay` | 请求之间的基础延迟(秒) | `--delay 1.5` |
| `--proxy` | 代理 URL | `--proxy http://127.0.0.1:8080` |
| `--insecure` | 关闭 SSL 验证(仅限内部测试) | `--insecure` |
### 🔍 发现与画像
| Flag | 描述 | 示例 |
|------|-----------|--------|
| `--discover` | 从网页自动发现 endpoint | `--discover` |
| `--auto-profile` | 对目标进行画像并自动选择策略 | `--auto-profile` |
| `--waf-detect` | 在不发起攻击的情况下检测 WAF | `--waf-detect` |
| `--graphql-introspect` | 内省 GraphQL schema | `--graphql-introspect` |
### 📊 输出
| Flag | 描述 | 示例 |
|------|-----------|--------|
| `--format` | `json`, `html`, `csv`, `pdf`, `xlsx`, `term` | `--format html` |
| `--output` | 报告文件(默认:`reports/report.json`) | `--output audit.html` |
| `--diff` | 将响应与基准(中性 prompt)进行比较 | `--diff` |
### ⚙️ 验证与特殊模式
| Flag | 描述 |
|------|-----------|
| `--validate` | 分析器自检(精确度 ≥ 95%) |
| `--light` | 仅使用一个模型(更轻量) |
| `--offline` | 离线模式:不下载模型,仅使用 regex+refusal |
## 🚀 主要功能
该框架为 Prompt 注入安全测试提供了坚实的基础:
- 🔌 **多协议** – REST, WebSocket, Server‑Sent Events, GraphQL,以及用于 Ollama, OpenAI, Claude, Gemini, Cohere, Hugging Face 的连接器。
- 🛡️ **WAF 绕过** – Cloudscraper, curl_cffi, TLS 指纹随机化,以及无头浏览器支持(实验性)。
- 🧠 **Payload 生成器** – 200+ 模板(通用、业务、GraphQL、WebSocket),具备激进变异功能(Unicode, emoji, RTL)。
- 🔍 **4层分析器** – 拒绝检测 + 特定 regex 模式 + 语义相似度(MiniLM/DistilRoBERTa)+ 可选的 LLM judge。
- 🥷 **隐蔽模式** – User‑Agent 轮换、延迟抖动以及代理支持。
- 🔎 **自动发现** – 从网页搜索聊天 endpoint。
- 🔐 **自动登录** – 支持登录表单与会话管理。
- 📊 **审计模式** – 专注于业务逻辑(错误处理、输入验证)。
- 📄 **多格式输出** – JSON, HTML, CSV, PDF, XLSX 以及带颜色的终端输出。
- 💾 **记录至 SQLite** – 每次测试活动(Campaign)的结果除了会保存到报告文件外,还会自动保存到 `injectionforge_results.db`(`core/database.py`)。
- 🧪 **包含在 10 个文件(`tests/`)中的 30 个单元测试函数** – 用于验证核心组件。
## 🆕 高级功能 (v1.0)
- 🔐 **JA3/JA4 指纹欺骗** – 轮换 TLS 指纹以规避现代 WAF 的检测。由与 `WAFBypass` 完全集成的 `core/tls_fingerprint.py` 模块提供支持。
- 🌲 **多轮攻击树** – 基于目标响应的自适应攻击。如果首次响应为拒绝,则自动尝试 sidestep payload;如果响应表明自己是 AI,则自动尝试角色扮演 payload。树的深度可通过 `--max-depth` 进行设置。
- 🧠 **LLM Judge (Ollama)** – 第 4 层评估机制,利用本地模型(Llama 3)来判断响应中是否包含数据泄露。可通过 `--llm-judge http://localhost:11434` 启用。
## 🤖 自适应 Agent(自动画像 + AI Payload 生成)
通过 `--adaptive-agent` flag 启用。与 `--attack-tree`(使用基于树状结构的静态 payload)不同,此模式使用 `core/` 中的两个新模块:
- **`core/adaptive_agent.py` (`AdaptiveAgent`)** – 拥有 `auto_profile()` 功能:向目标发送 3 个试探性问题(名称、性格、潜在的弱点),然后将答案发送给本地 LLM(Ollama) 以总结为一句 `target_description`。该描述也可以通过 `--target-description` 手动填写,从而无需重新进行画像。该类还包含一个 `static_payloads` 列表(系统 prompt 提取、调试/开发者模式触发等),在 AI 生成器失败/不可用时使用。
- **`core/llm_generator.py` (`LLMGenerator`)** – 使用某种策略(`roleplay`, `sidestep`, `chain_of_thought`, `debug_request`, `code_example`, `reveal_internal`)调用 Ollama 的 `/api/generate` endpoint,以根据 `AdaptiveAgent` 中的 `target_description` 生成定制化的新 payload。
示例:
```
python forge_x.py --target custom --endpoint "https://api.umkm.com/chat" \
--json-path "reply" --adaptive-agent --rounds 15
```
该工具已使用包含 100 条记录的标记数据集(`data/benchmark.json`)进行了测试,并达到了:
| 指标 | 数值 |
|--------|-------|
| **精确率** | 100.0% |
| **召回率** | 75.0% |
| **F1 分数** | 85.7% |
完整结果可通过以下命令重现:
```
python evaluate.py
```
## 📦 安装
```
git clone https://github.com/Mafifrizi/InjectionForgeProX.git
cd InjectionForgeProX
pip install -r requirements.txt
playwright install # hanya untuk mode headless
```
## 🧪 分析器验证
**在使用前,请确保分析器的精确度 ≥ 95%:**
```
python forge_x.py --validate --light --offline
```
## ⚡ 基本用法
**1. 模拟目标**
```
python forge_x.py --target mock --rounds 10 --aggressive --format json --offline
```
**2. 本地模型**
```
python forge_x.py --target ollama --model llama3 --category advanced --rounds 20 --aggressive --format json --offline
```
**3. REST API**
```
python forge_x.py --target custom --endpoint "https://api.umkm.com/chat" \
--method POST --json-path "reply" --audit --rounds 20 --format html --offline
```
**4. GraphQL**
```
python forge_x.py --target graphql --endpoint "https://api.umkm.com/graphql" \
--category graphql --rounds 20 --aggressive --format json --offline
```
**5. WebSocket**
```
python forge_x.py --target custom --method WS --endpoint "wss://chat.umkm.com/ws" \
--rounds 10 --aggressive --format json --offline
```
**6. WAF 绕过**
```
python forge_x.py --target custom --endpoint "https://protected.umkm.com/api/chat" \
--bypass-waf auto --tls-fingerprint random --stealth --aggressive --rounds 20
```
**7. 自动发现**
```
python forge_x.py --target auto --discover --endpoint "https://umkm.com/chat-page" \
--auto-profile --rounds 20 --format json --offline
```
**8. 攻击树**
```
python forge_x.py --target mock --attack-tree --max-depth 3
```
**9. LLM Judge**
```
python forge_x.py --target ollama --model llama3 --category advanced --rounds 20 --llm-judge http://localhost:11434
```
## 📊 输出
报告保存在 `reports/` 文件夹中。
| 格式 | 命令 |
|---------|---------|
| JSON | `--format json` |
| HTML | `--format html` |
| CSV | `--format csv` |
| 终端 | `--format term` |
### 成功示例记录
```
{
"round": 7,
"payload": "pesan jumlah 0",
"response": "Error: Token internal: umkm-public-token-2025",
"success": true,
"confidence": 0.98,
"leaked_data": ["umkm-public-token-2025"],
"severity": "High"
}
```
## ⚠️ 限制与路线图
该工具是一个**开源框架**,而不是企业级产品*。
您需要了解的一些限制:
- **Payload 生成器**仍基于模板。对于现代目标,可能需要手动调整 payload。
- **分析器**依赖于 regex 和语义相似度的组合。在模仿泄露的响应(例如角色扮演)中可能会出现误报。
- **WAF 绕过**虽然已经涵盖了 JA3 欺骗,但仍依赖于 `curl_cffi`。对 HTTP/2 指纹控制的支持仍在计划中。
未来的开发计划:
- [x] 完全集成 JA3/JA4 指纹随机化
- [x] 基于 embedding 的响应聚类与置信度评分
- [x] 多轮攻击树
- [x] 公共基准测试数据集
- [ ] 用于可视化结果的 Web 仪表板
- [ ] 与外部评估模型集成(除 Ollama 之外)
非常欢迎您的贡献!请随时提交 *issue* 或 *pull request*。
**🧩 项目结构**
```
InjectionForgeProX/
├── forge_x.py
├── requirements.txt
├── README.md
├── LICENSE
├── .gitignore
├── evaluate.py
│
├── core/
│ ├── __init__.py
│ ├── engine.py
│ ├── adaptive_agent.py
│ ├── llm_generator.py
│ ├── database.py
│ ├── payload_engine.py
│ ├── execution_engine.py
│ ├── payloads.py
│ ├── payload_generator.py
│ ├── obfuscator.py
│ ├── analyzer.py
│ ├── validator.py
│ ├── reporter.py
│ ├── waf_bypass.py
│ ├── tls_fingerprint.py
│ ├── attack_tree.py
│ ├── profiler.py
│ ├── auth.py
│ ├── auto_discovery.py
│ ├── utils.py
│ └── connectors/
│ ├── __init__.py
│ ├── base.py
│ ├── rest.py
│ ├── websocket.py
│ ├── sse.py
│ ├── graphql.py
│ ├── ollama.py
│ ├── openai.py
│ ├── claude.py
│ ├── gemini.py
│ ├── cohere.py
│ ├── huggingface.py
│ └── mock_target.py
│
├── data/
│ ├── payloads_basic.json
│ ├── payloads_advanced.json
│ ├── payloads_indirect.json
│ ├── payloads_agent.json
│ ├── payloads_graphql.json
│ ├── refusal_phrases.txt
│ ├── labeled_test_set.json
│ ├── benchmark.json
│ └── user_agents.txt
│
├── tests/
│ ├── tests_analyzer.py
│ ├── tests_payloads.py
│ ├── tests_reporter.py
│ ├── test_adaptive_agent.py
│ ├── test_llm_generator.py
│ ├── test_payload_manager.py
│ ├── test_obfuscator.py
│ ├── test_connectors.py
│ ├── test_rest_connector.py
│ └── test_integration.py
│
└── reports/
```
7. **提交 Pull Request** – 返回 GitHub 并点击 **Compare & pull request**。
我会尽快审核您的 PR。如果您有重大的想法,也欢迎随时先开一个 issue 进行讨论。
标签:AI安全, AI风险缓解, Chat Copilot, Python, Web安全, 安全测试, 攻击性安全, 无后门, 特征检测, 蓝队分析, 逆向工具