Aligning LLMs to Be Robust Against Prompt Injection

Sizhe Chen, Arman Zharmagambetov, Saeed Mahloujifar, Kamalika Chaudhuri, Chuan Guo

Introduction

As Large Language Models (LLMs) transition into "agentic" roles within complex systems, they are increasingly tasked with processing untrusted external data, such as web search results, emails, or API outputs. This integration introduces a critical vulnerability: Prompt Injection. In these attacks, malicious instructions are embedded within external data to override the system designer's intent—for instance, by instructing the model to ignore previous commands and leak sensitive user information.

An example of a sophisticated GCG prompt injection attack.

The fundamental challenge lies in the LLM's inability to reliably distinguish between trusted developer instructions and untrusted data. Traditional defenses, such as simple fine-tuning or "reminder" prompts, often prove brittle against sophisticated optimization-based attacks like Greedy Coordinate Gradient (GCG), which exploit the model's mathematical blind spots.

Methodology: The SecAlign Framework

To address these vulnerabilities, we introduce SecAlign, a novel defense framework that treats prompt injection as an alignment problem. Unlike standard Supervised Fine-Tuning (SFT), which only provides the model with "positive" examples of correct behavior, SecAlign utilizes Direct Preference Optimization (DPO) to explicitly teach the model what to avoid.

Overview of the SecAlign framework and its performance on Llama3-8B-Instruct.

The SecAlign process involves three key steps:

  1. Preference Dataset Construction: We automatically generate triples consisting of a prompt-injected input, a "winning" secure response (following system instructions), and a "losing" insecure response (following the injection).
  2. Direct Preference Optimization: SecAlign uses DPO to maximize the margin between the likelihood of the secure response and the insecure one. This "steers" the model's decision boundary away from malicious behavior.
  3. Automated Training: Because the security policy is well-defined, the training process requires no human labeling, making it highly efficient and scalable.

Evaluation and Results

SecAlign was evaluated across several benchmarks, including AlpacaEval2 for utility and the SEP Benchmark and InjecAgent for security. The results demonstrate that SecAlign significantly enhances robustness without compromising general-purpose performance.

  • Attack Success Rate (ASR): Against standard "Ignore" attacks, SecAlign reduced the ASR to 0% for models like Mistral-7B and Llama3-8B. More importantly, against the rigorous GCG attack, SecAlign maintained an ASR below 10%, a substantial improvement over previous state-of-the-art defenses like StruQ, which exhibited ASRs exceeding 40%.
  • Utility Preservation: Evaluation on AlpacaEval2 showed that SecAlign models maintained a win rate within ~1% of undefended models, ensuring that the security alignment does not degrade the model's helpfulness or instruction-following capabilities.
  • Generalization: The framework showed remarkable success in out-of-distribution scenarios, achieving a 0% ASR on the InjecAgent benchmark, which involves complex API-calling environments.

Discussion and Conclusion

The success of SecAlign highlights the power of "Security as Alignment." By providing negative examples and optimizing for a preference margin, the model learns a more robust distinction between instruction and data. While current implementations rely on clear delimiters to separate input components, the generalization to unseen, optimized attacks suggests that preference optimization is a promising path toward truly secure agentic LLMs. Future work will explore the framework's efficacy in multi-turn conversations and its potential to mitigate other threats like jailbreaking and data extraction.

Summary updated

BibTeX
@inproceedings{chen2025aligning,
  author = {Sizhe Chen and Arman Zharmagambetov and Saeed Mahloujifar and Kamalika Chaudhuri and Chuan Guo},
  title = {{Aligning LLMs to Be Robust Against Prompt Injection}},
  booktitle = {CCS},
  year = {2025},
  doi = {10.1145/3719027.3744836},
}
Figure