Skip to content

Formalization and Benchmarking of Prompt Injection Attacks and DefensesFormalizing and Benchmarking Prompt Injection Attacks and Defenses

arXiv· arXiv:2310.12815· Yupei Liu, Yuqi Jia, Runpeng Geng, Jinyuan Jia, Neil Zhenqiang GongThe Pennsylvania State University; Duke UniversityIndexed by this site · 27 days ago· Originally publishedSelected EvaluationSelectedAttention44

Quick read

Attacks

Based on the paper PDF (AI-generated); refer to the original

The authors propose a formal framework for prompt injection attacks and systematically evaluate 5 attacks, 10 defenses, 10 LLMs and 7 tasks; the authors state that existing defenses are all insufficient.

Threat modelThe attacker's goal is to make the LLM-Integrated Application complete the attacker-chosen injected task rather than the target task; the attacker knows the application is an LLM-Integrated Application but does not know internal details such as the instruction prompt, whether in-context learning is used, or the backend LLM; the attacker can only manipulate external data (inject arbitrary instructions/data), cannot manipulate the instruction prompt, and assumes the backend LLM remains intact.

Questions
Prompt injection attacks make LLM-Integrated Applications execute tasks injected by the attacker, but existing work is mostly case studies, lacking a formal framework and systematic evaluation, so the attack threat and defense effectiveness are both unclear.
Methods
The authors propose a formal framework: an attack is constructing compromised data x̃ = A(xt, se, xe) to make the application complete the injected task; existing attacks are special cases of the framework, and they combine them into a Combined Attack; they evaluate 5 attacks and 10 defenses on 10 LLMs and 7 tasks (49 combinations).
Experiments and results
The authors report: on GPT-4, Combined Attack has an average ASV of 0.75, higher than other attacks; across 10 LLMs the average ASV/MR is 0.62/0.78, and it is positively correlated with model scale; none of the 10 defenses is sufficient—prevention-type defenses either have limited effect or lose utility, and detection-type defenses have high FNR or FPR.
Limitations and future work
In the Discussion, the authors note: existing attacks are all heuristic and optimization-based attacks were not explored; fine-tuning-type defenses were not evaluated; there is a lack of mechanisms to recover clean data after detection; known-answer detection tested only one detection prompt and did not test adaptive attacks.
Experimental setupGPT-4、PaLM 2 text-bison-001 et al. · MRPC、Jfleg et al. · PNA、ASV et al.
Evaluated models
GPT-4PaLM 2 text-bison-001GPT-3.5-TurboBardVicuna-33b-v1.3Flan-UL2Vicuna-13b-v1.3Llama-2-13b-chatLlama-2-7b-chatInternLM-Chat-7B
Benchmarks
MRPCJflegHSOLRTESST2SMS SpamGigaword
Metrics
PNAASVMRFPRFNR
AI summaryand selection rationaleResearchers from Pennsylvania State University and Duke University proposed a framework for formalizing prompt injection attacks and designed stronger combined attacks; systematic evaluation shows existing defenses are all insufficient. The framework unifies existing attacks as special cases; the team evaluated 5 attacks and 10 defenses on 10 LLMs and 7 tasks. On GPT-4, the Combined Attack achieved an average attack success value (ASV) of 0.75, higher than each existing attack; across the 10 models overall, the average ASV and match rate (MR) were 0.62 and 0.78, respectively. Preventive defenses have limited effectiveness in stopping attacks and often lose target-task utility when no attack is present; for example, the paraphrasing defense on average reduced task performance by 0.14 when no attack was present. Detection-based defenses generally have many misses or many false positives. The evaluation platform is open-sourced at https://github.com/liu00222/Open-Prompt-Injection .

Researchers from Pennsylvania State University and Duke University proposed a framework for formalizing prompt injection attacks and designed stronger combined attacks; systematic evaluation shows existing defenses are all insufficient. The framework unifies existing attacks as special cases; the team evaluated 5 attacks and 10 defenses on 10 LLMs and 7 tasks. On GPT-4, the Combined Attack achieved an average attack success value (ASV) of 0.75, higher than each existing attack; across the 10 models overall, the average ASV and match rate (MR) were 0.62 and 0.78, respectively. Preventive defenses have limited effectiveness in stopping attacks and often lose target-task utility when no attack is present; for example, the paraphrasing defense on average reduced task performance by 0.14 when no attack was present. Detection-based defenses generally have many misses or many false positives. The evaluation platform is open-sourced at https://github.com/liu00222/Open-Prompt-Injection .

Why it was selectedThe paper incorporates existing prompt injection attacks into a unified framework and proposes compositional attacks, evaluating 10 defenses on 10 LLMs and 7 tasks; the results show that existing defenses are all insufficient.

Related events and news

1 events · 4 news mentions

News that mentions this paper

IBM发布LLMON预览模型防间接注入

Show 1 more news items

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    Establishes a formal framework for prompt injection attacks and systematically evaluates attacks and defenses, filling the gap of case studies only.

    Core problem: prompt injection attacks lack a formal definition and systematic quantitative evaluation, and the community lacks an overall understanding of the attack threat and defense effectiveness.

    • Problem setting: An LLM-Integrated Application uses an instruction prompt plus external data to query a backend LLM; the data comes from external resources (resumes, web pages, posts) and can be manipulated by an attacker. The authors give an example: a job applicant injects text into a resume so that an automated screening application misjudges them as qualified.
    • Shortcomings of existing work: The authors state that existing research papers and blogs are mostly case studies; first, they lack a formal framework, making it difficult to design new attacks and defenses; second, they lack comprehensive evaluation, so the threat level of attacks and the effectiveness of defenses are unclear.
    • Core observation: Different prompt injection attacks are essentially different strategies for constructing compromised data based on target data, injected instruction and injected data, and can be unified into one framework; combining existing strategies can yield new attacks.
    • Paper contributions: Propose a formal attack framework and design Combined Attack based on it; systematically evaluate 5 attacks, 10 defenses, 10 LLMs and 7 tasks; open-source platform Open-Prompt-Injection as a benchmark for subsequent research.
    • Threat model:
      • Attacker goal: Make the application complete the attacker's arbitrarily chosen injected task rather than the target task; the expected response can be a limited modification of the correct result or an arbitrary response.
      • Background knowledge: Knows that the application is an LLM-Integrated Application; assumes no knowledge of internal details such as the instruction prompt, whether in-context learning is used, or the backend LLM.
      • Capabilities: Can inject arbitrary instructions/data into the data; cannot manipulate the instruction prompt; assumes the backend LLM remains intact.
      • Victim system: An LLM-Integrated Application with an LLM as backend that uses external data (e.g., automated screening, spam detection, translation).
  2. What related research exists?

    Related work is divided into four categories: prompt injection case studies, defenses, jailbreak, and other LLM attacks; this paper's positioning is formalization and a systematic benchmark.

    Prompt injection attacks (case studies)

    • Willison [50], Harang [23], Perez and Ribeiro [35], Branch et al. [14], OWASP [34], etc.—reported prompt injection phenomena and techniques (such as escape characters, context ignoring) against GPT-3 or LLM applications; these are existing attacks formalized in this paper's framework.
    • Greshake et al. [22]—compromised real LLM-integrated applications with indirect prompt injection. The authors note that these works are all case studies and lack a formal framework and comprehensive evaluation.

    Prompt injection against LLM-Integrated Applications (another scenario)

    • Liu et al. [27], Perez and Ribeiro [35]—consider the scenario where the victim is the application itself and a malicious user leaks the application's private information. The paper notes that such attacks require at least black-box access, and some also require repeated queries, so they do not apply to this paper's scenario (e.g., a job applicant has no black-box access to an automated screening application).

    Defenses

    • Prevention type: Paraphrasing and Retokenization (Jain et al. [25], originally jailbreak defenses, extended in this paper to prompt injection), Delimiters [8, 30, 51], Sandwich prevention [9], Instructional prevention [4].
    • Detection type: PPL detection [11, 25], Windowed PPL detection [25], Naive LLM-based detection [43], Response-based detection [40], Known-answer detection [31].
    • Concurrent work: Jatmo (Piet et al. [37], fine-tuning a non-instruction-tuned model), StruQ (Chen et al. [17]), the benchmark and defenses of Yi et al. [54]—the authors state that these are concurrent works and were not included in this paper's benchmark evaluation.

    Jailbreak and other LLM attacks

    • Jailbreak attacks (Zou et al. [56], Wei et al. [49])—the authors emphasize the difference from prompt injection: jailbreak makes the LLM perform a refused unsafe target task, whereas prompt injection makes the LLM perform the injected task rather than the target task, and the task may be safe or unsafe.
    • Other attacks: privacy attacks [16, 29], poisoning attacks [13, 41, 46, 53], adversarial prompts [55]—their goals are respectively to infer privacy, contaminate training data or parameters, and make the original task answer incorrectly, which differ from the goal of prompt injection.

    Baselines and benchmark: the attack-side baselines are four existing attacks: Naive Attack, Escape Characters, Context Ignoring, Fake Completion; the defense side is the above 10 defenses; the evaluation uses seven datasets: MRPC, Jfleg, HSOL, RTE, SST2, SMS Spam, Gigaword.

    Positioning: the authors state that this paper proposes the first framework to formalize prompt injection attacks and provides a quantitative benchmark of attacks and defenses, distinguishing it from case-by-case reports and defense studies targeting only a single point.

  3. How does the paper address this problem?

    Uses a unified framework to formalize attacks as constructing compromised data, then defines five types of metrics for quantitative evaluation on 10 LLMs and 49 task combinations.

    Overall approach: formalize prompt injection attacks as "constructing compromised data so that the application completes the injected task"; existing attacks are special cases of different construction strategies; based on this, combine them into Combined Attack, and use unified metrics to systematically evaluate 5 attacks and 10 defenses on 10 LLMs and 7 tasks.

    Problem formalization

    • target task and injected task: The target task consists of target instruction st and target data xt, and the normal query is f(st ⊕ xt); the attacker arbitrarily chooses the injected task (injected instruction se, injected data xe).
    • Attack definition (Definition 1): Given the application's st and xt, a prompt injection attack modifies xt so that the application completes the injected task rather than the target task.
    • Attack framework: x̃ = A(xt, se, xe), i.e., attack A constructs compromised data x̃ based on target data, injected instruction and injected data, and the application queries the backend LLM with p = st ⊕ x̃.

    Construction strategies of the five attacks

    • Naive Attack: Directly concatenate x̃ = xt ⊕ se ⊕ xe.
    • Escape Characters: Add special character c (e.g., newline) after xt before concatenating, so that the LLM perceives a context switch.
    • Context Ignoring: Add task-ignoring text i after xt, explicitly telling the LLM to ignore the target task.
    • Fake Completion: Add a fake response r of the target task after xt, so that the LLM believes the target task is complete; the experiments uniformly use generic fake response text.
    • Combined Attack (proposed in this paper): Combine the above three strategies, x̃ = xt ⊕ c ⊕ r ⊕ c ⊕ i ⊕ se ⊕ xe, using special characters twice to separate the fake response and the task-ignoring text.

    Two major categories of defenses

    • Prevention-type (5): Paraphrasing (use the backend LLM to rewrite the data, breaking the order of injected content), Retokenization (retokenize with BPE-dropout), Delimiters (by default wrap the data in three single quotes; the appendix tests XML tags and random sequences), Sandwich prevention (append an instruction after the data reminding the target task), Instructional prevention (rewrite the instruction prompt so that the LLM ignores instructions in the data).
    • Detection-type (5): PPL detection and Windowed PPL detection (perplexity above a threshold is judged compromised; the threshold is set to a value such that the FPR on clean data does not exceed 1%, and the authors state that prior work did not give a principled method for threshold selection), Naive LLM-based detection (let the LLM judge whether to allow the data), Response-based detection (if the response is not a valid answer to the target task, it is judged compromised; the authors note it fails when the injected task is of the same type as the target task), Known-answer detection (concatenate a detection instruction with a known answer; if the response does not reproduce the secret key, it is judged compromised; the experiments use 7 random characters as the secret key).

    Evaluation protocol and metrics

    • Tasks and data: 7 NLP tasks (DSD, GC, HD, NLI, SA, SD, Summ), each with a benchmark dataset; for each task, 100 non-overlapping samples are randomly taken as target data and 100 as injected data, giving 7×7=49 target/injected combinations.
    • Metrics: PNA (task performance without attack, split into PNA-T/PNA-I), ASV (performance on the injected task under attack; PNA-I is its upper bound), MR (match rate between the response under attack and the response to directly querying the injected task), and FPR/FNR for detection-type defenses.
    • Task measures: accuracy for classification tasks, Rouge-1 for summarization, GLEU for grammatical error correction.
    • Sampling and reproducibility: To save computation, ASV, MR and FNR are each computed on 100 randomly sampled pairs; open-source models use a fixed random seed, closed-source models use temperature 0.1; the default LLM is GPT-4 (via the Azure OpenAI API, using the role-separation message format).
    • Same-label handling: When the target and injected tasks are both classification tasks, samples with different ground truth labels are selected; in SD/HD combinations, the target data takes only spam/hateful samples, simulating an attacker who wants their own spam post to be allowed.
  4. What experiments did the paper conduct?

    Evaluates 5 attacks and 10 defenses on 10 LLMs and 49 task combinations; the authors state that Combined Attack is the strongest and that existing defenses are all insufficient.

    Experimental setup

    • LLMs tested (10): GPT-4, PaLM 2 text-bison-001, GPT-3.5-Turbo, Bard, Vicuna-33b-v1.3, Flan-UL-2, Vicuna-13b-v1.3, Llama-2-13b-chat, Llama-2-7b-chat, InternLM-Chat-7B (Table 3). The default LLM is GPT-4.
    • Tasks and data: 7 tasks each paired with a dataset (MRPC, Jfleg, HSOL, RTE, SST2, SMS Spam, Gigaword); for each task, 100 target data and 100 injected data are taken, for 49 combinations in total.
    • Attacks: Naive, Escape Characters, Context Ignoring, Fake Completion, Combined Attack, 5 in total.
    • Defenses: 5 prevention type and 5 detection type; PPL-type detection uses Llama-2-13b-chat to compute perplexity (GPT-4 cannot compute perplexity), the other detections use GPT-4; the PPL threshold is selected at FPR ≤ 1% on 100 clean samples.
    • Metrics: PNA, ASV, MR (attacks and prevention-type defenses), FPR, FNR (detection-type defenses); ASV/MR/FNR are each computed on 100 randomly sampled pairs.
    • Evaluation method: no LLM judge; attack success is determined by task metrics (accuracy for classification tasks, Rouge-1 for summarization, GLEU for grammatical error correction).

    Main results

    Average ASV of the five attacks over the 7×7 task combinations (according to Table 4 and Table 10):

    Swipe horizontally to read the table

    LLMNaiveEscape CharactersContext IgnoringFake CompletionCombined
    GPT-40.620.660.650.700.75
    PaLM 20.620.640.650.660.71
    • Combined Attack is strongest: the authors report that its average ASV is the highest on both LLMs, and that it outperforms the other attacks on almost every target/injected combination in Figure 2 and Figure 6; one exception is that on GPT-4 with target GC and injected DSD, Fake Completion is slightly higher.
    • Fake Completion is second, Naive is weakest: the authors interpret this as explicitly telling the LLM that the "target task is complete" being more effective than escape characters and context ignoring; Escape Characters and Context Ignoring trade wins on the two LLMs.
    • Effective across models and tasks: Combined Attack has an average ASV of 0.62 and MR of 0.78 across 10 LLMs and 49 combinations; averaged by task (Table 6), ASV/MR is highest when the injected task is SA (0.89/0.90) and lowest when it is Summ (0.34/0.67); the authors consider SA easier to inject and Summ harder.
    • Positively correlated with model size: in Figure 3, the Pearson correlation coefficient of average ASV/MR with model size is 0.63/0.64; the authors speculate that larger LLMs are better at following instructions and thus more susceptible to injection.

    Evaluation of prevention-type defenses

    ASV/MR of Combined Attack on GPT-4 under each prevention defense (averaged over the 7 injected tasks by target task, according to Table 7a) and PNA-T without attack (according to Table 7b):

    Swipe horizontally to read the table

    DefenseASV rangeMR rangeAverage PNA-T change
    No defense0.73–0.760.85–0.880.00
    Paraphrasing0.06–0.460.12–0.55-0.14
    Retokenization0.27–0.580.32–0.69-0.06
    Delimiters0.29–0.720.30–0.84-0.08
    Sandwich prevention0.26–0.700.31–0.83-0.06
    Instructional prevention0.13–0.730.18–0.85-0.02
    • Authors' conclusion: no prevention-type defense is sufficient—ASV/MR decrease relative to no defense but remain high, or the defense incurs a utility loss; Paraphrasing reduces ASV the most but lowers average PNA-T by 0.14.
    • Exceptions and details: Sandwich and Instructional prevention increase PNA-T on several tasks but decrease PNA-T on Summ (sandwich lowers it from 0.38 to 0.24); the authors explain that the additional instructions are summarized together as data.

    Evaluation of detection-type defenses

    FNR and FPR of detecting Combined Attack (averaged over the 7 injected tasks by target task, according to Table 8):

    Detection method (model)FNR rangeFPR range
    PPL detection (Llama-2-13b-chat)0.77–1.000.00–0.03
    Windowed PPL (Llama-2-13b-chat)0.40–0.990.00–0.04
    Naive LLM-based (GPT-4)0.000.15–0.93
    Response-based (GPT-4)0.15–1.000.00–0.13
    Known-answer (GPT-4)0.00–0.120.00–0.07
    • Known-answer is relatively the most effective: both FNR and FPR are low; but the evaluation against other attacks (Table 9, injected task SA) shows that when the target task is GC, the FNR for detecting Context Ignoring reaches 0.76 and for Fake Completion reaches 0.86, and the ASV/MR of missed samples remains high (e.g., under the GC target, ASV/MR for Context Ignoring is 0.92/0.93).
    • Failure modes of other detections: the authors state that PPL-type methods fail because the text quality of compromised data remains good; Response-based is effective when the target is a classification task and differs in type from the injected task (many cells in Table 31 have FNR close to 0), and FNR reaches 1.00 when they are of the same type or the target is Summ; Naive LLM-based has FNR of 0.00 throughout but high FPR (0.93 on the HD task), which the authors speculate is because the LLM is fine-tuned to be overly conservative.

    Other ablations and analyses

    • Number of in-context learning examples: Figure 4 shows that 0–5 examples have little effect on the ASV of Combined Attack (GPT-4).
    • Injected data length: Figure 7 shows that ASV first rises and then plateaus with the number of tokens l; the authors state that the attack is stably effective once l is greater than about 30 (GPT-4).
    • Injected instruction length: Figure 8 shows a negligible effect on tasks such as SA and an effect on harder tasks such as GC; the authors state that performance on each task is better when the number of tokens is greater than about 20 (GPT-4; variants in Table 33).
    • Delimiters variants: Appendix Tables 24 and 25 give ASV/MR with random sequences and XML tags as delimiters; the main text does not expand on the comparison.
  5. What are some points that can be further explored?

    The authors list four directions: optimization-based attacks, fine-tuning defenses, attack recovery, and known-answer improvements; the experiments cover 10 LLMs and heuristic attacks.

    Limitations and future directions noted by the authors

    • Optimization-based attacks (Section 8): The authors note that existing prompt injection attacks are all heuristic, and future work could optimize special characters, task-ignoring text or fake responses, or develop optimization-based strategies for constructing compromised data.
    • Fine-tuning defenses (Section 8): The experiments used only standard LLMs; the authors suggest exploring whether fine-tuning LLMs can improve the security of applications or defenses, while noting that models fine-tuned this way may still be vulnerable to new attacks not considered during fine-tuning.
    • Attack recovery mechanisms (Section 8): The authors state that the literature lacks mechanisms to recover clean data from compromised data after successful detection; detection without recovery still leads to denial-of-service.
    • Limitations of known-answer detection (Section 8): The evaluation is limited to one specific detection prompt; the authors suggest finding detection prompts that are more easily overridden by injected instructions, and exploring adaptive attacks against known-answer detection.

    Scope of experimental coverage

    • 10 LLMs were tested (GPT-4, PaLM 2 text-bison-001, GPT-3.5-Turbo, Bard, Vicuna-33b/13b-v1.3, Flan-UL-2, Llama-2-13b/7b-chat, InternLM-Chat-7B); the attack comparison experiments default to GPT-4, the appendix gives corresponding results for PaLM 2, and Combined Attack has results on all 10 LLMs.
    • The tasks are 7 English NLP tasks (100 target/injected data each, 49 combinations); ASV, MR and FNR are computed by randomly sampling 100 pairs for each setting.
    • Attack evaluation covers 5 heuristic attacks; defense evaluation covers 5 prevention-type and 5 detection-type; the in-depth evaluation of known-answer against other attacks is conducted only when the injected task is sentiment analysis (Table 9).
    • The paper notes that the defenses of Jatmo, StruQ and Yi et al. are concurrent work and were not included in the benchmark evaluation (Section 7).
    • The paper does not report human evaluation, the number of queries or the time cost of attacks; closed-source models still exhibit nondeterminism at temperature 0.1, which the authors state has little effect on the results.
  6. Summarize the main content of the paper

    Formalizes prompt injection attacks and systematically evaluates them: Combined Attack is effective on 10 LLMs, and all 10 existing defenses are insufficient.

    A paper on the formalization and benchmark evaluation of prompt injection attacks, aimed at LLM-Integrated Applications that use external data.

    • Problem addressed: Existing work is mostly case studies, lacking a formal framework for attacks and quantitative evaluation, so the attack threat and defense effectiveness are unclear.
    • Method highlights: Define attacks as constructing compromised data so that the application completes the injected task; the unified framework x̃ = A(xt, se, xe) covers four existing attacks, and combining them yields Combined Attack; using five types of metrics—PNA, ASV, MR, FPR, FNR—evaluate 5 attacks and 10 defenses on 10 LLMs and 7 tasks (49 combinations).
    • Main results: On GPT-4, Combined Attack has an average ASV of 0.75, higher than the four existing attacks (Table 4); across 10 LLMs and 49 combinations, the average ASV/MR is 0.62/0.78; the Pearson correlation coefficients of average ASV/MR with model scale are 0.63/0.64 (Figure 3).
    • Defense evaluation: The authors report that no prevention-type defense is sufficient—Paraphrasing reduces ASV the most but decreases average PNA-T by 0.14 (Table 7); among detection-type defenses, known-answer is relatively the most effective (FNR 0.00–0.12 against Combined Attack), but its miss rate is high when the target task is grammatical error correction and other such cases, and the other detection methods have high FNR or FPR (Tables 8, 9).
    • Authors' conclusion: Prompt injection attacks are effective against multiple LLMs and tasks, and existing defenses are insufficient; the authors state that this benchmark can serve as a minimum evaluation standard for subsequent defenses, and note that optimization-based attacks, fine-tuning defenses, and post-attack recovery mechanisms are directions worth exploring.

Related papers

Read originalarxiv.org