Abstract
Recent advances in large language models (LLMs) have demonstrated remarkable potential in the field of natural language processing. Unfortunately, LLMs face significant security and ethical risks. Although techniques such as safety alignment are developed for defense, prior researches reveal the possibility of bypassing such defenses through well-designed jailbreak attacks. In this paper, we propose QueryAttack, a novel framework to examine the generalizability of safety alignment. By treating LLMs as knowledge databases, we translate malicious queries in natural language into structured non-natural query language to bypass the safety alignment mechanisms of LLMs. We conduct extensive experiments on mainstream LLMs, and the results show that QueryAttack not only can achieve high attack success rates (ASRs), but also can jailbreak various defense methods. Furthermore, we tailor a defense method against QueryAttack, which can reduce ASR by up to 64% on GPT-4-1106. Our code is available at https://github.com/horizonsinzqs/QueryAttack.
Introduction
Modern Large Language Models (LLMs) undergo rigorous safety alignment processes, such as Reinforcement Learning from Human Feedback (RLHF) and red teaming, to prevent the generation of harmful, unethical, or illegal content. However, these defense mechanisms are predominantly trained on natural language inputs. While adversaries have attempted to bypass these filters using ciphers or low-resource languages, these methods often degrade the model's coherence.
This research investigates a critical blind spot: LLMs are highly proficient in programming and structured query languages due to their vast training data. QueryAttack explores whether safety alignments remain robust when malicious requests are formatted in these non-natural, structured languages. By disguising malicious intent as standard programming tasks, QueryAttack reveals that current safety filters are fundamentally biased toward natural language patterns.
Methodology
The authors introduce QueryAttack, a three-step framework designed to bypass safety alignments by treating the LLM as a structured database or code interpreter.
- Query Components Extraction: Using a helper LLM (e.g., GPT-4), the framework extracts three core components from a malicious natural language prompt: the content requested, its modifier, and its high-level category. For instance, a request to build a bomb is decomposed into "crafting method," "bomb," and "crafting catalog."
- Query Template Filling: These components are injected into predefined templates across nine different programming and query languages, including C, C++, Python, Java, and SQL. A malicious request might be transformed into a SQL statement:
SELECT 'crafting method' FROM 'crafting catalog' WHERE NAME = 'bomb'. - In-Context Learning (ICL) for Query Understanding: The target LLM is provided with zero-shot or few-shot prompts instructing it to act as an expert code interpreter. The model is directed to understand the intent behind the code but respond in detailed natural language. This allows the attack to bypass the need for the model to encrypt its own output, leading directly to a harmful natural language response.
Key Insights: Why It Works
The success of QueryAttack is rooted in two primary phenomena:
- Structural Blindness: Attention analysis reveals that when LLMs process natural language, they focus heavily on malicious tokens (e.g., "bomb"), which triggers safety refusals. However, when the same request is formatted as code, the model's attention shifts to structural tokens (e.g.,
WHERE,SELECT), effectively hiding the malicious intent from the safety mechanism. - Embedding Divergence: There is a massive gap in the embedding space between a natural language malicious query and its code-equivalent. Because these representations are so distinct, safety classifiers trained on natural language fail to flag the structured queries as harmful.
- The Scaling Paradox: Interestingly, larger models are more vulnerable. The attack success rate increased from Llama-3.1-8B to Llama-3.1-70B. The larger model's superior coding abilities made it better at interpreting the malicious templates, inadvertently facilitating the jailbreak.
Experimental Results
QueryAttack was evaluated against mainstream LLMs using the AdvBench and HEx-PHI datasets. The results demonstrate a staggering vulnerability across the board.
Under an "Ensemble" configuration—where multiple code styles are attempted—QueryAttack achieved Attack Success Rates (ASRs) of 93.80% on GPT-4-1106, 96.35% on GPT-4o, and 99.62% on Gemini-flash. As shown in the performance charts, the refusal rates (RR) for these models remain near zero across almost all tested programming styles, indicating that the safety filters were almost never triggered.
QueryAttack significantly outperforms existing black-box attacks like PAIR, TAP, and CodeAttack. Furthermore, it proved robust against standard input-perturbation defenses such as paraphrasing or random character insertion, which failed to disrupt the structural integrity of the code-based prompts.
Proposed Defense: Cross-Lingual Alignment
To mitigate this vulnerability, the authors propose a "Cross-lingual alignment prompting" defense. This method forces the LLM to explicitly summarize the code's intent in natural language before generating a full response. By bringing the hidden intent back into the natural language domain, the model's standard safety alignments are reactivated. This defense reduced the ASR by an average of 63% across tested models, including a 64% drop on GPT-4-1106.
Conclusion
QueryAttack highlights a significant gap in current LLM safety alignment: the failure to generalize safety constraints to non-natural, structured languages. As LLMs become increasingly integrated into coding and database workflows, it is imperative that red-teaming and safety training pipelines incorporate these structured formats to ensure robust protection against sophisticated jailbreak attempts.
Summary updated