Learnable hierarchical graph representation source code vulnerability detection system and method

By introducing learnable hierarchical graph representation and comparative learning ideas into source code vulnerability detection, combined with graph neural network and CLS technology, the problem of poor detection results in the existing technology is solved and more effective detection of source code is achieved. and the ability to automatically identify vulnerabilities.

CN118690376APending Publication Date: 2024-09-24JIANGXI NORMAL UNIV
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202410914736.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-09
Publication Date
2024-09-24

AI Technical Summary

Technical Problem

Existing source code vulnerability detection methods based on graph neural networks fail to effectively utilize the syntax information and semantic information of the code when detecting real source code, resulting in poor detection results and insufficient processing capabilities.

Method used

A learnable hierarchical graph representation source code vulnerability detection system is proposed. The graph structure information of the source code is obtained through the code graph representation processing module and the semantic extraction module. It combines the contrastive learning idea and CLS technology and uses the graph neural network to learn implicit Vulnerability characteristics are introduced, and a learnable graph readout module and a cross-granularity comparison loss function are introduced to retain fine-grained local block structure information and improve the detection effect.

Benefits of technology

It achieves more effective detection of unknown source code, can automatically identify vulnerabilities and assist manual inspection, improving detection performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118690376A
    Figure CN118690376A
Patent Text Reader

Abstract

The invention provides a learnable hierarchical graph representation source code vulnerability detection system and method. The system comprises a code graph representation processing module, a code semantic extraction module and a vulnerability detection model. Firstly, a code graph representation processing module collects code samples and preprocesses and marks the code samples; secondly, graph structure information in the codes is extracted through a static program analysis technology; then, the code semantic extraction module maps code statements to a high-dimensional vector space by adopting a word embedding technology; thirdly, a CLS design thought is fused into the graph classification model, and the capacity of the model for capturing long-distance information in a code representation graph is enhanced; a contrast learning idea is introduced, and local structure information after graph pooling is maintained by setting a cross-granularity contrast loss function; and finally, the vulnerability detection model can automatically learn a vulnerability mode implied in the source code and predict vulnerability information in the sample. According to the method, a learnable hierarchical graph classification model is designed and is used for automatically learning implicit vulnerability modes in source codes.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer security technology, and in particular relates to a learnable hierarchical graph representation source code vulnerability detection system and method. Background Art

[0002] During the software development process, it is almost inevitable that developers may introduce vulnerabilities due to negligence or inherent flaws in the programming language.

[0003] Traditional vulnerability detection methods mainly rely on rules formulated by human experts. However, traditional methods lack flexibility and comprehensiveness, and it is difficult to effectively deal with diverse security threats. In current research, the application of machine learning technology, especially deep learning, reinforcement learning and artificial neural networks in the field of vulnerability detection has received widespread attention. The integration of these advanced technologies has brought new possibilities for vulnerability detection, making the detection method not only more efficient, but also better able to adapt to and identify new security threats.

[0004] In the vulnerability detection methods based on deep learning, there are many ways to represent source code, such as graph-based representation, text-based representation, abstract syntax tree-based representation, etc. Among them, the graph-based representation method can deeply represent the syntax information, semantic information, control flow, data flow and other information of the code. Therefore, the graph-based representation method has been widely used and paid attention to in the field of vulnerability detection, and has become a hot topic of current research. At present, a mature system has not been established for using graph neural network models for vulnerability detection. Most applications simply use existing models in the field of graph learning, and are not designed for the characteristics of the vulnerability code itself, resulting in problems such as incomplete use of the information contained in the representation graph of the real vulnerability code by the neural network and poor processing capabilities. Summary of the invention

[0005] The purpose of the present invention is to address the deficiencies of the prior art and propose a learnable hierarchical graph representation source code vulnerability detection system and method. Static analysis is applied to obtain the graph representation of the source code, and then the graph neural network is used to learn the implicit vulnerability features. The learning process introduces the idea of ​​comparative learning and CLS technology, thereby achieving better vulnerability detection effects on unknown real source code.

[0006] To achieve the above technical objectives, the present invention provides a learnable hierarchical graph representation source code vulnerability detection system, including a code graph representation processing module, a code semantic extraction module and a vulnerability detection model; the code graph representation extraction module is used to clean up the code, delete invalid comment information in the code, and symbolize the code, and use a static program analysis tool to extract graph structure information in the code; the code semantic extraction module is used to extract word tokens from the source code, and map the words in the code to a high-dimensional vector space through the Word2Vec technology to vectorize the source code; the vulnerability detection model includes a contrast loss calculation module and a learnable graph readout module, the contrast loss calculation module is used to learn how to effectively compress and represent the graph during the training process of the vulnerability detection model, while retaining the fine-grained local block structure information that is critical to the classification task; the learnable graph readout module is used to establish connections between the trainable CLS node and all other nodes, and use the graph attention mechanism of the GAT layer to aggregate all node pairs into a single classification vector.

[0007] Based on the above hierarchical graph representation source code vulnerability detection system, the present invention further provides a learnable hierarchical graph representation source code vulnerability detection method, comprising the following steps: Step S1: clean up samples of the code data set containing vulnerabilities, and the code graph representation extraction module deletes invalid annotation information in the code; Step S2, the code graph representation extraction module uses an abstract name replacement strategy to perform symbolic processing, replaces the custom variable names and function names with common symbolic names, and uses a unified mark (such as VAR_number for variable names, FUN_number for function names) to replace the original custom names; the number here is counted based on the order in which the variable names and function names appear in the code; Step S3: The code graph representation extraction module uses the static program analysis tool Joern to extract the program dependency graph (PDG) of the program code; Step S4, slicing the PDG according to four sensitive points: API call, arithmetic operator, pointer member access, and array index operation of the dangerous system; Step S5: label and remove duplicate samples; Step S6: training a vulnerability detection model; Step S7: Use the trained vulnerability detection model to detect unknown source code.

[0008] Specifically, the step S5 of labeling and deduplicating samples includes the following steps: Step S51, sample annotation: mark the source code slice graph representation containing the vulnerability as 1, mark the source code slice graph representation not containing the vulnerability as 0, and record the line number of the location of the vulnerability code; Step S52, sample deduplication: For duplicate samples and conflicting samples, use the MD5 encryption algorithm to calculate the sum of the hash values ​​of all tokens in the code line. If the hash values ​​are consistent and the sample labels are also the same, then it is a duplicate sample; if the hash values ​​are consistent and the sample labels are different, then it is a conflicting sample; Step S53: remove conflicting samples and retain one copy of duplicate samples.

[0009] Specifically, the training of the vulnerability detection model in step S6 includes the following steps: Step S61, the Bi-LSTM bidirectional long short-term memory network trains the contextual relationship of the token in each code line; Step S62, a convolutional neural network and a graph attention neural network with a three-layer alternating structure learn the topological structure features of the vulnerability code graph representation; Step S63: In the three-layer graph neural network, a learnable graph reading function is set in each layer to read out the code graph representation information of different granularities, and finally concatenate the three reading results as the vector representation for graph classification; Step S64: During the training of the vulnerability detection model, the cross-granularity contrast loss function is synchronously involved in the training to assist in parameter adjustment during the training process.

[0010] Specifically, the step S7 uses the trained vulnerability detection model to detect unknown source code, including the following steps: Step S71: Process data through steps S1 to S4, slice the processed code, assemble the batches and send them to the trained vulnerability detection model; Step S72: Analyze the problematic code slices according to the predicted values ​​output by the vulnerability detection model, and print out the suspected vulnerability type, the line number where the vulnerability code is located, and the source code storage path information.

[0011] Compared with the prior art, the present invention has the following beneficial effects: The present invention is based on graph neural networks, learns implicit features of vulnerability codes, automatically detects whether unknown codes contain vulnerabilities, and assists manual inspection.

[0012] In existing source code vulnerability detection methods based on graph neural networks, the model part often uses ready-made graph neural networks. When detecting real source code, it is specially designed according to the source code graph representation, so the effect is poor. The present invention introduces a learnable graph reading function, and introduces a custom contrast loss function in the model training process to guide the model graph colonization process, retaining the local block structure information of the source code, thereby obtaining better detection performance on the real source code. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present disclosure and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without paying creative work.

[0014] Figure 1 It is a flow chart of a learnable hierarchical graph representation source code vulnerability detection method of the present invention; Figure 2 It is a comparison of the samples before cleaning and before and after symbolization processing in the embodiment of the present invention; Figure 3 is a schematic diagram of code slicing in an embodiment of the present invention; Figure 4 is a flow chart of sample labeling and sample deduplication in an embodiment of the present invention; Figure 5 This is the process of obtaining the node feature vector in the embodiment of the present invention; Figure 6 is a schematic diagram of a learnable readout function in an embodiment of the present invention; Figure 7 is a schematic diagram of a cross-granularity contrast loss function in an embodiment of the present invention; Figure 8 Schematic diagram of the detection process in an embodiment of the present invention. DETAILED DESCRIPTION

[0015] In order to enable personnel in this technical field to better understand the technical solutions in this specification, a source code sample will be used below, combined with the drawings in the embodiments of this specification, to clearly and completely describe the technical solutions in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments.

[0016] Example The present invention provides a learnable hierarchical graph representation source code vulnerability detection system, comprising a code graph representation processing module, a code semantic extraction module and a vulnerability detection model; the code graph representation extraction module is used to clean up the code, delete invalid comment information in the code, and symbolize the code, and use a static program analysis tool to extract graph structure information in the code; the code semantic extraction module is used to extract word tokens from the source code, and map the words in the code to a high-dimensional vector space through the Word2Vec technology to vectorize the source code; the vulnerability detection model comprises a contrast loss calculation module and a learnable graph readout module, the contrast loss calculation module is used to learn how to effectively compress and represent the graph during the training process of the vulnerability detection model, while retaining fine-grained local block structure information that is crucial to the classification task; the learnable graph readout module is used to establish a connection between a trainable CLS node and all other nodes, and aggregate all node pairs into a single classification vector using the graph attention mechanism of the GAT layer.

[0017] like Figure 1 As shown, based on the above hierarchical graph representation source code vulnerability detection system, the present invention also provides a learnable hierarchical graph representation source code vulnerability detection method, comprising the following steps: Step S1: clean up samples of the code data set containing vulnerabilities, and the code graph representation extraction module deletes invalid annotation information in the code; Step S2, the code graph representation extraction module uses an abstract name replacement strategy to perform symbolic processing, replaces the custom variable names and function names with common symbolic names, and uses a unified mark (such as VAR_number for variable names, FUN_number for function names) to replace the original custom names; in this embodiment, number is counted based on the order in which the variable names and function names appear in the code; Specifically, Figure 2 The following is an example of source code before and after symbolic processing. The custom function name CWE121_ is replaced by FUN1, and the variable data is replaced by VAR1. Step S3: The code graph representation extraction module uses the static program analysis tool Joern to extract the program dependency graph (PDG) of the program code; Step S4, slicing the PDG according to four sensitive points: API call, arithmetic operator, pointer member access, and array index operation of the dangerous system; Specifically, Figure 3The figure shows an example of program code slicing. In the program dependency graph, starting from the code line where the sensitive point is located, the preceding and succeeding items are traversed along the data dependency edge and the control dependency edge until the traversal stops at the boundary node, thereby obtaining a code slice. Step S5: label and remove duplicate samples; Specifically, Figure 4 The flowchart of labeling and deduplication of samples is shown in the figure, including the following steps: Step S51, sample annotation: mark the source code slice graph representation containing the vulnerability as 1, mark the source code slice graph representation not containing the vulnerability as 0, and record the line number of the location of the vulnerability code; Step S52, sample deduplication: For duplicate samples and conflicting samples, use the MD5 encryption algorithm to calculate the sum of the hash values ​​of all tokens in the code line. If the hash values ​​are consistent and the sample labels are also the same, then it is a duplicate sample; if the hash values ​​are consistent and the sample labels are different, then it is a conflicting sample; Step S53: remove conflicting samples and keep one copy of duplicate samples; Step S6: training a vulnerability detection model; Specifically, the vulnerability detection model is an end-to-end model, which is divided into three parts: Part 1 uses a bidirectional LSTM neural network to learn the contextual relationship between tokens in the code line and obtain the feature vector of the node; Figure 5 The figure shows the process of obtaining the corresponding node feature vector from a code line. Before entering the Bi-LSTM network, the Word2Vec technology is used to correspond the token to a specific value (the value is the index value in the trained vocabulary), and then the Embedding layer is used to map each token into a high-dimensional vector, and finally sent to the Bi-LSTM network to train the contextual relationship between tokens in each code line.

[0018] Part 2 uses a hierarchical graph neural network to learn the topological structure information of the graph in the code table, and combines it with the Softmax layer to learn the implicit vulnerability patterns. Figure 6 The figure shows the working principle of the learnable readout function. First, the topological structure of the code graph representation is changed, and a learnable CLS is added. The attention mechanism of the graph attention neural network GAT layer is used to cleverly aggregate the information of all nodes in the graph to the CLS node. Finally, the information of the CLS node is used as the vector representation of the entire graph for subsequent classification training.

[0019] In the third part, in the hierarchical graph neural network, a cross-granularity contrast loss function is embedded between different layers to guide the model to retain the local structural information in the code graph representation and improve the model's ability to learn the deep information of the code graph representation; Figure 7 The figure shows a design example of a cross-granularity contrast loss function. Index Table 1 records the nodes in the original image and the nodes in their 1-hop neighborhoods. Index Table 2 records the top-k nodes filtered out after the first pooling and the nodes in their 1-hop neighborhoods. Index Table 3 records the top-k nodes filtered out after the second pooling and the nodes in their 1-hop neighborhoods. d and d' are a pair of positive sample pairs, and d and d'' are a pair of positive sample pairs. As for negative sample pairs, they are non-neighborhood nodes of d and d', and the non-neighborhood nodes of d and d' constitute negative sample pairs.

[0020] Step S7: Use the trained vulnerability detection model to detect unknown source code.

[0021] Specifically, Figure 8 As shown, step S7 uses the trained vulnerability detection model to detect unknown source code, including the following steps: Step S71: Process data through steps S1 to S4, slice the processed code, assemble the batches and send them to the trained vulnerability detection model; Step S72: Analyze the problematic code slices according to the predicted values ​​output by the vulnerability detection model, and print out the suspected vulnerability type, the line number where the vulnerability code is located, and the source code storage path and other information.

[0022] It should be noted that a person skilled in the art can understand that all or part of the steps of implementing the above embodiments can be completed by hardware, or can be completed by instructing related hardware through a program, and the program can be stored in a computer-readable storage medium, and the above-mentioned storage medium can be a read-only memory, a disk or an optical disk, etc.

[0023] The above is only a preferred embodiment of the present invention, and does not limit the present invention in other forms. Any technician familiar with the profession may use the above disclosed technical content to change or modify it into an equivalent embodiment with equivalent changes. However, any simple modification, equivalent change and modification made to the above embodiment according to the technical essence of the present invention without departing from the technical solution of the present invention still belongs to the protection scope of the technical solution of the present invention.

Claims

1. A learnable hierarchical graph representation source code vulnerability detection system, characterized in that: It includes a code graph representation processing module, a code semantic extraction module and a vulnerability detection model; the code graph representation extraction module is used to clean up the code, delete invalid comment information in the code, symbolize the code, and use static program analysis tools to extract graph structure information in the code; the code semantic extraction module is used to extract word tokens from the source code, and map the words in the code to a high-dimensional vector space through the Word2Vec technology to vectorize the source code; the vulnerability detection model includes a contrast loss calculation module and a learnable graph readout module, the contrast loss calculation module is used to learn how to effectively compress and represent the graph during the training process of the vulnerability detection model, while retaining the fine-grained local block structure information that is crucial to the classification task; the learnable graph readout module is used to establish connections between the trainable CLS nodes and all other nodes, and use the graph attention mechanism of the GAT layer to aggregate all node pairs into a single classification vector.

2. A learnable hierarchical graph representation source code vulnerability detection method, using the hierarchical graph representation source code vulnerability detection system as claimed in claim 1, characterized in that: The following steps are involved: Step S1: clean up samples of the code data set containing vulnerabilities, and the code graph representation extraction module deletes invalid annotation information in the code; Step S2: The code graph representation extraction module uses an abstract name replacement strategy to perform symbolic processing, replaces the custom variable names and function names with common symbolic names, and uses a unified mark to replace the original custom names; Step S3: The code graph representation extraction module uses the static program analysis tool Joern to extract the program dependency graph (PDG) of the program code; Step S4, slicing the PDG according to four sensitive points: API call, arithmetic operator, pointer member access, and array index operation of the dangerous system; Step S5: label and remove duplicate samples; Step S6: training a vulnerability detection model; Step S7: Use the trained vulnerability detection model to detect unknown source code.

3. A learnable hierarchical graph representation source code vulnerability detection method according to claim 2, characterized in that: Step S5 of labeling and removing duplicate samples includes the following steps: Step S51, sample annotation: mark the source code slice graph representation containing the vulnerability as 1, mark the source code slice graph representation not containing the vulnerability as 0, and record the line number of the location of the vulnerability code; Step S52, sample deduplication: For duplicate samples and conflicting samples, use the MD5 encryption algorithm to calculate the sum of the hash values ​​of all tokens in the code line. If the hash values ​​are consistent and the sample labels are also the same, then it is a duplicate sample; if the hash values ​​are consistent and the sample labels are different, then it is a conflicting sample; Step S53: remove conflicting samples and retain one copy of duplicate samples.

4. The method for detecting source code vulnerabilities by using a learnable hierarchical graph representation according to claim 2, characterized in that: The training of the vulnerability detection model in step S6 includes the following steps: Step S61, the Bi-LSTM bidirectional long short-term memory network trains the contextual relationship of the token in each code line; Step S62, a convolutional neural network and a graph attention neural network with a three-layer alternating structure learn the topological structure features of the vulnerability code graph representation; Step S63: In the three-layer graph neural network, a learnable graph reading function is set in each layer to read out the code graph representation information of different granularities, and finally concatenate the three reading results as the vector representation for graph classification; Step S64: During the training of the vulnerability detection model, the cross-granularity contrast loss function is synchronously involved in the training to assist in parameter adjustment during the training process.

5. The method for detecting source code vulnerabilities by using a learnable hierarchical graph representation according to claim 2, characterized in that: Step S7 uses the trained vulnerability detection model to detect unknown source code, including the following steps: Step S71: Process data through steps S1 to S4, slice the processed code, assemble the batches and send them to the trained vulnerability detection model; Step S72: Analyze the problematic code slices according to the predicted values ​​output by the vulnerability detection model, and print out the suspected vulnerability type, the line number where the vulnerability code is located, and the source code storage path information.

Citation Information

Patent Citations

  • C source code vulnerability detection method based on Bert model and BiLSTM

    CN113420296A

  • Source code vulnerability detection and positioning method based on graph neural network

    CN114942879A

  • Smart contract vulnerability detection method based on directed graph attention network

    CN117725592A

  • Multi-type vulnerability detection method based on graph neural network

    CN117972725A