Abstract
With the advancement of Large Language Models (LLMs), significant progress has been made in code generation, enabling LLMs to transform natural language into programming code. These Code LLMs have been widely accepted by massive users and organizations. However, a dangerous nature is hidden in the code, which is the existence of fatal vulnerabilities. While some LLM providers have attempted to address these issues by aligning with human guidance, these efforts fall short of making Code LLMs practical and robust. In this paper, we introduce DeceptPrompt, a novel algorithm that can generate adversarial natural language instructions that drive the Code LLMs to generate functionality correct code with vulnerabilities. DeceptPrompt is achieved through a systematic evolution-based algorithm with a fine grain loss design. The unique advantage of DeceptPrompt enables us to find natural prefix/suffix with totally benign and non-directional semantic meaning, meanwhile, having great power in inducing the Code LLMs to generate vulnerable code. Our extensive experiments and analyses validate the effectiveness of our approach and shed light on the huge weakness of LLMs in the code generation task. When applying the optimized prefix/suffix, the attack success rate (ASR) improves by an average of 50% compared with no prefix/suffix applying.
Introduction
As Large Language Models (LLMs) become staples in software development—exemplified by tools like GitHub Copilot—their security is paramount. While providers attempt to "align" these models to prevent the generation of malicious or vulnerable code, these defenses are often surface-level. Most existing adversarial research focuses on "soft prompts" (mathematical vectors unreadable to humans) or poisoning training data. DeceptPrompt addresses a more practical, alarming question: Can a user, using perfectly normal-looking natural language, steer an LLM to generate code that works correctly but contains a fatal security flaw (a CWE)? The challenge lies in doing this while maintaining the "stealth" of the prompt and the functionality of the resulting code.
Methodology
DeceptPrompt is an evolution-based framework that bypasses the need for model gradients, operating as a "gray-box" attack requiring only output logits. Its architecture consists of three main pillars:
- Prefix/Suffix Generation: The attack starts with a "seed" prompt that is intentionally benign (e.g., "My grandma wants to learn this method..."). This ensures the starting point has no obvious red flags.
- Fine-Grained "Split" Loss Function: Instead of optimizing for the entire vulnerable code block at once, the authors split the target code into "benign" parts (functional logic) and "vulnerable" parts (the security flaw). It uses KL Divergence to preserve benign functionality and Cross-Entropy loss to drive the LLM toward specific vulnerability injection.
- Semantic Preserving Evolution: To keep the adversarial prompt looking natural, the algorithm uses Genetic Algorithms (GA) with sentence-level crossover, word-level substitution, and oracle-based mutation (using ChatGPT) to paraphrase prompts, ensuring they remain coherent and stealthy.
Experimental Setup
The researchers constructed a comprehensive evaluation dataset covering 25 different CWE types (Common Weakness Enumerations), such as SQL injection (CWE-89), Buffer Overflow (CWE-119), and Use-After-Free (CWE-416). The dataset includes 40 distinct test cases across C and Python, sourced from benchmarks like SecurityEval and supplemented with manually constructed cases to ensure a wide variety of vulnerability injection methods.
Results and Key Insights
Across all tested models (CodeLlama, StarChat, WizardCoder), the attack success rate (ASR) improved by an average of 50% compared to vanilla prompts. CodeLlama-7B was the most susceptible, with ASR jumping from 0% to 62.5%.
Key insights from the evaluation include:
- Functionality vs. Vulnerability: Functionality and security are often decoupled in the model's latent space. DeceptPrompt can successfully inject a vulnerability while keeping the "Wrong Functionality Rate" (WFR) near zero.
- Stealth and Perplexity: By using ChatGPT as a paraphrasing oracle, the attack generates prompts that pass perplexity checks, meaning they are indistinguishable from natural human language to automated defense tools.
- Brittleness of Alignment: Models that showed 0% vulnerability in vanilla testing jumped to over 60% ASR when DeceptPrompt was applied, suggesting their safety alignment is remarkably brittle.
Conclusion
DeceptPrompt demonstrates that current safety alignments for code generation are insufficient against semantic-preserving adversarial perturbations. While highly effective when the vulnerability is related to the task, the attack highlights a significant weakness in LLM robustness. Future work should focus on developing more robust alignment techniques that can withstand such sophisticated natural language instructions.
Summary updated