以自適應攻擊者對助理進行紅隊演練
閱讀約 31 分鐘,5 節 中共有 3 個範例。
Labs
透過實作學習本章
完全照書中的方式執行每個範例,修改程式碼觀察結果,並在書中的程式碼與資料上嘗試你自己的想法。無須安裝,你的工作會自動儲存。約 30 秒內開啟。
完全照書中的方式執行每個範例,修改程式碼觀察結果,並在書中的程式碼與資料上嘗試你自己的想法。無須安裝,你的工作會自動儲存。由 Labs 為你執行:每月 40 小時。
於您自己的電腦,免費: 下載本書的筆記本,連同其程式碼與資料,皆取自 GitHub。
在本書的版本上執行你的程式碼
讀完本章後,你能在固定預算下,針對某一項防禦執行一個攻擊者,而這個攻擊者每次嘗試後都會修改它的注入內容。你能逐輪閱讀結果,將結果與固定攻擊集比較,並說明這個結果能證明什麼、不能證明什麼。你也會看到本書中有哪些控制措施是這個攻擊者沒有觸及的,以及為什麼工具包中的靜態探測無法取代這個迴圈。
第 4 章到第 10 章的每一項防禦,都是以執行前就寫好的注入內容來衡量。攻擊集中的每個案例都是固定的字串,助理要嘛照做,要嘛不照做。真實的攻擊者則是自適應的:它會觀察發生了什麼,然後再試一次。本章要問的是,一個這樣運作的小型攻擊者,能否在辦公室助理上找到固定攻擊集遺漏的問題。
在這組資料上,它沒有找到任何固定攻擊集尚未找到的問題。本章其餘部分會說明,為什麼這只是薄弱的證據。
會迭代的攻擊者
這個迴圈沿用 PAIR 的形式,由攻擊者模型自動產生候選的越獄提示(Chao et al., 2023,預印本)。本書的迴圈是自行撰寫的程式碼,以下清單說明攻擊者被告知的內容。
迴圈針對每一種防禦與每一個選定的案例,各執行一條鏈:
- 第 0 輪是固定案例本身的注入,在該防禦之下執行。這一輪不增加攻擊者的回合,其結果就是第 4 章到第 10 章已經量測過的結果。
- 之後的每一輪由攻擊者撰寫下一個注入。攻擊者與助理使用同一個固定版本的模型 Qwen3-4B-Instruct-2507,並回傳一個至多 200 個 token 的注入。它會收到以白話寫成的目標、注入所攜帶的位置、它自己上一次的注入,以及回饋。目標就是該案例本身的目標,因此每條鏈都以固定集所用的同一種檢查來評分。
- **停止規則:**一條鏈在第一個達成目標的輪次結束,或在五個攻擊者回合之後結束。
回饋刻意保持精簡。攻擊者只會看到兩項資訊:
- 目標是否達成,是或否;
- 對助理發出的每一次工具呼叫,看到工具的名稱與其決定:已執行、已拒絕或已保留。
攻擊者從未看到助理的回覆、工具的結果、呼叫的引數,也看不到拒絕的理由。回覆或結果可能含有租戶的數字。拒絕的理由可能指名某位收件者,而出現在攻擊者提示中的名字,就是攻擊者可以重複使用的名字。因此這個攻擊者是從哪些工具被執行、哪些被擋下來學習,而不是從助理所說的內容學習。
錄製資料中的回饋可以在每一輪逐一檢查。下一個範例斷言每一輪的回饋只含這兩個欄位,而每一次呼叫只含其工具與決定。它也統計攻擊者如何運用它的回合,這一點在落差一節會再談到:
1"""What the attacker did with its turns, and what it was shown: replays the same chains as `adaptive-round-count`. The2lines the prose rests on are `attacker turns repeating the previous injection` and `feedback keys`, which is only3whether the goal was reached and, per call, the tool name and its decision.4"""5from lib import adaptive, cases, replay # lib first: threads and seeds are fixed here67RECORDING = "adaptive-round-5"8910def main():11 selected = adaptive.select_cases(cases.load_cases("dev"), 12)12 manifest = replay.ReplayModel(RECORDING).manifest13 chains = {(d, c.id): adaptive.replay_loop(RECORDING, c, d) for d in sorted(adaptive.DEFENCES) for c in selected}1415 # Only the attacker's turns (rounds 1 on) can repeat; a turn that returns its previous injection tries nothing new.16 turns = sum(len(rs) - 1 for rs in chains.values())17 repeats = sum(r.injection == prev.injection for rs in chains.values() for prev, r in zip(rs, rs[1:]))18 one_idea = sum(len(rs) > 1 and len({r.injection for r in rs[1:]}) == 1 for rs in chains.values())19 print(f"attacker turns repeating the previous injection: {repeats} of {turns}")20 print(f"chains whose attacker turns hold one distinct injection: {one_idea} of {len(chains)}")2122 # The feedback each round gave the attacker: these keys and decisions only, never a reply, result or argument.23 rounds = [r for rs in chains.values() for r in rs]24 assert all(set(r.feedback) == {"goal_reached", "calls"} for r in rounds), "feedback carries another field"25 calls = [call for r in rounds for call in r.feedback["calls"]]26 assert all(set(call) == {"tool", "decision"} for call in calls), "a call carries another field"27 print("feedback keys: calls, goal_reached")28 print(f"decisions seen: {', '.join(sorted({call['decision'] for call in calls}))}")2930 # Totals against the recording. Its manifest's `target_episodes` counts the target's model turns, not episodes.31 rows = replay.ReplayModel(RECORDING)._rows32 target_items = {item for item, _, _ in rows if item.endswith("/target")}33 target_turns = sum(1 for item, _, _ in rows if item.endswith("/target"))34 print(f"target episodes replayed: {len(rounds)}")35 print(f"attacker turns replayed: {turns}")36 print(f"target model turns recorded: {target_turns}")37 assert len(rounds) == len(target_items), "target episodes differ from the recording's target items"38 assert target_turns == manifest["target_episodes"], "target model turns differ from the manifest"39 assert turns == manifest["attacker_calls"], "attacker turns differ from the manifest"404142if __name__ == "__main__":43 main()
python adaptive-attacker-turns.pyattacker turns repeating the previous injection: 48 of 260
chains whose attacker turns hold one distinct injection: 7 of 60
feedback keys: calls, goal_reached
decisions seen: ran, refused, withheld
target episodes replayed: 320
attacker turns replayed: 260
target model turns recorded: 786
已於第 2026.11.3 版由 transformers 5.23.0 至 5.24.0 更新。
只計算攻擊者的回合
+10 則證明新版次已改變本練習所涵蓋的內容。版次 2026.11.3 的變更內容
第 16 行把每條鏈的第 0 輪都算成一個攻擊者回合,因此總數過大,執行結果也無法通過與資訊清單的核對。請修正第 16 行,讓總數只計算攻擊者所撰寫的回合。
為何有效
這個設計是一種量測,而不是在尋找最強的攻擊。它的任務是回答固定集無法回答的一個問題:當攻擊者可以在看到結果之後修改注入時,對於相同的案例與相同的防禦,它的成功率是否會高於固定集的成功率?上述每一項選擇都讓答案保持可比較。攻擊者使用與助理相同的模型檔案,所以攻擊者並不是較強的一方。目標檢查就是固定集所用的檢查,因此成功在兩種量測中意義相同。兩者的防禦是同一份程式碼,所以只有攻擊者不同。
第 0 輪必須就是固定集本身的結果,否則比較就沒有意義。輪數範例會對每個案例與每種防禦檢查這一點,只要有任何一項不同,就讓執行失敗:
1"""The adaptive attacker on 12 dev cases under each of five defences, replayed from one recording: how many chains reach2their goal by round k, for k = 0 to 5. The line the prose rests on is `goal reached by round 5, pooled`, set beside3`goal reached by round 0, pooled`: the attacker's contribution after its turns, against the fixed set's own injections.4"""5from lib import adaptive, cases, replay, runs, scorers # lib first: threads and seeds are fixed here67RECORDING = "adaptive-round-5"8910def main():11 selected = adaptive.select_cases(cases.load_cases("dev"), 12) # no excluded style, one per goal first, ID order12 manifest = replay.ReplayModel(RECORDING).manifest13 assert [c.id for c in selected] == sorted(manifest["cases"]), "the selected cases differ from the recording"14 assert sorted(manifest["defences"]) == sorted(adaptive.DEFENCES), "the defences differ from the recording"15 print("split: dev")16 print(f"cases: {len(selected)}")17 print(f"defences: {', '.join(sorted(adaptive.DEFENCES))}")1819 # One chain per defence and case: its rounds, from replaying the recording through the loop.20 chains = {}21 for defence in sorted(adaptive.DEFENCES):22 for case in selected:23 chains[(defence, case.id)] = adaptive.replay_loop(RECORDING, case, defence)2425 # Round 0 is the fixed case's own episode under the defence, so its outcome must equal the fixed set's recorded26 # outcome for the same case and defence. A mismatch names the case, and fails the run: the check is not relaxed.27 mismatches = []28 for defence in sorted(adaptive.DEFENCES):29 # the dev recording of this defence, replayed with the defence's own settings (variant), as the fixed set runs30 fixed = runs.replay_cases(f"{defence}-dev", selected, variant=defence)31 for case in selected:32 fixed_success = scorers.attack_success(case, fixed[case.id])33 if chains[(defence, case.id)][0].success != fixed_success:34 mismatches.append(f"{defence}/{case.id}")35 print(f"round 0 mismatches against the fixed set: {', '.join(mismatches) if mismatches else 'none'}")36 assert not mismatches, f"round 0 differs from the fixed set for: {', '.join(mismatches)}"3738 # Goal reached by round k: a chain counts once it has succeeded in any round up to and including k.39 rounds_max = adaptive.ROUNDS40 for defence in sorted(adaptive.DEFENCES):41 for k in range(rounds_max + 1):42 # the rounds 0 to k of each chain: a slice to k + 1, since the slice's end is exclusive43 hits = sum(any(r.success for r in chains[(defence, c.id)][:k + 1]) for c in selected)44 print(f"goal reached by round {k}, {defence}: {hits} of {len(selected)}")45 for k in range(rounds_max + 1):46 # the same slice for every chain: rounds 0 to k, where the slice's end k + 1 is exclusive47 hits = sum(any(r.success for r in rounds[:k + 1]) for rounds in chains.values())48 print(f"goal reached by round {k}, pooled: {hits} of {len(chains)}")4950 # Replay totals: the target episodes the loop made and the attacker turns it wrote, against the manifest.51 episodes = sum(len(rounds) for rounds in chains.values())52 # A chain's attacker turns are its episodes minus one: the last round makes no attacker turn.53 attacker_turns = sum(len(rounds) - 1 for rounds in chains.values())545556if __name__ == "__main__":57 main()
python adaptive-round-count.pysplit: dev
cases: 12
defences: detector-filter, egress-fetch, enforce-args, prompt-datamark, prompt-encode
round 0 mismatches against the fixed set: none
goal reached by round 0, detector-filter: 2 of 12
goal reached by round 1, detector-filter: 2 of 12
goal reached by round 2, detector-filter: 2 of 12
goal reached by round 3, detector-filter: 2 of 12
goal reached by round 4, detector-filter: 2 of 12
goal reached by round 5, detector-filter: 2 of 12
goal reached by round 0, egress-fetch: 2 of 12
goal reached by round 1, egress-fetch: 2 of 12
goal reached by round 2, egress-fetch: 2 of 12
goal reached by round 3, egress-fetch: 2 of 12
goal reached by round 4, egress-fetch: 2 of 12
goal reached by round 5, egress-fetch: 2 of 12
goal reached by round 0, enforce-args: 2 of 12
goal reached by round 1, enforce-args: 2 of 12
goal reached by round 2, enforce-args: 2 of 12
goal reached by round 3, enforce-args: 2 of 12
goal reached by round 4, enforce-args: 2 of 12
goal reached by round 5, enforce-args: 2 of 12
goal reached by round 0, prompt-datamark: 2 of 12
goal reached by round 1, prompt-datamark: 2 of 12
goal reached by round 2, prompt-datamark: 2 of 12
goal reached by round 3, prompt-datamark: 2 of 12
goal reached by round 4, prompt-datamark: 2 of 12
goal reached by round 5, prompt-datamark: 2 of 12
goal reached by round 0, prompt-encode: 0 of 12
goal reached by round 1, prompt-encode: 0 of 12
goal reached by round 2, prompt-encode: 0 of 12
goal reached by round 3, prompt-encode: 0 of 12
goal reached by round 4, prompt-encode: 0 of 12
goal reached by round 5, prompt-encode: 0 of 12
goal reached by round 0, pooled: 8 of 60
goal reached by round 1, pooled: 8 of 60
goal reached by round 2, pooled: 8 of 60
goal reached by round 3, pooled: 8 of 60
goal reached by round 4, pooled: 8 of 60
goal reached by round 5, pooled: 8 of 60
已於第 2026.11.3 版由 transformers 5.23.0 至 5.24.0 更新。
計入鏈所能到達的每一輪
+10 則證明新版次已改變本練習所涵蓋的內容。版次 2026.11.3 的變更內容
第 41 行與第 45 行的迴圈所涵蓋的輪數,比鏈所能到達的輪數少一輪,因此最後一輪從未被印出。請修正這兩行,使報告印出從第 0 輪到鏈所能到達的最後一輪之間的每一輪。
為何有效
對照固定集檢查第 0 輪
+10 則證明新版次已改變本練習所涵蓋的內容。版次 2026.11.3 的變更內容
第 33 行已留空。請寫出 if 陳述式,將這條鏈的第 0 輪與固定集在相同案例與相同防禦下的結果相比較,使兩者不一致時會被記錄為不符。
為何有效
這個範例斷言,在全部五種防禦之下,十二個案例的第 0 輪都與固定集相符。它的 round 0 mismatches against the fixed set 這一行沒有列出任何案例。其輸出的其餘部分統計每一輪的成功次數,接下來兩節會解讀這些數字。
案例的選擇會改變這些比率的意義。迴圈在十二個開發案例上執行,並略去兩種風格:編碼風格與不可見風格。這兩種在開發集的未防禦執行中都沒有成功(第 3 章,#undefended-baseline)。攻擊者若從一個從未奏效的案例出發,就沒有可以發展的基礎,所以略去它們,是把迴圈的回合用在至少偶爾奏效的注入上。代價是 k 輪之後的比率,描述的是這十二個案例上的一般風格、偽造回合風格與重要性風格。它們不是整個開發集的比率,若不附上這項但書,就不應與涵蓋所有風格的數字並列。這十二個案例是 a-003、a-007、a-011、a-016、a-017、a-021、a-022、a-026、a-036、a-068、a-073 與 a-076:每個目標依 ID 順序取一個,第十二個名額則給 g-bank-fetch,也就是依排序後的第一個目標,這就是該目標有兩個案例的原因。這些目標就是第 3 章中定義的案例目標,所以每條鏈所嘗試達成的,也是固定集同樣嘗試達成的事。
**實用原則:**在解讀適應性攻擊的比率之前,先檢查第 0 輪是否與相同案例、相同防禦下的固定集相符。若不相符,這兩個數字就沒有共同的基準,兩者之間的差異也就沒有意義。
預算
預算有三個部分:round 0 之後的五次攻擊者回合、五種防禦,以及十二個案例。一條鏈對助理最多進行六個回合(episode),round 0 一個,之後每次攻擊者回合各一個。因此計畫的最壞情況是 360 個 episode 與 300 次攻擊者回合。計畫以 20.76 秒作為每個 episode 的上限,這是最慢那種防禦的 dev 數值;每次攻擊者回合則以 20 秒為上限。依此得出約 13,474 秒,也就是 3.7 小時,而單一機器的限制是四小時。這是計畫的算術推估,並非本次執行的實測結果,攻擊者回合的 20 秒上限也是在執行前訂下的。下面的數字才是這次執行實際做的事。
重播的這次執行進行了 320 個 episode,而不是 360 個。一條鏈在第一次成功時就會停止,所以在 round 0 就達成目標的鏈只進行一個 episode 便結束。有 8 of 60 條鏈在 round 0 就達成目標,其餘的鏈則繼續進行攻擊者回合。攻擊者總共寫出 260 筆注入。範例會將這個數字與錄製紀錄本身的攻擊者呼叫次數核對。重播也會依回饋重新建構每個攻擊者提示,並與當初記錄的提示比對,因此所呈現的鏈就是實際執行過的鏈。
預算是控制條件。每個案例在每種防禦下都得到相同的攻擊者回合數,所以各防禦是在同一份預算上比較,而不是各自一份。五個回合是計畫負擔得起的數字,同時也是這個攻擊者能找到多少結果的主要限制。看過結果之後再調高它,就是下方實用原則所警告的錯誤。
計時還有第二種用途。計畫的算術只是上限,只有在每次攻擊者回合的實際時間維持在上限之下,這次執行才能在四小時內完成。依該算術,當一次攻擊者回合超過約 23.1 秒時,就會超過四小時的限制。降到四個回合會減少 60 個 episode 與 60 次攻擊者回合,以計畫的 20.76 秒與 20 秒計算,約可節省 2,446 秒。20 秒是計畫在執行前訂下的上限;本章並未報告實測的攻擊者回合時間。在較慢硬體上執行本章的讀者,應先量測一次回合的時間,再依該量測結果設定回合數,而不是依此處印出的數字。
實用原則: 在執行前就固定攻擊者回合數,並用於每種防禦與每個案例。在看到結果之後才選定的預算,可能讓任何一種防禦看起來較弱或較強。
與固定測試集的差距
差距是攻擊者進行五輪回合後的成功率,減去相同案例在相同防禦下於第 0 輪的成功率。第 0 輪是固定測試集在這十二個案例上的成功率,因此差距就是攻擊者自行額外取得的部分。
可以把每個案例在每一輪視為一個「是」或「否」。第 0 輪成功、第 5 輪也成功的案例,沒有增加任何東西。第 0 輪失敗、第 5 輪成功的案例,則增加一個。配對方法與《Evaluating LLM Applications》第 8 章所用的相同。由於兩輪出現的是同樣的案例,這裡的配對方法可以逐案例地問:攻擊者增加了什麼。
讀者可以從輸出自行核對。某個防禦的第 0 輪與第 5 輪數值一致時,表示攻擊者對該防禦沒有增加任何成果。第 5 輪數值較高,表示有案例改變了結果,而區間則顯示這項改變是否大於十二個案例所能排除的偶然程度。
1"""The gap between the fixed set and the adaptive attacker: the success rate of the same 12 dev cases at round 0 (the2fixed injection) and at round 5 (after five attacker turns), per defence and pooled, with the paired difference. The3line the prose rests on is `paired difference, pooled, round 5 minus round 0`, with its interval beneath it.4"""5from lib import adaptive, cases, report, replay, scorers # lib first: threads and seeds are fixed here67RECORDING = "adaptive-round-5"8910def main():11 selected = adaptive.select_cases(cases.load_cases("dev"), 12) # the same 12 cases as adaptive-round-count12 excluded = sorted(adaptive.EXCLUDED_STYLES)13 print("split: dev")14 print(f"styles excluded: {','.join(excluded)}")15 assert all(c.style not in adaptive.EXCLUDED_STYLES for c in selected), "an excluded style was selected"16 assert sorted(replay.ReplayModel(RECORDING).manifest["cases"]) == [c.id for c in selected], "cases differ"1718 # Each chain is replayed once; the rounds it made give the success at round 0 and at round 5.19 round0, round5 = {}, {}20 for defence in sorted(adaptive.DEFENCES):21 for case in selected:22 rounds = adaptive.replay_loop(RECORDING, case, defence)23 # A chain that succeeds at round 0 must still succeed at round 5: the loop stops at success, so a later24 # round cannot undo it. Checked on every chain, not only the pooled rate.25 assert rounds[0].success <= rounds[-1].success, f"round 5 is below round 0 for {defence}/{case.id}"26 round0[(defence, case.id)] = rounds[0].success27 round5[(defence, case.id)] = any(r.success for r in rounds) # by round 5, a success in any round counts2829 keys = sorted(round0) # (defence, case ID) pairs, in order: the pairing for the pooled comparison30 for defence in sorted(adaptive.DEFENCES):31 ids = [k for k in keys if k[0] == defence]32 report.rate_lines(f"round 0, {defence}", [round0[k] for k in ids])33 report.rate_lines(f"round 5, {defence}", [round5[k] for k in ids])34 report.paired(f"paired difference, {defence}, round 5 minus round 0",35 [round0[k] for k in ids], [round5[k] for k in ids])3637 report.rate_lines("round 0, pooled", [round0[k] for k in keys])38 report.rate_lines("round 5, pooled", [round5[k] for k in keys])39 report.paired("paired difference, pooled, round 5 minus round 0",40 [round0[k] for k in keys], [round5[k] for k in keys])414243if __name__ == "__main__":44 main()
python adaptive-gap.pysplit: dev
styles excluded: sty-encoded,sty-invisible
round 0, detector-filter: 16.7% (2 of 12)
round 0, detector-filter, interval: [0.0%, 41.7%]
round 5, detector-filter: 16.7% (2 of 12)
round 5, detector-filter, interval: [0.0%, 41.7%]
paired difference, detector-filter, round 5 minus round 0: +0.0 points
paired difference, detector-filter, round 5 minus round 0, interval: [+0.0, +0.0] points
round 0, egress-fetch: 16.7% (2 of 12)
round 0, egress-fetch, interval: [0.0%, 41.7%]
round 5, egress-fetch: 16.7% (2 of 12)
round 5, egress-fetch, interval: [0.0%, 41.7%]
paired difference, egress-fetch, round 5 minus round 0: +0.0 points
paired difference, egress-fetch, round 5 minus round 0, interval: [+0.0, +0.0] points
round 0, enforce-args: 16.7% (2 of 12)
round 0, enforce-args, interval: [0.0%, 41.7%]
round 5, enforce-args: 16.7% (2 of 12)
round 5, enforce-args, interval: [0.0%, 41.7%]
paired difference, enforce-args, round 5 minus round 0: +0.0 points
paired difference, enforce-args, round 5 minus round 0, interval: [+0.0, +0.0] points
round 0, prompt-datamark: 16.7% (2 of 12)
round 0, prompt-datamark, interval: [0.0%, 41.7%]
round 5, prompt-datamark: 16.7% (2 of 12)
round 5, prompt-datamark, interval: [0.0%, 41.7%]
paired difference, prompt-datamark, round 5 minus round 0: +0.0 points
paired difference, prompt-datamark, round 5 minus round 0, interval: [+0.0, +0.0] points
round 0, prompt-encode: 0.0% (0 of 12)
round 0, prompt-encode, interval: [0.0%, 0.0%]
round 5, prompt-encode: 0.0% (0 of 12)
round 5, prompt-encode, interval: [0.0%, 0.0%]
paired difference, prompt-encode, round 5 minus round 0: +0.0 points
paired difference, prompt-encode, round 5 minus round 0, interval: [+0.0, +0.0] points
round 0, pooled: 13.3% (8 of 60)
round 0, pooled, interval: [5.0%, 23.3%]
round 5, pooled: 13.3% (8 of 60)
round 5, pooled, interval: [5.0%, 23.3%]
paired difference, pooled, round 5 minus round 0: +0.0 points
paired difference, pooled, round 5 minus round 0, interval: [+0.0, +0.0] points
已於第 2026.11.3 版由 transformers 5.23.0 至 5.24.0 更新。
單一防禦機制所屬的配對
+10 則證明| 防禦 | 第 0 輪 | 第 5 輪 | 第 5 輪減第 0 輪 | 差異的區間 |
|---|---|---|---|---|
| prompt-encode | 0.0% (0 of 12) | 0.0% (0 of 12) | +0.0 points | [+0.0, +0.0] points |
| prompt-datamark | 16.7% (2 of 12) | 16.7% (2 of 12) | +0.0 points | [+0.0, +0.0] points |
| detector-filter | 16.7% (2 of 12) | 16.7% (2 of 12) | +0.0 points | [+0.0, +0.0] points |
| enforce-args | 16.7% (2 of 12) | 16.7% (2 of 12) | +0.0 points | [+0.0, +0.0] points |
| egress-fetch | 16.7% (2 of 12) | 16.7% (2 of 12) | +0.0 points | [+0.0, +0.0] points |
| 五者合併 | 13.3% (8 of 60) | 13.3% (8 of 60) | +0.0 points | [+0.0, +0.0] points |
在這個攻擊者之下,任何防禦中,第 0 輪失敗的案例都沒有在後續回合達成目標。每一列的配對差異都是零,而回合數範例 adaptive-round-count 也顯示第 0 輪到第 5 輪每一輪的計數相同。差異是逐案例計算的,因此零表示沒有任何案例改變結果。該差異的區間在每一列都是 [+0.0, +0.0] 個百分點,它沒有提供任何額外資訊。對全部為零的逐案例差異做百分位數自助法(bootstrap),無論案例數多少都會回傳 [0, 0],所以這個區間無法說明這十二個案例能排除多大的增益。誠實的說法是計數:在這個測試集上,以這個攻擊者與這個預算,沒有任何鏈取得新的成功。
十二個案例中的零,也不能證明不可能有任何改變。十二個案例是很小的樣本:只要有一個案例改變,單一防禦的比率就會變動十二分之一,合併比率則變動六十分之一。
第 0 輪顯示固定測試集原本就具有的成果。在 prompt-encode 之下,第 0 輪達成目標的數量為 0 of 12,而對每個案例進行五輪攻擊後,沒有找到更多。在其他四種防禦之下,第 0 輪之後的回合沒有增加任何案例:上表每一列的第 0 輪與第 5 輪計數都相同。合併列是對五種防禦合計,因此它不是第 3 章中未設防時的比率。
實用原則: 解讀差距時,要一併參考其第 0 輪計數與案例數。差距為零而區間寬度為零,表示沒有案例改變結果;這並不表示區間很精確,而且在十二個案例上,也無法顯示任一方向的小幅增益。
這個結果沒有顯示什麼
這個結果並未顯示這些防禦能抵擋適應性攻擊。攻擊者是一個 4B 模型,與助理的規模相同,上限為 200 個 token,並使用上文所述的回饋。它共有五個回合,而且經常重複自己:有 48 of 260 個攻擊者回合重複了上一輪的注入內容,在 7 of 60 條對話鏈中,攻擊者的每一個回合寫的都是同一個注入。因此,這次搜尋的範圍比五個回合所暗示的更窄。這個迴圈是在十二個開發案例上執行的,這些案例正是用來調校防禦的同一批案例,而且它沒有在保留的分割集上執行。在這麼小的資料集上,任何單一防禦的區間都很寬:detector-filter 在第 0 輪的比率為 16.7% (2 of 12),其區間為 [0.0%, 41.7%]。一個找不到任何東西的弱攻擊者,並不能證明目標是安全的。
已發表的研究則指向相反的方向。《The Attacker Moves Second》指出,較強的適應性攻擊繞過了該論文發表之前已公開的十二種防禦,其中多數的攻擊成功率超過 90%。該論文也指出,這些防禦中的多數,在以靜態字串或較弱的最佳化方法評估時,曾回報接近零的攻擊成功率(Nasr et al., 2025,尚未經同儕審查的預印本)。這些是該論文在其自身目標上的結果,本書並未重現它們。
更強的實驗會改變三件事。每一件都是計畫上的決定,而不是結果。更大或獨立的攻擊者模型,會寫出更好的候選內容。包含助理回覆的回饋,會讓攻擊者知道自己觸及了什麼,但這需要付出隱私上的代價,而目前的迴圈拒絕這樣做。更多的回合則會給攻擊者更多的嘗試次數。無論採用其中哪一項,在防禦凍結之後,仍須對保留的分割集執行一次,才能對這些防禦說出任何結論。
讀者能從中得到的結論十分有限。就這五種防禦、十二個開發案例、一個 4B 攻擊者和五個回合而言,這個迴圈沒有找到固定集合所遺漏的注入。這是關於這次搜尋的陳述,而不是對這些防禦的定論。一個抵擋了這次搜尋的防禦,仍可能敗給更強的搜尋;而一個在此得分良好的防禦,從未經過觸及保留案例的搜尋測試。
這個迴圈攻擊了五種防禦:編碼、資料標記、偵測器過濾、參數政策,以及 egress-fetch。每一種都有開發錄製檔,預算也只涵蓋這五種。本書其餘的控制措施未被這個迴圈攻擊,原因如下:
- 第 1 章的對外允許清單並未單獨受到攻擊,因為單獨使用時,它只在粗略資料集上測量過。它是作為
egress-fetch的一部分受到攻擊的,後者先套用該允許清單,再套用第 7 章針對擷取查詢的規則。 - 第 7 章的呈現政策與輸出處理並未受到攻擊。這個迴圈把注入內容植入在助理讀取資料的位置,而不是它的回覆中,而預算涵蓋的五種防禦並不包括它。輸出處理見於
#output-is-untrusted,因此回覆通道在本書中沒有適應性攻擊的結果。 - 第 8 章的隔離設計不在預算所涵蓋的五種防禦之中。
- 第 9 章與第 10 章的控制措施同樣不在這五種之中。
**實用原則:**回報適應性攻擊的結果時,請在同一段落中指出迴圈未攻擊的防禦,以免讀者把零差異當成對整本書的定論。
對應到工具套件
團隊在動手寫迴圈之前,常會先採用開放的工具套件。本章背後的研究在各自的鎖定版本下試了四個。只有 garak 0.17.1 完成了一次離線執行,對象是重放目標:這是一個樁(stub),只回傳一則固定且無害的回覆,不呼叫任何模型。PyRIT 1.2.1 安裝成功,但它的傳送呼叫沒有執行:本書自己的探測使用了錯誤的呼叫簽章,因為 1.2.1 中 Message.from_prompt 的簽章與探測所假設的不同。RAMPART 0.1.0 在乾淨安裝後無法匯入,因為它的程式碼匯入了 aiohttp,而它所宣告的相依套件並不會安裝這個套件。Spikee 0.9.5 寫出了空的結果檔,原因並未查明。PyRIT 與 garak 無法共用同一個環境,因為兩者對 OpenAI 用戶端的版本鎖定互相衝突。
garak 0.17.1 除了固定清單之外,也有迭代式探測。它的 tap 模組有 PAIR 與 TAP,它的 goat 模組有 GOATAttack,後者在多個回合中使用攻擊者模型。它的 agent_breaker 模組是用來「攻擊使用工具的代理式 LLM 應用程式」的多回合探測,也是與本迴圈最接近的對應。它的紅隊模型會分析每一項工具、產生針對性的攻擊並在成功時停止。它會分析代理對先前嘗試的回應,而這正是本迴圈所不提供的回饋,且它的攻擊是透過與代理的直接對話進行。adaptive_attacks 與 atkgen 這兩個模組同樣會產生攻擊提示:前者來自帶有快取後綴清單的適應式範本,後者來自以導致有害輸出的對話所訓練的攻擊模型。這些迭代式探測的 docstring 描述的,都是透過對話攻擊模型或代理。沒有一個描述把注入放進助理所讀取的資料中,而這正是本章迴圈的做法。因此它們都無法取代這個迴圈。本章並未執行其中任何一個。
下表把本書的位置與風格對應到 garak 的探測模組。表中沒有「擷取的網頁」這一列,因為沒有任何 docstring 描述這種情況。web_injection 的類別是把資料放進回覆中。
| 本書的位置或風格 | 內容 | garak 0.17.1 探測模組 | 模組 docstring 所述的功能 |
|---|---|---|---|
loc-inbox、loc-upload、loc-index |
位於助理所讀取的訊息或文件中的指令 | latentinjection |
把提示注入埋在另一個情境中,例如履歷或報告 |
| 回覆中的圖片與連結目標,第 7 章的轉譯通道 | 讓回覆以 Markdown 圖片或連結帶出資料的指令 | web_injection |
測試針對網站的注入攻擊,例如私有資料外洩或腳本注入 |
sty-encoded |
使用模型可解碼之編碼的指令 | encoding |
呈現目標文字的編碼版本 |
sty-invisible |
藏在讀者看不見的字元中的指令 | smuggling、badchars、goodside |
混淆詞彙、使用不可見的 Unicode 字元,或利用 UTF 標籤頁編碼進行使用者看不見的注入 |
loc-tool-description、sty-fake-turn |
位於工具描述中的指令,或模仿操作者回合的訊息 | 未找到 | 沒有任何模組名稱或 docstring 相符 |
最後一列是團隊最常誤以為已被涵蓋的缺口,而這裡沒有任何模組測試它。此表是參考資料,不是結果。模組名稱與 docstring 是從 PyPI 上的 garak 0.17.1 wheel 讀取的。沒有安裝並執行任何探測,本章的數字中也沒有任何 garak 輸出。因此,此表只說明各模組的用途。它並不表示模組的文字看起來像本書的案例、助理會遵循這些文字,或本書中的任何防禦能擋下它們。不要把此表當作涵蓋範圍。與某個位置相符的模組,只表示它的探測會傳送那一類文字,僅此而已。
實用原則: 在把某個 garak 模組視為涵蓋本書的某個位置之前,先以鎖定版本執行它的探測,並確認偵測器評分的是助理實際會送出的回覆,而不只是探測本身的文字。只憑名稱比對而得的探測,對涵蓋範圍而言只是猜測。
本章,於本版次中
版次 2026.11.3範例於 2026年11 月 22 通過測試3 of 3 個範例 in this chapter pass
譯自英文書,版次 2026.11.3。