CodeChameleon: Personalized Encryption Framework for Jailbreaking Large Language Models

Huijie Lv, Xiao Wang, Yuansen Zhang, Caishuang Huang, Shihan Dou, Junjie Ye, Tao Gui, Qi Zhang, Xuanjing HuangarXiv 2024 · DOI

Introduction

As Large Language Models (LLMs) become increasingly integrated into critical infrastructure and daily workflows, ensuring their "alignment"—the process of preventing harmful, illegal, or unethical outputs—has become a paramount concern. Current safety mechanisms, such as Reinforcement Learning from Human Feedback (RLHF), primarily focus on natural language patterns. However, researchers have identified a fundamental "safety mechanism hypothesis": LLMs typically perform intent security recognition before response generation. If an attacker can disguise the intent such that the model fails to recognize it as "unsafe" during the initial phase, the model will proceed to generate a potentially harmful response.

CodeChameleon is a novel jailbreak framework designed to exploit this gap. It "cloaks" malicious queries in personalized encryption, transforming them into code-completion tasks. By providing the model with a decryption "key" in the form of Python code within the same prompt, CodeChameleon leverages the model's high proficiency in code comprehension to bypass standard safety filters.

Methodology

The CodeChameleon framework operates through a three-stage pipeline designed to shift the model's context from conversational interaction to programmatic task execution.

The CodeChameleon framework workflow, illustrating the transformation of a malicious query into a code-completion task using personalized encryption and decryption methods.
  1. Personalized Encryption: The user encrypts a malicious query using one of several custom tactics, such as reversing word order, splitting words into odd/even positions, or structuring the query as a binary tree. These formats are "long-tail" and rarely seen during safety alignment training.
  2. Decryption Embedding: The prompt includes a Python-based decryption function. This forces the LLM to use its advanced reasoning and code-processing capabilities to "understand" the hidden query during the execution of the task, rather than during a pre-generation safety check.
  3. Code Completion Template: The interaction is wrapped in a ProblemSolver class. The LLM is tasked with completing methods like analyze_problem (which calls the decryption function) and generate_solution. This structural shift effectively bypasses intent recognition by framing the request as a benign programming exercise.

Experimental Results

The researchers evaluated CodeChameleon using a combined dataset of 820 harmful queries from benchmarks like AdvBench, MaliciousInstruct, and ShadowAlignment. The framework achieved state-of-the-art (SoTA) results, significantly outperforming existing baselines such as GCG, AutoDAN, and PAIR.

Attack Success Rate (ASR) comparison across different encryption tactics (Reverse, Length, OddEven, BinaryTree) for Llama2 models of varying sizes.

Key findings include:

  • High Success Rates: CodeChameleon achieved an average ASR of 77.5% across seven major LLMs. Notably, it reached an 86.6% ASR on GPT-4-1106, a model that showed near-zero vulnerability to traditional baselines.
  • The Capability Paradox: Larger, more capable models (e.g., Llama2-70B) often proved more susceptible to certain encryption tactics than their smaller counterparts. Their superior reasoning allows them to successfully decrypt and execute the malicious intent, whereas smaller models might fail the complex decryption task itself.
  • Format Sensitivity: Safety filters are highly sensitive to format. Because personalized encryption produces outputs that do not resemble natural language, the "intent recognition" phase is easily circumvented.

Conclusion and Future Work

CodeChameleon demonstrates a critical vulnerability in current LLM safety alignment: the lack of "code-aware" security. While models are trained to refuse harmful natural language requests, they struggle to apply these same ethical boundaries to logic embedded within code or custom data structures. Future research must focus on developing safety mechanisms that can parse and evaluate intent within complex programmatic contexts to prevent such "cloaked" attacks.

Summary updated

BibTeX
@misc{lv2024codechameleon,
  author = {Huijie Lv and Xiao Wang and Yuansen Zhang and Caishuang Huang and Shihan Dou and Junjie Ye and Tao Gui and Qi Zhang and Xuanjing Huang},
  title = {{CodeChameleon: Personalized Encryption Framework for Jailbreaking Large Language Models}},
  year = {2024},
  eprint = {2402.16717},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.16717},
}
Figure