Hijacking Large Language Models via Adversarial In-Context Learning

Xiangyu Zhou, Yao Qiang, Saleh Zare Zade, Prashant Khanduri, Dongxiao Zhu
11/16/2023
cs.LGcs.CLcs.CR

Official code: xzhou98/Hijacking-LLMs-GGI (8 stars, last commit 2025-06-04). 1 more implementation below.

Abstract

In-context learning (ICL) has emerged as a powerful paradigm leveraging LLMs for specific downstream tasks by utilizing labeled examples as demonstrations (demos) in the preconditioned prompts. Despite its promising performance, crafted adversarial attacks pose a notable threat to the robustness of LLMs. Existing attacks are either easy to detect, require a trigger in user input, or lack specificity towards ICL. To address these issues, this work introduces a novel transferable prompt injection attack against ICL, aiming to hijack LLMs to generate the target output or elicit harmful responses. In our threat model, the hacker acts as a model publisher who leverages a gradient-based prompt search method to learn and append imperceptible adversarial suffixes to the in-context demos via prompt injection. We also propose effective defense strategies using a few shots of clean demos, enhancing the robustness of LLMs during ICL. Extensive experimental results across various classification and jailbreak tasks demonstrate the effectiveness of the proposed attack and defense strategies. This work highlights the significant security vulnerabilities of LLMs during ICL and underscores the need for further in-depth studies.

View on arXivView PDF

Code Implementations(2)

80Python, Jupyter NotebookFeb 20, 20241 years ago

Codebase for the paper "Evaluating the Adversarial Robustness of Retrieval-Based In-Context Learning for Large Language Models"

31Shell, PythonMay 24, 20242 years ago

Similar Papers

Cite this paper

@article{zhou2023hijacking,
  title  = {Hijacking Large Language Models via Adversarial In-Context Learning},
  author = {Xiangyu Zhou and Yao Qiang and Saleh Zare Zade and Prashant Khanduri and Dongxiao Zhu},
  year   = {2023},
  eprint = {2311.09948},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2311.09948v3}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2311.09948.svg)](https://codeofpaper.com/paper/2311.09948)

Discussion