UniGuardian: A Unified Defense for Detecting Prompt Injection, Backdoor Attacks and Adversarial Attacks in Large Language Models

Huawei Lin, Yingjie Lao, Tong Geng, Tan Yu, Weijie ZhaoarXiv 2025 · DOI

TL;DR

This paper introduces UniGuardian, a novel, training-free defense mechanism that unifies the detection of three major LLM attack types: prompt injection, backdoor attacks, and adversarial attacks. By analyzing how prompt perturbations affect LLM behavior, UniGuardian offers a more efficient and comprehensive approach to securing LLMs against malicious inputs.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly powerful but also susceptible to various attacks that manipulate their behavior through carefully crafted prompts. These attacks, including prompt injection (overriding instructions), backdoor attacks (embedding hidden triggers), and adversarial attacks (subtle input modifications), pose significant security risks. Existing defenses often focus on a single attack type or require extensive retraining, making them inefficient and incomplete. This paper addresses the need for a unified, efficient, and training-free defense that can detect these diverse prompt-based attacks simultaneously.

Methods

The core of UniGuardian lies in its unified approach to detecting "Prompt Trigger Attacks" (PTA), a new classification encompassing the three attack types. The method is based on the observation that LLMs exhibit distinct behavioral changes when processing prompts containing these triggers compared to benign prompts.

UniGuardian employs a training-free, inference-time detection strategy. It works by:

  1. Base Generation: The LLM first generates a standard output for the given prompt.
  2. Masked Prompt Variations: Multiple versions of the input prompt are created by randomly masking subsets of words. This masking is done using a "single-forward strategy" where masked prompts are processed alongside the original prompt in a single pass, optimizing for speed.
  3. Uncertainty Score Calculation: The model's internal "logits" (probability distributions for the next token) are compared between the base generation and each masked prompt variation. An "uncertainty score" is calculated based on the difference in these logits.
  4. Suspicion Score: These uncertainty scores are standardized using z-scores. A high z-score indicates that masking a particular word significantly altered the LLM's output, suggesting the presence of a trigger. The highest z-score across all variations becomes the "suspicion score" for the prompt.
  5. Detection: Prompts with high suspicion scores are flagged as malicious.

This approach leverages the idea that removing trigger words from an attacked prompt will cause a much larger disruption in the LLM's output (and thus its internal loss/logits) than removing non-trigger words.

Datasets

The paper evaluates UniGuardian on several datasets, noting that they primarily use the test splits:

  • Prompt Injections: A dataset of adversarial prompt injection examples.
  • Jailbreak: Designed to test defenses against attempts to bypass content moderation.
  • SST2 (Stanford Sentiment Treebank): A benchmark for sentiment analysis.
  • Open Question: A dataset of open-ended questions for evaluating comprehension and reasoning.
  • SMS Spam: A classic dataset for spam detection.
  • Emotion: A dataset annotated with emotional labels.

For backdoor attacks, they also used the Alpaca 52K dataset for poisoning.

Key Insights and Ideas

  • Unified Attack Taxonomy (PTA): The paper introduces "Prompt Trigger Attacks" (PTA) as a unifying concept for prompt injection, backdoor, and adversarial attacks, highlighting their shared mechanism of manipulating LLM behavior via prompt triggers. This reframing simplifies defense strategies.
  • Behavioral Distinctions: The core insight is that LLMs exhibit measurably different internal behaviors (reflected in logits) when processing prompts with malicious triggers versus benign prompts. This difference is amplified when trigger words are masked.
  • Training-Free Detection: UniGuardian's ability to detect attacks at inference time without requiring model retraining is a significant advantage, making it practical for real-world deployment.
  • Single-Forward Strategy: The proposed optimization allows for simultaneous text generation and attack detection, drastically reducing latency and making real-time defense feasible.

Key Results

UniGuardian demonstrated strong performance across various attack types and LLM sizes.

  • Prompt Injection: Achieved high auROC and auPRC scores (often above 0.8, and in some cases near 1.0) across multiple datasets and models, outperforming most baselines.
  • Backdoor Attacks: Showed particularly strong performance, with auROC and auPRC scores frequently near 1.0, indicating effective differentiation between poisoned and clean inputs.
  • Adversarial Attacks: Also showed competitive results, with UniGuardian consistently achieving the highest auROC and auPRC scores compared to baselines on the SST2 and Emotion datasets.
  • Efficiency: The single-forward strategy significantly reduces the overhead for detection, enabling it to run concurrently with text generation.

Limitations and Future Work

  • Language and Model Architecture: The current work primarily focuses on English-language datasets and large transformer-based models. Its applicability to other languages or significantly different model architectures remains unverified.
  • False Positives/Negatives: While effective, UniGuardian's suspicious scores may still produce false positives or miss subtle variations in highly obfuscated attacks, suggesting a need for finer-grained detection mechanisms.
  • Real-World Scenarios: The evaluation was not extensively tested on models with significantly different prompt structures or task-specific fine-tuning, which could impact performance.
  • Future Work: The authors suggest exploring finer-grained detection mechanisms and evaluating UniGuardian in more diverse real-world scenarios.

Summary updated

BibTeX
@misc{lin2025uniguardian,
  author = {Huawei Lin and Yingjie Lao and Tong Geng and Tan Yu and Weijie Zhao},
  title = {{UniGuardian: A Unified Defense for Detecting Prompt Injection, Backdoor Attacks and Adversarial Attacks in Large Language Models}},
  year = {2025},
  eprint = {2502.13141},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.13141},
}
Figure