Skip to content

ShadowCode: (Automated) External Prompt Injection Attacks Against Code LLMsShadowCode: Towards (Automatic) External Prompt Injection Attack against Code LLMs

arXiv· arXiv:2407.09164· Yuchen Yang, Yiming Li, Hongwei Yao, Bingrun Yang, Yiling He, Tianwei Zhang, Dacheng Tao, Zhan QinState Key Laboratory of Blockchain and Data Security, Zhejiang University; Hangzhou High-Tech Zone (Binjiang) Institute of Blockchain and Data Security; College of Computing and Data Science, Nanyang Technological UniversityIndexed by this site · 24 days ago· Originally publishedSelected AttacksSelectedAttention40

Translation in progress — untranslated text is omitted.

Quick read

Attacks

Based on the paper PDF (AI-generated); refer to the original

AI summaryResearchers from Zhejiang University and Nanyang Technological University propose an external prompt injection attack paradigm against code large models and an automated method, ShadowCode, which injects a short non-functional inducing perturbation into the victim's code context to induce the model to generate malicious code at a specified location during code completion. The perturbation can enter the context via copied code, installed dependency packages, or a RAG knowledge base, and bypasses static analysis and sandbox detection because it contains no malicious functional code. The method designs an optimization objective based on code context simulation, optimizes token by token with greedy search, and improves effectiveness and stealthiness with forward reasoning enhancement and keyword perturbation design. On 31 threat cases across 13 malicious targets and 3 programming languages, the method achieves an average attack success rate above 80% on CodeGemma-2b, CodeGemma-7b, and CodeGeeX2-6b, reaching up to 97.9%; using perturbations generated by open-source models to transfer attacks to commercial applications, it reaches up to 93.3% on CodeGeeX and up to 90.4% on GitHub Copilot, requiring only a 12-token non-functional perturbation.

Questions
Code LLMs are widely used for code completion, but the authors believe existing backdoor attacks require intervening in the training process and adversarial attacks struggle to achieve precise malicious goals, so the actual threat is limited. The paper proposes external prompt injection (EPI): the attacker plants non-functional inducing perturbations into the user's code context via channels such as dependency packages or RAG, inducing the completion of malicious code.
Methods
ShadowCode has two stages: first, the malicious goal is converted into a (location code, target code) output tuple, and a code context simulation containing conditional code and noise code is constructed to design the loss function; then greedy gradient search optimizes the perturbation token by token, combined with two modules: forward reasoning enhancement (FRE) and keyword-based perturbation design (KPD).
Experiments and results
On 3 open-source Code LLMs and 31 threat cases, ShadowCode achieves an average ASR of 82.0%–86.6%, higher than the baselines C-GCG and IPI-M; perturbations generated with open-source models transferred to the CodeGeeX app (gray-box) and GitHub Copilot (black-box), with Python ASR of 93.3% and 90.4%, respectively. The authors state that semantic analysis detection and character deletion struggle to defend against it.
Limitations and future work
The authors note: keywords are selected only by grid search from the output tuples, which may miss better solutions; the perturbations are not required to be fully natural and readable; and only the code completion scenario is covered. The experiments test only 3 open-source models and 2 commercial applications, and the commercial scenario involves manual testing of 620 samples.
Why it was selectedThe paper proposes an external prompt injection attack method against code LLMs and reports success rates on open-source models and commercial applications such as GitHub Copilot; developers of code completion tools can use this to assess injection risk.

The paper proposes an external prompt injection attack method against code LLMs and reports success rates on open-source models and commercial applications such as GitHub Copilot; developers of code completion tools can use this to assess injection risk.

Related papers

Read originalarxiv.org