ShadowCode: Towards (Automatic) External Prompt Injection Attack against Code LLMs

📄 arXiv: 2407.09164v6 📥 PDF

作者: Yuchen Yang, Yiming Li, Hongwei Yao, Bingrun Yang, Yiling He, Tianwei Zhang, Dacheng Tao, Zhan Qin

分类: cs.CR, cs.AI

发布日期: 2024-07-12 (更新: 2025-07-22)

🔗 代码/项目: GITHUB


💡 一句话要点

提出ShadowCode以解决代码LLM的外部提示注入攻击问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码安全 大型语言模型 外部提示注入 自动化攻击 恶意代码识别

📋 核心要点

  1. 现有的代码LLM安全性研究滞后,缺乏有效的攻击检测与防御机制。
  2. 本文提出ShadowCode,通过代码模拟自动生成扰动,实现外部提示注入攻击。
  3. 实验表明,ShadowCode在多种编程语言中成功攻击多个Code LLM,攻击成功率超过90%。

📝 摘要(中文)

随着代码导向的大型语言模型(Code LLMs)在编程任务中的广泛应用,其安全性研究却相对滞后。本文提出了一种新的攻击范式:针对Code LLMs的(自动)外部提示注入攻击,攻击者通过生成简洁的非功能性扰动并将其注入受害者的代码上下文中,操控Code LLMs以实现恶意目标。与现有攻击相比,该方法更具现实性和威胁性,不需要控制模型的训练过程,并能够实现对抗攻击难以达到的特定恶意目标。此外,本文提出了ShadowCode,一种基于代码模拟自动生成扰动的方法,能够有效且隐蔽地进行外部提示注入。实验结果表明,ShadowCode在多个流行编程语言中成功攻击了多种开源和商业Code LLM应用,攻击成功率高达97.9%。

🔬 方法详解

问题定义:本文旨在解决针对代码LLM的外部提示注入攻击问题。现有方法如后门攻击需要控制模型训练过程,限制了攻击的灵活性和适用性。

核心思路:ShadowCode通过生成非功能性扰动并将其注入代码上下文,利用代码模拟来自动化生成有效的攻击扰动,从而实现隐蔽的外部提示注入。

技术框架:ShadowCode的整体架构包括扰动生成模块、代码上下文模拟模块和优化模块。扰动生成模块负责生成扰动,代码上下文模拟模块提供真实的代码环境,而优化模块则通过贪婪算法进行扰动优化。

关键创新:ShadowCode的主要创新在于其自动化生成扰动的能力,结合代码上下文的真实模拟,使得攻击更具隐蔽性和有效性。这与传统的对抗攻击方法有本质区别。

关键设计:在设计中,ShadowCode采用了前向推理增强和基于关键词的扰动设计,优化目标通过模拟真实代码上下文来设定,确保生成的扰动能够有效影响Code LLM的输出。具体的参数设置和损失函数设计在实验中经过多次调优,以实现最佳效果。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

实验结果显示,ShadowCode在13个不同的恶意目标上成功攻击了三种开源Code LLM,攻击成功率高达97.9%。在两个主流商业Code LLM集成应用中,攻击成功率也超过90%,仅使用12个令牌的非功能性扰动,展示了其高效性和隐蔽性。

🎯 应用场景

该研究的潜在应用领域包括代码安全性检测、恶意代码识别和软件开发工具的安全增强。通过提高对代码LLM的攻击检测能力,能够有效保护开发者和用户免受潜在的安全威胁,具有重要的实际价值和未来影响。

📄 摘要(原文)

Recent advancements have led to the widespread adoption of code-oriented large language models (Code LLMs) for programming tasks. Despite their success in deployment, their security research is left far behind. This paper introduces a new attack paradigm: (automatic) external prompt injection against Code LLMs, where attackers generate concise, non-functional induced perturbations and inject them within a victim's code context. These induced perturbations can be disseminated through commonly used dependencies (e.g., packages or RAG's knowledge base), manipulating Code LLMs to achieve malicious objectives during the code completion process. Compared to existing attacks, this method is more realistic and threatening: it does not necessitate control over the model's training process, unlike backdoor attacks, and can achieve specific malicious objectives that are challenging for adversarial attacks. Furthermore, we propose ShadowCode, a simple yet effective method that automatically generates induced perturbations based on code simulation to achieve effective and stealthy external prompt injection. ShadowCode designs its perturbation optimization objectives by simulating realistic code contexts and employs a greedy optimization approach with two enhancement modules: forward reasoning enhancement and keyword-based perturbation design. We evaluate our method across 13 distinct malicious objectives, generating 31 threat cases spanning three popular programming languages. Our results demonstrate that ShadowCode successfully attacks three representative open-source Code LLMs (achieving up to a 97.9% attack success rate) and two mainstream commercial Code LLM-integrated applications (with over 90% attack success rate) across all threat cases, using only a 12-token non-functional induced perturbation. The code is available at https://github.com/LianPing-cyber/ShadowCodeEPI.