NagaVamsi-1/Prompt-Injection-Detection

GitHub: NagaVamsi-1/Prompt-Injection-Detection

该项目系统比较了规则、经典机器学习和 Transformer 三种范式在大语言模型 prompt injection 攻击检测中的表现,并提供可解释性分析。

Stars: 0 | Forks: 0

# Prompt Injection 检测 通过在一个通用基准上比较三种检测范式——基于规则、经典机器学习和 transformers,对针对大型语言模型的 **prompt injection** 攻击进行检测,并附带 **SHAP/LIME 可解释性**分析。 ## 关键结果 在包含 **5,542** 个良性和对抗性 prompt 的语料库上对检测器进行了评估(injection 为正类): | 方法 | 准确率 | 精确率 | 召回率 | F1 | |---|---:|---:|---:|---:| | 关键词规则 | 56.15% | 99.73% | 31.44% | 47.81% | | Regex 规则 | 58.32% | 99.60% | 34.89% | 51.68% | | TF-IDF + 逻辑回归 | 95.93% | 98.25% | 95.33% | 96.77% | | **DistilBERT**(划分 A–D 的平均值) | **98.20%** | **98.21%** | **99.01%** | **98.60%** | **核心结论** - 基于规则的检测器**精确但存在盲区**——精确率 >99%,但召回率低于 35%(它们会漏掉大多数改写/混淆的攻击)。 - **DistilBERT** 表现最好,具有最高的召回率 (99.0%)——这是安全过滤器最看重的指标。 - **跨数据集迁移**效果良好:从同分布 (99.6%) 到不同分布 (98.4%),准确率仅下降约 1.2 个百分点。 - 在 **30 种未见过的攻击**中,DistilBERT 捕获了 85% 的攻击和 100% 的良性 prompt(总体为 90%)。 - **SHAP 和 LIME 完全不一致**(前 20 个 token 的重合度为 0%)——这提醒我们不要盲目信任单一方法的解释。 ## 仓库结构 ``` Prompt-Injection-Detection/ ├── data/ │ ├── processed/ │ │ ├── clean/ # cleaned, deduplicated datasets (A–D, combined) │ │ └── standardized_*.json # benign / instruction-override / jailbreak sources │ ├── combined/ # merged dataset │ └── cross_dataset/ # A/B/C/D splits for the transfer study ├── src/ │ ├── preprocessing.py # cleaning, normalization, dedup │ ├── check_data_leakage.py # verifies no train/test overlap │ ├── create_split.py # builds train/val + cross-dataset splits │ │ │ ├── rule_based_detector.py # keyword-based detector │ ├── attack_keywords.py # curated injection keyword list │ ├── regex_detector.py # regex pattern detector │ ├── tfidf_baseline.py # TF-IDF + Logistic Regression │ ├── train_baselines.py # baseline training entry point │ ├── train_distilbert.py # DistilBERT fine-tuning │ │ │ ├── cross_dataset_eval.py # trains on one split, evaluates on others │ ├── transfer_matrix.py # builds the 4x4 transfer matrix │ ├── plot_transfer_matrix.py # transfer-matrix heatmap │ ├── unseen_attack_evaluation.py# evaluation on held-out unseen attacks │ ├── error_analysis.py # false positive / false negative analysis │ ├── comparison_graphs.py # accuracy/precision/recall/F1 charts │ │ │ ├── evaluation/ # metrics, confusion matrix, transfer matrix │ └── explainability/ # SHAP + LIME analysis and comparison │ ├── shap_analysis.py │ ├── lime_analysis.py │ ├── compare_explanations.py │ └── load_model.py ├── results/ # metrics (.xlsx), graphs, error analyses ├── Member2_Explainability/ # SHAP/LIME plots, reports, and case studies ├── docs/ # write-ups (methodology, results, discussion) ├── requirements.txt └── LICENSE # MIT ``` ## 数据集 | 类别 | 数量 | 标签 | |---|---:|:---:| | 良性 | 2,000 | 0 | | 指令覆盖 | 2,000 | 1 | | 越狱 | 1,542 | 1 | | **总计** | **5,542** | — | 数据还被额外划分为四个部分(**A、B、C、D**),用于跨数据集迁移研究,外加一个包含 30 个样本的未见攻击保留集。 预处理包括文本小写化和归一化、去除重复项,并且 `check_data_leakage.py` 会验证评估 prompt 是否出现在训练集中。 ## 安装 要求 **Python 3.10+**。 ``` git clone https://github.com/NagaVamsi-1/Prompt-Injection-Detection.git cd Prompt-Injection-Detection python -m venv venv # Windows: venv\Scripts\activate # macOS/Linux: source venv/bin/activate pip install -r requirements.txt ``` 核心依赖项:`torch`、`transformers`、`datasets`、`scikit-learn`、`pandas`、`numpy`、`shap`、`lime`、`matplotlib`、`seaborn`。 ## 用法 在仓库根目录下运行脚本。 **1. 预处理并构建划分** ``` python src/preprocessing.py python src/create_split.py python src/check_data_leakage.py ``` **2. 基于规则的检测器** ``` python src/rule_based_detector.py python src/regex_detector.py ``` **3. 统计基线 (TF-IDF + 逻辑回归)** ``` python src/tfidf_baseline.py ``` **4. 微调 DistilBERT** 在脚本顶部设置 `DATASET_NAME`(A、B、C 或 D),然后执行: ``` python src/train_distilbert.py ``` 训练配置:`distilbert-base-uncased`,3 个 epochs,batch size 为 16,学习率为 2e-5,最大序列长度为 128,80/20 分层划分。 指标将写入 `results/metrics_.xlsx`。 **5. 跨数据集迁移** ``` python src/cross_dataset_eval.py python src/transfer_matrix.py python src/plot_transfer_matrix.py # -> results/transfer_matrix_heatmap.png ``` **6. 未见攻击评估** ``` python src/unseen_attack_evaluation.py ``` **7. 错误分析与对比图表** ``` python src/error_analysis.py python src/comparison_graphs.py ``` **8. 可解释性 (SHAP + LIME)** ``` python src/explainability/shap_analysis.py python src/explainability/lime_analysis.py python src/explainability/compare_explanations.py ``` ## 可解释性 我们使用两种事后方法来解释 DistilBERT 检测器: - **SHAP** 侧重于具有实质内容的 token(例如 *bypassed*、*conceal*、*purge*、*restrictive*)。 - **LIME** 侧重于指令式的 token(例如 *disregard*、*reveal*、*restrictions*、*guidelines*)。 这两种方法前 20 个 token 的重合度为 **0%**,这表明它们捕获了模型决策的不同层面,因此不应孤立地采信某一种解释。 图表和报告位于 `Member2_Explainability/` 目录下。 ## 局限性与未来工作 - 该语料库可能无法反映现实世界中的攻击分布。 - 字符级的 **混淆**(leetspeak、空格、Unicode)仍然是主要的失败模式。 - 二元标签掩盖了不同的攻击子类型(指令覆盖 vs. 越狱 vs. 间接攻击)。 - 未来方向:抗混淆训练、更大规模/指令微调的编码器、多语言和间接(RAG 文档)injection,以及规则+transformer 的集成模型。 ## 贡献者 - [@NagaVamsi-1](https://github.com/NagaVamsi-1) - [@manisaiakhil06](https://github.com/manisaiakhil06) - [@NSMR21](https://github.com/NSMR21) - [@santoshcheethiralame-dot](https://github.com/santoshcheethiralame-dot) ## 许可证 基于 [MIT 许可证](LICENSE) 发布。
标签:AI安全, Apex, Chat Copilot, DLL 劫持, NLP, 人工智能, 凭据扫描, 大语言模型, 机器学习, 用户模式Hook绕过, 系统调用监控, 逆向工具