A fine-grained hybrid semantic vulnerability detection method and system
By combining the fine-grained hybrid semantic vulnerability detection method of CodeBERT, MSFCNN and ResGCN, the problems of coarse granularity and insufficient feature extraction capabilities in the existing technology are solved, and more accurate vulnerability detection and location are achieved.
Patent Information
- Application Number
- CN202410881154.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-03
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-07-03
AI Technical Summary
Existing vulnerability detection methods have too coarse granularity and insufficient feature extraction capabilities, making it easy to miss fine-grained vulnerabilities, affecting the accuracy of vulnerability detection.
Combining the CodeBERT pre-trained language model, multi-scale fusion convolutional neural network MSFCNN and graph convolutional neural network ResGCN with residual structure, fine-grained vulnerability detection is carried out through a bidirectional multi-head self-attention mechanism to improve feature extraction capabilities and detection accuracy.
It improves the accuracy of vulnerability detection and feature extraction capabilities, can more effectively capture complex local information and structured features, and enhances the understanding of code and vulnerability location capabilities.
Smart Images

Figure CN118410498B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of network security technology, and in particular to a fine-grained hybrid semantic vulnerability detection method and system. Background Art
[0002] The statements in this section merely mention background art related to the present invention and do not necessarily constitute prior art.
[0003] Against the backdrop of the rapid development of information technology, software systems are becoming increasingly complex, and modern software systems are often plagued by various software vulnerabilities. Recent reports on common vulnerabilities and exposures show a rapid increase in the number of software vulnerabilities, which exhibit behaviors contrary to established specifications and may lead to serious security issues such as data leakage, functional failures, and even system crashes. Therefore, effective vulnerability detection is essential to ensure the security of software systems.
[0004] Many static analysis methods for vulnerability detection have been proposed in the prior art, but these methods rely heavily on the expertise of human experts in identifying effective code rules and features, and perform poorly in detecting new vulnerability variants. In order to overcome the above problems, vulnerability detection methods based on deep learning have been continuously developed and become the mainstream of vulnerability detection. Now, vulnerability detection methods based on deep learning can generally be divided into similarity-based and pattern-based vulnerability methods.
[0005] Similarity-based vulnerability detection methods identify potential vulnerabilities by comparing the similarities between data or behavior in the target system and known vulnerability patterns or abnormal behaviors. Pattern-based vulnerability detection methods focus on detecting situations where code or system configuration does not match known vulnerability patterns or specifications. However, the above methods are too coarse-grained for vulnerability detection and have insufficient feature extraction capabilities. They are prone to missing fine-grained vulnerabilities, affecting the accuracy of vulnerability detection. Summary of the invention
[0006] In order to address the shortcomings of the prior art, the present invention provides a fine-grained hybrid semantic vulnerability detection method, system, electronic device and computer-readable storage medium, which combine the CodeBERT pre-trained language model, the multi-scale fusion convolutional neural network MSFCNN and the graph convolutional neural network ResGCN with a residual structure to construct a vulnerability detection framework and improve the feature extraction capability; at the same time, fine-grained vulnerability detection is performed based on the bidirectional multi-head self-attention mechanism to improve the accuracy of vulnerability detection.
[0007] In a first aspect, the present invention provides a fine-grained hybrid semantic vulnerability detection method;
[0008] A fine-grained hybrid semantic vulnerability detection method, comprising:
[0009] Obtain the source code to be detected and perform preprocessing to obtain the sequence code representation and the graph code representation respectively;
[0010] The sequence code representation is input into the pre-trained language model to extract global semantic information, and obtain the global semantic feature vector and attention score embedding matrix; the sequence code representation is input into the preset multi-scale fusion convolutional neural network to extract local feature information and obtain the local feature vector; the graph code representation is input into the graph convolutional neural network with residual structure to extract structured information and obtain the graph embedding vector;
[0011] The global semantic feature vector, local feature vector and graph embedding vector are fused and input into the trained vulnerability detection model for processing to obtain the vulnerability detection results. The sequence code representation is fine-grainedly detected according to the vulnerability detection results and the attention score embedding matrix to obtain the vulnerability location results.
[0012] In some embodiments, the pre-trained language model includes a plurality of encoders consisting of a stack of bidirectional multi-head self-attention layers and feed-forward neural networks;
[0013] The bidirectional multi-head self-attention layer is used to calculate the corresponding attention score according to the sequence code representation and perform linear transformation to obtain the multi-head attention output; the feedforward neural network is used to perform nonlinear transformation on the multi-head attention output.
[0014] In some embodiments, the sequence code indicates that inputting a preset multi-scale fusion convolutional neural network to extract local feature information includes:
[0015] The sequence code representation is processed by multiple groups of convolutional filters and adaptive pooling operations are performed to obtain multiple output feature vectors;
[0016] Weighted fusion of multiple output feature vectors to obtain local feature vectors;
[0017] Each group of convolution filters includes multiple convolution filters of different sizes, and the output feature vector is the sum of the outputs of the multiple convolution filters.
[0018] In some implementations, inputting the graph code representation into a graph convolutional neural network with a residual structure to extract structured information includes:
[0019] Based on the graph code representation, the final vector representation of each node in the graph code representation is determined through a soft attention mechanism and a nonlinear activation function;
[0020] The final vector representation of each node is concatenated and pooled to generate a graph embedding vector.
[0021] In some embodiments, the fusion of the global semantic feature vector, the local feature vector and the graph embedding vector is specifically: dimensionally expanding the graph embedding vector and adding it to the global semantic feature vector and the local feature vector.
[0022] In some implementations, the fusing of the global semantic feature vector, the local feature vector, and the graph embedding vector and inputting the resultant into a trained vulnerability detection model for processing includes:
[0023] The fused vector is nonlinearly transformed through the fully connected layer and then output to the softmax classification layer for binary classification to obtain the vulnerability detection result.
[0024] In some implementations, performing fine-grained detection on the sequence code representation based on the vulnerability detection result and the attention score embedding matrix includes:
[0025] Split the sequence representation into multiple token lists by line breaks, and summarize the attention scores of the word tokens in each token list to determine the attention score of the token list;
[0026] Attention score consistency analysis is performed on each tag list, and potential vulnerable code is identified based on the preset vulnerability alert threshold.
[0027] In a second aspect, the present invention provides a fine-grained hybrid semantic vulnerability detection system;
[0028] A fine-grained hybrid semantic vulnerability detection system, comprising:
[0029] The preprocessing module is configured to: obtain the source code to be detected and perform preprocessing to obtain a sequence code representation and a graph code representation respectively;
[0030] The feature extraction module is configured to: input the sequence code representation into the pre-trained language model to extract global semantic information, obtain the global semantic feature vector and the attention score embedding matrix; input the sequence code representation into the preset multi-scale fusion convolutional neural network to extract local feature information, and obtain the local feature vector; input the graph code representation into the graph convolutional neural network with a residual structure to extract structured information and obtain the graph embedding vector;
[0031] The vulnerability detection and positioning module is configured as follows: after fusing the global semantic feature vector, the local feature vector and the graph embedding vector, the vectors are input into the trained vulnerability detection model for processing to obtain the vulnerability detection results; and according to the vulnerability detection results and the attention score embedding matrix, the sequence code representation is finely granularly detected to obtain the vulnerability positioning results.
[0032] In a third aspect, the present invention provides an electronic device;
[0033] An electronic device includes a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps of the above-mentioned fine-grained hybrid semantic vulnerability detection method are completed.
[0034] In a fourth aspect, the present invention provides a computer-readable storage medium;
[0035] A computer-readable storage medium is used to store computer instructions. When the computer instructions are executed by a processor, the steps of the above-mentioned fine-grained hybrid semantic vulnerability detection method are completed.
[0036] Compared with the prior art, the present invention has the following beneficial effects:
[0037] 1. The technical solution provided by the present invention combines the CodeBERT pre-trained language model, the multi-scale fusion convolutional neural network MSFCNN and the graph convolutional neural network ResGCN with a residual structure to construct a vulnerability detection framework and improve the feature extraction capability; and determines the statement where the vulnerability code is located through the bidirectional multi-head self-attention mechanism in the CodeBERT architecture to achieve fine-grained vulnerability detection.
[0038] 2. The technical solution provided by the present invention introduces one-dimensional convolution kernels of different sizes in the multi-scale fusion convolutional neural network to more flexibly capture local patterns in the input data; each convolution kernel operates at a different scale, enabling the model to fully understand local features. This multi-scale feature extraction strategy enhances the model's sensitivity to local structures in the input data, enabling it to more effectively capture and express complex local information.
[0039] 3. The technical solution provided by the present invention adopts a sequence-based construction graph method to construct a code graph representation, which can be applied to any type of vulnerability data; by using a graph neural network with a residual structure to capture structured information between codes, it can aggregate information from adjacent nodes, effectively capture the relationship between nodes, and provide a comprehensive understanding of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0041] Figure 1 A schematic diagram of the architecture of a vulnerability detection framework provided by an embodiment of the present invention;
[0042] Figure 2 A schematic diagram of the network architecture of the CodeBERT model provided by an embodiment of the present invention;
[0043] Figure 3A schematic diagram of the network architecture of a multi-scale fusion convolutional neural network provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0044] It should be noted that the following detailed descriptions are exemplary and are intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used in the present invention have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.
[0045] It should be noted that the terms used herein are only for describing specific embodiments, and are not intended to limit exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that the terms "include" and "have" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0046] In the absence of conflict, the embodiments of the present invention and the features of the embodiments may be combined with each other.
[0047] Terminology explanation:
[0048] Self-attention mechanism: A technology that can assign weights to inputs to remember key information. It allows each element in the sequence to interact with other elements to calculate the representation of each element. By calculating the attention weight, each element is assigned a weight to indicate its relevance to other elements. The model can directly parallelize the entire sequence without introducing convolutional or recursive structures, thereby better capturing long-distance dependencies and contextual information in the sequence.
[0049] Pre-trained language models: refers to models that perform a large amount of unsupervised learning on large-scale text corpora. These models learn the language rules and semantic information in the text through self-supervised learning of text data, thereby capturing the relationship between words.
[0050] Embodiment 1
[0051] The feature extraction capability of existing vulnerability detection algorithms is weak, and it is impossible to fully utilize code features for vulnerability detection. The granularity of vulnerability detection is coarse and the accuracy needs to be improved; therefore, the present invention provides a fine-grained hybrid semantic vulnerability detection method.
[0052] Next, combine Figure 1-Figure 3, a fine-grained hybrid semantic vulnerability detection method disclosed in this embodiment is described in detail. The fine-grained hybrid semantic vulnerability detection method includes the following steps:
[0053] S1. Obtain the source code to be detected and perform preprocessing to obtain the sequence code representation and the graph code representation respectively.
[0054] In order to facilitate the subsequent neural network to process the source code to be detected, in this embodiment, the source code to be detected is first converted into a processing format for the subsequent neural network.
[0055] Specifically, the source code to be detected is processed by a byte pair encoding (BPE) algorithm to obtain a sequence code representation; the source code to be detected is processed by a sequence-based graph construction method to obtain a graph code representation.
[0056] Exemplarily, the code in the Big-Vul dataset is used as the code to be detected. The Big-Vul dataset is one of the largest datasets in the field of vulnerability detection based on deep learning.
[0057] The specific process of converting the code to be detected into a sequence code representation through the BPE algorithm is as follows:
[0058] (1) Determine the vocabulary size, add a tag after each word and count the frequency of each word.
[0059] (2) Split all words into single characters and construct an initial vocabulary.
[0060] (3) Count the frequency of each token in the code to be detected, select the token with the highest frequency, merge its corresponding characters, add the new characters to the vocabulary, and then merge all the characters in the code to be detected to generate a new token with a higher frequency. Repeat steps (2) and (3) until the number of words in the vocabulary reaches the preset threshold or the frequency of the next character pair is 1.
[0061] (4) After the vocabulary is constructed, the code to be detected is encoded to obtain a sequence code representation.
[0062] Here, using the BPE algorithm to process the code to be detected is a prior art in the art and will not be described in detail here.
[0063] The specific process of processing the source code to be detected based on the sequence-based graph construction method is as follows:
[0064] First, the code to be detected is regarded as a flat sequence, and a sliding window is set to move in the sequence; then, the first node in each sliding window is connected to other nodes in the sliding window to build a graph structure. After the initial graph structure is built, it is optimized through iterative merging operations. For example, for frequently co-occurring node pairs, the connection between them is strengthened or merged into new nodes.
[0065] Finally, an adjacency matrix of the graph is obtained, which is used as the code graph representation of the code to be detected.
[0066] S2. Input the sequence code representation into the pre-trained language model to extract global semantic information, and obtain the global semantic feature vector and the attention score embedding matrix; input the sequence code representation into the preset multi-scale fusion convolutional neural network to extract local feature information and obtain the local feature vector; input the graph code representation into the graph convolutional neural network with a residual structure to extract structured information and obtain the graph embedding vector. In order to improve the model's feature extraction capability for the code to be detected, in this embodiment, feature extraction is performed on the code to be detected by using a pre-trained language model, a multi-scale fusion convolutional neural network, and a graph convolutional neural network with a residual structure.
[0067] As an implementation method, the CodeBERT pre-trained language model is used to extract global semantic information from the sequence code representation to obtain the global semantic feature vector and attention score embedding matrix.
[0068] Among them, the CodeBERT pre-trained language model includes 12 encoders consisting of bidirectional multi-head self-attention layers and feedforward neural network stacks.
[0069] In order to make the attention mechanism capture information more comprehensively, better understand the dependencies in the sequence, and improve the performance of the model in natural language processing tasks, in this embodiment, a bidirectional multi-head self-attention mechanism is used as a bidirectional multi-head self-attention layer. The bidirectional multi-head self-attention mechanism is an attention mechanism for processing sequence data. Its core idea is to improve the model's ability to represent the input sequence by allowing the model to learn multiple sets of attention weights in parallel. In the bidirectional multi-head self-attention mechanism, each position in the input sequence can interact with other positions in the sequence, so that the model can take into account the information of each position in the sequence at the same time and capture the bidirectional dependencies in the sequence.
[0070] Feedforward neural networks are usually used to perform nonlinear transformations on the final output of the encoder. In the CodeBERT model, the feedforward neural network usually consists of two linear layers, each followed by a ReLU activation function, and the output dimensions of the two linear layers are usually the same. The role of the feedforward neural network is to perform nonlinear transformations on the hidden layer output of the encoder to enhance the model's ability to represent the source code.
[0071] For example, let the sequence code be represented by X={x1,x2,...,x n}, the sequence code is a feature vector with a dimension of [8,512,768], and the sequence code is represented by X={x1,x2,...,x n} is input into the bidirectional multi-head self-attention layer for processing. The bidirectional multi-head self-attention layer converts the sequence code representation X={x1,x2,...,x n} is mapped to the query (query, Q), key (key, K) and value (value, V) space to obtain the query vector, key vector and value vector of each node. The specific mapping process can be expressed by the following formula:
[0072] ;
[0073] ;
[0074] ;
[0075] In the formula, represents the first learnable weight matrix, represents the second learnable weight matrix, Represents the learnable third weight matrix. For each head h in the bidirectional multi-head self-attention mechanism, the attention score is calculated as follows:
[0076] ;
[0077] Among them, d k is the dimension of the key vector. The softmax function normalizes the attention scores, and then concatenates the attention outputs of all heads as the output of the bidirectional multi-head attention layer. At this time, since the output dimension of the bidirectional multi-head attention mechanism is the same as the input dimension, the output dimension is still [8,512,768].
[0078] Once again, a linear transformation is performed to obtain the final multi-head attention output:
[0079] ;
[0080] In the formula, It is a linear transformation weight matrix, which is used to concatenate the outputs of all attention heads and perform linear transformation, mapping the output of the multi-head attention mechanism back to the original dimension.
[0081] Then, the output of the multi-head self-attention layer is residually connected to the original input. After the residual connection, it is input into the LayerNorm layer for normalization and then input into the feedforward neural network for processing. The feedforward neural network consists of two fully connected layers and a nonlinear activation function. The specific process of the feedforward neural network processing the input features is as follows:
[0082] (1) The input features are linearly transformed through the first fully connected layer, mapping the dimension of the input features from 728 to a higher dimension, such as 3072.
[0083] (2) The input features mapped to a higher dimension are processed by the Relu activation function and then input into the second fully connected layer to map the dimension from the higher dimension back to the original dimension, such as mapping from 3072 back to 768. The output of the feedforward neural network is a sequence code representing the hidden state at each position, and the hidden state is used as the input for vulnerability detection.
[0084] Based on the above technical features, the CodeBERT pre-trained language model can better process sequence code representation and obtain high-quality hidden states as output, which can be better used for various code-related natural language processing tasks.
[0085] As an implementation method, the multi-scale fusion convolutional neural network includes multiple groups of convolution filters, and each group of convolution filters includes multiple convolution filters of different sizes.
[0086] Exemplarily, the process of inputting the sequence code representation into a preset multi-scale fusion convolutional neural network to extract local feature information is as follows:
[0087] The sequence code representation is processed using k groups of n convolutional filters of size m, where the convolutional filters in each group have different sizes. Afterwards, an adaptive pooling layer is used to add and fuse the outputs of each group of convolutional filters to ensure a consistent output size. Finally, the features generated by convolutional filters of different sizes are added and fused to integrate information at different levels.
[0088] Here, the adaptive pooling layer outputs a feature map of any size as needed by adjusting the size and stride of the pooling window. In this embodiment, the size is [batch, 512, 768].
[0089] As an implementation method, a graph convolutional neural network with a residual structure is used to process the code graph representation.
[0090] Residual connections are used to merge information learned from lower layers into higher layers, and more importantly, allow gradients to pass directly through layers to avoid gradient vanishing or exploding problems. Inspired by this, in this embodiment, the graph neural network is defined as:
[0091] ;
[0092] In the formula, The matrix representation of the nodes of the k+1th layer iteration, represents the node matrix representation of the k-th layer iteration, represents a graph convolutional neural network, The adjacency matrix representing the graph structure, Indicates the number of layers of the graph neural network iteration.
[0093] The update method of node p is:
[0094] ;
[0095] in, is the Laplace renormalized adjacency matrix D -1 / 2 AD -1 / 2 The edge constant between node p and node u in , D is the diagonal matrix obtained by , 𝜙 represents the nonlinear activation function, and P represents the node set in the code graph representation.
[0096] The graph neural network updates each node of the code graph representation through the above update method, and then inputs the graph-level readout layer to generate the initial graph embedding representation. The graph-level readout layer is used to aggregate the node representation of the entire updated graph into an initial graph embedding representation of a fixed size.
[0097] Subsequently, concatenation pooling is used to produce better results for graph classification, and max pooling is used to exploit more information about node representations. A hybrid between concatenation and max pooling is considered to generate the graph embedding vector e g , combining the representation capabilities of different feature vectors to form a richer and more comprehensive feature representation, realizing information fusion to better characterize the features of the code to be detected. It is expressed as:
[0098] ;
[0099] ;
[0100] in, is the final vector representation of node p, is the soft attention mechanism as a node, is the vector representation of node p on the last layer, W represents the fourth learnable weight matrix, W Trepresents the weight vector, b represents the bias vector, and MaxPool represents the maximum pooling.
[0101] S3. The global semantic feature vector, the local feature vector and the graph embedding vector are fused and input into the trained vulnerability detection model for processing to obtain the vulnerability detection result.
[0102] In this embodiment, the graph embedding vector is dimensionally expanded and fused with the global semantic feature vector and the local feature vector to capture the semantic, local and structural features of the code, providing a comprehensive perspective for vulnerability detection.
[0103] Specifically, the graph embedding vector is dimensionally expanded and added to the global semantic feature vector and the local feature vector to obtain a vulnerability detection feature vector; the vulnerability detection feature vector is then input into the fully connected layer for nonlinear transformation; finally, the vulnerability detection feature vector after nonlinear transformation is input into the softmax classification layer for binary classification to obtain the vulnerability detection result, that is, whether the code to be detected contains a vulnerability. If the code to be detected contains a vulnerability, S4 is executed.
[0104] S4. Perform fine-grained detection on the sequence code representation based on the attention score embedding matrix to obtain the vulnerability location results.
[0105] In the code, the token with the highest attention score is most likely to be vulnerable to attack. Specifically, first, the sequence representation is split into multiple token lists by line breaks, each list represents a line of code, and the sum of the attention scores of the 12 encoders corresponding to the word tokens in each token list is summarized as the attention score of the word token; then, each token list is subjected to consistency analysis to obtain consistency analysis parameters, and compared with the preset vulnerability alarm threshold. If the consistency analysis parameters are greater than the preset vulnerability alarm threshold, the token list is a potential vulnerability code. Here, the attention score consistency analysis of the token list is specifically as follows: compare whether the difference in attention scores between each word token is large, and analyze the consistency of the attention score of each lexical token with the attention scores of its surrounding tokens. The specific process is as follows:
[0106] First, the average of the attention scores for each token list is calculated, expressed as follows:
[0107] ;
[0108] In the formula, is the attention score of the i-th word token, i is the ordinal number of the word token, and n is the total number of word tokens in the token list.
[0109] Then, the consistency analysis parameters of the marker list are calculated and expressed as:
[0110] .
[0111] The larger the consistency analysis parameter, the greater the fluctuation of the attention score, and the more inconsistent it is, the potential vulnerability exists; the smaller the standard deviation, the smaller the fluctuation of the attention score, and the more consistent the word marking in the marking code.
[0112] Next, the effect of the method described in this embodiment is verified using the publicly available Big-Vul dataset. The Big-Vul dataset is one of the largest datasets in the field of deep learning vulnerability detection. The Big-Vul dataset contains 3754 code vulnerabilities, covering 91 different vulnerability types.
[0113] Before model training, in this embodiment, the data set is divided into training set, validation set, and test set according to the 8:1:1 standard. The pytorch framework is mainly used in the experiment, and the Transformer library is used to provide APIs and pre-trained weights for calling the CodeBERT model. In the feature extraction stage, two representations of the code are constructed using the BPE algorithm and the sequence-based graph representation method; in the model training stage, the training set is propagated as the input of the network model to obtain the predicted probability of each sample. The label of the same data set is used to calculate the loss value of the model through the cross entropy loss function, and the parameter gradient of the network model is calculated by back propagation for the loss value, and the parameters of the network model are updated using the AdamW optimizer algorithm.
[0114] Embodiment 2
[0115] This embodiment discloses a fine-grained hybrid semantic vulnerability detection system, including:
[0116] The preprocessing module is configured to: obtain the source code to be detected and perform preprocessing to obtain a sequence code representation and a graph code representation respectively;
[0117] The feature extraction module is configured to: input the sequence code representation into the pre-trained language model to extract global semantic information, obtain the global semantic feature vector and the attention score embedding matrix; input the sequence code representation into the preset multi-scale fusion convolutional neural network to extract local feature information, and obtain the local feature vector; input the graph code representation into the graph convolutional neural network with a residual structure to extract structured information and obtain the graph embedding vector;
[0118] The vulnerability detection and positioning module is configured as follows: after fusing the global semantic feature vector, the local feature vector and the graph embedding vector, the vectors are input into the trained vulnerability detection model for processing to obtain the vulnerability detection results; and according to the vulnerability detection results and the attention score embedding matrix, a fine-grained detection is performed on the sequence code representation to obtain the vulnerability positioning results.
[0119] It should be noted that the above-mentioned preprocessing module, feature extraction module and vulnerability detection and positioning module correspond to the steps in Embodiment 1, and the examples and application scenarios implemented by the above-mentioned modules and the corresponding steps are the same, but are not limited to the contents disclosed in the above-mentioned Embodiment 1. It should be noted that the above-mentioned modules as part of the system can be executed in a computer system such as a set of computer executable instructions.
[0120] Embodiment 3
[0121] Embodiment 3 of the present invention provides an electronic device, including a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps of the above-mentioned fine-grained hybrid semantic vulnerability detection method are completed.
[0122] Embodiment 4
[0123] Embodiment 4 of the present invention provides a computer-readable storage medium for storing computer instructions. When the computer instructions are executed by a processor, the steps of the above-mentioned fine-grained hybrid semantic vulnerability detection method are completed.
[0124] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0125] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0126] These computer program instructions can also be loaded onto a computer or other programmable data processing device, and a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1The steps for the functions specified in one or more boxes.
[0127] The description of each embodiment in the above embodiments has different emphases. For parts not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0128] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A fine-grained hybrid semantic vulnerability detection method, characterized in that: include: Obtain the source code to be detected and perform preprocessing to obtain the sequence code representation and the graph code representation respectively; The sequence code representation is input into the pre-trained language model to extract global semantic information, and obtain the global semantic feature vector and attention score embedding matrix; the sequence code representation is input into the preset multi-scale fusion convolutional neural network to extract local feature information and obtain the local feature vector; the graph code representation is input into the graph convolutional neural network with residual structure to extract structured information and obtain the graph embedding vector; The global semantic feature vector, local feature vector and graph embedding vector are fused and input into the trained vulnerability detection model for processing to obtain vulnerability detection results; the sequence code representation is fine-grainedly detected according to the vulnerability detection results and the attention score embedding matrix to obtain vulnerability location results; Preprocess the source code to be detected to obtain the graph code representation as follows: regard the code to be detected as a flat sequence, set a sliding window that moves in the sequence; establish edges between the first node in each sliding window and other nodes in the sliding window to construct a graph structure; After the initial graph structure is constructed, the initial graph structure is optimized through iterative merging operations to obtain an adjacency matrix of the graph, which is used as the code graph representation of the code to be detected; The pre-trained language model includes a plurality of encoders stacked by bidirectional multi-head self-attention layers and feedforward neural networks; the bidirectional multi-head self-attention layers are used to calculate the corresponding attention scores according to the sequence code representation and perform linear transformation to obtain the multi-head attention output; the feedforward neural network is used to perform nonlinear transformation on the multi-head attention output; The step of inputting the sequence code representation into the pre-trained language model to extract global semantic information and obtain the global semantic feature vector and the attention score embedding matrix specifically includes: The sequence code representation is input into the bidirectional multi-head self-attention layer for processing. The bidirectional multi-head self-attention layer maps the sequence code representation to the query, key and value space to obtain the query vector, key vector and value vector of each node; For the head h in the bidirectional multi-head self-attention mechanism, the attention score is calculated as follows: ; Among them, Q represents the query vector, K represents the key vector, V represents the value vector, and d k is the dimension of the key vector. The softmax function normalizes the attention scores and then concatenates the attention outputs of all heads as the output of the bidirectional multi-head attention layer. The final multi-head attention output is obtained through a linear transformation: ; In the formula, X represents the sequence code representation, is a linear transformation weight matrix, which is used to concatenate the outputs of all attention heads and perform linear transformation, mapping the output of the multi-head attention mechanism back to the original dimension; The output of the multi-head self-attention layer is connected to the original input through a residual connection. After the residual connection, it is input into the LayerNorm layer for normalization and then input into the feedforward neural network for processing. The feedforward neural network consists of two fully connected layers and a nonlinear activation function. The specific process of the feedforward neural network processing the input features is as follows: The first fully connected layer linearly transforms the input features and maps the dimensions of the input features to a higher dimension. The input features mapped to a higher dimension are processed by the Relu activation function and then input into the second fully connected layer to map the dimension of the input features from a higher dimension back to the original dimension. The fine-grained detection of the sequence code representation according to the vulnerability detection result and the attention score embedding matrix includes: Split the sequence code representation into multiple token lists by line breaks, and summarize the attention scores of the word tokens in each token list to determine the attention score of the token list; Perform attention score consistency analysis on each tag list and identify potential vulnerable code based on the preset vulnerability alert threshold; The sequence code representation is divided into multiple tag lists by line breaks, and the attention scores of the word tags in each tag list are summarized. The attention scores of the tag lists are determined as follows: the sequence representation is divided into multiple tag lists by line breaks, each list represents a line of code, and the sum of the attention scores of the encoders corresponding to the word tags in each tag list is summarized as the attention score of the word tag; The attention score consistency analysis for each tag list specifically includes: Calculate the average of the attention scores for each token list, expressed as follows: ; In the formula, is the attention score of the i-th word token, i is the ordinal number of the word token, and n is the total number of word tokens in the token list; Calculate the consistency analysis parameters for the marker list, expressed as: ; The consistency analysis parameter is compared with a preset vulnerability alarm threshold. If the consistency analysis parameter is greater than the preset vulnerability alarm threshold, the marked list is a potential vulnerability code.
2. The fine-grained hybrid semantic vulnerability detection method according to claim 1, characterized in that: The sequence code indicates that inputting a preset multi-scale fusion convolutional neural network to extract local feature information includes: The sequence code representation is processed by multiple groups of convolutional filters and adaptive pooling operations are performed to obtain multiple output feature vectors; Weighted fusion of multiple output feature vectors to obtain local feature vectors; Each group of convolution filters includes multiple convolution filters of different sizes, and the output feature vector is the sum of the outputs of the multiple convolution filters.
3. The fine-grained hybrid semantic vulnerability detection method according to claim 1, characterized in that: The step of inputting the graph code representation into a graph convolutional neural network with a residual structure to extract structured information includes: Based on the graph code representation, the final vector representation of each node in the graph code representation is determined through a soft attention mechanism and a nonlinear activation function; The final vector representation of each node is concatenated and pooled to generate a graph embedding vector.
4. The fine-grained hybrid semantic vulnerability detection method according to claim 1, characterized in that: The fusion of the global semantic feature vector, the local feature vector and the graph embedding vector specifically includes: dimensionally expanding the graph embedding vector and adding the graph embedding vector to the global semantic feature vector and the local feature vector.
5. The fine-grained hybrid semantic vulnerability detection method according to claim 1, characterized in that: The fusing of the global semantic feature vector, the local feature vector and the graph embedding vector and inputting the result into the trained vulnerability detection model for processing comprises: The fused vector is nonlinearly transformed through the fully connected layer and then output to the softmax classification layer for binary classification to obtain the vulnerability detection result.
6. A fine-grained hybrid semantic vulnerability detection system, characterized in that: include: The preprocessing module is configured to: obtain the source code to be detected and perform preprocessing to obtain a sequence code representation and a graph code representation respectively; The feature extraction module is configured to: input the sequence code representation into the pre-trained language model to extract global semantic information, obtain the global semantic feature vector and the attention score embedding matrix; input the sequence code representation into the preset multi-scale fusion convolutional neural network to extract local feature information, and obtain the local feature vector; input the graph code representation into the graph convolutional neural network with a residual structure to extract structured information and obtain the graph embedding vector; The vulnerability detection and positioning module is configured to: fuse the global semantic feature vector, the local feature vector and the graph embedding vector and input them into the trained vulnerability detection model for processing to obtain the vulnerability detection result; perform fine-grained detection on the sequence code representation according to the vulnerability detection result and the attention score embedding matrix to obtain the vulnerability positioning result; Preprocess the source code to be detected to obtain the graph code representation as follows: regard the code to be detected as a flat sequence, set a sliding window that moves in the sequence; establish edges between the first node in each sliding window and other nodes in the sliding window to construct a graph structure; After the initial graph structure is constructed, the initial graph structure is optimized through iterative merging operations to obtain an adjacency matrix of the graph, which is used as the code graph representation of the code to be detected; The pre-trained language model includes a plurality of encoders stacked by bidirectional multi-head self-attention layers and feedforward neural networks; the bidirectional multi-head self-attention layers are used to calculate the corresponding attention scores according to the sequence code representation and perform linear transformation to obtain the multi-head attention output; the feedforward neural network is used to perform nonlinear transformation on the multi-head attention output; The step of inputting the sequence code representation into the pre-trained language model to extract global semantic information and obtain the global semantic feature vector and the attention score embedding matrix specifically includes: The sequence code representation is input into the bidirectional multi-head self-attention layer for processing. The bidirectional multi-head self-attention layer maps the sequence code representation to the query, key and value space to obtain the query vector, key vector and value vector of each node; For the head h in the bidirectional multi-head self-attention mechanism, the attention score is calculated as follows: ; Among them, Q represents the query vector, K represents the key vector, V represents the value vector, and d k is the dimension of the key vector. The softmax function normalizes the attention scores and then concatenates the attention outputs of all heads as the output of the bidirectional multi-head attention layer. The final multi-head attention output is obtained through a linear transformation: ; In the formula, X represents the sequence code representation, is a linear transformation weight matrix, which is used to concatenate the outputs of all attention heads and perform linear transformation, mapping the output of the multi-head attention mechanism back to the original dimension; The output of the multi-head self-attention layer is connected to the original input through a residual connection. After the residual connection, it is input into the LayerNorm layer for normalization and then input into the feedforward neural network for processing. The feedforward neural network consists of two fully connected layers and a nonlinear activation function. The specific process of the feedforward neural network processing the input features is as follows: The first fully connected layer linearly transforms the input features and maps the dimensions of the input features to a higher dimension. The input features mapped to a higher dimension are processed by the Relu activation function and then input into the second fully connected layer to map the dimension of the input features from a higher dimension back to the original dimension. The fine-grained detection of the sequence code representation according to the vulnerability detection result and the attention score embedding matrix includes: Split the sequence code representation into multiple token lists by line breaks, and summarize the attention scores of the word tokens in each token list to determine the attention score of the token list; Perform attention score consistency analysis on each tag list and identify potential vulnerable code based on the preset vulnerability alert threshold; The sequence code representation is divided into multiple tag lists by line breaks, and the attention scores of the word tags in each tag list are summarized. The attention scores of the tag lists are determined as follows: the sequence representation is divided into multiple tag lists by line breaks, each list represents a line of code, and the sum of the attention scores of the encoders corresponding to the word tags in each tag list is summarized as the attention score of the word tag; The attention score consistency analysis for each tag list specifically includes: Calculate the average of the attention scores for each token list, expressed as follows: ; In the formula, is the attention score of the i-th word token, i is the ordinal number of the word token, and n is the total number of word tokens in the token list; Calculate the consistency analysis parameters for the marker list, expressed as: ; The consistency analysis parameter is compared with a preset vulnerability alarm threshold. If the consistency analysis parameter is greater than the preset vulnerability alarm threshold, the marked list is a potential vulnerability code.
7. An electronic device, characterized in that: The invention comprises a memory and a processor and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the fine-grained hybrid semantic vulnerability detection method according to any one of claims 1 to 5 is completed.
8. A computer-readable storage medium, characterized in that: Used to store computer instructions, which, when executed by a processor, complete the fine-grained hybrid semantic vulnerability detection method described in any one of claims 1-5.
