Manuscript Offprint

arXiv:2610.17907

SpecCloak: Probing Harmful Code Generation through Engineering Specifications

Wenbin Chen, Jiexin Wang, Guanting Wu, Jiaxi Liang, Yi Cai

Wenbin Chen, D◯\textcircled{D}, Jiexin Wang, Guanting Wu D◯\textcircled{D}, Jiaxi Liang and Yi Cai ∗*

Abstract—The code generation capabilities of large language models (LLMs) can equally be misused to implement harmful functionality, underscoring the need to understand their safety behavior in response to programming requests. While request phrasing is known to influence refusal, it remains unclear whether the form in which coding task requirements are stated affects the refusal process for malicious code generation. We introduce PseudoSpec, a template that organizes functional requirements as engineering specifications, and investigate its effects through controlled comparisons, representation analysis, and activation interventions. Across five open-weight models, removing explicit risk keywords only partially reduces refusal, whereas specification prompting substantially lowers refusal even when these keywords are retained. This reduction accompanies consistent shifts toward code-output-related representations, and interventions along condition-difference directions alter refusal behavior. Output analysis further distinguishes code-related responses from generation degeneration. Building on these findings, we propose SpecCloak, a black-box red-teaming framework combining PseudoSpec with discriminator-guided optimization, achieving an average execution-verified attack success rate of 28.91% across eight target models on RedCode-Gen. We also introduce SpecReveal, a defense prototype combining natural-language intent reconstruction with dual-view detection without target-model fine-tuning. It achieves a 96.88% attack detection rate in a separate prompt-detection evaluation, with false positives assessed on benign programming and specification-style inputs. These results connect specification-induced response differences to practical security consequences and identify intent reconstruction as a complementary detection approach.

Index Terms—Large language models, malicious code generation, jailbreak attack, mechanistic interpretability, risk detection

I. INTRODUCTION

Large language models (LLMs) are increasingly used to assist software development, generating, completing, and modifying code from user requirements [1]–[3]. However, these capabilities can also be exploited for malicious purposes. Attackers may use coding assistants to turn harmful requirements into program implementations, reducing the effort needed to develop malicious code [4]–[6]. This risk makes resistance to harmful code generation an important security requirement for LLM-based development tools: models should assist legitimate programming tasks while refusing requests to implement harmful functionality.

Research on jailbreak attacks has demonstrated that model safeguards can be bypassed through adversarial suffixes

Figure

Fig. 1. A motivating example. The same malicious intent is presented to the same aligned code LLM in two forms: (R1) as a direct natural-language request, which the model refuses with a safety explanation; (R2) as a specification-style (PseudoSpec) prompt preserving the operational semantics, to which the same model instead produces complete, executable code. The specification-style version elicits code instead of a refusal. Prompts and outputs are excerpted for display.

and automated prompt refinement [7], [8]. Related studies investigate alternative representations of harmful requests. CodeAttack embeds harmful queries in code-completion tasks [9], QueryAttack translates them into structured query languages [10], and LogiBreak expresses them through formal logic [11]. More directly targeting code generation, CodeJailbreaker embeds malicious intent in commit messages while presenting the explicit instruction as a benign code-modification task [6]. Although these studies show that request formulation can affect refusal behavior, how the organization and expression of functional requirements shape refusal of harmful code generation requests remains insufficiently characterized.

To investigate this issue, we introduce PseudoSpec, a pseudo-specification template that organizes functional requirements into engineering-style requests with configuration fields, predicate APIs, temporal constraints, and implementation requirements. Figure 1 illustrates the motivating observation: the same model checkpoint refuses a direct natural-language request but generates code when the same target functionality is presented through PseudoSpec. The template serves both as an attack primitive and as an experimental instrument for systematically examining this response discrepancy. Across five open-weight models, we find that specification prompting substantially reduces refusal even when explicit risk keywords are retained. Representation analysis reveals consistent shifts toward code-output-related representations, while activation interventions along estimated condition-

*Corresponding author: Yi Cai (e-mail: ycai@scut.edu.cn).

Wenbin Chen, Jiexin Wang, Guanting Wu, Jiaxi Liang and Yi Cai are with the South China University of Technology. (e-mail: chenwenbin221@gmail.com; jiexinwang@scut.edu.cn; 13434297975@163.com; 202511095231@mail.scut.edu.cn; ycai@scut.edu.cn) difference directions demonstrate that these directions can alter refusal behavior. We further examine output composition and assess whether the generated code implements the requested harmful functionality, connecting refusal bypass to its practical security consequences in LLM-assisted code development.

Building on PseudoSpec, we develop SpecCloak, a black-box red-teaming framework that searches for specification-style requests capable of inducing harmful code generation. SpecCloak extends the template into a configurable search space and uses discriminator feedback to guide the optimization of prompt variants. On RedCode-Gen, it achieves an average execution-verified attack success rate of 28.91% across eight target models. Here, execution-based verification assesses whether the generated code implements the requested harmful functionality, providing a functional measure of attack success beyond non-refusal alone.

To detect such requests, we introduce SpecReveal, a defense prototype that reconstructs the requested functionality in natural language and jointly assesses the original request and the reconstructed description. This design provides a complementary view of the operational intent expressed through specification-style requirements and requires no fine-tuning of the target model. In a separate prompt-detection evaluation, SpecReveal achieves an attack detection rate of 96.88%. We also evaluate false positives on benign programming and specification-style inputs to assess its impact on legitimate development requests.

Our contributions are as follows:

  1. We introduce PseudoSpec, an engineering-specification template for expressing harmful code generation requirements. It provides both an attack primitive for refusal bypass and a common basis for controlled comparisons of model responses.

  2. We characterize specification-induced refusal changes across five open-weight models. Refusal decreases even when explicit risk keywords are retained, accompanied by consistent shifts toward code-output-related representations. Activation interventions further establish that estimated condition-difference directions can alter refusal behavior, while output analysis distinguishes code-related responses from generation degeneration.

  3. We develop SpecCloak, a black-box red-teaming framework combining PseudoSpec with discriminator-guided optimization. Evaluation on RedCode-Gen across eight target models yields an average execution-verified attack success rate of 28.91%.

  4. We propose SpecReveal, a defense prototype that combines natural-language intent reconstruction with dual-view prompt detection without target-model fine-tuning. It achieves a 96.88% attack detection rate in a separate evaluation, with false positives assessed on benign programming and specification-style inputs.

References (35)