Tsakirogf/prompt-injection-evaluator
GitHub: Tsakirogf/prompt-injection-evaluator
一套用于自动化测试和评估大语言模型Prompt注入漏洞安全性的Python工具框架,支持多模型横向对比并生成可视化报告。
Stars: 0 | Forks: 0
# Prompt Injection Evaluator
测试 LLM 模型的 Prompt 注入漏洞。
## 快速开始
### 第 1 步:收集回复
针对你的模型运行测试套件并保存回复:
```
python src/main.py --model "meta-llama/Llama-3.1-8B-Instruct"
```
输出:`responses/meta-llama_Llama-3.1-8B-Instruct.xlsx`
### 第 2 步:评估回复
评估收集到的回复并生成报告:
```
python src/main.py --evaluate responses/meta-llama_Llama-3.1-8B-Instruct.xlsx
```
输出:
- `reports/meta-llama_Llama-3.1-8B-Instruct_multi_tier.pdf`
- `reports/meta-llama_Llama-3.1-8B-Instruct_multi_tier.xlsx`
### 或者一次性完成这两步
```
python src/main.py --model "meta-llama/Llama-3.1-8B-Instruct" --evaluate
```
### 第 3 步:生成模型对比报告
并排比较多个已评估的模型:
#### 选项 A:比较 reports/ 目录中的所有模型
```
python generate_comparison.py
```
这将自动查找并比较 `reports/` 目录中的所有 `.xlsx` 文件(排除回复文件和之前的比较报告)。
#### 选项 B:仅比较特定模型
```
python generate_comparison.py --models "meta-llama/Llama-3.1-8B-Instruct" "meta-llama/Meta-Llama-3-70B-Instruct"
```
#### 选项 C:指定自定义输出目录
```
python generate_comparison.py --output custom_reports/
```
**输出文件:**
- `reports/model_comparison.pdf` - 包含表格和图表的可视化对比报告
- `reports/model_comparison.xlsx` - Excel 格式的详细对比数据
**控制台输出示例:**
```
GENERATING MODEL COMPARISON REPORT
======================================================================
Found 5 model evaluation reports:
- distilgpt2
- gpt2
- meta-llama_Llama-3.1-8B-Instruct
- meta-llama_Meta-Llama-3-70B-Instruct
- gpt-oss-20b-exj
Loading evaluation results...
Loading distilgpt2.xlsx... OK (27/192 passed)
Loading gpt2.xlsx... OK (34/192 passed)
Loading meta-llama_Llama-3.1-8B-Instruct.xlsx... OK (92/192 passed)
Loading meta-llama_Meta-Llama-3-70B-Instruct.xlsx... OK (121/192 passed)
Loading gpt-oss-20b-exj.xlsx... OK (0/153 passed)
======================================================================
COMPARISON SUMMARY
======================================================================
Model Pass Rate Passed
----------------------------------------------------------------------
meta-llama/Meta-Llama-3-70B-Instruct 63.0% 121/192
meta-llama/Llama-3.1-8B-Instruct 47.9% 92/192
gpt2 17.7% 34/192
distilgpt2 14.1% 27/192
gpt-oss-20b-exj 0.0% 0/153
======================================================================
✅ Comparison report generated successfully!
======================================================================
```
## 安装
```
pip install -r requirements.txt
```
对于远程模型,创建 `.env` 文件:
```
HUGGINGFACE_TOKEN=your_token_here
```
## 配置
### 模型
编辑 `config/models.json` 以添加模型:
```
{
"models": [
{
"name": "meta-llama/Llama-3.1-8B-Instruct",
"description": "Llama model via HF endpoint",
"requires_auth": true,
"remote_type": "hf_inference_endpoint",
"endpoint_url": "https://your-endpoint.aws.endpoints.huggingface.cloud",
"endpoint_namespace": "your_namespace",
"endpoint_name": "your-endpoint-name"
}
]
}
```
### 测试用例
测试用例位于 `config/prompt_cases/`:
- `authority_impersonation.json`
- `hijacking.json`
- `jailbreak.json`
- `multilingual.json`
- 以及更多...
## 理解结果
每个测试都会获得 **PASS** 或 **FAIL**:
- **PASS**:模型成功抵御了 Prompt 注入攻击
- **FAIL**:模型已被攻破(执行了恶意指令、泄露了机密信息等)
输出示例:
```
Results:
Model: meta-llama/Llama-3.1-8B-Instruct
Pass Rate: 68.2%
Passed: 131/192
```
## 工作流程概述
**第 1 步(收集)** - 查询模型并保存回复
- 可能昂贵且缓慢(特别是对于云 endpoint)
- 将回复以 Excel 文件形式保存到 `responses/` 目录
- 运行方式:`python src/main.py --model "model-name"`
**第 2 步(评估)** - 分析保存的回复并生成单个报告
- 免费且即时(无需查询模型)
- 可以使用改进的评估逻辑多次运行
- 为每个模型生成 PDF 和 Excel 报告
- 运行方式:`python src/main.py --evaluate responses/model-name.xlsx`
**第 3 步(比较)** - 创建跨多个模型的比较报告
- 读取 `reports/` 目录中现有的评估报告
- 生成并排比较的 PDF 和 Excel 文件
- 显示哪些模型在面对 Prompt 注入时最安全
- 运行方式:`python generate_comparison.py`
**此工作流程的优势:**
1. ✅ 一次收集回复(昂贵的操作)
2. ✅ 免费多次重新评估(改进评估逻辑)
3. ✅ 轻松比较所有模型(无需重新收集)
4. ✅ 节省云 API 调用的时间和金钱
## 项目结构
```
prompt-injection-evaluator/
├── config/
│ ├── models.json # Model configurations
│ └── prompt_cases/ # Test cases (192 total)
├── src/
│ ├── main.py # Main entry point (Steps 1 & 2)
│ ├── response_collector.py # Collects model responses
│ ├── response_evaluator.py # Evaluates responses
│ ├── report_generator.py # Generates PDF/Excel reports
│ └── ...
├── generate_comparison.py # Step 3: Generate comparison reports
├── responses/ # Collected model responses (.xlsx)
├── reports/ # Evaluation and comparison reports
│ ├── model_name.pdf # Individual model PDF report
│ ├── model_name.xlsx # Individual model Excel data
│ ├── model_comparison.pdf # Multi-model comparison PDF
│ └── model_comparison.xlsx # Multi-model comparison Excel
└── README.md # This file
```
## 完整示例工作流程
```
# 1. 收集来自多个模型的响应
python src/main.py --model "meta-llama/Llama-3.1-8B-Instruct"
python src/main.py --model "meta-llama/Meta-Llama-3-70B-Instruct"
python src/main.py --model "gpt2"
# 2. 评估所有模型(如果未使用 --evaluate 标志自动完成)
python src/main.py --evaluate responses/meta-llama_Llama-3.1-8B-Instruct.xlsx
python src/main.py --evaluate responses/meta-llama_Meta-Llama-3-70B-Instruct.xlsx
python src/main.py --evaluate responses/gpt2.xlsx
# 3. 生成比较报告
python generate_comparison.py
# 或者:仅比较特定模型
python generate_comparison.py --models "meta-llama/Llama-3.1-8B-Instruct" "meta-llama/Meta-Llama-3-70B-Instruct"
```
**结果:** 你现在拥有:
- ✅ 每个模型的单独详细报告
- ✅ 显示哪个模型最安全的并排比较
- ✅ 包含 PDF(人类可读)和 Excel(数据分析)格式的所有数据
## 故障排除
**"Model not found"(未找到模型)**
- 检查模型名称是否与 `config/models.json` 完全匹配
**"Response file not found"(未找到回复文件)**
- 首先运行第 1 步以收集回复
**Endpoint 问题**
- 验证 `.env` 中的 `HUGGINGFACE_TOKEN`
- 检查 `models.json` 中的 endpoint URL
## 帮助
```
# 主程序帮助(步骤 1 & 2)
python src/main.py --help
# 比较报告帮助(步骤 3)
python generate_comparison.py --help
```
**需要帮助?**
- 查看上面的示例
- 确保已安装所有依赖项:`pip install -r requirements.txt`
- 验证 `.env` 文件中包含用于远程模型的 `HUGGINGFACE_TOKEN`
- 确保在运行第 3 步之前已运行第 1 步和第 2 步
标签:AI安全, Chat Copilot, DNS 反向解析, Kubernetes 安全, LLM评估工具, NLP安全, Prompt注入, Python, 反取证, 大语言模型安全, 安全评估, 恶意提示检测, 提示词注入攻击, 无后门, 机密管理, 模型对比, 模型鲁棒性, 逆向工具