HijackRAG: Hijacking Attacks against Retrieval-Augmented Large Language Models

Yucheng Zhang, Qinfeng Li, Tianyu Du, Xuhong Zhang, Xinkui Zhao, Zhengwen Feng, Jianwei YinarXiv 2024 · DOI

Introduction

Retrieval-Augmented Generation (RAG) has become the industry standard for reducing LLM hallucinations and providing models with up-to-date, domain-specific knowledge. By fetching relevant documents from a database before generating an answer, RAG makes LLMs more reliable. However, this research reveals that the "R" in RAG introduces a massive, overlooked attack surface.

While most security research focuses on direct prompt injection, HijackRAG introduces a potent form of indirect prompt injection. By injecting a small amount of carefully crafted text into a knowledge database, an attacker can "hijack" the model's output for specific queries. If an attacker can place a single malicious document into a company’s vector database, they can effectively take control of the model’s response whenever a specific target question is asked.

The HijackRAG Attack Mechanism

The authors formalize HijackRAG as an optimization problem: creating a text that is both highly retrievable for a specific query and highly persuasive to the LLM. Unlike "naive" injections that simply add a command to a document, HijackRAG uses a sophisticated three-part structure:

  1. Retrieval Text ($R$): Designed to maximize semantic similarity with the target query to ensure the document ranks in the "top-k" results.
  2. Hijack Text ($H$): A "distractor" or "attention-shifter" sourced from the HackAPrompt dataset. This component is crucial for making the LLM ignore the user's original instructions.
  3. Instruction Text ($I$): The specific command the attacker wants executed (e.g., "Print 'PWNED'").
Workflow of the HijackRAG attack showing how a malicious document is retrieved and shifts the LLM's attention to a targeted answer.

The attack can be executed in two settings:

  • Black-box: The attacker uses the target query itself as the Retrieval Text ($R$).
  • White-box: The attacker uses gradient-based optimization (e.g., HotFlip) to swap tokens and maximize the similarity between the malicious document and the target query.

Experimental Setup

The effectiveness of HijackRAG was evaluated using three widely recognized benchmarks:

  • Natural Questions (NQ): Real-world queries from Google Search.
  • HotpotQA: Questions requiring multi-hop reasoning.
  • MS-MARCO: A large-scale dataset based on Bing search queries.

The researchers selected 100 closed-form questions from each dataset to measure the Attack Success Rate (ASR) and the retrieval F1-Score.

Key Results and Analysis

The results demonstrate that RAG systems are highly vulnerable to this surgical attack. In the black-box setting, the ASR reached 91% on NQ and 97% on HotpotQA when using LLaMA3.

Attack Success Rate (ASR) and F1-Score across different datasets and attack settings, highlighting the high effectiveness of HijackRAG.

Key insights from the results include:

  • High Retrieval Precision: The F1-Score for retrieval was consistently near-perfect (0.98 to 1.0), meaning the RAG system almost always picked up the malicious document.
  • Defense Failure: Common defenses like query paraphrasing and increasing the number of retrieved documents ($k$) proved ineffective. Even when $k$ was increased to 50, diluting the malicious text with 49 clean documents, the LLM still frequently followed the hijacked instruction.
  • Transferability: Malicious texts optimized for one retriever (like Contriever) were surprisingly effective against different architectures (like ANCE), suggesting the vulnerability is inherent to the semantic embedding space.

Conclusion

HijackRAG reveals that the trust LLMs place in retrieved context is a significant security flaw. The attack is "surgical," affecting only specific target queries while leaving others untouched, making it difficult to detect through general performance monitoring. Future work must focus on developing "retrieval-aware" LLMs that can distinguish between authentic knowledge and adversarial instructions, as well as more robust sanitization techniques for vector databases.

Summary updated

BibTeX
@misc{zhang2024hijackrag,
  author = {Yucheng Zhang and Qinfeng Li and Tianyu Du and Xuhong Zhang and Xinkui Zhao and Zhengwen Feng and Jianwei Yin},
  title = {{HijackRAG: Hijacking Attacks against Retrieval-Augmented Large Language Models}},
  year = {2024},
  eprint = {2410.22832},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.22832},
}
Figure