Introduction
Software vulnerabilities represent a significant threat to global digital infrastructure, yet their automated detection remains a persistent challenge. Traditional approaches typically rely on rule-based systems, which are rigid and labor-intensive, or deep learning models that require massive, language-specific labeled datasets. While Large Language Models (LLMs) like ChatGPT have demonstrated remarkable capabilities in code generation and understanding, their initial performance in security contexts was often underwhelming.
Researchers from Xiamen University and Alibaba have addressed this gap by developing a "prompt-enhanced" framework. Their work suggests that the limitation lies not in the model's inherent intelligence, but in the quality of the prompts provided. By moving beyond simple queries and incorporating structural code information—such as data flows and API sequences—they have transformed ChatGPT into a highly effective vulnerability detector.
Methodology: Beyond Simple Queries
The core of this research involves sophisticated prompt engineering designed to provide ChatGPT with the context necessary to navigate complex program logic. The authors explored several key strategies:
- Role-Based Prompting: Instead of a generic query, the model is assigned a specific persona: "I want you to act as a vulnerability detection system." This simple instruction helps align the model's internal weights toward security-focused analysis.
- Auxiliary Structural Information: LLMs process text linearly, which makes it difficult for them to grasp the non-linear structure of code. To mitigate this, the researchers extracted API call sequences and Data Flow Graphs (DFG). These structures were then converted into natural language descriptions (e.g., "The data value of variable X at token 5 comes from variable Y...") and included in the prompt.
- Chain-of-Thought (CoT): A multi-round dialogue approach was employed where ChatGPT was first asked to describe the intent of the code. Only after the model demonstrated an understanding of the code's purpose was it tasked with identifying vulnerabilities.
- Reverse-Questioning: To identify and mitigate potential bias, the researchers used "reverse prompts," asking if the code was "correct" rather than "buggy." This helped ensure the model wasn't simply reacting to the presence of negative keywords.
Experimental Evaluation
The framework was evaluated using two primary datasets:
- Java Dataset: 2,088 functions sourced from the Software Assurance Reference Dataset (SARD).
- C/C++ Dataset: 1,937 functions sourced from the National Vulnerability Database (NVD).
The performance of the prompt-enhanced ChatGPT was compared against two established baselines: CFGNN (a graph neural network) and Bugram (an n-gram based detector).
Key Results and Insights
The experimental results demonstrated that prompt engineering significantly boosts ChatGPT's detection capabilities, often outperforming specialized deep learning models.
- Superiority over Baselines: Even with basic prompts, ChatGPT outperformed CFGNN and Bugram. In the Java dataset, ChatGPT's accuracy was 58% to 64% higher than the traditional baselines.
- The Impact of Structural Context: Incorporating API call sequences and DFG information provided the most significant gains. For Java, the $P_{r-a-b}$ prompt strategy (Role + API + Buggy) achieved an accuracy of 0.747, an 8% improvement over basic role-based prompting.
- Language Sensitivity: The effectiveness of specific prompt components varied by language. API call sequences were highly effective for Java but less so for C/C++, suggesting that optimal prompting strategies are language-dependent.
- The "Buggy" Bias: The researchers found that ChatGPT is susceptible to suggestion. Prompts explicitly mentioning "bugs" increased the likelihood of the model labeling code as vulnerable, a bias partially corrected by reverse-questioning.
Limitations and Future Directions
Despite its success, the framework faces several challenges. The "context window" of LLMs limits the size of code snippets that can be analyzed (capped at 700 tokens in this study), making multi-file or project-wide vulnerabilities difficult to detect. Furthermore, while ChatGPT is excellent at identifying the existence of a bug, it provided a comprehensive explanation in only 52-64% of correct detections.
The model also struggles with vulnerabilities requiring deep domain knowledge, such as the use of un-salted hashes (CWE-759). Future research will focus on expanding the context window to handle entire project architectures and refining prompts to capture more nuanced, logic-heavy security flaws.
Summary updated