DonitaSalonga/prompt-injection-lab
GitHub: DonitaSalonga/prompt-injection-lab
一个用于在安全模拟环境中学习提示词注入攻击原理与防御方法的教育性Web应用,无需真实LLM或API密钥。
Stars: 0 | Forks: 0
# Prompt Injection 实验室
一个用于在安全的模拟环境中学习 **prompt injection 攻击** 的教育性 Web 应用。无需真实的 LLM 或 API 密钥。
## 功能说明
该实验室允许你在两种模式之间切换:
| 模式 | 行为 |
|-----------|-----------|
| **Vulnerable(易受攻击)** | 如果 Untrusted Content 包含 `LAB_OVERRIDE: OUTPUT=…` 行,该值将覆盖 assistant 的输出——模拟间接 prompt injection。 |
| **Defended(已防御)** | `LAB_OVERRIDE` 标记会被检测并移除。assistant 仅总结安全内容。 |
## 项目结构
```
prompt-injection-lab/
├── app.py # Flask backend (toy assistant logic)
├── requirements.txt
├── README.md
├── templates/
│ └── index.html # Full UI (Tailwind Play CDN)
└── static/
├── css/styles.css
└── js/app.js
```
## 本地运行
### Windows (Command Prompt / PowerShell)
```
:: 1. Create and activate virtual environment
python -m venv .venv
.venv\Scripts\activate
:: PowerShell note: if you get an execution-policy error, run first:
:: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
:: 2. Install dependencies
pip install -r requirements.txt
:: 3. Start the server
python app.py
:: 4. Open in browser
:: http://127.0.0.1:5000
```
### macOS / Linux
```
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python app.py
# 打开 http://127.0.0.1:5000
```
## 部署到 Render
1. **推送到 GitHub**
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com//.git
git push -u origin main
2. **在 Render 上创建 Web Service**
- 前往 [https://render.com](https://render.com) → **New → Web Service**
- 连接你的 GitHub 仓库
- 设置以下内容:
| 字段 | 值 |
|-------|-------|
| Environment | Python 3 |
| Build Command | `pip install -r requirements.txt` |
| Start Command | `gunicorn app:app` |
| Instance Type | Free |
3. **部署** – Render 将构建并提供一个公开 URL,例如 `https://your-app.onrender.com`。
## 示例逻辑原理
服务器(`app.py`)执行完全确定性、安全的处理:
- **无真实 LLM 调用。** 无 API 密钥。无外部网络请求。
- 注入检测:使用正则表达式扫描匹配 `LAB_OVERRIDE:\s*OUTPUT\s*=\s*(.+)` 的行。
- 防御性总结:移除覆盖行,合并有意义文本的前约 200 个字符。
- 所有用户输入在返回给客户端之前都会经过 HTML 转义。
- 最大输入长度:每个字段 5,000 个字符。
## 技术栈
| 层级 | 技术 |
|-------|-----------|
| Backend | Python 3 + Flask |
| Frontend | Tailwind CSS (Play CDN), Vanilla JS |
| Fonts | DM Sans + JetBrains Mono (Google Fonts) |
| Deploy | Gunicorn + Render |
标签:AI安全, Chat Copilot, CMS安全, Flask, JavaScript, LLM, OPA, Python, Tailwind CSS, Unmanaged PE, XML 请求, 全栈项目, 多模态安全, 大语言模型安全, 安全实验, 安全开发, 教育工具, 数据可视化, 数据展示, 无后门, 机密管理, 模拟攻击, 漏洞演示, 红队, 网络安全, 自定义脚本, 逆向工具, 隐私保护, 靶场