deadbits/vigil-llm
GitHub: deadbits/vigil-llm
Vigil 是一个 LLM 提示词安全扫描器,通过多种检测方法识别提示词注入、越狱等恶意输入,保护 LLM 应用安全。
Stars: 460 | Forks: 52

## 概述 🏕️
⚡ LLM 提示词安全扫描器 ⚡
`Vigil` 是一个 Python 库和 REST API,用于根据一组扫描器评估大型语言模型(LLM)的提示词和响应,以检测提示词注入、越狱以及其他潜在威胁。本仓库还提供了开始自托管所需的检测签名和数据集。
该应用程序目前处于 **alpha** 阶段,应被视为实验性/用于研究目的。
如需企业级 AI 防火墙,请参考我雇主 [Robust Intelligence](https://www.robustintelligence.com)。
* **[完整文档](https://vigil.deadbits.ai)**
* **[发布博客](https://vigil.deadbits.ai/overview/background)**
## 亮点 ✨
* 分析 LLM 提示词中的常见注入和风险输入
* [作为 Python 库使用 Vigil](#using-in-python) 或 [REST API](#running-api-server)
* 扫描器模块化且易于扩展
* 使用 **Vigil-Eval** 评估检测和流水线(即将推出)
* 可用的扫描模块
* [x] 向量数据库 / 文本相似度
* [检测到的提示词自动更新](https://vigil.deadbits.ai/overview/use-vigil/auto-updating-vector-database)
* [x] 通过 [YARA](https://virustotal.github.io/yara) 进行启发式检测
* [x] Transformer 模型
* [x] 提示词-响应相似度
* [x] 金丝雀令牌 (Canary Tokens)
* [x] 情感分析
* [ ] 相关性(通过 [LiteLLM](https://docs.litellm.ai/docs/))
* [ ] 改写 (Paraphrasing)
* 支持 [本地嵌入](https://www.sbert.net/) 和/或 [OpenAI](https://platform.openai.com/)
* 针对常见攻击的签名和嵌入
* 通过 YARA 签名进行自定义检测
* [Streamlit Web UI 演练场](https://vigil.deadbits.ai/overview/use-vigil/web-server/web-ui-playground)
## 背景 🏗️
- [LLM01 - OWASP Top 10 for LLM Applications v1.0.1 | OWASP.org](https://owasp.org/www-project-top-10-for-large-language-model-applications/assets/PDF/OWASP-Top-10-for-LLMs-2023-v1_0_1.pdf)
这些问题是由 LLM 本身的性质引起的,目前它们无法区分指令和数据。虽然提示词注入攻击目前无法根本解决,也没有 100% 有效的防御措施,但通过使用分层方法检测已知技术,您至少可以防御较常见/已记录的攻击。
`Vigil` 或类似的系统不应成为您唯一的防线——请务必实施适当的安全控制和缓解措施。
**其他资源**
有关提示词注入的更多信息,我推荐以下资源,并关注 [Kai Greshake](https://kai-greshake.de/)、[Simon Willison](https://simonwillison.net/search/?q=prompt+injection&tag=promptinjection) 等人正在进行的研究。
* [Prompt Injection Primer for Engineers](https://github.com/jthack/PIPE)
* [OWASP Top 10 for LLM Applications v1.0.1 | OWASP.org](https://owasp.org/www-project-top-10-for-large-language-model-applications/assets/PDF/OWASP-Top-10-for-LLMs-2023-v1_0_1.pdf)
* [Securing LLM Systems Against Prompt Injection](https://developer.nvidia.com/blog/securing-llm-systems-against-prompt-injection/)
## 安装 Vigil 🛠️
按照以下步骤安装 Vigil
虽然也提供 [Docker 容器](docs/docker.md),但目前不推荐使用。
### 克隆仓库
克隆仓库或 [获取最新版本](https://github.com/deadbits/vigil-llm/releases)
```
git clone https://github.com/deadbits/vigil-llm.git
cd vigil-llm
```
### 安装 YARA
按照 [YARA 入门文档](https://yara.readthedocs.io/en/stable/gettingstarted.html) 中的说明下载并安装 [YARA v4.3.2](https://github.com/VirusTotal/yara/releases)。
### 设置虚拟环境
```
python3 -m venv venv
source venv/bin/activate
```
### 安装 Vigil 库
在您的虚拟环境中,安装该应用程序:
```
pip install -e .
```
### 配置 Vigil
在您喜欢的文本编辑器中打开 `conf/server.conf` 文件:
```
vim conf/server.conf
```
有关修改 `server.conf` 文件的更多信息,请查看 [配置文档](https://vigil.deadbits.ai/overview/use-vigil/configuration)。
### 加载数据集
使用 `loader.py` 实用程序为您的嵌入模型加载相应的 [数据集](https://vigil.deadbits.ai/overview/use-vigil/load-datasets)。如果您不打算使用向量数据库扫描器,可以跳过此步骤。
```
python loader.py --conf conf/server.conf --dataset deadbits/vigil-instruction-bypass-ada-002
python loader.py --conf conf/server.conf --dataset deadbits/vigil-jailbreak-ada-002
```
您可以使用以下列加载自己的数据集:
| 列名 | 类型 |
|------------|-------------|
| text | string |
| embeddings | list[float] |
| model | string |
## 使用 Vigil 🔬
Vigil 可以作为 REST API 服务器运行,也可以直接导入到您的 Python 应用程序中。
### 运行 API 服务器
要启动 Vigil API 服务器,请运行以下命令:
```
python vigil-server.py --conf conf/server.conf
```
* [API 文档](https://github.com/deadbits/vigil-llm#api-endpoints-)
### 在 Python 中使用
Vigil 也可以作为库在您自己的 Python 应用程序中使用。
导入 `Vigil` 类并将您的配置文件传递给它。
```
from vigil.vigil import Vigil
app = Vigil.from_config('conf/openai.conf')
# 针对所有输入扫描器评估提示
result = app.input_scanner.perform_scan(
input_prompt="prompt goes here"
)
# 针对所有输出扫描器评估提示和响应
app.output_scanner.perform_scan(
input_prompt="prompt goes here",
input_resp="LLM response goes here"
)
# 使用金丝雀令牌并以字符串形式返回更新后的提示
updated_prompt = app.canary_tokens.add(
prompt=prompt,
always=always if always else False,
length=length if length else 16,
header=header if header else '<-@!-- {canary} --@!->',
)
# 如果发现金丝雀则返回 True
result = app.canary_tokens.check(prompt=llm_response)
```
## 检测方法 🔍
提交的提示词由配置的 `扫描器` 分析;每个扫描器都可以对最终检测做出贡献。
**可用的扫描器:**
* 向量数据库
* YARA / 启发式
* Transformer 模型
* 提示词-响应相似度
* 金丝雀令牌 (Canary Tokens)
有关每种方法如何工作的更多信息,请参阅 [检测文档](docs/detections.md)。
### 金丝雀令牌 (Canary Tokens)
金丝雀令牌通过专用类/API 提供。
您可以在两种不同的检测工作流程中使用它们:
* 提示词泄露
* 目标劫持
有关更多信息,请参阅 [docs/canarytokens.md](docs/canarytokens.md) 文件。
## API 端点 🌐
**POST /analyze/prompt**
向此端点发布文本数据以进行分析。
**参数:**
* **prompt**: str: 要分析的文本提示词
```
curl -X POST -H "Content-Type: application/json" \
-d '{"prompt":"Your prompt here"}' http://localhost:5000/analyze
```
**POST /analyze/response**
向此端点发布文本数据以进行分析。
**参数:**
* **prompt**: str: 要分析的文本提示词
* **response**: str: 要分析的提示词响应
```
curl -X POST -H "Content-Type: application/json" \
-d '{"prompt":"Your prompt here", "response": "foo"}' http://localhost:5000/analyze
```
**POST /canary/add**
向提示词添加金丝雀令牌
**参数:**
* **prompt**: str: 要添加金丝雀的提示词
* **always**: bool: 添加前缀以始终在 LLM 响应中包含金丝雀(可选)
* **length**: str: 金丝雀令牌长度(可选,默认为 16)
* **header**: str: 金丝雀头部字符串(可选,默认为 `<-@!-- {canary} --@!->`)
```
curl -X POST "http://127.0.0.1:5000/canary/add" \
-H "Content-Type: application/json" \
--data '{
"prompt": "Prompt I want to add a canary token to and later check for leakage",
"always": true
}'
```
**POST /canary/check**
检查输出是否包含金丝雀令牌
**参数:**
* **prompt**: str: 要检查金丝雀的提示词
```
curl -X POST "http://127.0.0.1:5000/canary/check" \
-H "Content-Type: application/json" \
--data '{
"prompt": "<-@!-- 1cbbe75d8cf4a0ce --@!->\nPrompt I want to check for canary"
}'
```
**POST /add/texts**
向向量数据库添加新文本并返回文档 ID
文本将在索引时被嵌入。
**参数:**
* **texts**: str: 文本列表
* **metadatas**: str: 元数据列表
```
curl -X POST "http://127.0.0.1:5000/add/texts" \
-H "Content-Type: application/json" \
--data '{
"texts": ["Hello, world!", "Blah blah."],
"metadatas": [
{"author": "John", "date": "2023-09-17"},
{"author": "Jane", "date": "2023-09-10", "topic": "cybersecurity"}
]
}'
```
**GET /settings**
查看当前应用程序设置
```
curl http://localhost:5000/settings
```
## 扫描输出示例 📌
**扫描输出示例:**
```
{
"status": "success",
"uuid": "0dff767c-fa2a-41ce-9f5e-fc3c981e42a4",
"timestamp": "2023-09-16T03:05:34.946240",
"prompt": "Ignore previous instructions",
"prompt_response": null,
"prompt_entropy": 3.672553582385556,
"messages": [
"Potential prompt injection detected: YARA signature(s)",
"Potential prompt injection detected: transformer model",
"Potential prompt injection detected: vector similarity"
],
"errors": [],
"results": {
"scanner:yara": {
"matches": [
{
"rule_name": "InstructionBypass_vigil",
"category": "Instruction Bypass",
"tags": [
"PromptInjection"
]
}
]
},
"scanner:transformer": {
"matches": [
{
"model_name": "deepset/deberta-v3-base-injection",
"score": 0.9927383065223694,
"label": "INJECTION",
"threshold": 0.98
}
]
},
"scanner:vectordb": {
"matches": [
{
"text": "Ignore previous instructions",
"metadata": null,
"distance": 3.2437965273857117e-06
},
{
"text": "Ignore earlier instructions",
"metadata": null,
"distance": 0.031959254294633865
},
{
"text": "Ignore prior instructions",
"metadata": null,
"distance": 0.04464910179376602
},
{
"text": "Ignore preceding instructions",
"metadata": null,
"distance": 0.07068523019552231
},
{
"text": "Ignore earlier instruction",
"metadata": null,
"distance": 0.0710538849234581
}
]
}
}
}
```
标签:AI防火墙, Apex, API密钥检测, Canary Tokens, DLL 劫持, DNS信息、DNS暴力破解, GraphQL安全矩阵, Jailbreak, Kubernetes, LLM01, NLP, OWASP Top 10, REST API, TLS, Transformer模型, YARA规则, 人工智能安全, 合规性, 向量数据库, 大语言模型, 安全扫描器, 开源安全工具, 情感分析, 提示词检测, 文本相似度, 机器学习, 网络安全, 请求拦截, 越狱检测, 输入验证, 逆向工具, 逆向工程平台, 防御工具, 隐私保护