NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Query-Based Adversarial Prompt Generation

Jonathan Hayase, Ema Borevkovic, Nicholas Carlini, Florian Tramèr, Milad Nasr

TL;DR

This paper introduces Greedy Coordinate Query (GCQ), a query-based adversarial attack that forces safety-aligned language models (like GPT-3.5) to output specific harmful strings via API access. By combining local proxy models with direct target queries, GCQ bypasses the limitations of transfer attacks, achieving near-100% success rates against production safety classifiers for pennies per query.

What Problem Does This Paper Address?

Safety alignment ensures that large language models (LLMs) refuse harmful requests (e.g., "how do I build a bomb?"). However, adversarial prompt injection can bypass these guardrails.

Existing automated attacks are either white-box (requiring full access to model weights, which commercial APIs like OpenAI or Anthropic do not provide) or transfer-based (crafting prompts on open-source models like Vicuna and replaying them on closed models). Transfer attacks are notoriously weak at targeted attacks—forcing the model to emit an exact malicious string—and fail when the open surrogate model differs significantly from the target. The authors address this gap by designing a query-based black-box attack that optimizes prompts directly on the remote target model.

Methods

The authors propose Greedy Coordinate Query (GCQ), which adapts the popular Greedy Coordinate Gradient (GCG) white-box attack into a query-based black-box setting.

  • Proxy-Guided Search: Instead of using white-box gradients from the target model, GCQ uses an unaligned local proxy model (like Mistral 7B) to score and filter a large batch of candidate token substitutions.
  • Best-First Buffer: GCQ maintains a buffer of the top $B$ adversarial suffixes. In each iteration, it expands the best node by evaluating the top proxy-selected candidates directly against the target remote model's API loss (negative log-probability of the target string).
  • Efficiency Optimizations: To minimize API costs, they implement short-circuiting (aborting API evaluation early if a candidate's loss exceeds the worst item in the buffer) and a smart initialization trick (repeating the target string to fill the prompt length).
  • Proxy-Free Variant: For settings without a proxy model, they introduce a pure query-only greedy attack that isolates token replacement to a single optimal position per iteration, reducing query overhead by 2× compared to standard GCG.

Datasets

The authors evaluate their approach using the harmful string dataset introduced by Zou et al. (2023), which consists of 574 targeted harmful or unsafe strings.

Key Insights and Ideas

  • Targeted vs. Untargeted Vulnerability: Forcing an LLM to say an exact harmful string is much harder than simply making it misbehave generally. Transfer attacks completely fail here, but direct query optimization succeeds.
  • Alignment Inversion via Repetition: Initializing the adversarial prompt by simply repeating the target string tricks the model into continuing the pattern, immediately solving 28% of the dataset without any optimization.
  • Scale Disconnect in Transferability: Within the same model family (Vicuna 1.3), a 7B model transfers poorly to a 33B model, and a 13B model transfers poorly to a 7B model. This highlights why relying purely on transferability is an unreliable strategy for attackers and a false sense of security for defenders.

Key Results

  • GPT-3.5 Turbo Exploitation: GCQ achieved a 79.6% attack success rate (ASR) on GPT-3.5 Turbo with a budget of just \$0.10 per target string, rising to 86.0% at \$0.20. By increasing the prompt length to 40 tokens, they achieved a 100% success rate at a mean cost of \$0.41 per target.
  • Safety Classifier Evasion: The attack successfully evaded OpenAI's text-moderation-007 and Llama Guard 7B. A universal 20-token suffix achieved a 99.2% success rate in preventing harmful strings from being flagged by OpenAI's moderation endpoint.
  • Query Efficiency: The proxy-free variant cut the required number of target model queries by 2× compared to standard GCG while maintaining high success rates.
Figure 2: Attack success rate at generating harmful strings on GPT-3.5 Turbo, as a function of cost and iterations.

Limitations and Future Work

  • Perplexity Filtering: The generated adversarial suffixes consist of seemingly random tokens, making them highly vulnerable to basic input perplexity filters. Bypassing these filters remains future work.
  • API Dependency and Cost Changes: The attack relies on extracting token log-probabilities. OpenAI has since updated its API to restrict logit-bias interactions with top logprobs, making the attack significantly more expensive via binary search workarounds.
  • API Nondeterminism: Commercial APIs exhibit inherent randomness (even at temperature 0), meaning successful prompts only replicate the harmful output about 90% of the time.

Summary updated

BibTeX
@inproceedings{hayase2024query,
  author = {Jonathan Hayase and Ema Borevkovic and Nicholas Carlini and Florian Tramèr and Milad Nasr},
  title = {{Query-Based Adversarial Prompt Generation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=jBf3eIyD2x},
}
Figure