人工智能黑客
AI 安全资源

RAG 安全:完整指南

确保检索增强生成系统免受文档中毒、检索操纵和嵌入攻击

Updated: November 2026 • OWASP LLM08 的一部分

什么是 RAG?

Retrieval-Augmented Generation (RAG) is an AI architecture that combines large language models with external knowledge retrieval. Enterprise LLM deployments now use RAG extensively, making its security critical.

1。用户查询

用户向系统提交问题或提示

2. 嵌入

查询转换为向量嵌入

3.检索

从向量数据库检索的类似文档

4。增强

检索到的上下文已添加到提示

5。一代

LLM 使用上下文生成响应

信任悖论

RAG 系统存在一个根本性的安全缺陷:用户查询被视为不受信任的输入,但检索到的上下文是隐式受信任的 - 即使两者输入相同的提示。这会产生传统安全无法解决的重要攻击面。

攻击媒介

1。文档中毒

将恶意内容注入存储在知识中的文档中基地。

工作原理
  • 攻击者上传或注入恶意文档
  • 文档嵌入并存储在矢量数据库中
  • 进行相关查询时,检索中毒文档
  • LLM 将恶意上下文合并到响应中
影响
  • 90% success 只需 5 个中毒文档
  • 即使在具有数百万个文档的数据库中也能工作
  • 可能导致有害、有偏见或不正确的输出
  • 部署后难以检测

2.检索操作

操纵检索哪些文档以影响输出。

工作原理
  • 攻击者精心设计查询以触发特定检索
  • 利用排名算法的弱点
  • 使用语义相似性劫持检索
  • 共享系统中的跨用户操作
影响
  • 强制检索攻击者控制的内容
  • 可以抑制合法内容
  • 实现有针对性的操作
  • 打破了对检索质量的信任

3。嵌入反转

从矢量恢复原始数据嵌入。

工作原理
  • 攻击者获得对向量数据库的访问权限
  • 在嵌入上使用反转技术
  • 从向量重建原始文本
  • 恢复敏感的嵌入数据
影响
  • Recover 50-70% of input words
  • 在嵌入中暴露敏感数据
  • 侵犯隐私
  • 合规性问题(GDPR 等)

4。跨租户攻击

利用多租户系统中的共享RAG基础设施。

工作原理
  • 攻击者是共享系统的租户之一
  • 注入影响其他租户的内容
  • 利用共享向量数据库
  • 从其他租户检索数据
影响
  • 租户之间的数据泄露
  • 未经授权访问竞争对手数据
  • 合规违规
  • 声誉损害

真实的 CVE

RAG 系统中已记录的漏洞。

CVE ID 产品 说明 严重性 CVSS
CVE-2025-68700 RAGFlow RCE via Canvas CodeExec component - untrusted data parsed with eval() Critical 9.1
CVE-2025-69286 RAGFlow Insecure API key generation allows mutual token derivation (authentication bypass) Critical 8.9
CVE-2025-25282 RAGFlow IDOR vulnerability allowing cross-tenant access and unauthorized user addition High 8.1
CVE-2025-69286 RAGFlow Token generation using same URLSafeTimedSerializer for API keys High 8.9
GHSA-8xw3-v6c2-j84j RAGFlow RCE via stdout parsing in CodeExec component High 8.5

防御策略

1. 摄取阶段安全

文档验证

  • 扫描所有文档是否存在恶意软件
  • 验证文档格式和结构
  • 检查可疑内容模式
  • 限制允许的文档类型

内容过滤

  • 在嵌入之前删除 PII
  • 过滤敏感数据模式
  • 阻止已知恶意模式
  • 实施允许/阻止列表

访问控制

  • 验证文档来源
  • 实施 RBAC 进行摄取
  • 所有上传的审核跟踪
  • 隔离新文档

2.检索阶段安全

查询清理

  • 验证和清理用户查询
  • 检测注入尝试
  • 限制查询复杂性
  • 速率限制

检索过滤

  • 实施重新排名安全性
  • 与可信来源的交叉引用
  • 检测异常检索模式
  • 限制文档数量

多租户隔离

  • 独立的向量命名空间
  • 严格的租户边界
  • 跨租户查询预防
  • 每个租户加密

3。生成阶段安全

输出验证

  • 验证LLM输出
  • 检查注入的内容
  • 对来源进行事实核查
  • 内容过滤

上下文验证

  • 验证检索到的内容真实性
  • 检测操纵尝试
  • 标记异常上下文模式
  • 记录所有上下文使用情况

人机交互

  • 审查敏感输出
  • 批准高风险操作
  • 手动覆盖功能
  • 升级路径

4。数据安全

加密

  • 静态加密向量
  • 传输中数据的 TLS
  • 密钥管理最佳实践
  • 考虑同态加密

矢量数据库安全

  • 强大的身份验证
  • 网络隔离
  • 定期安全审计
  • 补丁管理

隐私保护

  • 数据最小化
  • PII 检测和删除
  • 保留策略
  • 删除权支持

测试方法

RAG 安全测试清单

  • 文档注入测试
  • 检索操纵测试
  • 嵌入反转尝试
  • 跨租户隔离测试
  • PII 泄漏测试
  • 上下文溢出测试
  • 排名操纵测试
  • 身份验证绕过测试
  • API 注入测试
  • 拒绝服务测试
  • 数据渗透测试
  • 合规性审核

测试工具

Garak

带有RAG特定探针的LLM漏洞扫描器

查看 →

Apache RAG

RAG 特定的安全测试框架

查看 →

向量 DB 扫描器

用于测试矢量数据库安全性的工具

查看 →

代码示例

文档验证示例

```python
import re
from typing import List

class RAGDocumentValidator:
    SUSPICIOUS_PATTERNS = [
        r"ignore previous instructions",
        r"system prompt:",
        r"{{.*}}",
        r"you are now dan",
    ]
    
    PII_PATTERNS = [
        r"\b\d{3}-\d{2}-\d{4}\b",  # SSN
        r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b",  # Email
        r"\b\d{16}\b",  # Credit card
    ]
    
    def validate_document(self, content: str) -> dict:
        """Validate document before embedding."""
        warnings = []
        blocked = False
        
        # Check for suspicious patterns
        for pattern in self.SUSPICIOUS_PATTERNS:
            if re.search(pattern, content, re.IGNORECASE):
                warnings.append(f"Suspicious pattern: {pattern}")
                blocked = True
        
        # Check for PII
        for pattern in self.PII_PATTERNS:
            if re.search(pattern, content):
                warnings.append(f"PII detected: {pattern}")
        
        return {
            "valid": not blocked,
            "warnings": warnings,
            "pii_detected": len([w for w in warnings if "PII" in w]) > 0
        }
```
AH
AI Hacking Team

The AI Hacking team researches and documents AI/LLM security vulnerabilities, red teaming techniques, and defensive strategies. Our guides are based on real-world pentesting experience and continuous monitoring of the AI security landscape.

Frequently Asked Questions

AI中的RAG是什么?
RAG(Retrieval-Augmented Generation)是一种将大型语言模型与外部知识检索相结合的人工智能架构。该系统从知识库中检索相关文档,并使用它们来增强提示,提高准确性并根据事实数据做出响应。
RAG 主要的安全漏洞有哪些?
主要的 RAG 漏洞包括:文档中毒(将恶意内容注入知识库)、检索操纵(操纵检索哪些文档)、嵌入反转攻击(从向量恢复输入)以及多用户系统中的跨租户攻击。
文档中毒在 RAG 系统中如何运作?
Document poisoning involves inserting malicious content into documents stored in the knowledge base. Research shows just 5 carefully crafted documents can manipulate AI responses with high success rate, even in databases containing millions of documents. The poisoned content influences retrieved context and can cause harmful outputs.
什么是嵌入反转?
Embedding inversion attacks can recover much of original input words from vector embeddings alone. If attackers gain access to the vector database, they can reconstruct sensitive information that was embedded, including potentially private or confidential data.

Stay Ahead of AI Security

Get the latest AI/LLM security research, OWASP updates, and new vulnerabilities delivered straight to your inbox.