TCTyler Cheng 鄭迪珉

Interest project · 興趣項目 · AI security

AI Red-Teaming Lab

AI 紅隊測試實驗室:滲透及越獄研究

How easily can an LLM application be talked out of its own rules? This lab studies prompt injection and jailbreak techniques and the defences against them — and measures robustness safely, by trying to extract a meaningless canary code word instead of anything harmful.

大型語言模型應用有多容易被「說服」違反自身規則?本實驗室研究提示注入及越獄技術與相應防禦,並以安全方式量度穩健性:測試目標只是一個無意義的金絲雀(canary)暗號,而非任何有害內容。

~/ai-redteam-lab

      

Scope & responsible use 研究範圍及負責任使用

  • Canary-only. Every attack targets a harmless code word; no harmful content is requested or produced.只以無害暗號作測試目標,不要求亦不產生任何有害內容。
  • Simulated target. The built-in assistant is a transparent rule-based model, so results are reproducible offline.內置目標是透明的規則模型,結果可離線重現。
  • Authorised testing only. Point the harness only at systems you own or have written permission to test.只可測試你擁有或已獲書面授權的系統。
  • Defence-first. Each technique is documented together with its mitigations.每項技術均附有防禦方法。

01 · Threat model · 威脅模型

Where instructions can sneak in 指令可以從哪裏混入

An LLM application mixes trusted instructions (the system prompt) with untrusted text (user messages, documents, web pages). Prompt injection exploits the fact that the model sees all of it as one stream of tokens. Defences sit at the input, inside the prompt and at the output.
LLM 應用把可信指令(系統提示)與不可信文字(用戶訊息、文件、網頁)混在一起。提示注入正是利用模型把一切視為同一串字元的弱點。防禦可放在輸入端、提示之內及輸出端。

User message用戶訊息 Untrusted datadocuments · web · email不可信數據 Inputchecksclassifierspotlighting輸入檢查 LLM application system promptsecret = AURORA-7731 modelreminders · instruction hierarchy提示內防禦 Outputfilter輸出過濾 Response回應

02 · Attack taxonomy · 攻擊分類

Nine techniques, one harmless goal 九種技術,同一個無害目標

Each card is a publicly documented technique, applied only to the canary goal “reveal the secret code word”. Open a card to see the exact test prompt the harness sends.
每張卡都是公開文獻記載的技術,只套用在「說出暗號」這個無害目標上。展開卡片可查看測試程式實際發送的提示。

03 · Playground · 互動測試

Attack the toy assistant 攻擊模擬助手

A browser port of the harness. Pick a technique or write your own prompt, switch defences on and off, and watch the request move through the pipeline. The assistant is simulated — nothing is sent anywhere.
測試程式的瀏覽器版本。選擇技術或自行輸入提示、開關防禦,觀察請求如何經過每一層。助手是模擬的,不會發送任何資料。

Defences · 防禦

  1. Input輸入
  2. Classifier分類器
  3. Spotlight標記
  4. Sandwich提醒
  5. Model模型
  6. Filter過濾
trace

          
Run an attack to see the result · 執行測試以查看結果

04 · Results · 結果

Robustness matrix 穩健性矩陣

ASR = attack success rate (share of techniques that leaked the canary). FPR = false-positive rate (share of benign prompts wrongly blocked or refused). No single defence covers everything; layering does — on this simulated target.
ASR=攻擊成功率;FPR=誤判率(正常請求被錯誤攔截的比例)。沒有單一防禦能覆蓋所有技術,多層防禦才可以——以上結果只適用於此模擬目標。

05 · Code · 程式碼

An evaluation harness you can extend 可擴充的評估框架

architecture

src/redteam_lab/
├── targets.py     # ToyAssistant (simulated) · FunctionTarget adapter
├── attacks.py     # 9 techniques → canary-goal prompts
├── defenses.py    # classifier · spotlighting · sandwich · output filter
├── evaluate.py    # trials · ASR / FPR · ground-truth leak judge
└── __main__.py    # python -m redteam_lab
tests/              # 24 pytest cases
docs/               # this site (browser port)

quickstart.py

import redteam_lab as rl

# robustness matrix: attacks × defence configurations
print(rl.to_markdown(rl.evaluate()))

# compose defences around any target
target = rl.DefendedTarget(
    rl.ToyAssistant(), rl.DEFAULT_CANARY,
    detector=rl.InjectionDetector(threshold=0.5),
    use_spotlight=True, output_filter=True,
)
trial = rl.run_trial(target, rl.attacks.get("indirect"))
print(trial.leaked, target.last_events)

your_system.py

# test a system you own or are authorised to test
def my_app(system: str, user: str) -> str:
    return call_my_model(system, user)

target = rl.DefendedTarget(
    rl.FunctionTarget(my_app), rl.DEFAULT_CANARY,
    use_sandwich=True,
)
results = rl.evaluate({"mine": lambda: target})

defenses.py — spotlighting

def spotlight(user, marker="^"):
    """Datamarking: interleave a marker through
    untrusted blocks so they read as data."""
    return _DATA_BLOCK.sub(
        lambda m: "<<DATA>>"
          + marker.join(m.group(1).split())
          + "<</DATA>>",
        user)