Alien0525/llm-sentinel

GitHub: Alien0525/llm-sentinel

LLM Sentinel 是一个基于 AWS 的云原生多层防御框架,通过自定义 ML 分类器和 Bedrock Guardrails 保护云托管大语言模型免受提示注入和对抗性攻击,并提供安全基准测试能力来量化防御效果。

Stars: 0 | Forks: 0

# LLM Sentinel 针对大型语言模型对抗性攻击的云原生防御框架。 ## 概述 LLM Sentinel 模拟对云托管 LLM 的对抗性攻击,并衡量多层防御架构的有效性。系统在恶意提示词到达目标模型之前对其进行拦截,并在将输出结果返回给用户之前进行验证。 项目在两种场景下进行安全基准测试: - **基线** — 对未受保护的 LLM 进行直接攻击,以建立漏洞指标 - **受保护** — 通过所有三层防御层路由相同的攻击,以衡量改进效果 ## 团队 | 姓名 | 角色 | 侧重点 | |------|------|-------| | Varsha | 安全架构负责人 | 三层防御设计、AWS 基础设施、ML 流水线 | | Krisha Joshi | ML 和数据负责人 | 数据集管理、模型训练、TF-IDF 分类器 | | Aman NS | 云与集成负责人 | AWS 设置、API Gateway、Lambda、DynamoDB、CloudWatch | | Sisvanth | 后端与测试负责人 | 攻击模拟、基线测试、日志流水线 | ## 架构 ``` User Request | v API Gateway (POST /prompt) | v Layer 1: Lambda + Custom ML Classifier | Blocks known patterns, Base64/hex encoded payloads v Layer 2: Amazon Bedrock Guardrails (Input) | Handles sophisticated jailbreaks and semantic attacks v Target LLM — EC2 (t2.micro) + Ollama + TinyLlama | v Layer 3: Amazon Bedrock Guardrails (Output) | Scans response for PII, system prompt leakage, secrets v Response + Logging | +-- DynamoDB (attack logs and metrics) +-- S3 (bypass payloads for retraining) +-- CloudWatch + SNS (monitoring and alerts) | v Streamlit Dashboard (EC2) ``` ## 防御层 ### 第 1 层 — Edge Filter 基于已标注的提示注入数据集训练的自定义二元分类器。作为容器化的 Lambda 函数通过 Amazon ECR 部署。针对已知攻击模式(包括 Base64 和十六进制编码的 payload)提供快速、低延迟的拦截。 - 模型:Scikit-learn TF-IDF 向量化器 + 逻辑回归 - 在本地训练,保存为 `model.pkl`,使用 Docker 容器化 - 部署到 AWS Lambda 以进行实时推理 ### 第 2 层 — Semantic Guard 在传入的提示词通过第 1 层后对其应用 Amazon Bedrock Guardrails。通过分析语义含义,检测基于模式的分类器所遗漏的复杂越狱和注入尝试。 ### 第 3 层 — Egress Auditor 在将 LLM 输出返回给用户之前,对其应用第二种 Bedrock Guardrails 配置。防止在响应中意外泄露系统提示词、机密信息或 PII(个人身份信息)。 ## AWS 服务 | 组件 | 服务 | 用途 | |-----------|---------|---------| | LLM 托管 | EC2 t2.micro | 运行 Ollama 与 TinyLlama | | API 层 | API Gateway (HTTP API) | 带有速率限制的公共 endpoint | | Edge Filter | Lambda + ECR | 运行 ML 分类器容器 | | Semantic Guard | Bedrock Guardrails | 输入保护 | | Egress Auditor | Bedrock Guardrails | 输出保护 | | 存储 | DynamoDB | 攻击日志与指标 | | 旁路存储 | S3 | 用于重新训练的攻击 payload | | 容器注册表 | ECR | 存储 ML 模型 Docker 镜像 | | 仪表板 | EC2 (同一实例) | Streamlit UI | | 监控 | CloudWatch | 日志与实时指标 | | 告警 | CloudWatch Alarms + SNS | 高攻击率时的电子邮件告警 | | 重新训练触发器 | S3 Event Notifications | 触发重新训练流水线 | ## ML 流水线 1. 从 HuggingFace 收集并标注提示注入数据集 2. 本地训练:带有 `TfidfVectorizer` 和 `LogisticRegression` 的 `sklearn.pipeline` 3. 评估准确率 — 目标大于 85% 4. 将训练好的模型保存为 `model.pkl` 5. 构建 Docker 容器镜像 6. 将镜像推送到 Amazon ECR 7. 通过 AWS Lambda 部署以进行实时提示词分类 ## 设置说明 ### 前置条件 - 启用了免费套餐的 AWS 账户 - Python 3.10+ - Docker - 本地已配置 AWS CLI ### EC2 和 Ollama ``` # 安装 Ollama curl -fsSL https://ollama.com/install.sh | sh sudo systemctl enable ollama sudo systemctl start ollama # 拉取 TinyLlama ollama pull tinyllama # 在所有接口上暴露 Ollama (添加到 systemd override) sudo systemctl edit ollama # 添加: # [Service] # Environment="OLLAMA_HOST=0.0.0.0:11434" sudo systemctl daemon-reload sudo systemctl restart ollama ``` 如果 EC2 实例内存不足(t2.micro 仅有 1GB),请添加 swap: ``` sudo swapoff /swapfile 2>/dev/null; sudo rm -f /swapfile sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab ``` ### Lambda 代理 Lambda 函数将提示词从 API Gateway 转发到 EC2 上的 Ollama 并返回响应。将环境变量 `OLLAMA_EC2_URL` 设置为 `http://:11434`。 ### 测试 endpoint ``` curl -X POST \ https://.execute-api.us-east-1.amazonaws.com/dev/prompt \ -H "Content-Type: application/json" \ -d '{"prompt": "What is 2+2?"}' ``` ## 安全说明 - 端口 11434 (Ollama) 必须仅限于受信任的 IP — 未经身份验证不得将其公开暴露 - 为 EC2 实例分配一个 Elastic IP,以便 Ollama URL 在重启后不会发生更改 - Bedrock Guardrails 按使用量付费 — 在开发期间请批量测试提示词,而不是持续测试 - 在 API Gateway 阶段设置节流速率,以防止成本失控 ## 预算估算 | 服务 | 每月费用 | |---------|-------------| | EC2 t2.micro | $0 (免费套餐) | | Lambda | $0 (免费套餐,100 万次请求) | | API Gateway | $0 (免费套餐,100 万次请求) | | DynamoDB | $0 (免费套餐,25GB) | | S3 | $0 (免费套餐,5GB) | | ECR | $0 (免费套餐,500MB) | | Bedrock Guardrails | $2 - $5 (按使用量付费) | | CloudWatch | $0 (免费套餐) | | SNS | $0 (免费套餐,1000 封电子邮件) | | **总计** | **$2 - $17** | ## 参考资料 - [Amazon Bedrock Guardrails 文档](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html) - [Ollama](https://ollama.com) - [OWASP LLM Top 10](https://owasp.org/www-project-top-10-for-large-language-model-applications/) - [HuggingFace 提示注入数据集](https://huggingface.co/datasets?search=prompt+injection) - [Scikit-learn TF-IDF 文档](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html)
标签:AI风险缓解, Amazon API Gateway, Amazon Bedrock, Amazon EC2, Amazon ECR, Amazon S3, AMSI绕过, Apex, API安全, AWS Lambda, CISA项目, CloudWatch, CSV导出, DNS 反向解析, DynamoDB, ETW劫持, JSON输出, Kubernetes, LLM评估, LLM防御, Ollama, SNS, Streamlit, TF-IDF, TinyLlama, 人工智能安全, 合规性, 多层防御架构, 大语言模型安全, 威胁检测, 安全基准测试, 实时检测, 容器化部署, 对抗攻击, 提示注入防御, 敏感信息检测, 数据隐私保护, 日志记录, 机器学习, 机密管理, 源代码安全, 漏洞探索, 系统提示泄露防护, 网络安全, 自定义ML分类器, 访问控制, 请求拦截, 输出验证, 边缘过滤, 逆向工具, 隐私保护, 零日漏洞检测