用注意力机制识别并清除长文本中的注入指令,提升大模型安全防护能力。
PISanitizer: Preventing Prompt Injection to Long-Context LLMs via Prompt Sanitization
- 通过分析注意力分布定位潜在注入指令
- 在生成前净化高关注令牌,阻断攻击指令影响
- 对强对抗攻击仍有效,适合实际部署场景
长上下文大模型易受提示注入攻击,攻击者可向长上下文中注入指令以诱导模型生成期望输出。现有防御方法针对短上下文设计,扩展到长上下文时效果有限,因注入指令仅占整体内容极小比例。本文提出PISanitizer,先在后台大模型生成响应前,定位并净化上下文中的潜在注入令牌,从而消除攻击指令影响。该方法基于两个观察:(1) 提示注入本质是构造能迫使模型服从的指令;(2) 大模型通过注意力机制聚焦关键输入令牌进行生成。PISanitizer先故意让模型遵循任意指令,再净化驱动服从行为的高注意力令牌。设计上形成攻防矛盾:指令越有效,越易被净化。大量实验表明,PISanitizer能有效防止提示注入,保持模型实用性,优于现有方法,且对基于优化和强自适应攻击具有鲁棒性。代码已开源。
原文摘要 · Abstract (English)
Long context LLMs are vulnerable to prompt injection, where an attacker can inject an instruction in a long context to induce an LLM to generate an attacker-desired output. Existing prompt injection defenses are designed for short contexts. When extended to long-context scenarios, they have limited effectiveness. The reason is that an injected instruction constitutes only a very small portion of a long context, making the defense very challenging. In this work, we propose PISanitizer, which first pinpoints and sanitizes potential injected tokens (if any) in a context before letting a backend LLM generate a response, thereby eliminating the influence of the injected instruction. To sanitize injected tokens, PISanitizer builds on two observations: (1) prompt injection attacks essentially craft an instruction that compels an LLM to follow it, and (2) LLMs intrinsically leverage the attention mechanism to focus on crucial input tokens for output generation. Guided by these two observations, we first intentionally let an LLM follow arbitrary instructions in a context and then sanitize tokens receiving high attention that drive the instruction-following behavior of the LLM. By design, PISanitizer presents a dilemma for an attacker: the more effectively an injected instruction compels an LLM to follow it, the more likely it is to be sanitized by PISanitizer. Our extensive evaluation shows that PISanitizer can successfully prevent prompt injection, maintain utility, outperform existing defenses, is efficient, and is robust to optimization-based and strong adaptive attacks. The code is available at https://github.com/sleeepeer/PISanitizer.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。