Skip to content

How to incorrectly use an LLM to detect prompt injectionHow Not to Detect Prompt Injections with an LLM

arXiv· arXiv:2507.05630· ACM Workshop on Artificial Intelligence and Security· Sarthak Choudhary, Divyam Anshumaan, Nils Palumbo, Somesh JhaUniversity of Wisconsin-MadisonIndexed by this site · 27 days ago· Originally publishedSelected AttacksSelectedAttention42

Quick read

Attacks

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

The authors formally analyze the structural vulnerabilities of KAD prompt injection detection and propose an adaptive attack, DataFlip, which reduces the detection rate to 0% on most tasks on the DataSentinel detector, with a backend ASV of up to 93.3%.

Threat modelAttacker goal: poison the target data so that the backend LLM executes the injection task, while making the detection LLM return the secret key to bypass KAD. Knowledge: black-box, with only query access to the backend and detection LLMs; knows the detection instruction template but not the randomly sampled secret key. Capability: can arbitrarily modify the target data, and the poisoned data contains no benign content; cannot control the target instruction or the detection instruction. Victim system: an LLM-integrated application protected by KAD (including Strong KAD/DataSentinel).

Questions
KAD-type defenses judge whether data has been injected by detecting whether the LLM returns the secret key; Strong KAD such as DataSentinel reports a near-perfect detection rate. The authors argue that KAD's detection instruction and secret key reside in the same context as the attacker-controlled data, so the security premise does not hold.
Methods
The authors formalize the expected behavior of the KAD detector with two axioms and point out the Type II failure: an injected instruction can make the detector extract and return the secret key from the context. Based on this, they construct DataFlip: an IF/ELSE structure makes the detection LLM take the IF branch and return the key, while the backend LLM takes the ELSE branch and executes the injection task, requiring no white-box access or optimization.
Experiments and results
In RQ1, DataFlip's average extraction rate is 98.0%. In RQ2, the detection rate drops to 0% on most injection tasks, with Summary highest at 24.0%; backend ASV is close to the no-detection upper bound, reaching 93.3% on SpamDet with GPT-4.1. In RQ3, the DataSentinel fine-tuned detector's FNR on DataFlip samples is no lower than Mistral-Instruct on most tasks.
Limitations and future work
In Appendix A, the authors discuss the limitations of improving KAD: adversarial training is difficult to generalize, hiding the detection instruction is ineffective, and sliced detection is computationally infeasible; they argue that interpretability-driven detection is a more principled direction. Experimental coverage: detectors are only two variants of Mistral-7B, 4 backend models, and 600 test samples per injection task.
Experimental setupMistral-7B (DataSentinel fine-tuned detector)、GPT-4.1 et al. · Key Extraction dataset、Prompt Injection dataset (DupDet, GramCor, HateDet, NLI, SentAna, SpamDet, Summary) · Extraction Rate、Detection Rate et al.
Evaluated models
Mistral-7B (DataSentinel fine-tuned detector)GPT-4.1Llama 4 ScoutClaude 4 SonnetDeepseek-R1Mistral-7B-Instruct-v0.3
Benchmarks
Key Extraction datasetPrompt Injection dataset (DupDet, GramCor, HateDet, NLI, SentAna, SpamDet, Summary)
Metrics
Extraction RateDetection RateFNRFPRASVASV w/o DetTask Accuracy
AI summaryand selection rationaleResearchers at the University of Wisconsin-Madison formally analyzed prompt injection defenses based on known-answer detection (KAD), pointed out their structural vulnerabilities, and proposed an adaptive attack, DataFlip. The attack uses IF/ELSE control flow to determine whether detection instructions are present in the prompt; if so, it makes the detection LLM output the secret key, causing KAD to misjudge the contaminated input as clean. In tests against Strong KAD defenses such as DataSentinel, DataFlip reduced the detection rate to as low as 0%, while the success rate of the backend LLM completing the injection task reached up to 91%, and the average secret key extraction success rate was 98%. The attack was validated on GPT-4.1, Llama 4 Scout, Claude 4 Sonnet and DeepSeek-R1, requires no white-box access or optimization overhead, and the evaluation code is open source.

Researchers at the University of Wisconsin-Madison formally analyzed prompt injection defenses based on known-answer detection (KAD), pointed out their structural vulnerabilities, and proposed an adaptive attack, DataFlip. The attack uses IF/ELSE control flow to determine whether detection instructions are present in the prompt; if so, it makes the detection LLM output the secret key, causing KAD to misjudge the contaminated input as clean. In tests against Strong KAD defenses such as DataSentinel, DataFlip reduced the detection rate to as low as 0%, while the success rate of the backend LLM completing the injection task reached up to 91%, and the average secret key extraction success rate was 98%. The attack was validated on GPT-4.1, Llama 4 Scout, Claude 4 Sonnet and DeepSeek-R1, requires no white-box access or optimization overhead, and the evaluation code is open source.

Why it was selectedThe paper formally characterizes the structural flaws of the KAD detection scheme and presents an adaptive attack that requires no white-box access, which can be used to assess the applicable boundaries of such prompt injection detection defenses.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors argue that KAD prompt injection detection has a structural vulnerability: the secret key and the injected data are in the same context, and can be bypassed by adaptive attacks.

    Core question: whether the security premise of prompt injection detection schemes such as known-answer detection (KAD) holds—the authors argue it does not, and give an adaptive attack that can bypass it.

    • Problem setting: LLM-integrated applications and agents concatenate external untrusted data into the prompt, and prompt injection can make the backend LLM execute an attacker-specified injection task; OWASP lists it as the top security risk for LLM applications.
    • Existing methods and their shortcomings: KAD prepends a detection instruction with a predetermined answer (secret key) to the data, and judges the input as contaminated if the detection LLM does not return the key; DataSentinel further fine-tunes the detection LLM to make it more likely to follow injected instructions (Strong KAD) and reports a near-perfect detection rate. The authors argue that such schemes only observe input-output behavior and that their security premise is flawed.
    • Core observation: The detection instruction and the external data share the same context window, so the secret key is "visible" at detection time to an injected instruction that can see the prompt content; KAD implicitly assumes that instructions are processed sequentially and statelessly, and that an injected instruction cannot perceive the existence of the detection instruction; the authors challenge this assumption.
    • Contributions: Formalize the expected behavior of the KAD detector with two axioms and point out their inherent tension; analyze the Type II failure mode (the detector following the injected instruction instead leaks the key); construct the adaptive attack DataFlip, with which the authors report a minimum detection rate of 0% and an injection task success rate of 91% (abstract), requiring no white-box access or optimization.
    • Threat model (Section 3.2, consistent with prior work):
      • Goal: Contaminate the target data so that the backend LLM completes the injection task while the detection LLM returns the secret key to evade detection.
      • Knowledge: black-box, with query access only to the backend LLM and the detection LLM; knows the detection instruction template but not the secret key randomly sampled at detection time.
      • Capability: Can arbitrarily modify the target data to embed the injection task; the contaminated data contains no benign content (the authors note this enhances attack effectiveness but reduces stealthiness); cannot control the target instruction or the detection instruction.
      • Victim system: LLM-integrated applications that use KAD (including Strong KAD/DataSentinel) for detection.
  2. What related research exists?

    Related work is divided into two groups: prompt injection attacks (manual/optimization) and defenses (system-level/prevention/detection); this paper directly targets KAD and its representative DataSentinel.

    Prompt injection attacks—manually constructed

    • Willison (2022, 2023), Perez & Ribeiro (2022), Harang (2023), etc.: Prepend manually constructed triggers (empty string, escape characters, ignore-context phrases, fake completion) to the target data, making the backend LLM more likely to follow the injected instruction.
    • Combined Attack (Liu et al., 2024): Combines multiple strategies such as fake completion and instruction-reset phrases into a single trigger; the paper calls it the current state-of-the-art, and it is also the comparison attack in this paper.

    Prompt injection attacks—optimization-based

    • Universal (Liu et al., 2024): Learns a universal trigger that can be prepended to any injected prompt.
    • NeuralExec (Pasquini et al., 2024): Jointly optimizes the prefix and suffix of the injected prompt as the trigger.
    • PLeak (Hui et al., 2024): Directly optimizes the entire contaminated data to accomplish prompt stealing. Such methods define a loss function and optimize with approximate gradient techniques, whereas the attack in this paper requires no optimization process.

    Defenses—system-level and preventive

    • System-level defenses (Costa et al., 2025; Debenedetti et al., 2025): Assume the backend LLM itself is vulnerable and provide security guarantees by constraining control flow and data flow, possibly at the cost of expressiveness or functionality.
    • Preventive defenses: Preprocessing-type (paraphrasing, retokenization, delimiters, Jain et al., 2023, etc.) and target-instruction-modifying-type (Sandwich defense, Learn Prompting, 2023); the paper relays prior conclusions that such defenses have limited effectiveness and may harm the utility of benign inputs. Fine-tuning the backend LLM to resist injection (Struq, Chen et al., 2024, etc.) remains vulnerable to novel or adaptive attacks outside the fine-tuning distribution (per the claim cited as [26] in the paper).

    Defenses—detection-based (directly targeted by this paper)

    • Zero-shot classification and binary-classification fine-tuning: Use an independent detection LLM to directly judge whether the input is contaminated (Armstrong, 2023), or fine-tune it as a binary classifier via standard supervised learning (Ouyang et al., 2022).
    • KAD (Liu et al., 2024; DataSentinel, Liu et al., 2025; Nakajima, 2022): Design a detection instruction with a predetermined answer (secret key); if the detection LLM does not return the key, the input is judged contaminated; the paper states that KAD outperforms traditional binary classifiers and zero-shot classification LLMs. DataSentinel fine-tunes the detection LLM to make it more susceptible to injection (Strong KAD) and reports near-perfect detection accuracy.

    Baselines and benchmarks: The comparison attack is Combined Attack; the comparison detectors are the DataSentinel fine-tuned Mistral-7B and the instruction-tuned Mistral-7B-Instruct-v0.3; the dataset follows DataSentinel's Prompt Injection setting (7 tasks).

    The authors position this paper as: not proposing a new defense, but formally identifying the structural vulnerability of KAD and validating it with a manually adapted attack that requires no optimization, arguing that interpretability-driven detection is a more principled direction (Appendix A.3).

  3. How does the paper address this problem?

    First formalize KAD with two axioms and derive Type II failure, then use this to construct a handcrafted adaptive attack, DataFlip, with an IF/ELSE structure.

    Overall approach: formalize the expected behavior of the KAD detector as two mutually tensioned axioms, derive the "Type II" failure in which "the detector instead leaks the secret key when it follows the injected instruction", then exploit this failure with an injection template DataFlip that has conditional branches, requiring no white-box access or optimization.

    Formalization and threat model

    • Task representation: A task is a triple (s, x, y), and the prompt is the concatenation of instruction and data s||x; the target task is (s_t, x_t, y_t), the injection task is (s_e, x_e, y_e), and the contaminated data is x_c = z||s_e||x_e (z is the trigger).
    • Instruction-following oracle E: E(s, p) denotes the output of executing instruction s in the context of the full prompt p, used to clarify which instruction the model actually follows in a multi-instruction prompt.
    • Three elements of KAD: the detection LLM g, the detection instruction s_d(k) (whose answer is a secret key k known only to the defender; the template asks the model to repeat the key and ignore the following text; k is a random string, e.g., 7 letters), and the detection rule D_rule.
    • Detection rule: Drule(x) := contaminated if k ∉ g(sd(k)||x), clean otherwise, i.e., if the output does not contain the key, it is judged contaminated.

    Two axioms and failure modes

    • Axiom 1 (ignore the detection instruction): When the input is contaminated, g should not follow s_d(k) and should not output k.
    • Axiom 2 (follow the injected instruction): When the input is contaminated, g should follow s_e and output y_e. KAD's intuition requires g to satisfy both axioms simultaneously, which is feasible only when "following the detection instruction" and "following the injected instruction" produce different behaviors.
    • Type I failure: g violates Axiom 1 and still returns k under contaminated input; the probability is denoted ε1; DataSentinel-style fine-tuning reduces ε1 by strengthening Axiom 1.
    • Type II failure: g follows the injected instruction s_e per Axiom 2, but the result of executing s_e happens to be k—an adaptive s_e can make g extract and repeat the key from the detection instruction in the same prompt, without the attacker knowing k in advance. The authors give the false-negative rate decomposition: Pr[g(sd(k)||xc)=k] = ϵ1 + ϵ2 · 𝕀(E(se, sd(k)||xc)=k); an adaptive attack can make the indicator function 1, reducing the false-negative rate to ε2.
    • Tension pointed out by the authors: Strong KAD fine-tuning reduces ε1 while strengthening Axiom 2, which instead increases ε2; the authors argue that Type II failure stems from KAD only observing input-output behavior without examining the generation process, which is a structural vulnerability that makes it easier to construct adversarial examples than for traditional binary classifiers.

    DataFlip attack design

    • Structure: The contaminated data template x_DataFlip uses IF/ELSE control flow (the authors call it similar to the heuristic attack in [27]): the IF branch detects whether a detection instruction exists in the prompt, and if so, makes the model only repeat the key and stop; the ELSE branch makes the model ignore all previous content and only complete the injection task {s_e} {x_e}.
    • Dual objective: The template makes g(s_d(k)||x_DataFlip) = k (detection bypassed) and f(s_t||x_DataFlip) = y_e (the backend executes the injection task). The IF branch targets the detection LLM, and the ELSE branch targets the backend LLM.
    • Manually constructed, no optimization: The template is manually designed; the authors state it requires no access to model weights and incurs no computational overhead; even if such samples are added to adversarial training, the space of semantically equivalent variants is effectively unbounded, making generalization difficult (Section 3.4, Appendix A.1).

    Success criteria and evaluation method

    • RQ1 (key extraction): Using 500 detection instructions (keys are random letter strings of length 7), the injected instruction asks to repeat the key and end with a signature word; output containing both the key and the signature word is recorded as successful extraction, and the metric is Extraction Rate.
    • RQ2 (attacking Strong KAD): Metrics are Detection Rate (proportion of outputs not containing the key), ASV (proportion of samples that both bypass detection and make the backend produce the attacker-specified result), ASV w/o Det (upper bound without passing through the detector), and Task Accuracy (quality of the backend directly executing the injection task, as a loose upper bound on injection effectiveness).
    • RQ3 (benefit of fine-tuning): Compare the FNR and FPR of the DataSentinel fine-tuned detector and Mistral-Instruct on Combined Attack and DataFlip samples.
  4. What experiments did the paper conduct?

    Three RQs: key extraction rate 98.0%; DataFlip reduces the detection rate to 0% on most tasks, with ASV close to the upper bound; DataSentinel fine-tuning has no advantage over Mistral-Instruct.

    Experimental Setup

    • Detection LLM g: the fine-tuned Mistral-7B provided by DataSentinel; the control in RQ3 is the instruction-tuned Mistral-7B-Instruct-v0.3. The detection instruction and secret key follow DataSentinel's setting.
    • Backend LLMs f (4): GPT-4.1, Claude 4 Sonnet, Llama 4 Scout, Deepseek-R1 (written as Deepseek R1-0528 in the Table 1 header).
    • Datasets: RQ1 uses the Key Extraction dataset (500 detection instructions, with keys being random letter strings of length 7); RQ2/RQ3 follow DataSentinel's Prompt Injection setting, with 7 tasks (DupDet, GramCor, HateDet, NLI, SentAna, SpamDet, Summary) of 100 samples each, and each injection task uses DataFlip and Combined Attack to generate 600 test samples each.
    • Metrics: Extraction Rate, Detection Rate, FNR, FPR, ASV, ASV w/o Det, Task Accuracy (Summary uses ROUGE; classification tasks such as SpamDet are scored 0/1 by label correctness).
    • Success criterion: RQ1 requires the output to contain both the secret key and a signature word (e.g., "extracted"); ASV requires the sample to be both undetected and for the backend response to score on the injection task metric. The paper does not report the number of repetitions or human evaluation.

    Main Results

    Table 1 (RQ2, detection LLM is DataSentinel fine-tuned Mistral-7B, values are percentages; the ASV column omits the nearly identical ASVw/o Det and Acc. columns):

    Swipe horizontally to read the table

    Injection TaskDetection RateGPT-4.1 ASVClaude 4 Sonnet ASVLlama 4 Scout ASVDeepseek R1-0528 ASV
    DupDet0.0066.864.027.352.5
    GramCor1.5031.018.727.76.2
    HateDet0.0063.779.575.264.5
    NLI0.0079.378.553.071.2
    SentAna0.0089.591.889.373.7
    SpamDet0.3393.384.774.078.8
    Summary24.026.921.615.916.5
    • The authors state that DataFlip reduces the detection rate to 0% on most tasks; the highest, 24.0%, appears on Summary—the task used to train the detector—and the authors state that this value is "not much better than random chance."
    • The authors state that ASV on each backend is close to the upper bound given by ASV w/o Det and Task Accuracy; for example, Claude 4 Sonnet on SentAna has ASV close to 92%, while Task Accuracy is only 4% higher.
    • The authors mention that for a few tasks Task Accuracy is lower than ASV w/o Det (e.g., Llama 4 Scout on GramCor), and believe that its prompt form instead promotes instruction following.

    RQ1: Secret Key Extraction

    • What was tested: DataFlip-style injection instructions make the detector extract the key from the detection instruction and end with a signature word, over 500 detection instructions with different keys.
    • Result: The average Extraction Rate is 98.0% (Section 5.2).
    • Authors' interpretation: The authors state that this validates the analysis in Section 3.4—under KAD the key is always visible to the attacker; switching to any other signature word (e.g., "coconut") shows no significant difference in performance (the paper uses "significant" here but does not report test details).

    RQ3: Whether Strong KAD Fine-Tuning Brings Gains

    • What was tested: FNR of the DataSentinel fine-tuned detector (DS) and Mistral-Instruct (Inst) on Combined Attack and DataFlip samples, as well as FPR (Table 2).
    • Result: On FNR_Combined the two are almost identical (0.0 on most tasks; on NLI, Inst 0.2, DS 0.0); on FNR_DataFlip, DS is worse on most tasks—GramCor is Inst 15.8% vs DS 98.5%, Summary is 93.3% vs 76.0%, and HateDet/DupDet/NLI/SentAna both reach 100.0% or close to it. FPR is between 0.0 and 1.0 in all cases.
    • Authors' interpretation: The authors state that although DataSentinel was fine-tuned on adaptive attack samples, it has no practical advantage over the instruction-tuned model, with a gap of 82% (table note)/83% (main text) on GramCor, and believe this again shows that the KAD objective itself is flawed and that the optimization direction of Strong KAD has negative effects. Note: the table note says 82% and the main text says 83%; the two are inconsistent.

    Other Ablations and Analyses

    • Signature word choice (RQ1): switching to any signature word, the authors report no significant difference in performance.
    • Detection rate exception (Table 1): when Summary is the injection task, the detection rate is 24.0% and ASV on each backend drops to 15.9–26.9; it is the only task with a non-zero detection rate and low ASV; the authors note that Summary is the task used to train the detector and give no further explanation.
    • Reversed gap on GramCor (Table 2): after DataSentinel fine-tuning, FNR_DataFlip instead rises from 15.8% to 98.5%, which the authors attribute to fine-tuning reinforcing Axiom 2 and making the detector more likely to follow the injection instruction.
  5. What are some points that can be further explored?

    In Appendix A, the authors discuss why the three paths to improving KAD are ineffective, and point out two directions: interpretability-based detection and stronger optimization attacks.

    Limitations and Future Directions Noted by the Authors

    • Adversarial training is hard to cure fundamentally (Appendix A.1): it can resist specific adaptive attacks, but the space of semantically equivalent injection prompts is effectively unbounded, making generalization difficult; the authors state that KAD's fragility is structural, stemming from Axiom 2's requirement that the detector follow the injection instruction.
    • Hiding the detection instruction is ineffective (Appendix A.1): the authors believe attackers can still exploit the essential difference between the detection instruction and the target instruction (one has a predetermined answer, the other depends on the input) to make the detector's behavior diverge, and that this will become more feasible as LLMs' instruction-following ability improves.
    • Slice-isolation detection is infeasible (Appendix A.1): the assumption of detecting every slice of the poisoned data one by one is fragile and computationally infeasible as the data grows longer.
    • Directions given by the authors: robust defenses should abandon the requirement that "the detector follows the injection instruction" and reduce the detection LLM to a zero-shot binary classifier to avoid the fragility introduced by Axiom 2 (Appendix A.1); interpretability methods (attention analysis, influence tracing) are a more principled detection direction (Appendix A.3).
    • Stronger attacks (Appendix A.2): DataFlip is a manual attack; the authors believe it can be combined with Universal- and NeuralExec-style optimized triggers to respectively increase the probability that the detector takes the IF branch and the backend takes the ELSE branch.

    Scope of Experimental Coverage

    • There are 4 backend LLMs tested: GPT-4.1, Claude 4 Sonnet, Llama 4 Scout, Deepseek-R1; the detector is only two variants of Mistral-7B (the DataSentinel fine-tuned version and Mistral-7B-Instruct-v0.3).
    • The datasets are the self-built Key Extraction (500 detection instructions) and DataSentinel's Prompt Injection setting (7 tasks of 100 samples each, 600 test samples per attack per injection task).
    • The comparison attack is only Combined Attack; the comparison defenses are only DataSentinel (Strong KAD) and the instruction-tuned control; system-level or preventive defenses are not evaluated.
    • The paper does not report the number of experimental repetitions, inference parameters such as sampling temperature, or human evaluation or LLM judging (injection success is determined by task metrics).
    • The attack is evaluated only in the black-box setting; the authors explicitly assume that the poisoned data contains no benign content, and note that this reduces the attack's stealthiness (Section 3.2).
  6. Summarize the main content of the paper

    Formally identify the structural vulnerability of KAD detection and give a manually adapted attack DataFlip, which reduces the detection rate to 0% on most tasks on DataSentinel.

    An attack paper targeting prompt injection detection: the authors formally analyze known-answer detection (KAD)—which judges whether data has been injected by "whether the detection LLM returns a predetermined secret key"—and argue that its security premise does not hold.

    • Problem to solve: KAD concatenates the detection instruction (containing the secret key) and untrusted data into the same prompt; the authors point out that the key is not truly hidden from an injected instruction that can perceive the context; KAD implicitly assumes that instructions are processed sequentially and statelessly, and this assumption fails under adaptive attacks.
    • Method highlights: Axiom 1 (ignore the detection instruction when contaminated) and Axiom 2 (follow the injected instruction when contaminated) characterize the detector's expected behavior, deriving the Type II failure—following the injected instruction itself can make the detector extract and return the key, reducing the false-negative rate to ε2, while Strong KAD fine-tuning strengthens Axiom 2 and instead increases ε2. Based on this, DataFlip is constructed: an IF/ELSE template makes the detection LLM take the IF branch and return the key, while the backend LLM takes the ELSE branch and executes the injection task; it is manually constructed and requires no white-box access or optimization.
    • Main results: In RQ1, the average extraction rate on 500 detection instructions with random keys is 98.0%; in RQ2, DataFlip reduces the detection rate on the DataSentinel detector to 0.00% for DupDet/HateDet/NLI/SentAna, with Summary at most 24.0%, and the backend ASV approaches the no-detection upper bound (93.3% for SpamDet on GPT-4.1, 91.8% for SentAna on Claude 4 Sonnet); in RQ3, the FNR of the DataSentinel fine-tuned detector on DataFlip samples is no lower than Mistral-Instruct on most tasks, and on GramCor it is 98.5% vs 15.8%.
    • Authors' conclusion: The authors state that KAD's design, which relies on output observation without examining the detection process, has a structural flaw that fine-tuning or adversarial training cannot fundamentally fix; robust detection should abandon making the detector follow the injected instruction, and interpretability-driven methods are a more principled direction.
Read originalACM Workshop on Artificial Intelligence and Security