How well does LLM generate security tests?

Ying Zhang, Wenjia Song, Zhengjie Ji, Danfeng, Yao -, Na MengarXiv 2023 · DOI

Abstract

Developers often build software on top of third-party libraries (Libs) to improve programmer productivity and software quality. The libraries may contain vulnerabilities exploitable by hackers to attack the applications (Apps) built on top of them. People refer to such attacks as supply chain attacks, the documented number of which has increased 742% in 2022. People created tools to mitigate such attacks, by scanning the library dependencies of Apps, identifying the usage of vulnerable library versions, and suggesting secure alternatives to vulnerable dependencies. However, recent studies show that many developers do not trust the reports by these tools; they ask for code or evidence to demonstrate how library vulnerabilities lead to security exploits, in order to assess vulnerability severity and modification necessity. Unfortunately, manually crafting demos of application-specific attacks is challenging and time-consuming, and there is insufficient tool support to automate that procedure. In this study, we used ChatGPT-4.0 to generate security tests, and to demonstrate how vulnerable library dependencies facilitate the supply chain attacks to given Apps. We explored various prompt styles/templates, and found that ChatGPT-4.0 generated tests for all 55 Apps, demonstrating 24 attacks successfully. It outperformed two state-of-the-art security test generators -- TRANSFER and SIEGE -- by generating a lot more tests and achieving more exploits. ChatGPT-4.0 worked better when prompts described more on the vulnerabilities, possible exploits, and code context. Our research will shed light on new research in security test generation. The generated tests will help developers create secure by design and secure by default software.

Introduction

The modern software ecosystem relies heavily on third-party libraries, leading to a massive increase in "supply chain attacks." While automated scanners (like npm audit or Snyk) are effective at flagging vulnerable dependencies, developers often ignore these reports. This skepticism stems from high false-positive rates and a lack of evidence that a vulnerability is actually reachable or exploitable within the specific context of the developer's application.

Manually writing a proof-of-concept (PoC) exploit to prove a vulnerability matters is a grueling, time-consuming task requiring deep security expertise. This study explores whether Large Language Models (LLMs), specifically ChatGPT-4.0, can automate the generation of these "security tests" to help developers prioritize fixes by providing concrete evidence of exploitability.

Methodology

The researchers conducted an empirical study using a three-phase methodology to evaluate ChatGPT-4.0's capability in generating PoC security exploits.

1. Dataset Construction

The team curated a list of 30 vulnerability entries (CVEs) across 26 Java libraries (e.g., Bouncy Castle, Apache Commons, Jackson). They then identified 55 real-world GitHub applications that utilized these specific vulnerable versions. The dataset spanned several categories, including Denial of Service (13), Directory Traversal (6), and Remote Code Execution (4).

2. Conceptual Framework and Prompt Design

The core of the experiment involved providing the LLM with sufficient context to bridge the gap between a library-level vulnerability and an application-level exploit.

Conceptual framework showing the relationship between the App, the vulnerable Library, and the generated security test.

As shown in the conceptual framework, the goal is to generate a test that calls the application (App), which in turn calls a vulnerable API within a library (Lib). To achieve this, the researchers developed a "Default" prompt template ($P$) that provided ChatGPT-4.0 with:

  • The application code context.
  • The specific vulnerable API being called.
  • The method in the app that triggers the call.
  • An exemplar test case from the library itself (showing how the vulnerability is triggered at the library level).
  • The CVE ID for context.
The prompt template used to guide ChatGPT-4.0 in generating JUnit security tests.

3. Result Validation

Generated tests were integrated into the target projects. The researchers evaluated whether the tests compiled (applying minor fixes where necessary) and, crucially, if they successfully triggered the intended security failure, such as a crash for DoS or an unauthorized file write for Directory Traversal.

Key Results

ChatGPT-4.0 was compared against two state-of-the-art (SOTA) specialized security tools, TRANSFER and SIEGE, which utilize Genetic Algorithms and symbolic execution.

Metric ChatGPT-4.0 TRANSFER SIEGE
Tool Applicability (A) 55/55 16/55 1/55
Test Compilability (C) 40/55 13/55 1/55
Vulnerability Exploitation (V) 24/55 4/55 0/55

ChatGPT-4.0 successfully demonstrated exploits in 43.6% of the cases, significantly outperforming the SOTA tools, which combined for less than 8%. ChatGPT proved superior at synthesizing complex input objects and navigating the boilerplate code required for functional tests.

Discussion and Insights

  • The "Mimicry" Advantage: ChatGPT’s strength lies in its ability to mimic and adapt. When provided with an exploit that works at the library level, it is remarkably effective at "translating" that logic to work through the layers of a client application.
  • The Necessity of Context: The "exemplar test" from the library was the most critical piece of information. When this was removed from the prompt, the success rate for exploits dropped to zero, suggesting that LLMs currently require a "seed" of exploit logic to be effective.
  • Mocking Capabilities: Unlike traditional tools that struggle with deep code dependencies, ChatGPT can "hallucinate" plausible and often correct mocking code using frameworks like Mockito, allowing tests to run even when environment setup is complex.
  • Inter-procedural Weakness: The model struggles when the path from the application's input to the library's vulnerable "sink" is excessively long or involves complex intermediate logic.

Limitations and Future Work

While promising, 15 of the 55 tests failed to compile due to Java access rule violations or undefined classes. Furthermore, the study was a "one-shot" attempt. Future research could integrate ChatGPT into an iterative feedback loop, where compilation errors and stack traces are fed back to the model to refine the generated code. Expanding the scope beyond Java and automating the "last mile" of test integration (e.g., fixing hardcoded paths) remain important areas for future development.

Summary updated

BibTeX
@misc{zhang2023how,
  author = {Ying Zhang and Wenjia Song and Zhengjie Ji and Danfeng and Yao - and Na Meng},
  title = {{How well does LLM generate security tests?}},
  year = {2023},
  eprint = {2310.00710},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.00710},
}
Figure