CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion
作者: Zhen Liang, Hai Huang, Wentao Chen
分类: cs.CR, cs.AI
发布日期: 2026-09-30
备注: This paper will be accepted at NeurIPS 2026
💡 一句话要点
提出CodeMimicry以解决大型语言模型安全对齐滞后问题
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 大型语言模型 安全对齐 代码补全 越狱攻击 结构化代码 黑箱攻击 恶意代码检测
📋 核心要点
- 现有大型语言模型在安全对齐方面存在滞后,尤其是在代码领域的应用中容易受到攻击。
- 论文提出CodeMimicry框架,通过生成结构化代码提示,利用代码补全的盲点来诱导有害输出。
- 实验结果显示,CodeMimicry在攻击成功率和查询效率上均显著优于现有方法,验证了其有效性。
📝 摘要(中文)
大型语言模型在多个领域取得了显著能力,但其安全对齐仍然容易受到越狱攻击的影响。本文识别了一种未被充分探讨的失败模式——安全对齐滞后,主要体现在自然语言训练的对齐无法有效转移到代码领域。我们展示了这一滞后导致的代码补全盲点,使得嵌入在语法上有效代码中的恶意意图能够逃避安全机制。为此,我们提出了CodeMimicry,一个完全自动化的黑箱越狱框架,通过生成结构化的面向对象代码提示来诱导有害输出。实验结果表明,CodeMimicry在8个最先进的商业LLM上实现了96.25%的攻击成功率,平均查询次数为1.51,显著优于基于模板和优化的基线方法。
🔬 方法详解
问题定义:本文旨在解决大型语言模型在代码领域的安全对齐滞后问题,现有方法在自然语言训练的对齐无法有效转移到代码,导致安全机制失效。
核心思路:通过识别代码补全中的盲点,利用结构化的代码提示诱导模型生成有害输出,从而实现对安全机制的越狱。
技术框架:CodeMimicry框架包括代码提示生成模块和攻击执行模块,前者负责生成有效的代码提示,后者负责执行攻击并评估输出结果。
关键创新:最重要的创新在于识别并利用安全对齐滞后这一特定漏洞,通过结构化代码提示实现高效的黑箱越狱。
关键设计:在设计中,采用了特定的代码结构和语法规则,以确保生成的代码在语法上有效,同时通过优化查询策略提高攻击成功率。
🖼️ 关键图片
📊 实验亮点
实验结果显示,CodeMimicry在8个主流商业LLM上实现了96.25%的攻击成功率,平均查询次数仅为1.51,显著优于现有的模板和优化基线方法,展示了其在代码安全领域的强大能力。
🎯 应用场景
该研究的潜在应用领域包括安全性评估、代码审计和恶意代码检测等。通过识别和利用大型语言模型的安全漏洞,可以为开发更安全的AI系统提供重要参考,未来可能推动安全对齐技术的改进和发展。
📄 摘要(原文)
Large language models have achieved remarkable capabilities across diverse domains, yet their safety alignment remains vulnerable to jailbreak attacks. In this work, we identify a previously underexplored failure mode - safety generalization lag - where alignment trained predominantly on natural language fails to transfer to the code domain. We show that this lag induces a code-completion blind spot, allowing malicious intent embedded within syntactically valid code to evade safety mechanisms. To exploit this vulnerability, we propose CodeMimicry, a fully automated black-box jailbreak framework that generates structured, object-oriented code prompts to induce harmful outputs via code completion. Experiments on 8 state-of-the-art commercial LLMs demonstrate that CodeMimicry achieves a 96.25% attack success rate with 1.51 queries on average, significantly outperforming both template-based and optimization-based baselines. Beyond empirical performance, we provide a mechanistic analysis of code-based jailbreaks through latent space representations, including projection onto refusal-related directions and activation steering. This analysis offers an explanation of how CodeMimicry bypasses safety mechanisms in code-related domains. Our findings reveal a weakness in current safety alignment and highlight the need for robust alignments in structured domains such as code.