对应 Maps to:Fall 2023 Lecture 1(“Trustworthiness of LLMs” 里的 alignment 部分:input filtering、output filtering、RLHF;jailbreaking;prompt attacks —— indirection、payload splitting;adversarial suffix);Fall 2023 Lecture 4 “Application area: Security”(Ankur Taly 主持,客座 Nicholas Carlini 与 Zifan Wang;本讲使用 Zifan Wang 的公开 slides “Adversarial Attacks on Aligned LLMs”,Carlini 的部分没有公开 slides,内容取自他与合作者的公开论文);Fall 2023 Lecture 6(安全方向的项目建议:fuzzing、攻击、评估鲁棒性、检测对抗 prompt、检测 LLM 生成的文本 —— watermarking、DetectGPT、“Can AI-Generated Text be Reliably Detected?”);Autumn 2025 Oct 30 客座讲座(Matt Fredrikson & Andy Zou:“Security for agents: adversarial attacks and guardrails”;本讲使用 Andy Zou 的公开 slides “Red Teaming in the real-world and Robust Mitigations”,Fredrikson 的部分没有公开 slides,内容取自他参与的公开论文 —— GCG 与大规模 agent 红队竞赛)。 配套代码 Code:
code/lecture15/(只用 Python 标准库;5 个脚本 + 3 个工具模块,每个脚本 0.2–21 秒) 预计阅读时间:3 小时(含运行代码)
0. 本讲导航 · How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Write down a threat model for an LLM application or agent: who controls which part of the input (developer, user, retrieved documents, tool outputs, other agents), what the attacker wants (harmful content, data exfiltration, unauthorized actions), and what the attacker knows and can afford.
- 解释对齐(alignment)为什么能被绕过:Wei et al.(2023)的两种失败模式 —— competing objectives 和 mismatched generalization —— 并能把常见的越狱家族(role-play、payload splitting、编码 / 低资源语言、many-shot、优化攻击)归到这两类里。
- Explain GCG (Zou et al. 2023) as the discrete-token descendant of PGD (L7): the affirmative-prefix objective, gradients with respect to one-hot tokens, top-k candidates evaluated with real forward passes, universal suffixes, and transfer via ensembles.
- 解释 prompt injection 为什么是”LLM 应用的 SQL 注入”(指令和数据之间没有分界),以及它在 agent 上的新形态:工具滥用、通过工具调用或 markdown 图片外泄数据、confused deputy。
- Evaluate a defense the way L8 taught: against an adaptive attacker, with confidence intervals, with ASR@k, and together with its utility cost (over-refusal, blocked legitimate actions). Recognize the pattern “perfect against the attacks it was tuned on, broken by an adaptive search”.
- 比较几类护栏的机制与代价:输入 / 输出分类器、困惑度过滤、spotlighting、instruction hierarchy、最小权限 + 人工确认、dual-LLM / CaMeL 式的能力隔离、representation engineering 与 circuit breakers。
- Explain how a green-list watermark is embedded and detected (a z-test), why detection power grows like √T, why paraphrasing removes it, and why detectors’ false positives are a fairness problem.
0.2 先修与本讲范围 Prerequisites & scope
| 默认你已经会 Assumed | 本讲只给直觉或略过 Previewed / omitted |
|---|---|
| 第 7 讲:threat model、FGSM / PGD、迁移攻击、“最坏情况 vs 随机” | RLHF 的训练细节(只讲它在安全上起什么作用) |
| 第 8 讲:上界 vs 下界、梯度掩蔽、自适应攻击、红旗清单 | Transformer 内部机制(refusal direction 只做直觉介绍) |
| 第 1 讲 §8:指标有误差、赢家诅咒、Goodhart | 真实的越狱 prompt —— 本讲刻意不包含(见下) |
| 第 14 讲(并行):agent 的构件、工具调用、轨迹评估 | 模型权重 / 训练数据投毒(backdoor、sleeper agents)只在 §2 提一句 |
关于本讲的边界。 这是一讲防御与评估导向的课。我们讲攻击的机制(为什么能成功、为什么能迁移、为什么能绕过某种防御),因为不理解机制就无法设计和评估防御;但讲义和代码里没有任何可用的越狱 prompt、有害内容或真实的利用字符串。代码里的”有害请求”是一个抽象的占位符 token(t03 这类 forbidden topic),“注入”是几个数值特征,攻击者想触发的”恶意行动”是一个无害的 canary:send_email(to="[email protected]", body 含 "SECRET-1234")。
0.3 本讲地图 Map
§1 从"说"到"做":为什么安全是另一回事
§2 威胁模型:谁控制哪部分输入、攻击者想要什么、知道什么
§3 对齐的手段,以及它为什么能被绕过(competing objectives / mismatched generalization) ← 代码 01 演示 1
§4 越狱家族(机制层面)
§5 GCG:离散 token 上的 PGD;universal 与 transfer ← 代码 01 演示 2–5
§6 越狱的防御,以及"看起来完美"的陷阱;representation engineering 与 circuit breakers ← 代码 03
§7 Prompt injection:LLM 应用的 SQL 注入;agent 的攻击面 ← 代码 02 演示 1
§8 Agent 的护栏:spotlighting、分类器、instruction hierarchy、最小权限、能力隔离 ← 代码 02 演示 2
§9 怎样测量安全:置信区间、ASR@k、过度拒绝、benchmark 与红队竞赛 ← 代码 05
§10 这段文字是 LLM 写的吗?—— 检测与水印 ← 代码 04
§11–15 小结、误区、练习、延伸阅读、下一讲预告
0.4 Notation
| 符号 | 含义 |
|---|---|
| , | 用户请求(prompt);攻击者追加的后缀(suffix), 表示拼接 |
| toy 对齐模型的拒绝 logit: 拒绝, 照做(comply) | |
| ASR | attack success rate:攻击成功(系统最终做了攻击者想要的事)的比例 |
| ASR@k | 允许每个目标尝试 次时,至少成功一次的比例 |
| , , | 水印的绿名单比例、绿名单 logit 加成、文本长度 |
| static / adaptive | 固定的攻击集合 / 知道防御机制并针对它设计的攻击者(第 8 讲 §3.1) |
0.5 关于数字 · About the numbers
讲义里所有实验数字都来自配套代码的实际输出(固定种子;同一台机器上重复运行逐字相同,并且 Python 3.9 与 3.13 的输出完全一致)。数字来自一个参数写明的模拟器,说明的是机制,不代表任何真实模型的表现:
- token 世界(
sec_world.py):一个”知道真相”的玩具对齐模型 —— 随机固定的”预训练特征” + 只在自然请求上训练的拒绝头。每个请求的真实意图(话题是否 forbidden)确切已知。 - agent 世界(
agent_sim.py):一个把读到的每条”指令”打分、按概率执行的 agent,外加权限闸。 - 水印世界(
watermark_tools.py):60 个 token 上的 toy LM 和 Kirchenbauer et al. 的 green-list 水印。
真实世界的数字(GCG 的迁移成功率、红队竞赛的规模)都注明了出处,和 toy 数字分开。
1. 从”说”到”做” · Why security is different
1.1 Two eras of the same question
The Fall 2023 course listed, among the trustworthiness dimensions of an LLM, alignment (“not harmful, toxic, biased, dishonest, unreliable”) and a separate line: resist adversarial manipulation — malicious input should not subvert desirable properties. Every other lens in this course (grounding, calibration, fairness…) is a property measured on typical inputs. Security is the property that must survive chosen inputs. 这正是第 7 讲开头那句话在 LLM 上的版本:只要输入可能来自一个有意图的人,平均情况下的指标就不再是相关的指标。
Zifan Wang’s 2023 guest slides summarize the first big result in three steps: Step 1: attack open-source LLMs using a (somewhat involved, but pretty simple) white-box attack. Step 2: copy the generated attacks into public chatbots. Step 3: … there is no step 3. The slides also explain why the authors chose to publish code and examples: having a chatbot say mean things is not that harmful yet, but “if we start to release autonomous agents that rely on these systems (i.e., that can read the web, take actions automatically), this gets pretty scary”.
Two years later, Andy Zou’s Autumn 2025 talk opens with exactly that shift: the red-teaming objective moved from AI chatbots to AI agents, and from medium risk to high risk. A jailbroken chatbot produces text that a human still has to act on; a hijacked agent acts — it sends the email, runs the code, moves the money.
1.2 What carries over from Lectures 7–8
| 第 7–8 讲(图像分类器) | 本讲(LLM / agent) |
|---|---|
| 对抗扰动 , | 对抗后缀 (离散 token),或任意”语义上合理”的改写 |
| 目标:分错类 | 目标:输出被禁止的内容 / 执行未授权的行动 / 泄露数据 |
| PGD:沿梯度符号走一步再投影 | GCG:用梯度挑候选 token,再用真实前向传播挑最好的(§5) |
| 迁移攻击、替身模型集成 | universal + transferable 后缀;在多个开源模型上联合优化(§5.4) |
| 梯度掩蔽让防御”看起来鲁棒” | 过滤器 / 检测器只在已知攻击上”看起来完美”(§6) |
| 证书 + 弃权(certify or abstain) | 最小权限 + 人工确认:不识别攻击,而是约束行动(§8) |
| 鲁棒准确率是”对所有攻击取最小” | ASR 是”对所有攻击者取最大”,还要乘上尝试次数(§9) |
Andy Zou’s slides make the same connection explicitly: next to the panda-to-gibbon picture they write “this is the same essential attack we used to bypass refusal!” — and then show the >30-point gap between standard and robust ImageNet accuracy as a warning about how hard robustness has been for vision. 第 8 讲已经看到了这个代价;§6.4 会看到 circuit breakers 是怎样试图绕开它的。
2. 威胁模型 · Threat models for LLM apps and agents
第 8 讲 §1.1:任何防御声明都必须先写清 threat model —— 目标、知识、能力。对 LLM 应用,最重要的新维度是谁控制了哪一部分输入。
2.1 Who controls which input?
| 输入的来源 | 谁写的 | 可信吗 | 典型攻击 |
|---|---|---|---|
| system prompt / 开发者指令 | 应用开发者 | 可信(但可能被泄露:prompt leaking) | — |
| user 消息 | 使用者 | 对”使用者本人想做的事”可信;但使用者本身可能就是攻击者 | jailbreak(直接越狱)、direct prompt injection |
| 检索到的文档、网页、邮件 | 任何能写入这些内容的人 | 不可信 | indirect prompt injection(§7) |
| 工具的返回值(API、代码执行结果) | 外部系统 | 不可信 | 注入、被篡改的数据 |
| 其他 agent 的消息(multi-agent) | 另一个模型(可能已被攻破) | 不可信 | 注入沿着 agent 之间传播 |
| 模型权重、训练 / 微调数据 | 模型提供方、数据来源 | 视供应链而定 | 数据投毒、backdoor(本讲不展开) |
关键的不对称:在一个普通的聊天机器人里,唯一不可信的输入是使用者,攻击者就是使用者。而在 agent 里,攻击者可以是一个从来不和 agent 对话的第三方 —— 他只需要把一段文字放到 agent 将来会读到的地方(一封邮件、一个网页、一份共享文档)。
2.2 What does the attacker want?
- Harmful content:让模型说出它被训练成拒绝说的东西(HarmBench 的 “standard / contextual / copyright / multimodal behaviors”)。
- Data exfiltration:把私有数据(system prompt、用户文件、API key、对话历史)送到攻击者手里。Andy Zou 的 slides 里的例子:“sending password to social media”。
- Unauthorized actions:以 user 的身份做 user 没要求的事 —— 发邮件、下单、执行代码、修改数据库(slides 的例子:executing unsafe code;fraud;data tampering)。
- Integrity / availability:悄悄篡改 agent 的输出(把报价改掉)、让 agent 拒绝服务。
2.3 What does the attacker know and afford?
| 知识 | 例子 | 可用的攻击 |
|---|---|---|
| White-box | 开源权重(Llama、Vicuna……) | 梯度攻击(GCG)、embedding-space 攻击 |
| Gray-box | API 返回 log-probabilities | 基于 logprob 的随机搜索(Andriushchenko et al. 2024) |
| Black-box | 只看到生成的文本 | 迁移攻击、attacker-LLM 搜索(PAIR)、人工红队 |
| 预算 | 每个目标能试多少次 | §9.2 的 ASR@k:开放给所有人的系统,预算实际上是无限的 |
2.4 Jailbreak vs prompt injection
两者经常被混用,但 threat model 不同:
- Jailbreak:攻击者就是使用者,想让模型违反开发者 / 提供方的政策(说出被禁止的内容)。受害者是政策本身(以及可能被这些内容伤害的第三方)。
- Prompt injection:攻击者是第三方,想让模型违背使用者的意图(以使用者的权限做事、泄露使用者的数据)。受害者是使用者。
Simon Willison 的说法很有用:prompt injection 是应用的问题(把可信的指令和不可信的数据拼在一起),而不只是模型的问题。一个完全”对齐”、从不说有害内容的模型,照样可以被注入,然后把你的收件箱转发出去。第 5–6 节讲越狱,第 7–8 节讲注入。
3. 对齐的手段,以及它为什么能被绕过 · Alignment and why it is bypassable
3.1 Three layers
The Fall 2023 slides list how providers “align” LLM responses with policies that forbid harmful content:
- Input filtering:一个分类器先看请求,命中就拒绝;
- Output filtering:生成之后再看一遍输出,命中就拦下;
- Tuning the weights(通常是 RLHF / safety fine-tuning):让模型自己学会拒绝。
“Circumventing these defenses is called jailbreaking.” The slides’ first examples are prompt attacks that get past both filters: indirection (wrap the request in a story or a role) and payload splitting (split the forbidden content into innocuous-looking pieces that the model reassembles), citing Kang et al. (2023), “Exploiting Programmatic Behavior of LLMs: Dual-Use Through Standard Security Attacks”. Kang et al.’s point was that instruction-following LLMs behave like programs: they can concatenate strings, follow variable assignments, decode — so classic computer-security tricks (obfuscation, code injection, virtualization) transfer directly.
3.2 Alignment as a learned boundary(toy 模型)
为了看清机制,代码用一个最小的”对齐模型”:
- 、 随机固定,代表预训练学到的特征; 是 token 嵌入。
- 只有拒绝头 被安全训练:在 2400 个自然请求上做逻辑回归,标签 = 话题是否 forbidden。这类比 safety fine-tuning / RLHF:它在一个输入分布上教模型”哪里该拒绝”。
- 上下文用”出现过的不同 token 的嵌入均值”概括(bag of distinct tokens):重复一个词不会让它的作用变大 —— 所以”随便重复一个怪词”不是越狱,攻击者必须找到很多个不同的、都朝同一方向推的 token。
关键的一点:安全训练只约束了模型在它见过的输入上的行为。在那之外 —— 编码过的话题、奇怪的后缀 —— 拒绝头的输出只是预训练特征的某种外推,没人保证它是什么。
3.3 Wei et al.: two failure modes
Wei, Haghtalab & Steinhardt (2023), “Jailbroken: How Does LLM Safety Training Fail?”, proposed two hypotheses that organize almost every jailbreak:
- Competing objectives. The model is trained for several things at once — follow instructions, be helpful, continue text plausibly, and be harmless. A prompt can set these against each other: ask the model to start its answer with “Sure, here is…”, to stay in character, or to never refuse. Pretraining and instruction-following then pull toward compliance. GCG’s objective (§5.1) is the optimized version of this: it searches for whatever input makes the affirmative prefix most likely.
- Mismatched generalization. Pretraining covers a far broader input space than safety training. If a capability (decoding base64, understanding a low-resource language, reading a cipher) exists but safety training never saw inputs in that form, the model can understand the request without recognizing it as one to refuse.
代码 01 演示 1 在 toy 世界里复现第二种失败。话题有三种表层形式:明文、“编码 A”(类比 base64:嵌入和明文的相关 ρ = 0.35)、“编码 B”(类比低资源语言:ρ = 0.55)。模型的能力对三种形式完全一样(都解码成同一个话题),安全训练只见过明文。拒绝率(每行 400 个自然请求):
| 请求类型 | 只用明文做安全训练 | 明文 + 编码 A 一起训练 |
|---|---|---|
| harmful · 明文 | 0.983 | 0.998 |
| harmful · 编码 A(类比 base64) | 0.092 | 0.555 |
| harmful · 编码 B(类比低资源语言) | 0.472 | 0.838 |
| edgy · 明文(无害但”听起来危险”) | 0.087 | 0.352 |
| benign · 明文 | 0.000 | 0.028 |
| harmful 明文 + 10 个随机 token 后缀 | 0.975 | 0.988 |
Three readings:
- 在训练分布上,对齐是有效的(明文 harmful 请求拒绝 0.983,benign 0.000)。一个只用自然请求做的评估会宣布这个模型”安全”。
- 换一种表层形式,拒绝就崩了(编码 A 只剩 0.092),而能力丝毫未变。编码 B 的嵌入和明文更接近,泛化得更好(0.472)—— 安全行为”迁移”多少,取决于表示有多相似。 真实世界的对应:Wei et al. 用 base64 等编码,Yong, Menghini & Bach(2023)把有害请求翻译成低资源语言,都显著提高了越狱成功率。
- 逐个修补表层形式是有代价的打地鼠。 把编码 A 加进安全训练,A 只修好一半(0.092 → 0.555),B 也跟着改善(0.472 → 0.838),但无害的 edgy 请求的过度拒绝从 0.087 升到 0.352:拒绝头被要求覆盖一组更”散”的有害输入,就把边界推进了无害的区域。 这和我们原先的预期不同(预期”补 A 就修好 A、修不了 B”),代码 README 记录了这件事。
最后一行:随机后缀几乎没有影响(0.975)。越狱必须被找出来 —— 这就是 §4–5 的内容。
3.4 A note on mechanism: the refusal direction
Why can a suffix move refusal at all? Interpretability work suggests refusal in chat models is mediated by a low-dimensional internal signal — Arditi et al. (2024) report a single “refusal direction” in the residual stream whose removal disables refusal. If refusal is a direction, then a suffix that pushes the hidden state against that direction suppresses it. Our toy’s is a caricature of exactly that: a linear readout on top of features that the context can shift.
4. 越狱家族(机制层面)· Jailbreak families at the level of mechanism
本节只描述机制和它对应的失败模式,不给出具体的 prompt。
| 家族 | 机制 | 主要利用 | 代表工作 |
|---|---|---|---|
| Role-play / indirection | 把请求包进故事、角色、假设情境;让”保持角色""帮助用户”压过”拒绝” | competing objectives | Fall 2023 slides;Kang et al. 2023 |
| Payload splitting | 把被禁止的内容拆成几段看似无害的片段,让模型自己拼起来;每段都通过输入过滤,拼起来的东西在输出过滤看来也可能不连贯 | mismatched generalization(过滤器看不到”程序”的结果) | Kang et al. 2023 |
| Encoding / low-resource language | 用模型会解码、但安全训练没覆盖的形式表达请求 | mismatched generalization | Wei et al. 2023;Yong et al. 2023 |
| Many-shot | 在长上下文里放大量”助手照做了”的虚构对话示例,用 in-context learning 覆盖安全训练 | competing objectives(ICL vs 安全训练);成功率随示例数近似按幂律增长 | Anil et al. 2024 |
| Gradient-based optimization | 白盒地优化一个后缀,最大化”肯定开头”的概率 | 两者兼有 | GCG(Zou et al. 2023);§5 |
| Search with an attacker LLM | 让另一个 LLM 提出、评估、改进越狱 prompt,只需黑盒访问 | 两者兼有 | PAIR(Chao et al. 2023,常在约 20 次查询内成功);TAP(Mehrotra et al. 2023) |
| Evolutionary / random search | 遗传算法改写 prompt(AutoDAN,Liu et al. 2023);用 logprob 指导的随机后缀搜索 | 两者兼有 | Andriushchenko, Croce & Flammarion 2024 |
| Reward-based (RL) | 训练一个攻击者模型,用”目标模型是否违规”作奖励 | 两者兼有 | Andy Zou 2025 slides 的三类方法之一 |
| Human red teaming | 大量真人在开放平台上尝试 | 一切 | Gray Swan Arena;§9.4 的竞赛 |
Andy Zou’s 2025 slides group the frontier automated methods into three families and describe where each has been moving: gradient-based (faster, more transferable, more interpretable), reward-based (stronger, more query-efficient, more diverse), and search-based (better update criteria, better search spaces). Two observations matter for defenders:
- Readable attacks exist. AutoDAN in the Zhu et al. (2023) sense (“Interpretable Gradient-Based Adversarial Attacks”) adds a fluency objective so the suffix reads like normal text — designed to slip past perplexity filters (§6.2). PAIR’s prompts are natural language by construction.
- Gradient access is not required. Random search guided by log-probabilities, attacker LLMs, and transfer from open models all work against black-box APIs. 第 7 讲 §9.5 的结论原样适用:“不公开参数”不是防御,只是提高了攻击的成本。
5. GCG:离散 token 上的 PGD · Greedy Coordinate Gradient
5.1 The objective
A chat model sees the user’s text inside a template (“System: … User: … Assistant:”). GCG appends a suffix of tokens (initialized to something innocuous, e.g. ”! ! ! …”) and optimizes it so that the assistant’s response begins with an affirmative target like “Sure, here is …”:
为什么只优化”开头”?因为 autoregressive 模型一旦以肯定的语气开头,后面”接着写下去”的概率就很高 —— 这正是 competing objectives:续写的惯性压过了拒绝。(论文同时测了两种目标:让模型输出一个精确的有害字符串(“harmful strings”),和让模型照做一条有害指令(“harmful behaviors”);评测集 AdvBench 各有 500 条。)
In our toy, “comply” plays the role of the affirmative prefix: the attacker minimizes .
Why a suffix can win at all — the same linear intuition as L1 §5.3 and L7 §3: each suffix token moves the context features a little; random tokens move them in random directions that cancel, but if every distinct suffix token is chosen to push the same way, the pushes add up. In the toy, ten random suffix tokens leave refusal at 0.975 (§3.3), while an optimized ten-token suffix flips it.
5.2 The optimizer: gradients propose, forward passes decide
Tokens are discrete, so PGD’s “step along the gradient” has no direct meaning. GCG (Zifan Wang’s slides, “the full algorithm”) repeats until success:
- Compute the loss, optionally summed over many harmful requests and several models.
- Take the gradient of the loss with respect to the one-hot token indicators of the suffix, . Entry approximates the change in loss from “putting a little bit of token ” at position — exactly the approximation of HotFlip (Ebrahimi et al. 2018) and AutoPrompt (Shin et al. 2020).
- For each position keep the top-k tokens with the most negative gradient; sample a batch of candidates, each replacing one random position with one of its top-k tokens (a coordinate step).
- Evaluate every candidate with a full forward pass and keep the best (greedy).
The slides’ advice: don’t operate in continuous “soft token” space, and don’t trust the gradient approximation too much — use it only to shortlist. 代码 01 演示 4 检查了这个近似在 toy 模型上有多准:对 20 个请求各抽 200 个单 token 替换,预测的损失变化和真实变化的 Spearman 秩相关中位数 0.89(范围 0.70–0.96);但”预测最好的候选”真的是最好的,只有 11/20 次。足够用来筛 top-k,不够用来直接做决定。
5.3 GCG vs random search(代码 01 演示 2–3)
一个被拒绝的请求(无后缀时 ;加上初始的自然后缀, 反而升到 ):GCG 第 1 步(34 次损失评估)后 ,第 2 步(68 次)后 ,模型从拒绝变成照做。最终后缀里出现了两个”怪 token”(j270、j050)—— 自然文本里从不出现、嵌入范数大、杠杆大的 token,这正是真实 GCG 后缀看起来像乱码的原因。
在 30 个被拒绝的请求上,各自单独攻击,到给定查询预算为止的 ASR:
| 查询预算(损失评估次数) | 35 | 70 | 140 | 280 | 560 | 1120 | 2240 |
|---|---|---|---|---|---|---|---|
| GCG(梯度挑候选) | 0.00 | 0.37 | 0.87 | 0.97 | 1.00 | 1.00 | 1.00 |
| 随机搜索(在整个词表里随机替换,变好才接受) | 0.00 | 0.00 | 0.40 | 0.83 | 0.93 | 0.97 | 1.00 |
- 两种方法最终都攻破了全部 30 个请求:在这个 toy 里,对齐只是输入空间里的一条边界,搜索得足够久总能越过它。
- GCG 的查询效率更高(140 次查询时 0.87 vs 0.40):梯度把搜索集中在每个位置少数几个有希望的 token 上(候选一共 460 个)。
- 随机搜索只需要能查询损失 —— 也就是一个能拿到 log-probabilities 的黑盒攻击者。Andriushchenko et al.(2024)证明这种简单的自适应随机搜索能越狱许多领先的安全对齐模型。藏起梯度只是提高了成本,并没有消除攻击。
5.4 Universal and transferable(代码 01 演示 5)
GCG becomes far more dangerous when the loss is summed over many requests (one suffix for all of them — universal) and over several models (transferable). 代码在话题 0–3 的 6 个请求上优化一个后缀,在没见过的话题 4–7 的 100 个请求上测,并迁移到另外两个模型(C:另一套预训练特征 + 另一份安全数据;D:相关但不同的嵌入,ρ = 0.8):
| 优化时用的模型 | 训练请求 ASR | A,held-out 话题 [95% CI] | C(迁移) | D(迁移) |
|---|---|---|---|---|
| A | 1.00 | 0.92 [0.85, 0.96] | 0.00 | 0.00 |
| A + C | 1.00 | 0.99 [0.95, 1.00] | 0.98 | 0.93 |
- Universal:只用 6 个训练请求,在没见过的话题上就有 0.92 —— 后缀学到的是”怎样把拒绝读数压下去”,这件事和具体话题无关。
- Single-model suffixes did not transfer at all in this toy (0.00): they exploit A’s idiosyncratic feature directions. 我们原本预期会有部分迁移,结果没有;README 记录了这件事。
- 集成才迁移。 在 A 和 C 上联合优化的后缀,对完全没参与优化的 D 达到 0.93 —— 对多个模型同时有效的扰动,更可能利用它们共有的特征。这就是第 7 讲 §9.1(Liu et al. 2017),也是 Zou et al. 得到迁移结果的做法。
The real numbers, from Zifan Wang’s slides and the paper: white-box GCG reached 88% (harmful strings) and 99% (harmful behaviors) on Vicuna-7B, and 57% / 56% on Llama-2-7B-Chat. A suffix optimized on an ensemble of open models (Vicuna and Guanaco variants) and transferred to black-box APIs reached about 87% on GPT-3.5, 47% on GPT-4, 66% on PaLM-2, 48% on Claude-1, but only 2% on Claude-2, versus 0–8% for the bare harmful request. The slides’ discussion slide asks “How do we fix this?” and answers honestly: ”… I don’t know. We have been trying to fix adversarial examples in computer vision for the past ten years.”
5.5 Lessons carried over from L7
- The worst case is found by optimization, not by sampling — random suffixes do nothing (0.975 refusal); optimized ones do everything.
- Transfer is a property of shared features, so it is strongest between models trained on similar data (Vicuna was fine-tuned on ChatGPT conversations), and ensembles help.
- Black-box ≠ safe: random search, attacker LLMs and transfer all work without gradients.
6. 越狱的防御,以及”看起来完美”的陷阱 · Defenses against jailbreaks
6.1 The landscape
| 防御 | 看什么 | 典型实现 | 主要弱点 |
|---|---|---|---|
| 输入分类器 | 请求的文本 | 关键词、训练好的分类器;Llama Guard(Inan et al. 2023)这类”用 LLM 做安全分类” | 只能识别它见过的形式;编码、拆分、自适应优化 |
| 输出分类器 | 生成的文本 | 同上,作用在回答上 | 攻击者可以让输出也”换形式”;增加延迟 |
| 困惑度过滤 | “像不像人话” | 一个小 LM 算 perplexity(Alon & Kamfonas 2023;Jain et al. 2023) | 可读的攻击(AutoDAN、PAIR);误伤少见的写法 |
| 输入变换 | 改写 / 重新分词后再给模型 | paraphrase、retokenization(Jain et al. 2023) | 损害正常请求;自适应攻击可以针对变换优化 |
| 对抗训练 | 模型本身 | 在越狱样本上继续训练;HarmBench 提出的 R2D2 | 第 8 讲的老问题:贵、对没见过的攻击泛化有限 |
| Representation engineering / circuit breakers | 模型内部表示 | 探针读”有害”方向;在产生有害内容时把表示改道 | 依赖”有害内容必然有可识别的内部表示”;embedding-space 攻击 |
This section’s demos make one point three times, then show what a better-generalizing defense looks like.
6.2 Perplexity filters(代码 03 演示 1)
GCG suffixes are gibberish; a small language model finds them very surprising. So: compute the mean negative log-likelihood (NLL) of the prompt under a reference LM, and block anything above a threshold. 代码里的参考 LM 是在自然 prompt 上估计的 bigram 模型,阈值取正常文本 NLL 的 95 分位(2.91);自然 harmful 请求的 NLL 约 2.17。对 25 个被拒绝的请求:
| 攻击者 | 系统最终照做(ASR)[95% CI] | 被过滤器拦下 | 后缀平均 NLL |
|---|---|---|---|
| 普通 GCG(不理过滤器) | 0.000 [0.00, 0.13] | 25 | 4.75 |
| 自适应:GCG + 流畅度项 λ = 1 | 0.280 [0.14, 0.48] | 18 | 3.13 |
| 自适应:λ = 4 | 0.480 [0.30, 0.67] | 8 | 2.92 |
对它被设计来对付的那种攻击,过滤器是完美的。但一个知道部署了困惑度过滤器的攻击者,会在 GCG 的损失里加一个流畅度惩罚(AutoDAN 的思路),把后缀的 NLL 压到正好贴着阈值(2.92 vs 2.91),ASR 回升到 0.48。而且 0.48 只是这一次、这一种攻击达到的数 —— 更多步数、更长的后缀、更好的流畅度模型都能把它推得更高。这正是第 8 讲 §3 的教训:一个防御的数字只对你跑过的攻击成立。
过滤器还有一个被单个误报率数字掩盖的代价。它的阈值是按 toy 语言的主流”方言”定的(实测误报率 0.052,和设计一致);而用第二种方言写作的用户 —— 请求完全无害 —— 被拦下的比例是 1.000。“拦下一切看起来不寻常的东西”是一个公平问题(第 5 讲):代价落在那些写法对参考模型来说”不寻常”的人身上。
6.3 Learned detectors(代码 03 演示 2)
A more sophisticated version: collect known attacks, train a classifier to separate them from normal prompts. 代码用 40 个请求上的普通 GCG 攻击训练一个 naive Bayes 检测器(正常文本误报率 0.028),然后攻击新的 20 个请求:
| 攻击者 | ASR [95% CI] | 被检测器拦下 |
|---|---|---|
| 新的普通 GCG(和训练同类) | 0.000 [0.00, 0.16] | 20 |
| 自适应:GCG + 躲检测器 λ = 1 | 0.950 [0.76, 0.99] | 1 |
检测器学到的是”罕见 token ⇒ 攻击”。它对同一类的新攻击泛化得很完美 —— 而这恰恰是”收集来的攻击里留出一部分做测试集”所测量的东西。自适应攻击者只需避开检测器看重的那些 token。结构和 §6.2 一样,只是更极端。
This pattern has a name now. Nasr, Carlini et al. (2025), “The Attacker Moves Second: Stronger Adaptive Attacks Bypass Defenses against LLM Jailbreaks and Prompt Injections”, took 12 recent defenses — most of which had reported near-zero attack success — and, by scaling up gradient descent, reinforcement learning, random search and human-guided exploration, bypassed most of them with ASR above 90%. Their conclusion is L8’s, restated for LLMs: a defense’s evaluation must include attacks designed against that defense with substantial compute.
6.4 Representation engineering and circuit breakers(代码 03 演示 3)
Andy Zou’s mitigation half of the 2025 talk starts from a different question: instead of recognizing inputs (an unbounded, adversarially chosen space), can we recognize and interrupt the model’s internal process of producing harmful content?
- Representation Engineering (RepE), Zou et al. (2023): find directions in hidden activations that correspond to high-level concepts by contrasting stimuli (e.g., honest vs dishonest statements); read them with a linear probe, and control behavior by adding or subtracting them. The slides show reading “dishonest brain activity” when a model lies, and steering toward honesty and away from hallucination.
- Circuit Breakers, Zou et al. (2024), “Improving Alignment and Robustness with Circuit Breakers”: train the model (with a representation rerouting loss) so that when its internal representations start to look like those of harmful generation, they are redirected to an orthogonal, useless region — the generation “short-circuits” — while a retain loss keeps representations on benign data unchanged. The slides show harmfulness probes separating a harmful request from a benign one whose responses have the same surface form (both begin “Sure, step 1…”) — the probe reads what is being produced, not how it is phrased — and the resulting Cygnet model.
The argument for why this might generalize: whatever the jailbreak — role-play, encoding, suffix, many-shot — if the model ends up producing harmful content, it must represent that content internally. A defense placed there is attack-agnostic in a way input filters are not.
代码用一个读”意思”的线性探针模拟这件事:探针读的是内容的语义表示 (编码形式和明文共享同一个语义),用 difference of means 找方向(RepE 的读法),在模型照做时若 就熔断。 是一个写明的假设:后缀能在多大程度上移动内容表示。
(a) 编码越狱:400 个编码 A 形式的 forbidden 请求里,只在明文上对齐的模型照做了 368 个;探针拦下其中 358 个(0.97)。读意思的防御不怕换表层形式。
(b) 自适应后缀攻击(把探针分数写进 GCG 的损失;20 个请求):
| 防御 | 自然 harmful 召回 | edgy 过度拒绝 | benign 误报 | 自适应后缀 ASR [95% CI] |
|---|---|---|---|---|
| 无探针(只有对齐) | — | — | — | 1.000 [0.84, 1.00] |
| 探针,α = 0.3(表示”硬”) | 0.97 | 0.54 | 0.01 | 0.350 [0.18, 0.57] |
| 探针,α = 1.0(表示”可塑”) | 0.99 | 0.59 | 0.09 | 0.550 [0.34, 0.74] |
- 探针把自适应 ASR 从 1.000 降到 0.35–0.55,但没有降到 0。The two probe rows’ intervals overlap (20 requests each), but the direction matches the hypothesis: the less a suffix can move the content representation, the harder the defense is to bypass. Real-world follow-up work points the same way: Schwinn & Geisler (2024, “Revisiting the Robust Alignment of Circuit Breakers”) report that slightly modified embedding-space attacks — which need open weights, since they optimize continuous inputs rather than tokens — reach 100% ASR against circuit-breaker models. Token-level attacks and embedding-space attacks are different threat models (§2.3), but the lesson is “much harder for some attackers, not impossible for all”.
- The cost is over-refusal. A probe that reads meaning also fires on harmless requests that mean something close to harmful ones: 0.54–0.59 of edgy requests are blocked. (The circuit-breaker paper reports small capability loss on standard benchmarks; over-refusal on borderline requests is the thing to measure separately, §9.3.)
Andy Zou’s closing slide lists three imperatives for the following months: continuous red-teaming, generalized circuit breaking, simulated benchmarking — and a plot titled “The Threat Keeps Moving”.
6.5 第 8 讲的清单,LLM 版
| 第 8 讲的检查 | 在 LLM 防御上的样子 |
|---|---|
| 写清 threat model | 攻击者是使用者还是第三方?白盒 / logprob / 只看文本?每个目标能试几次? |
| 自适应攻击 | 把防御的打分写进攻击目标(§6.2–6.4 的 λ 项);对 LLM-judge 式防御,攻击 judge 和模型的组合 |
| 攻击收敛 | 报告 ASR 随步数 / 查询数的曲线(§5.3),而不是一个数 |
| 黑盒不应强于白盒 | 迁移攻击、PAIR 式攻击比梯度攻击更成功 ⇒ 梯度攻击没跑对,或防御在”掩蔽” |
| 标准化攻击集合 | HarmBench、AgentDojo 等(§9.4)—— 但它们是静态的,只是起点 |
7. Prompt injection:LLM 应用的 SQL 注入 · Prompt injection and the agent attack surface
7.1 Direct and indirect
- Direct prompt injection(Perez & Ribeiro 2022, “Ignore Previous Prompt”):使用者在自己的输入里让模型忽略开发者的指令 —— goal hijacking(改变任务)、prompt leaking(泄露 system prompt)。
- Indirect prompt injection(Greshake et al. 2023, “Not what you’ve signed up for”):攻击者把指令放进 LLM 应用将会读到的内容里 —— 网页、邮件、文档、代码注释、工具返回值。使用者什么都没做错,只是让助手”总结一下这个网页”。Greshake et al. 在真实的 LLM 集成应用(包括带搜索的聊天助手)上演示了数据窃取、钓鱼、让助手传播注入等攻击。
7.2 Why “the SQL injection of LLM apps”
SQL injection happens when an application builds a query by concatenating code (the query template) with data (user input), and the data contains something the interpreter parses as code. The fix was structural: parameterized queries keep code and data in separate channels, so data can never be executed.
LLM 的 prompt 正是把指令(system prompt、user 请求)和数据(检索到的文档、工具返回值)拼接成同一个 token 流,而”解释器” —— 模型 —— 被训练成无论指令出现在哪里都去遵循。这里没有”参数化查询”的等价物:分隔符和角色标签本身也只是 token,模型尊重它们的倾向是学出来的、统计性的、可以被攻击的。所以 prompt injection 不是一个可以打补丁的 bug,而是这种架构的性质;也正因如此,最强的防御(§8.4)都是在模型之外、而不是模型之内起作用。
7.3 Agent-specific risks
When the model can call tools, injection gains reach:
- Tool misuse / unauthorized actions:注入让 agent 调用它有权限、但 user 没要求的工具(发邮件、下单、改文件、执行代码)。
- Exfiltration through tool calls:注入让 agent 读一份私有文件,再把内容作为参数发给攻击者控制的地址(一次
send_email、一个带查询参数的 HTTP 请求)。 - Exfiltration through rendering:即使 agent 没有任何”发送”工具,如果它的回答里含有一个 markdown 图片
,聊天客户端自动渲染这张图时就会把 URL 里的数据发出去。安全研究者在 2023 年前后在多个聊天产品上报告过这类问题;常见的修复是不渲染外部图片或只允许白名单域名。 - Confused deputy(Hardy 1988 的经典概念):agent 带着 user 的权限,却在为一段不可信的文本办事。它不是”被攻破”,而是被借用了权限。
- Multi-agent propagation:一个 agent 的输出是另一个 agent 的输入;注入可以沿着流水线传播。
A useful rule of thumb from practitioners (Simon Willison’s “lethal trifecta”, 2025; Meta’s “Agents Rule of Two”, 2025, in similar spirit): an agent that simultaneously (1) processes untrusted content, (2) has access to private data or sensitive systems, and (3) can communicate externally or change state should be assumed exploitable. Remove any one leg and the worst-case outcome is bounded.
7.4 The toy agent(代码 02 演示 1)
agent_sim.py 里的 agent 对它读到的每条”指令”(来自 user 或来自文档)打一个分:
一条注入是几个 0–1 的特征(多像命令、自称多权威、和任务多相关、混淆程度),没有任何真实文本。 表示 agent 完全不区分指令来自 user 还是文档 —— 这就是 §7.2 说的”没有分界”。对静态注入模板,这样的 agent 想执行它的概率约 0.94;加上”高风险行动需人工确认”之后,同一个 agent 照样想执行(概率不变),但 canary 行动在权限闸前被拦下。这个对比是 §8 的主线:防御可以作用在模型的判断上,也可以作用在行动的权限上。
7.5 What a month of public red-teaming found
The Autumn 2025 talk’s centerpiece was a large public competition (Zou et al. 2025, “Security Challenges in AI Agent Deployment: Insights from a Large Scale Public Competition”, arXiv 2507.20526, with Fredrikson among the authors). The paper reports 22 frontier AI agents across 44 realistic deployment scenarios; participants submitted 1.8 million prompt-injection attacks, of which over 60,000 elicited policy violations — unauthorized data access, illicit financial actions, regulatory non-compliance. The talk’s slides summarize the same event in rounder terms (“50 agents”, “10 AI labs”, “100+ policies”, “ran for a month”, about 2 million chats) and headline numbers: 100% ASR across agents, labs and policy types; “1 policy violation in 25 chats”; “67% transfer rates”. The slides’ example categories: copyright violation, PII leakage, malicious code execution, fraud, data tampering, unauthorized actions.
From the paper: nearly all agents violated policies for most behaviors within 10–100 queries; attacks transferred strongly across models and tasks; and robustness showed limited correlation with model size, capability, or inference-time compute — scaling up the model did not buy security. The curated high-impact attacks became the ART (Agent Red Teaming) benchmark, evaluated on 19 models. The slide that sums it up: “The vulnerabilities introduced by AI Agents can completely compromise applications.”
8. Agent 的护栏 · Defenses for agents
8.1 Spotlighting
Hines et al. (2024), “Defending Against Indirect Prompt Injection Attacks With Spotlighting”, transform untrusted input so the model can tell where it came from:
- Delimiting:用特殊标记把文档包起来,并在 system prompt 里说”标记内的内容永远不要当指令执行”。弱点:标记是固定的,攻击者可以在文档里伪造一个”结束标记”。
- Datamarking:在文档的每个词之间插入一个特殊字符(可以每次会话随机选),让”这是数据”的信号遍布整段文本,攻击者无法预知、也就无法伪造。
- Encoding:把文档整个编码(如 base64)再交给模型;信号最强,但要求模型能在编码后的内容上完成任务,较弱的模型会掉效用。
They report that on GPT-family models, spotlighting reduced attack success from above 50% to below 2% with minimal impact on task performance. 注意这些都是提示层面的措施:它们提高了模型”认出数据”的概率,但没有改变”一切都是同一个 token 流”这件事。
8.2 Injection detectors
Classifiers that scan untrusted content for instruction-like text before it enters the context. They are cheap and preserve utility when they are accurate — and they inherit every weakness of §6.3.
8.3 Instruction hierarchy
Wallace et al. (2024), “The Instruction Hierarchy: Training LLMs to Prioritize Privileged Instructions”: train the model so that system > user > tool output in priority, and lower-priority instructions are followed only when aligned with higher-priority ones. This attacks the problem at its source (the model’s willingness to follow any instruction), but it is again learned behavior — and “aligned with the user’s task” is exactly what a careful attacker imitates.
8.4 Least privilege, confirmation, and capability separation
These do not try to recognize attacks at all:
- Least privilege:只给 agent 完成这个任务需要的工具和数据(一个”总结网页”的任务不需要发邮件的权限)。
- Human confirmation for side effects:发信到外部地址、付款、删除、执行代码 —— 先给 user 看一眼、点”同意”。代价是摩擦(以及”确认疲劳”:点多了就不看了)。
- Dual LLM(Willison 2023)与 CaMeL(Debenedetti et al. 2025, “Defeating Prompt Injections by Design”):一个”特权” LLM 只看可信的 user 请求,生成执行计划(控制流);一个”隔离” LLM 处理不可信内容,但只能返回值,不能调用工具;再加上数据流策略(例如:私有数据只能发给 user 明确提到的收件人)。这样,不可信内容在构造上就无法改变 agent 做什么,只能改变它处理的值。谨慎的说法是:它把很大一类注入变成了”不可能”,代价是那些控制流本身依赖不可信内容的任务(“照我经理邮件里说的做”)做不了或需要额外确认;而且它不保护值本身的完整性(注入仍可能让 agent 把一个错的价格转告给对方)。
8.5 Static vs adaptive, and the price of each(代码 02 演示 2)
每格 600 次模拟(agent 的执行是随机的),ASR 后面是 Wilson 95% 区间;“static” 是一个固定的注入模板,“adaptive” 是知道部署了哪种防御、针对它调整特征的注入(对分类器:把命令伪装成陈述 / 报错信息;对 spotlighting 与 hierarchy:冒充 user 的权威;对确认:把注入伪装得和任务更相关)。效用列在没有攻击时测:benign task = 任务完成率;doc-follow = 文档里一条合法的指令(“会议改到 3 点”)被执行的比例。
| 防御 | ASR static | ASR adaptive | benign task | doc-follow |
|---|---|---|---|---|
| 无防御 | 0.928 [0.90, 0.95] | 0.928 [0.90, 0.95] | 0.988 | 0.777 |
| instruction hierarchy() | 0.822 [0.79, 0.85] | 0.867 [0.84, 0.89] | 0.977 | 0.530 |
| spotlighting | 0.173 [0.15, 0.21] | 0.232 [0.20, 0.27] | 0.962 | 0.058 |
| 指令分类器 | 0.000 [0.00, 0.01] | 0.850 [0.82, 0.88] | 0.988 | 0.777 |
| 最小权限 + 确认 | 0.043 [0.03, 0.06] | 0.043 [0.03, 0.06] | 0.988 | 0.777 |
| 全部叠加 | 0.000 [0.00, 0.01] | 0.002 [0.00, 0.01] | 0.962 | 0.015 |
逐行读:
- 分类器是最具误导性的一行:对静态模板完美(0.000),对把指令伪装起来的攻击者 0.850。和 §6.3 完全同一个模式,只是换成了 agent。
- spotlighting 对静态攻击有效(0.928 → 0.173),在自适应攻击下也只是温和变差(0.232)—— 但它同时教会 agent 忽略文档里合法的指令(doc-follow 0.777 → 0.058)。这是否要紧取决于应用;对一个本职就是”按读到的内容去办事”的 agent,非常要紧。
- 单独的 instruction hierarchy 在这里买到的很少(0.822),却已经让 doc-follow 减半 —— 一个学出来的”更信 user 一点”的权重不是边界。
- Least privilege + confirmation is the only row where static and adaptive agree (0.043 both), and it costs nothing on these benign tasks. The remaining 0.043 is the 5% of the time the simulated user clicks “approve” (
confirm_catch = 0.95) — so the reliability of the human in the loop becomes a quantity to measure, not an assumption. - Stacking everything drives ASR to ~0 but also drives doc-follow to 0.015: over-defense makes the agent useless for tasks that require trusting its inputs.
两条一般性结论。第一,能经受住自适应攻击者的防御,都是不依赖”认出攻击”的防御 —— 和第 8 讲里”证书 + 弃权”是同一个逻辑:不去猜输入是不是恶意的,而是给危险的结果加一个与输入无关的约束。第二,安全永远要和效用一起报告:单看 ASR 列,“全部叠加”是最好的;加上 doc-follow 列,它可能是最差的。
9. 怎样测量安全 · Measuring security
9.1 ASR and its error bars(代码 05 演示 1)
ASR is a proportion, and proportions measured on tens of attempts are noisy. The case that matters most is “we saw no successes”. 0/n 的 Wilson 95% 上界:
| 试验次数 n | 观察到的 ASR | Wilson 95% 上界 | rule of three(3/n) |
|---|---|---|---|
| 20 | 0.000 | 0.161 | 0.150 |
| 50 | 0.000 | 0.071 | 0.060 |
| 100 | 0.000 | 0.037 | 0.030 |
| 500 | 0.000 | 0.008 | 0.006 |
| 2000 | 0.000 | 0.002 | 0.002 |
“0% ASR on 50 attacks” supports “probably below 7%” — if the attacks were strong and adaptive. Against a weak attack, 0/500 proves nothing (L8: an attack gives only an upper bound on robustness; here, a lower bound on vulnerability).
9.2 Per-attempt vs per-target: ASR@k(代码 05 演示 2)
模拟 300 个”有害行为”,每个有自己的单次成功率 (大多数很难,少数很容易;均值 0.038,中位数 0.0092):
| 尝试次数 k | ASR@k(至少成功一次) | 理论值 |
|---|---|---|
| 1 | 0.043 | 0.038 |
| 10 | 0.243 | 0.235 |
| 100 | 0.537 | 0.550 |
| 1000 | 0.767 | 0.758 |
单次约 4% 的 ASR 听起来很安全;允许 100 次尝试,超过一半的行为被攻破。增长是先快后慢的:容易的行为先倒下,剩下的才是真正难的。 Hughes et al. (2024, “Best-of-N Jailbreaking”) documented this kind of scaling on real models with simple random augmentations. The competition in §7.5 found violations within 10–100 queries for most behaviors — and a deployed system’s is effectively unbounded. Always say whether an ASR is per attempt or per target within attempts.
9.3 The safety–utility curve(代码 05 演示 3)
Refusing more is the cheapest way to look safer. 在 toy 对齐模型上把拒绝规则改成 (τ 越小越保守),在 400 个自然请求上量拒绝率,并对 20 个请求跑预算固定为 15 步(约 500 次查询)的 GCG:
| τ | 自然 harmful 拒绝 | edgy 过度拒绝 | benign 过度拒绝 | 自适应 ASR [95% CI] | 成功所需查询(中位数) |
|---|---|---|---|---|---|
| 0 | 0.980 | 0.055 | 0.000 | 1.00 [0.84, 1.00] | 133 |
| −1 | 0.998 | 0.188 | 0.005 | 1.00 [0.84, 1.00] | 166 |
| −2 | 0.998 | 0.383 | 0.020 | 1.00 [0.84, 1.00] | 166 |
| −3 | 1.000 | 0.578 | 0.060 | 1.00 [0.84, 1.00] | 199 |
| −4 | 1.000 | 0.685 | 0.150 | 1.00 [0.84, 1.00] | 232 |
我们原本预期更保守的阈值至少能把固定预算下的 ASR 压下来一些;结果是一点都没有:τ = −4 时 edgy 请求被拒绝 0.685、连 benign 都被拒绝 0.150,而 ASR 仍然是 1.00,攻击只是多花了查询(中位数 133 → 232)。The refusal readout itself is not robust to suffixes, so moving its threshold only lengthens the attack. Contrast L1 §7.3, where abstaining bought robustness because a certificate guaranteed that the remaining decisions could not be flipped; here nothing is guaranteed.
The measurement lesson: report three things together — ASR under a stated adaptive attack and budget; over-refusal on borderline-but-benign requests (the XSTest suite of Röttger et al. 2024 was built for exactly this: prompts like “how do I kill a Python process”); and utility on normal tasks. Any one of them alone can be gamed (L1 §8.3, Goodhart).
9.4 Benchmarks and arenas
Andy Zou’s slides split red-teaming benchmarks into static, automated ones and manual, real-world ones.
- HarmBench(Mazeika et al. 2024):标准化的自动红队评测框架 —— standard、contextual、copyright、multimodal 四类有害行为;比较了 18 种红队方法和 33 个目标 LLM 与防御(GCG、PAIR、AutoDAN、人工、few-shot、persona……);并提出了一个高效的对抗训练方法(R2D2)。它的贡献是让”ASR”在不同论文之间可比:同样的行为集、同样的判定分类器、同样的生成长度。
- AgentHarm(Andriushchenko et al. 2024):110 个明确恶意的 agent 任务(加上变体共 440 个),覆盖 fraud、cybercrime、harassment 等 11 类危害。发现:领先的 LLM 常常不需要越狱就会执行恶意的 agent 请求;简单的通用越狱模板稍加改造就能越狱 agent;越狱后的 agent 仍能连贯地完成多步恶意任务。
- AgentDojo(Debenedetti et al. 2024):一个动态的 prompt injection 评测环境,有一组真实感的用户任务(邮件、银行、出行等)和与之配对的注入任务,同时测效用与攻击成功率,可以插入新的攻击和防御。
- ART(§7.5):从真人竞赛中筛出的高影响攻击。
- Gray Swan Arena:持续开放的公开红队平台。
Every static benchmark is a fixed attack set, so it measures exactly what §6 warned about: performance against attacks known in advance. It is a necessary floor, not a ceiling. That is why the talk’s three imperatives put continuous red-teaming first.
9.5 A reporting checklist
- Threat model:攻击者是谁、控制哪部分输入、知道什么、预算多少(§2)。
- ASR 的定义:判定”成功”的标准(谁判?LLM judge 本身要验证,第 1 讲 §8.3、第 11 讲)、per-attempt 还是 ASR@k。
- 静态 和 自适应攻击;自适应攻击把防御写进目标;报告随预算的曲线。
- 置信区间,尤其是 0 或接近 0 的 ASR。
- 效用与过度拒绝:normal tasks、borderline-benign 请求、少见语言 / 方言的用户。
- 迁移:在你的模型上调出的攻击 / 防御,在别的模型上还成立吗?
10. 这段文字是 LLM 写的吗?· Detecting LLM-generated text
10.1 Why it belongs in a security lecture
The Fall 2023 project list put “detecting whether a text is LLM generated” next to jailbreaks: generated text enables spam, phishing, disinformation and academic misconduct at scale, and provenance is a security property. Two families of approaches:
- Post-hoc detectors look at a text and guess. Trained classifiers do this directly; DetectGPT (Mitchell et al. 2023) uses the observation that model-generated text tends to sit near a local maximum of the model’s log-probability — small rewrites (from a mask-filling model) lower its log-probability more than they lower human text’s — so a “probability curvature” score separates them without training a classifier. Weaknesses: they need access to (something like) the generating model, degrade under paraphrase, and have uncalibrated false positives — Liang et al. (2023) found popular GPT detectors misclassify non-native English writing as AI-generated far more often.
- Watermarks change the generation process so that the text carries a statistical signal that a key-holder can test for.
10.2 The green-list watermark
Kirchenbauer et al. (2023), “A Watermark for Large Language Models”:
- At each step, hash the previous token (with a secret key) to seed a pseudo-random split of the vocabulary into a green list (fraction γ) and a red list.
- Add δ to the logits of green tokens, then sample as usual. When the model’s distribution has entropy, this tilts choices toward green; when it is nearly deterministic, the tilt barely changes the output (so quality is protected where it matters most, and the watermark is weak there).
- To detect, recompute the green lists from the text itself and count green tokens among . Under the null hypothesis “this text knows nothing about the key”, :
and a threshold like has a computable false-positive rate (one-sided normal tail ≈ ). No access to the model is needed, only the key.
Why does power grow like ? If each token is green with probability instead of , then — the same signal-to-noise arithmetic as any sum of independent votes.
10.3 Results in the toy(代码 04)
60 个 token 上的 unigram toy LM,γ = 0.5,阈值 z > 4,每格 300 段文本:
| T = 20 | T = 50 | T = 100 | T = 200 | T = 400 | |
|---|---|---|---|---|---|
| δ = 1 检测率 | 0.010 | 0.170 | 0.737 | 0.993 | 1.000 |
| δ = 1 平均 z | 2.08 | 3.30 | 4.66 | 6.45 | 9.16 |
| δ = 2 检测率 | 0.290 | 0.957 | 1.000 | 1.000 | 1.000 |
| δ = 2 平均 z | 3.42 | 5.35 | 7.54 | 10.71 | 15.13 |
| 无水印 · 误报率(n = 1000) | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |
Quadrupling the length roughly doubles (δ = 1: 4.66 → 9.16 from T = 100 to 400). Short texts are essentially undetectable at δ = 1.
Paraphrase attack(T = 200,δ = 2;以概率 rate 把每个 token 换成随机 token,toy 版的 paraphrase):
| 替换率 | 0.0 | 0.1 | 0.2 | 0.3 | 0.5 | 0.7 |
|---|---|---|---|---|---|---|
| 检测率 | 1.000 | 1.000 | 1.000 | 0.907 | 0.110 | 0.000 |
| 平均 z | 10.72 | 8.72 | 6.87 | 5.34 | 2.75 | 1.02 |
| 绿 token 比例 | 0.879 | 0.808 | 0.743 | 0.689 | 0.597 | 0.536 |
Local edits are tolerated; heavy paraphrase removes the mark. The green fraction falls faster than the substitution rate would suggest, because replacing one token also changes the next token’s green list. Sadasivan et al. (2023), “Can AI-Generated Text be Reliably Detected?”, made the general argument: recursive paraphrasing defeats watermarks and post-hoc detectors, and as model text becomes closer to human text in distribution, any detector’s AUROC is bounded by the total-variation distance between the two.
False positives and the independence assumption(T = 200,每类 1000 段无水印文本):
| 文本 | 误报率 [Wilson 95%] |
|---|---|
| 普通”人类”文本(另一个分布,不知道密钥) | 0.002 [0.001, 0.007] |
| 重复短语文本 · 每个位置都计分 | 0.482 [0.451, 0.513] |
| 重复短语文本 · 每个 token 对只计一次 | 0.000 [0.000, 0.004] |
The z-test assumes each position’s green/red outcome is independent. A text that cycles through a three-token phrase has only three distinct (previous, current) pairs, each counted ~67 times; if two of the three happen to be green (probability 1/2), the green fraction is ~2/3 and . Counting each distinct pair once — Kirchenbauer et al. also suggest ignoring repeated n-grams — restores the nominal rate. Even ordinary toy “human” text shows 0.002, above the nominal , because frequent token pairs repeat. 教训和 §6.2 一样:一个检测器的误报率是在某个人类文本分布上量出来的;模板化的、低熵的、非母语的写作可能远高于它 —— 而被误判的人要承担后果。
Quality cost: under the original (unwatermarked) model, the mean per-token NLL of generated text rises from 3.704 (δ = 0) to 3.740 (δ = 4). Small in this flat toy; Kirchenbauer et al. report perplexity increasing with δ on real models, with the cost concentrated where the best next token happens to be red.
11. 小结 · Summary
| 主题 | 核心机制 | 本讲代码里的证据 | 主要陷阱 |
|---|---|---|---|
| 对齐为什么能被绕过 | competing objectives;mismatched generalization | 编码 A 的拒绝率 0.092 vs 明文 0.983;补一种编码只补一半,过度拒绝 0.087 → 0.352 | 只在自然请求上评估对齐 |
| GCG | 梯度挑候选 + 前向传播挑最好;universal;集成迁移 | 140 次查询 ASR 0.87 vs 随机搜索 0.40;集成后缀迁移到未参与的 D:0.93 | 以为拿不到梯度就安全 |
| 越狱防御 | 过滤器、检测器只认识见过的攻击;读”意思”的防御泛化更好 | 困惑度过滤 0.000 → 0.480;检测器 0.000 → 0.950;探针拦下 0.97 的编码越狱,自适应 ASR 1.00 → 0.35 | 只用静态攻击评估;忽略过度拒绝 |
| Prompt injection | 指令和数据在同一个 token 流里 | 无防御 ASR 0.928;分类器 0.000 → 0.850 | 把它当成可以”修补”的 bug |
| Agent 护栏 | 约束行动(最小权限、确认、能力隔离)比识别攻击更稳 | 确认:static = adaptive = 0.043,效用不变;全部叠加 doc-follow 0.015 | 不报效用;高估人工确认的可靠性 |
| 测量 | CI、ASR@k、安全–效用三件套 | 0/50 的上界 0.071;ASR@100 = 0.537;τ = −4 过度拒绝 0.685 但 ASR 仍 1.00 | per-attempt 与 per-target 混用 |
| 水印 | 绿名单 + z 检验;信号 ∝ √T | T = 100→400 z 翻倍;替换 50% 检测率 0.110;重复文本误报 0.482 | 忽略独立性假设;误报的公平代价 |
Takeaways:
- Security is evaluated against chosen inputs. Everything that L7–L8 taught about worst cases, adaptive attacks and upper vs lower bounds applies to LLMs — with discrete tokens, natural-language attacks, and much larger attack surfaces.
- Safety training generalizes no further than its data (mismatched generalization), and a model’s helpfulness can be turned against its harmlessness (competing objectives).
- Optimization finds what sampling misses, transfers through shared features, and does not need gradients.
- “Perfect against known attacks” is the most dangerous number in this field. Filters, detectors and classifiers repeatedly collapse when the attacker moves second.
- For agents, constrain actions, not just inputs. Least privilege, confirmation and capability separation hold up under adaptation because they do not depend on recognizing the attack.
- Report ASR with its budget and error bars, and always with utility and over-refusal.
12. 常见误区 · Common pitfalls
- “Our model refuses 98% of harmful prompts in our test set, so it’s aligned.” That measures the training distribution. Re-test with encodings, other languages, many-shot contexts and optimized suffixes (§3.3).
- “We don’t expose gradients or weights, so GCG can’t hurt us.” Transfer from open models, log-prob random search and attacker-LLM search are all black-box (§5.3–5.4).
- “Our detector catches 100% of the attacks in our held-out set.” If the held-out attacks came from the same generator the detector was trained on, that number says nothing about an adaptive attacker (§6.3: 0.000 → 0.950).
- “A perplexity filter solves suffix attacks.” Readable attacks exist, and the filter’s false positives fall on unusual-but-legitimate users (§6.2).
- “We put the retrieved documents between delimiters, so prompt injection is handled.” Delimiters are tokens; their effect is learned and statistical, and fixed delimiters can be forged (§8.1).
- “Human confirmation makes it safe.” It bounds the damage by the human’s error rate and attention; confirmation fatigue is real, and that rate should be measured (§8.5).
- “ASR went to zero after we added all defenses.” Check what happened to utility and over-refusal (§8.5: doc-follow 0.015; §9.3).
- “0/50 attacks succeeded, so the risk is negligible.” The 95% upper bound is ~7%, and that assumes the attacks were strong (§9.1).
- “Watermark detection has a false-positive rate of 3×10⁻⁵.” Only if the independence assumption holds; repetitive or low-entropy human text breaks it (§10.3).
13. 练习 · Exercises
练习 1(威胁模型). 一个”邮件助手” agent:能读用户的收件箱、能读网页、能以用户身份发邮件、能读用户的云盘文件。(a) 列出所有不可信输入的来源。(b) 用 §7.3 的”三条腿”(不可信内容、私有数据、对外通信)分析:哪条攻击路径最危险?(c) 只允许你砍掉一条腿,你砍哪条?对效用的代价是什么?
参考答案
(a) 收到的每一封邮件(发件人可以是任何人)、网页内容、云盘里别人共享给用户的文件、任何工具的返回值。(b) 最危险的是”读一封外部邮件 → 被注入 → 读云盘里的私有文件 → 发邮件给攻击者”:三条腿全齐,而且发邮件本身就是外发通道;如果客户端渲染 markdown 图片,还有一条不需要工具的外发通道。(c) 常见的选择是砍掉”自动对外通信”:所有发往用户没有明确提到的收件人的邮件都需要确认,并且不渲染外部图片。代价:用户每次让助手”回复这封邮件”之外的发信都要多点一次;对”帮我联系供应商”这种收件人来自不可信内容的任务,摩擦最大。另一个选择是把”读不可信内容”和”读私有文件”放进不同的会话(能力隔离),代价是跨来源的任务(“把这封邮件里提到的合同找出来发给我”)需要拆成几步。
练习 2(两种失败模式). 把下面每种越狱归类为 competing objectives、mismatched generalization 或两者兼有,并说一句理由:(a) 要求模型”以’当然,这是……’开头回答”;(b) 用一种低资源语言提问;(c) 在长上下文里放几百个虚构的”助手照做了”的对话;(d) GCG 后缀;(e) payload splitting。
参考答案
(a) competing objectives:利用”遵循格式指令 / 续写”压过”拒绝”。(b) mismatched generalization:模型能理解这种语言,安全训练没覆盖它(§3.3 的编码 B)。(c) 主要是 competing objectives:in-context learning 学到的”这里的助手会照做”与安全训练竞争;Anil et al. 发现成功率随示例数近似按幂律上升。也可以说有 mismatched generalization 的成分(安全训练很少见到这么长的上下文)。(d) 两者兼有:目标函数是”肯定的开头”(competing objectives),找到的后缀是安全训练从未见过的离散输入区域(mismatched generalization)。(e) 主要是 mismatched generalization:每一段都在过滤器和安全训练覆盖的”正常”区域内,模型的拼接能力(Kang et al. 说的”程序式行为”)把它们组合成了训练没覆盖的东西。
练习 3(GCG 的一阶近似). In GCG, the score for replacing position with token is . (a) Show this equals the gradient with respect to the one-hot vector, up to a term that is the same for all . (b) Why is it cheap to compute for the whole vocabulary at once? (c) Give two reasons the true change in loss can differ from this score. (d) Why does GCG evaluate a batch of candidates instead of taking the single best-scored swap?
参考答案
(a) 嵌入是 ( 是 one-hot, 是嵌入矩阵),所以 ,第 个分量是 ;减去当前 token 的那一项 对所有 相同,不影响排序。(b) 一次反向传播得到 ,然后一次矩阵–向量乘 就给出所有 个分数。(c) 换 token 是一个大步(不是”一点点”token ),模型是非线性的(toy 里的 tanh;真实模型里的 attention 和 MLP);换一个 token 还会改变它和别的 token 的交互(toy 里”上下文中不同 token 的个数”会变)。(d) 因为一阶分数只在排序上大致可靠(toy 里秩相关中位数 0.89),但”预测最好”的往往不是”真的最好”(20 次里只有 11 次是)。从 top-k 里抽一批、用前向传播实打实地比较,既利用了梯度的筛选能力,又不依赖它的精确性。
练习 4(读一份防御报告). 一篇论文提出一个”注入检测器”:在 5000 条从公开数据集收集的注入样本上训练,在另外 1000 条同来源的样本上测得召回率 99.6%,在正常文档上误报率 0.8%;部署后,在 AgentDojo 的静态攻击集上 ASR 从 45% 降到 1%。(a) 这些数字支持什么结论、不支持什么结论?(b) 你会要求作者补做哪三个实验?(c) 如果补做的实验里自适应 ASR 是 60%,这个检测器还有用吗?
参考答案
(a) 支持:“对和训练数据同一来源的已知攻击,检测器很准、误报低”。不支持:“能防住注入”——测试集和训练集来自同一个攻击生成过程(§6.3 的 0.000),AgentDojo 的攻击集也是静态的。(b) ① 自适应攻击:攻击者知道检测器(白盒或至少能查询它),把”不被检测到”写进目标,报告 ASR 随查询预算的曲线;② 换攻击家族的泛化:用训练时没见过的注入风格(陈述式、多语言、编码、分散在多个文档里);③ 效用与误报的分层:文档里合法指令(“请在周五前回复”)被误拦的比例、对非英语文档的误报率。外加置信区间。(c) 仍可能有用,但角色变了:它是提高攻击成本、拦住低成本攻击的一层(defense in depth),而不是安全保证。真正的保证要来自不依赖识别攻击的机制(最小权限、确认、能力隔离)。论文应当这样表述它的贡献。
练习 5(ASR@k). 一个系统对某个有害行为的单次越狱成功率是 。(a) 写出 次独立尝试内至少成功一次的概率。(b) 时,要使 ASR@k ≥ 0.5 需要多少次尝试?(c) 代码 05 演示 2 里,ASR@100 = 0.537,但单次均值只有 0.038;如果所有行为的 都等于 0.038,ASR@100 会是多少?为什么真实的值更低?
参考答案
(a) 。(b) 。(c) 。真实值(0.537)更低,是因为 高度不均匀:均值被少数容易的行为拉高,大多数行为的 很小(中位数 0.0092),对它们 100 次也不够。 是 的凹函数,所以由 Jensen 不等式,。报告 ASR@k 时,用平均的 去外推会高估;但反过来,对攻击者来说”总有一些行为很容易”本身就是风险。
练习 6(水印的统计). 设 γ = 0.25,水印让每个 token 落进绿名单的概率变成 0.35(独立)。(a) 个 token 时 的期望约是多少?(b) 要让期望 达到 4,需要多长的文本?(c) 如果攻击者随机替换 30% 的 token,且每次替换让”它自己”和”下一个 token”的绿名单判定都变成随机的(绿的概率 γ),绿概率大约变成多少?此时需要多长?
参考答案
(a) 。(b) 。(c) 一个位置”未受影响”需要它自己和它前一个 token 都没被替换,概率约 ;受影响的位置绿概率是 γ。所以绿概率 ≈ ,提升只剩 0.049,约为原来的一半;,是原来的 4 倍以上。这就是代码 04 里”绿比例掉得比替换率更快”的原因。
代码相关的练习(“动手改一改”)见 code/lecture15/README.md。
14. 延伸阅读 · Further reading
本讲的主要来源(课程材料)
- CS329T Fall 2023 Lecture 1(alignment slides)、Lecture 4(Security:Taly、Carlini、Wang)、Lecture 6(项目方向);Zifan Wang, “Adversarial Attacks on Aligned LLMs”(slides)。
- CS329T Autumn 2025, Oct 30:Matt Fredrikson & Andy Zou;Andy Zou, “Red Teaming in the real-world and Robust Mitigations”(slides)。
越狱与优化攻击
- Zou, Wang, Carlini, Nasr, Kolter & Fredrikson, “Universal and Transferable Adversarial Attacks on Aligned Language Models”, 2023(GCG、AdvBench)。
- Wei, Haghtalab & Steinhardt, “Jailbroken: How Does LLM Safety Training Fail?”, NeurIPS 2023。
- Kang et al., “Exploiting Programmatic Behavior of LLMs: Dual-Use Through Standard Security Attacks”, 2023。
- Chao et al., “Jailbreaking Black Box Large Language Models in Twenty Queries”(PAIR),2023;Mehrotra et al., “Tree of Attacks”(TAP),2023。
- Liu et al., “AutoDAN: Generating Stealthy Jailbreak Prompts on Aligned LLMs”, 2023;Zhu et al., “AutoDAN: Interpretable Gradient-Based Adversarial Attacks on LLMs”, 2023。
- Andriushchenko, Croce & Flammarion, “Jailbreaking Leading Safety-Aligned LLMs with Simple Adaptive Attacks”, 2024。
- Anil et al., “Many-shot Jailbreaking”, 2024;Yong, Menghini & Bach, “Low-Resource Languages Jailbreak GPT-4”, 2023;Hughes et al., “Best-of-N Jailbreaking”, 2024。
- Arditi et al., “Refusal in Language Models Is Mediated by a Single Direction”, 2024。
Prompt injection 与 agent 安全
- Perez & Ribeiro, “Ignore Previous Prompt: Attack Techniques for Language Models”, 2022。
- Greshake et al., “Not What You’ve Signed Up For: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection”, AISec 2023。
- Hines et al., “Defending Against Indirect Prompt Injection Attacks With Spotlighting”, 2024。
- Wallace et al., “The Instruction Hierarchy: Training LLMs to Prioritize Privileged Instructions”, 2024。
- Debenedetti et al., “AgentDojo”, NeurIPS 2024 Datasets & Benchmarks;Debenedetti et al., “Defeating Prompt Injections by Design”(CaMeL),2025。
- Zou et al., “Security Challenges in AI Agent Deployment: Insights from a Large Scale Public Competition”, arXiv 2507.20526, 2025。
- Simon Willison 关于 prompt injection、dual LLM pattern 与 “lethal trifecta” 的系列博客。
防御与评估
- Jain et al., “Baseline Defenses for Adversarial Attacks Against Aligned Language Models”, 2023;Alon & Kamfonas, “Detecting Language Model Attacks with Perplexity”, 2023;Inan et al., “Llama Guard”, 2023。
- Zou et al., “Representation Engineering: A Top-Down Approach to AI Transparency”, 2023;Zou et al., “Improving Alignment and Robustness with Circuit Breakers”, NeurIPS 2024;Schwinn & Geisler, “Revisiting the Robust Alignment of Circuit Breakers”, 2024。
- Mazeika et al., “HarmBench”, ICML 2024;Andriushchenko et al., “AgentHarm”, ICLR 2025;Röttger et al., “XSTest”, NAACL 2024。
- Nasr, Carlini et al., “The Attacker Moves Second: Stronger Adaptive Attacks Bypass Defenses against LLM Jailbreaks and Prompt Injections”, 2025。
生成文本检测
- Kirchenbauer et al., “A Watermark for Large Language Models”, ICML 2023;Mitchell et al., “DetectGPT”, ICML 2023;Sadasivan et al., “Can AI-Generated Text be Reliably Detected?”, 2023;Liang et al., “GPT Detectors Are Biased Against Non-Native English Writers”, Patterns 2023。
15. 下一讲预告 · Next lecture
Lecture 16:以评估为驱动的优化与课程总结(Evaluation-Driven Optimization & Synthesis)。
本讲反复出现一个模式:一旦有了一个可以被优化的分数,就会有人去优化它 —— 攻击者优化 GCG 的损失、绕过检测器的分数、绕过困惑度阈值;防御者如果只盯着静态 benchmark 的 ASR,也会把系统”调”到对那个 benchmark 完美、对新攻击脆弱。第 16 讲把这个视角反过来用在建设上:DSPy、TextGrad、GEPA 把 LLM 程序本身当作优化对象,RL for agents 用环境里的奖励改进 agent。同样的问题会出现在另一侧 —— 优化会不会”刷”评估(Goodhart、reward hacking)?怎样用 dev / test 分离和第 1 讲 §8 的测量心态防住它?最后,第 16 讲回顾整门课的六个透镜,以及从模型到应用再到智能体,“被信任的对象”和”评估方法”是怎样一起演变的。