A security vulnerability detection method based on GraphCodeBERT
Through the combination of GraphCodeBERT and BiLSTM models, a static analysis and detection method for security vulnerabilities that takes into account data flow information is constructed, which solves the problem of ignoring code context in the existing technology, achieves efficient software security vulnerability detection, and significantly improves detection efficiency and accuracy. sex.
Patent Information
- Application Number
- CN202210161142.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-22
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2042-02-22
AI Technical Summary
The existing technology relies on the pre-processing process in software security vulnerability detection and ignores the contextual relationship between codes. As a result, the simple word vectorization method cannot meet the needs of vulnerability detection, and manual processing has low efficiency and error risks.
The encoder-decoder framework based on GraphCodeBERT is used to generate data flow tokens by preprocessing source code, and combined with the bidirectional long-short-term memory model, a static detection model for security vulnerabilities is constructed, and data flow information is considered to achieve two-classification detection at function granularity.
It improves the contextual connection of the code and enhances the model's ability to learn vulnerability patterns. Experimental results show that it achieves the best F1 score on real project data sets and cross-project mixed data sets, significantly improving detection efficiency and accuracy.
Smart Images

Figure CN114491540B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software security vulnerability detection, and in particular to a security vulnerability detection method based on GraphCodeBERT. Background Art
[0002] In the development process of modern software engineering, software security and information security issues will always be a difficult problem accompanying software updates and rapid technological development. Naturally, automated detection of software security vulnerabilities has become one of the important technical means to ensure software security, and has also become a key research topic for researchers in the fields of information and network security, software engineering, and program analysis.
[0003] At present, for the security assurance tasks in the development process of large-scale software projects and the rapid response and processing tasks for software security emergencies at this stage, software security vulnerability investigation, software repair and version replacement still require a lot of manpower and time. In other words, if task processing is required, only analysts who are familiar with software projects and software security vulnerabilities can cope with it, and a lot of time is consumed in program analysis and debugging and program patch writing. Developers first need to conduct a code review from beginning to end, fully understand the operation process and execution details of the entire software, in order to find the cause of the software security problem; then developers need to patch the vulnerability and conduct a series of repair tests to verify the functional integrity of the software and the correctness of the repair. Compared with ordinary software defect repair, the requirements for security issues are more stringent, and the impact of timeliness and the butterfly effect caused by code modification will be greater, which puts more pressure on developers and analysts. Not only that, in the process of manually handling security issues, even the most experienced experts and programmers who are involved in the target project and are very familiar with it will still have a certain probability of error, and there will be more or less problems such as missed inspections, misdetections and efficiency impacts. Therefore, an automated software security vulnerability detection software has great potential and has huge research value and application significance.
[0004] Traditional vulnerability detection algorithms have the characteristics of high flexibility and low overhead, but they are more dependent on the preprocessing process of security vulnerabilities. Whether based on traditional machine learning algorithms or deep learning algorithms, they all extract independent features of the code and convert them into high-dimensional space for calculation. However, this process ignores the contextual relationship between code and code. Therefore, simple word vectorization (bag of words model, word embedding model, etc.) cannot meet the needs of vulnerability detection code analysis.
[0005] How to use deep learning models to effectively learn the meaning of codes and how to efficiently represent codes have always been important issues in code processing based on deep learning, and are also the topics faced by the present invention. Summary of the invention
[0006] The present invention provides a security vulnerability detection method based on GraphCodeBERT, and solves the technical problem of using a security vulnerability static analysis detection method based on GraphCodeBERT to implement binary classification detection at the function granularity, thereby determining whether the code contains a security vulnerability.
[0007] The idea of the present invention is: the present invention first preprocesses the source code to obtain data stream tokens, source code tokens and annotation tokens as the input of the security vulnerability detection model. The security vulnerability detection model adopts an encoder-decoder framework, the encoder side uses the pre-trained GraphCodeBERT, and the decoder side uses a bidirectional long short-term memory (BiLSTM) model as a classification model, and finally outputs a binary classification result to determine whether the code contains security vulnerabilities; the present invention is based on function granularity and models the security vulnerability detection problem as a binary classification problem. Different from traditional word vector methods (such as bag-of-words models, word embedding models, etc.), by considering data flow information on the encoder side, the model can take data flow information into account and learn vulnerability patterns more easily.
[0008] The present invention is implemented by the following measures: a security vulnerability detection method based on GraphCodeBERT, wherein the detection method comprises the following steps:
[0009] (1) The source code is modeled as an abstract syntax tree. Then, by traversing the abstract syntax tree, the variable sequence is identified. Then, the variable relationship is extracted from the variable sequence. The set V and the set E are used to represent the node set and the directed edge set respectively. Then, the elements in the set are regarded as data flow tokens.
[0010] (2) Based on the encoder-decoder architecture, a static security vulnerability detection model is constructed. Specifically, the pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, code text-based tokens, and comment text-based tokens, and its output is the embedded representation of the code (represented by vectors or matrices). On the decoder side, a classifier based on the bidirectional long short-term memory (BiLSTM) model is used to construct a classifier. Its input is the code embedded representation output by the encoder side, and its output is whether the code contains security vulnerabilities.
[0011] (3) When faced with new code, the corresponding data stream tokens, code text-based tokens, and annotation text-based tokens are identified and input into the security vulnerability static detection model constructed in step (2) to obtain a prediction result, i.e., whether the code contains a security vulnerability or not.
[0012] The step (1) of identifying data stream tokens from the source code can effectively improve the contextual connection of the code in the encoder part, and specifically includes the following steps:
[0013] 1-1) Model the source code as an abstract syntax tree;
[0014] 1-2) Traverse the abstract syntax tree established in step 1-1), identify the variable sequence therein, and then extract the variable relationship from the variable sequence. Specifically, if the value of a variable node A comes from its ancestor node B, then a relationship from B to A is obtained; if the value of a variable node A comes from its child nodes B and C, then the relationship from B to A and from C to A can be obtained respectively; if two variables A and B are two child nodes of an operator node, then two independent directed graphs are obtained, and no relationship is established; if two variables A and B are the left and right child nodes of an assignment operator node or an assignment function node, then the relationship from B to A can be obtained according to the in-order and reverse order, and finally a data flow graph based on function granularity can be constructed, using set V and set E to represent the node set and the directed edge set respectively, and then the elements in the set are regarded as data flow tokens.
[0015] The step (2) of constructing a static security vulnerability detection model specifically includes the following steps:
[0016] 2-1) The pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, code text-based tokens, and comment text-based tokens. Its output is the embedded representation of the code, represented by a vector or matrix. Specifically, GraphCodeBERT-base is first used as the initial pre-trained model. GraphCodeBERT-base can effectively handle mainstream programming languages such as C# and Java, and has good generalization capabilities. Subsequently, the collected security vulnerability detection dataset is used to fine-tune the above pre-trained model.
[0017] 2-2) A bidirectional long short-term memory (BiLSTM) model is used on the decoder side to build a classifier. Its input is the code embedding representation output by the encoder side, and the output is whether the code contains security vulnerabilities. The decoder structure is designed as follows: first, three layers of BiLSTM layers are stacked, then one fully connected layer is used to reduce the dimension of the intermediate vector, and finally a softmax layer is added to output the binary classification results. The entire training process uses cross entropy Loss as the loss function, and gradient descent is used to train the model.
[0018] 2-3) Based on the encoder-decoder architecture, a static security vulnerability detection model is constructed according to the combination of the encoder side described in 2-1) and the decoder side described in 2-2).
[0019] Compared with the prior art, the beneficial effects of the present invention are as follows: a static analysis and detection method for security vulnerabilities based on GraphCodeBERT is proposed, binary classification detection at the function granularity is realized, and BiLSTM is used as the Decoder part; unlike traditional word vector methods (such as bag-of-words model, word embedding model, etc.), GraphCodeBERT, which introduces the data flow graph mechanism, can effectively improve the contextual connection of the code in the Encoder part, and by considering the data flow information on the encoder side, the model can take the data flow information into account and learn the vulnerability pattern more easily. The experimental results show that after a large number of training iterations and fine-tuning for downstream tasks, the framework has achieved the best F1 score on both real project datasets and cross-project mixed datasets. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0021] Figure 1 This is an overall flow chart of the security vulnerability detection method based on GraphCodeBERT of the present invention.
[0022] Figure 2 The figure is a flow chart of identifying data stream words in the security vulnerability detection method based on GraphCodeBERT of the present invention.
[0023] Figure 3 This is a flowchart of the decoder side in the security vulnerability detection model based on GraphCodeBERT of the present invention. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. The specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0025] Example 1
[0026] The present invention provides a technical solution, which is a security vulnerability detection method based on GraphCodeBERT, wherein the detection method comprises the following steps: Figure 1 As shown:
[0027] (1) Model the source code as an abstract syntax tree, then identify the variable sequence by traversing the abstract syntax tree, and then extract the variable relationship from the variable sequence. We use set V and set E to represent the node set and directed edge set in the graph respectively, and then treat the elements in the set as data flow tokens;
[0028] (2) Based on the encoder-decoder architecture, a static security vulnerability detection model is constructed. Specifically, the pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, code text-based tokens, and comment text-based tokens, and its output is the embedded representation of the code (represented by vectors or matrices); on the decoder side, a classifier based on the bidirectional long short-term memory (BiLSTM) model is used to construct the classifier. Its input is the code embedding representation output by the encoder side, and the output is whether the code contains security vulnerabilities;
[0029] (3) When faced with new code, the corresponding data flow tokens, code text-based tokens, and annotation text-based tokens are identified and input into the security vulnerability static detection model constructed in step (2) to obtain the prediction result (i.e., whether it contains a security vulnerability or not).
[0030] The step (1) identifies data stream tokens from the source code, effectively improving the contextual connection of the code in the encoder part, and specifically includes the following steps: Figure 2 As shown:
[0031] 1-1) Model the source code as an abstract syntax tree;
[0032] 1-2) Traverse the abstract syntax tree established in step 1-1), identify the variable sequence therein, and then extract the variable relationship from the variable sequence. Specifically, if the value of a variable node A comes from its ancestor node B, then a relationship from B to A is obtained; if the value of a variable node A comes from its child nodes B and C, then the relationship from B to A and from C to A can be obtained respectively; if two variables A and B are two child nodes of an operator node, then two independent directed graphs are obtained, and no relationship is established; if two variables A and B are the left and right child nodes of an assignment operator node or an assignment function node, then the relationship from B to A can be obtained according to the in-order and reverse order. Finally, a data flow graph based on function granularity can be constructed. Use set V and set E to represent the node set and directed edge set respectively, and then treat the elements in the set as data flow tokens.
[0033] The step (2) of constructing a static security vulnerability detection model specifically includes the following steps:
[0034] 2-1) The pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, code text-based tokens, and comment text-based tokens, and its output is the embedded representation of the code (represented by vectors or matrices). Specifically, GraphCodeBERT-base is first used as the initial pre-trained model. GraphCodeBERT-base can effectively handle mainstream programming languages such as C# and Java, and has good generalization capabilities. Then the collected security vulnerability detection dataset is used to fine-tune the above pre-trained model. The parameter settings for the GraphCodeBERT-base model are shown in Table 1.
[0035] Table 1 Parameter settings of GraphCodeBERT-base model
[0036]
[0037] The training parameter settings for fine-tuning are shown in Table 2.
[0038] Table 2 Training parameter settings for fine-tuning
[0039]
[0040]
[0041] 2-2) A bidirectional long short-term memory (BiLSTM) model is used on the decoder side to build a classifier. Its input is the code embedding representation output by the encoder side, and its output is whether the code contains security vulnerabilities. The decoder structure is designed as follows: Figure 3As shown in the figure: First, 3 BiLSTM layers are stacked, then 1 fully connected layer is used to reduce the intermediate vector dimension, and finally a softmax layer is added to output the binary classification result. The entire training process uses cross entropy Loss as the loss function and uses gradient descent to train the model.
[0042] 2-3) Based on the encoder-decoder architecture, a static security vulnerability detection model is constructed according to the combination of the encoder side described in 2-1) and the decoder side described in 2-2).
[0043] To illustrate the experimental effect of the embodiments of the present invention, commonly used security vulnerability detection methods such as the VulDeePecker framework, SySeVR framework, and Devign framework are adopted as benchmarks. The FFmpeg+QEMU dataset, REVEAL dataset, and Big-Vul dataset are used to test the performance of this method in a single project and a single dataset (independent dataset) and across projects and cross datasets (mixed datasets).
[0044] On an independent dataset, the precision, recall and F1 scores of the present invention and various benchmark control methods are shown in Table 3.
[0045] Table 3 Comparison of the precision, recall and F1 scores of the proposed method and the benchmark methods on independent datasets
[0046]
[0047]
[0048] On the mixed dataset, the precision, recall and F1 scores of the present invention and various benchmark control methods are shown in Table 4.
[0049] Table 4 Comparison of the precision, recall and F1 scores of the method of the present invention and the benchmark methods on the mixed data set
[0050]
[0051] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A security vulnerability detection method based on GraphCodeBERT, characterized in that: The steps include: (1) The source code is modeled as an abstract syntax tree. Then, by traversing the abstract syntax tree, the variable sequence is identified. Then, the variable relationship is extracted from the variable sequence. The set V and the set E are used to represent the node set and the directed edge set respectively. Then, the elements in the set are regarded as data flow tokens. In step (1), data stream tokens are identified from the source code, which specifically includes the following steps: 1-1) Model the source code as an abstract syntax tree; 1-2) Traverse the abstract syntax tree established in step 1-1), identify the variable sequence therein, and then extract the variable relationship from the variable sequence. If the value of a variable node A comes from its ancestor node B, a relationship from B to A is obtained; if the value of a variable node A comes from its child nodes B and C, the relationship from B to A and from C to A are obtained respectively; if two variables A and B are two child nodes of an operator node, two independent directed graphs are obtained, and no relationship is established; if two variables A and B are the left and right child nodes of an assignment operator node or an assignment function node, a relationship from B to A is obtained according to the in-order and reverse order, and finally a data flow graph based on function granularity is constructed, using set V and set E to represent the node set and the directed edge set respectively, and then the elements in the set are regarded as data flow tokens; (2) Based on the encoder-decoder architecture, a static security vulnerability detection model is constructed. The pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, tokens based on code text, and tokens based on comment text. Its output is the embedded representation of the code, represented by a vector or matrix. On the decoder side, a bidirectional long short-term memory BiLSTM model is used to construct a classifier. Its input is the code embedded representation output by the encoder side, and its output is whether the code contains security vulnerabilities. In step (2), a static security vulnerability detection model is constructed, which specifically includes the following steps: 2-1) The pre-trained model GraphCodeBERT is used on the encoder side. Its input is data stream tokens, code text-based tokens, and comment text-based tokens. Its output is the embedded representation of the code, represented by vectors or matrices. First, GraphCodeBERT-base is used as the initial pre-trained model. GraphCodeBERT-base processes mainstream programming languages C# and Java. Then, the collected security vulnerability detection dataset is used to fine-tune the above pre-trained model; 2-2) A bidirectional long short-term memory BiLSTM model is used on the decoder side to build a classifier. Its input is the code embedding representation output by the encoder side, and its output is whether the code contains security vulnerabilities. The decoder structure is designed as follows: first, three layers of BiLSTM layers are stacked, then one layer of fully connected layer is used to reduce the intermediate vector dimension, and finally a softmax layer is added to output the binary classification result. The entire training process uses cross entropy Loss as the loss function, and uses gradient descent to train the model; 2-3) Based on the encoder-decoder architecture, a security vulnerability static detection model is constructed according to the combination of the encoder side described in 2-1) and the decoder side described in 2-2); (3) When faced with new code, the corresponding data stream tokens, code text-based tokens, and annotation text-based tokens are identified and input into the security vulnerability static detection model constructed in step (2) to obtain a prediction result, i.e., whether the code contains a security vulnerability or not.
