Similar vulnerability detection method based on tree attention and weighted graph matching
Through the tree attention and weighted graph matching method, the semantic and syntax features of the code are extracted, and the problem of poor vulnerability detection in the existing technology is solved, and similar vulnerability detection with high accuracy is achieved.
Patent Information
- Application Number
- CN202211678532.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-26
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2042-12-26
AI Technical Summary
Existing similar vulnerability detection methods based on hash functions are difficult to represent the deep semantic characteristics of vulnerabilities, resulting in poor detection of vulnerabilities with similar semantics in software, and it is difficult to distinguish the nuances of vulnerabilities and patches, and there is interference information interference.
Using similar vulnerability detection methods based on tree attention and weighted graph matching, code attribute graphs are generated through static analysis tools, and semantic and syntax information of statement nodes is extracted using CodeBERT model and tree attention mechanism, combining graph neural network and weighted graph matching Methods calculate code similarity and build a similar vulnerability detection model.
Effectively capture the syntactic and semantic features of the code, improve the accuracy of similar vulnerability detection, can identify nuances and ignore irrelevant information, reduce interference, and improve the ability to distinguish vulnerabilities and patches.
Smart Images

Figure CN115859307B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a software similar vulnerability detection method, and in particular to a similar vulnerability detection method based on tree-type attention and weighted graph matching. Background Art
[0002] In recent years, with the rapid increase in software scale and complexity, code reuse has become more and more frequent, resulting in an increasing number of similar vulnerabilities in software. Detecting similar vulnerabilities in software is becoming an important issue in the field of software security. Using an automated method to identify the vulnerability being processed as a vulnerability similar to a known vulnerability can not only prevent new vulnerabilities from being treated as unknown vulnerabilities, but also provide developers with a reference for fixing vulnerabilities and speed up the efficiency of software vulnerability repair. At present, similar vulnerability detection methods based on code similarity mainly determine similar vulnerabilities by comparing the similarity between the code to be tested and the known vulnerability code, and usually use a vulnerability signature based on a hash function to directly calculate the similarity. However, the existing vulnerability signature or fingerprint method based on a hash function cannot represent the deep semantic features related to the vulnerability, and is difficult to apply to semantically similar vulnerability detection in software.
[0003] In the similar vulnerability detection task, there are the following challenges: 1) There are only small differences between the vulnerability instance and the patch of the code, and it is difficult to distinguish whether the target function is similar to the vulnerability or the patch. 2) In the vulnerability code of real projects, there are often a large number of statements that are not related to the vulnerability. Therefore, an effective similar vulnerability detection method should have the following two characteristics: 1) It can identify the subtle differences between the vulnerability code and the patched code in the real project. 2) It can focus on the key information and difference information in the vulnerability and patch, while ignoring or even eliminating the interference caused by redundant information not related to the vulnerability. Summary of the invention
[0004] The purpose of the present invention is to provide a similar vulnerability detection method based on tree-type attention and weighted graph matching, which mainly consists of three parts: (1) a slice composite graph (SCG) that can represent the syntax and semantic information of the code as a code representation; (2) a sentence node embedding network based on the CodeBERT model and the tree-type attention mechanism to learn the sentence feature vector representation; (3) a graph matching model composed of a graph neural network and a weighted graph matching method as a method for calculating the similarity of two vulnerability codes. The present invention can capture the grammatical and semantic features related to the vulnerability in the code, and effectively use the patch information to distinguish between vulnerabilities and patch functions with only slight differences, thereby improving the accuracy of similar vulnerability detection.
[0005] The objective of the present invention is achieved through the following technical solutions:
[0006] A similar vulnerability detection method based on tree attention and weighted graph matching includes the following steps:
[0007] Step 1: Use a static parsing tool to parse the source code and generate a Code Property Graph (CPG), which combines the Abstract Syntax Tree (AST), Control Flow Graph (CFG), and Program Dependency Graph (PDG) into one data structure;
[0008] Step 2: Generate Function Composite Graph (FCG), Vulnerability Slice Composite Graph (vSCG), and Patch Slice Composite Graph (pSCG) for the target function, vulnerability function, and patch function respectively;
[0009] Step 3: Using the sentence node embedding network composed of the CodeBERT model and the tree-type attention mechanism, the semantic and grammatical information of the FCG, vSCG, and pSCG sentence nodes are extracted respectively, and the initial sentence node embedding vector composed of the semantic feature vector and the grammatical feature vector is generated;
[0010] Step 4: Use graph neural networks to learn representations of FCG, vSCG, and pSCG respectively, and learn the vector representation of each node;
[0011] Step 5: Use the weighted graph matching method to calculate the similarity between FCG and vSCG, and between FCG and pSCG;
[0012] Step 6: Using the label information and the similarity calculated in step 5, calculate the triplet loss function, and adjust the parameters of the sentence node embedding network and the graph matching model composed of the graph neural network and the weighted graph matching method according to the error back propagation until the response of the sentence node embedding network and the graph matching model to the input reaches the predetermined target range. The training is completed and a similar vulnerability detection model composed of the sentence node embedding network and the graph matching model is obtained;
[0013] Step 7: Use the trained similar vulnerability detection model to detect similar vulnerabilities in the code.
[0014] Compared with the prior art, the present invention has the following advantages:
[0015] (1) The new code intermediate representation method SCG proposed in the present invention can represent vulnerability code and patch code as a composite graph data structure containing grammatical and semantic information, and eliminate redundant information irrelevant to vulnerabilities and patches as much as possible. Compared with other code intermediate representation methods, the SCG proposed in the present invention not only retains the rich grammatical and semantic information related to vulnerabilities and patches, but also eliminates information irrelevant to vulnerabilities as much as possible, which can reduce its interference with the learning process.
[0016] (2) This paper proposes a tree-based attention mechanism and combines it with the CodeBERT model to learn the feature vector representation of statement nodes in SCG. When detecting similar vulnerabilities, the tree-based attention mechanism can pay more attention to the grammatical subtrees in the AST (Abstract Syntax Tree) that are related to the vulnerabilities, so that the features extracted from these key grammatical structures have a greater impact on the matching results of SCG.
[0017] (3) The present invention uses a weighted graph matching method to detect similar vulnerabilities in software. Different from the currently popular methods of detecting similar vulnerabilities based on vulnerability signatures or using deep learning models to first learn code representations and then calculate cosine similarity, the present invention uses a weighted graph matching method to describe the vulnerability correlation of nodes by setting different weights. It can achieve effective matching between SCGs while avoiding the loss of code syntax and semantic information, thereby improving the accuracy of similar vulnerability detection.
[0018] (4) The present invention proposes a sentence node embedding network composed of CodeBERT and a tree-type attention mechanism for SCG, which can efficiently and accurately capture the grammatical and semantic features related to vulnerabilities in sentences.
[0019] (5) The present invention proposes a graph matching model composed of a graph neural network and a weighted graph matching mechanism. The model uses a graph neural network to learn the structural and semantic information in the code, and uses a weighted graph matching method to calculate the structural and semantic similarity between the code to be tested and the vulnerability and patch codes. When calculating the structural and semantic similarity of the code, the patch information, the difference between the vulnerability code and the patch code, and the importance of different statement nodes in the code are fully considered, thereby improving the accuracy of similar vulnerability detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 It is a flow chart of a similar vulnerability detection method based on tree-type attention and weighted graph matching of the present invention.
[0021] Figure 2 This is an example of a similar vulnerability code.
[0022] Figure 3These are the FCG, vSCG, and pSCG corresponding to similar vulnerability instances.
[0023] Figure 4 It is a tree-type attention process. DETAILED DESCRIPTION
[0024] The technical solution of the present invention is further described below in conjunction with the accompanying drawings, but is not limited thereto. Any modification or equivalent replacement of the technical solution of the present invention without departing from the spirit and scope of the technical solution of the present invention should be included in the protection scope of the present invention.
[0025] The present invention provides a similar vulnerability detection method based on tree-type attention and weighted graph matching. First, FCG, vSCG, and pSCG are generated for the target function, vulnerability function, and patch function, respectively. Secondly, the semantic and grammatical information of the FCG, vSCG, and pSCG statement nodes are extracted respectively using a sentence node embedding network composed of a CodeBERT model and a tree-type attention mechanism. Next, a graph matching model composed of a graph neural network and a weighted graph matching method is used to learn and calculate the similarity between FCG and vSCG and between FCG and pSCG, and then the calculated triplet loss function is used to adjust the network parameters and train the detection model. Finally, the trained model is used to detect similar vulnerabilities in the software. Figure 1 As shown, the specific steps include:
[0026] Step 1: Use a static parsing tool to parse the source code and generate a CPG. Since the graph combines AST, CFG, and PDG into one data structure, AST and PDG can be extracted for each function from CPG.
[0027] Step 2: Generate FCG, vSCG, and pSCG for the target function, vulnerability function, and patch function respectively. The specific steps are as follows:
[0028] Step 21: For the target function to be tested, first extract its corresponding PDG, then expand the AST corresponding to the statement to each node of the PDG, and finally form the FCG.
[0029] Step 22: For the vulnerable function that needs to be matched with the target function, first find the deleted statements by analyzing the vulnerability and the patch (i.e., the diff file), regard the deleted statements as vulnerable statements, use the vulnerable statements as the slicing criteria, use the program slicing technology to find the slicing nodes in the PDG, and then connect these nodes through data dependency and control dependency, and then use each node in the slice as the root node to expand its corresponding AST to form a slice composite graph of the vulnerable function, recorded as vSCG; if there are only added statements but no deleted statements in the diff file, generate vSCG by extracting the same nodes and edges from the PDG of the vulnerable function and the pSCG of its corresponding patch function.
[0030] Step 23: The process of generating pSCG is similar to that of vSCG. For the patch function that needs to be matched with the target function, the added statements are found by analyzing the vulnerability and the patch (i.e., the diff file). The added statements are regarded as patch statements, and the patch statements are used as slicing criteria. The program slicing technology is used to find the slice nodes in PDG, and then these nodes are connected through data dependency and control dependency. Each node in the slice is used as the root node, and its corresponding AST is expanded to form a slice composite graph of the patch function, which is recorded as pSCG. If there are only deleted statements but no added statements in the diff file, pSCG is generated by extracting the same nodes and edges from the PDG of the vulnerable function and the vSCG generated in step 22.
[0031] Step 3: Use the sentence node embedding network composed of the CodeBERT model and the tree-type attention mechanism to extract the semantic and grammatical information of the FCG, vSCG, and pSCG sentence nodes, respectively, and generate the initial sentence node embedding vector composed of the semantic feature vector and the grammatical feature vector. The specific steps are as follows:
[0032] Step 31: For any statement node v in FCG, vSCG, or pSCG i , use the CodeBERT model trained and fine-tuned with vulnerability data to learn the dependencies between tokens in the sentence to obtain node v i The corresponding semantic feature vector representation is recorded as
[0033] Step 32: Generate node v using tree attention mechanism i The grammatical feature vector of is as follows:
[0034] Step 321: Obtain a node sequence by depth-first traversal of the AST;
[0035] Step 322: Use the word2vec model to obtain the digital vectors of all nodes, calculate the position code of the node and add it to the digital vector of the node;
[0036] Step 323: extract the non-leaf node with multiple child nodes in the AST and use it as the root node of the subtree;
[0037] Step 324: Use the subtree mask to hide the sibling nodes and parent node of the subtree root node, only calculate the attention value between it and itself and its child nodes, and add them up to obtain the hidden vector representation of the subtree;
[0038] Step 325: Use the soft attention mechanism to find out the importance of all AST subtrees, and then use these importances as weights for weighted summation to obtain the grammatical feature vector representation of the sentence.
[0039] The positional coding used in the present invention has three main parts: layer-order coding, parent coding and hierarchical coding. The layer-order coding numbers the current node according to the layer-order traversal order of the AST, the parent coding records the number of the parent node of the current node, and the hierarchical coding records the depth of the current node. After obtaining the above initial coding, it is sent to a learnable fully connected network to obtain the final positional coding.
[0040] The subtree mask used in the present invention is a method for shielding the brother nodes and the parent node of the current node, and is used to realize parallel computing and improve computing efficiency.
[0041] The specific calculation formula of the tree-type attention mechanism is as follows:
[0042] Q i =B i W Q ,K i =T i W K ,V i =T i W v
[0043]
[0044]
[0045]
[0046] Among them, Q i , K i 、V i Respectively represent the node v in the tree-type attention mechanism i The corresponding query vector, target vector and value vector; T i =[t i,1 ,t i,2,…,t i,m ]∈R m×d The node v i The corresponding syntax tree T i The node embedding matrix of T i The number of tree nodes contained in t i,m Indicates T i The feature vector corresponding to the mth tree node in B, d represents the vector dimension of the node; i =[b i,1 ,b i,2 ,…,b i,n ]∈R n×d Indicates T i The embedding matrix of the corresponding subtree root node, n is T i The number of neutron tree root nodes, b i,n Indicates T i The feature vector corresponding to the root node of the nth subtree in W Q , W K , W V is the learnable weight matrix; M i Then T i The corresponding subtree mask, S i =[s i,1 ,s i,2 ,…,s i,n ] T is the vector matrix of all subtrees obtained, where s i,j Represents the syntax tree T i The embedding vector of the jth subtree in ; u is a learnable random initialization vector, a i,j Indicates i,j The corresponding attention score, For statement node v i The final grammatical feature vector.
[0047] Step 33: Concatenate the semantic feature vector and the grammatical feature vector and send them into a fully connected network to map them to the same semantic space, and finally obtain the initial embedding vector representation of the node.
[0048] Step 4: Use graph neural networks to learn representations of FCG, vSCG, and pSCG respectively, and learn the vector representation of each node. The specific steps are as follows:
[0049] Step 41: For Figure G G =(V G ,E G ,X G ,D G ) any node v i ∈V G, based on the information of neighbor nodes, the GNN model is used to update the state of the current node. The specific calculation formula is as follows:
[0050]
[0051]
[0052] Among them, h i and i Indicates its corresponding hidden vector representation and output vector representation; and The nodes v are obtained after l-1 and l layers of GNN respectively. i The hidden vector representation of is node v j The hidden vector representation of v j Yes i Neighbor, v j ∈N(v i ), there is a path from v j to v i The edge of the eigenvector is d ji ,N(v i ) indicates v i The set of neighbor nodes; f is the propagation function of the GNN model, which is used to collect information about neighboring nodes to update the state of the current node; z is the output function, which is used to calculate the final output feature vector of the node;
[0053] Step 42: Use the GNN model to obtain the output graph data structure of FCG, vSCG and pSCG: Among them, V F 、V V and V P for and The set of nodes contained in E F 、E V and E P for and The set of edges contained in O F , O V and O P for and The output feature vector set of the middle node, D F , D V and D P for and The set of eigenvectors for the edges.
[0054] Step 5: Use the weighted graph matching method, i.e., the improved bipartite graph matching method, to calculate the similarity between FCG and vSCG, and between FCG and pSCG. The specific steps are as follows:
[0055] Step 51: and Combining into a bipartite graph Where V FV =V F ∪V V , Likewise, and Also merged into a bipartite graph Where V FP =V F ∪V P ,
[0056] Step 52: For two bipartite graphs and The nodes in the graph are matched pairwise, and the minimum matching cost is calculated based on the distance between the two matching nodes as the similarity of the two bipartite graphs. Specifically, the distance between the two nodes is calculated according to the following formula: and The matching cost between nodes and The matching cost between:
[0057]
[0058]
[0059] in, Is a node and The matching cost between Is a node and The matching cost between Representation Node and The distance between Representation Node and The distance between and Represents the nodes in vSCG data dependency weight, control dependency weight and comprehensive weight; and It means that the node in pSCG data dependency weight, control dependency weight and comprehensive weight; and For Node and The corresponding output feature vector.
[0060] The weights are calculated as follows:
[0061] For vSCG, we use the vulnerability node as the root node and its data dependency weight is Suppose there is a node Assuming that the node can be connected to the root node through at least k data dependency edges, then The data dependency weight is Among them, L α ∈(0,1) is a hyperparameter, which indicates the decay rate of data dependency weight. The control dependency weight of the root node is Assuming that the node can be connected to the root node through at least k control dependency edges, then The control dependency weight is Among them, L β ∈(0,1) is a hyperparameter, which indicates the decay rate of the control dependency weight. The comprehensive weight of the root node is Assuming that the node can be connected to the root node through at least k edges, then The comprehensive weight of Among them, L γ ∈(0,1) is a hyperparameter, which indicates the decay rate of the comprehensive weight.
[0062] The weights of pSCG are calculated in a similar way, but we do not set the weights of FCG here because the locations of the holes in the objective function are unknown.
[0063] Step 53: Standardize the matching cost. The specific steps are as follows: Create an empty graph φ with the same structure for each composite graph, and set the feature vector of its nodes to 0 vector. By comparing with the empty graph, the standardized matching cost C is obtained. The specific calculation formula is as follows:
[0064]
[0065]
[0066] in, and They represent the matching costs between FCG and vSCG, and between FCG and pSCG, respectively; For Node and The matching cost between For Node and The matching cost between For Node and the matching cost between 0 vector nodes, For Node and the matching cost between 0 vector nodes, For Node and 0 vectors of matching costs between nodes.
[0067] Step 54: Calculate the similarity of the graph using the matching cost, and the calculation formula is as follows:
[0068]
[0069]
[0070] in, and Represent the graph similarity between FCG and vSCG, and between FCG and pSCG, respectively.
[0071] Step 6: Using the label information and the similarity calculated in step 5, calculate the triplet loss function, and adjust the parameters of the sentence node embedding network and the graph matching model composed of the graph neural network and the weighted matching method according to the error back propagation until the response of the sentence node embedding network and the graph matching model to the input reaches the predetermined target range. The training is completed and a similar vulnerability detection model composed of the sentence node embedding network and the graph matching model is obtained, where:
[0072] The specific calculation formula of the triplet loss function is as follows:
[0073]
[0074] Among them, Loss is the triple loss; Sim represents the similarity between the benchmark sample and the positive sample, and Diff represents the similarity between the benchmark sample and the negative sample. In the similar vulnerability detection task, when the benchmark sample is a vulnerability code, the positive sample corresponds to the vulnerability code and the negative sample corresponds to the non-vulnerability code; when the benchmark sample is a non-vulnerability code, the positive sample corresponds to the non-vulnerability code and the negative sample corresponds to the vulnerability code. As a penalty term, it is used to improve the similarity between the benchmark sample and the positive sample. ∈, c and λ are all hyperparameters.
[0075] Step 7: Use the trained similar vulnerability detection model to detect similar vulnerabilities in the code. When the following two conditions are met at the same time, the target function is judged to be similar to the known vulnerability function:
[0076] (1) The similarity between the target function and the vulnerability function exceeds a certain threshold;
[0077] (2) The similarity between the target function and the vulnerability function exceeds its similarity with the patch function.
[0078] Example:
[0079] by Figure 2 Take the vulnerability code shown as an example, Figure 2 (a) is the code to be tested, Figure 2 (b) is a similar vulnerability code and its patch. Figure 2 (b) Line 7 in the code is the vulnerability statement, and line 8 is the patch statement.
[0080] For this example, we can first extract the FCG of the code to be tested, and then use the vulnerability statements and patch statements in the similar vulnerability code as the slicing criteria to extract the corresponding vSCG and pSCG, such as Figure 3 shown.
[0081] Then, the sentence node embedding network is used to learn the semantic and grammatical information in the FCG, vSCG and pSCG sentence nodes. Specifically, the CodeBERT model is first used to generate the corresponding semantic feature vector according to the token sequence in the sentence code. At the same time, the tree-type attention mechanism is used to extract the grammatical feature vector of the abstract syntax tree corresponding to the sentence node.
[0082] Figure 4 (a) indicates Figure 2 The AST of the information leakage vulnerability statement in the code, where the rounded rectangle nodes are statement nodes, the rectangular nodes are non-leaf nodes, and the ellipse nodes are leaf nodes. Figure 4 (b) represents the positional encoding corresponding to the AST shown in (a). The entire AST contains three subtrees, such as Figure 4 As shown in (c), the root nodes of the subtrees are gray nodes. The root nodes of these subtrees are selected if they contain two or more non-leaf nodes. For subtree 1, the attention value of the root node to itself and other nodes is calculated respectively, and then all nodes are weighted summed according to the attention value to obtain the hidden vector representation of subtree 1. The remaining subtrees can be obtained by masking the siblings and parent nodes of the subtree root node through the subtree mask. Then, the vector representations of the two remaining subtrees are also obtained through the above process. Finally, the vector representations of the three subtrees are weighted summed to obtain the final grammatical feature vector.
[0083] After concatenating the semantic feature vector and the grammatical feature vector, the initial feature vectors of the sentence nodes in FCG, vSCG, and pSCG can be obtained. The graph neural network is used to learn the hidden vector representation of the nodes in FCG, vSCG, and pSCG, and then the weighted graph matching method is used to calculate the similarity between FCG and vSCG, and between FCG and pSCG, which are 0.9623 and 0.9413, respectively. When the threshold is set to 0.8, since 0.9623>0.8, the judgment condition 1 is met; and since 0.9623>0.9413, the judgment condition 2 is met. Therefore, the code under test is judged to be a vulnerability similar to the known vulnerability function.
Claims
1. A similar vulnerability detection method based on tree-type attention and weighted graph matching, characterized in that The method comprises the following steps: Step 1: Use static parsing tools to parse the source code and generate CPG, which combines AST and PDG into one data structure; Step 2: Generate FCG, vSCG, and pSCG for the target function, vulnerability function, and patch function respectively. The specific steps are as follows: Step 21: For the target function to be tested, first extract its corresponding PDG, then expand the AST corresponding to the statement to each node of the PDG, and finally form the FCG; Step 22: For the vulnerable function that needs to be matched with the target function, first find the deleted statements by analyzing the vulnerability and diff files, regard the deleted statements as vulnerable statements, use the vulnerable statements as slicing criteria, use program slicing technology to find slicing nodes in PDG, and then connect these nodes through data dependency and control dependency, and then use each node in the slice as the root node to expand its corresponding AST to form a slicing composite graph of the vulnerable function, recorded as vSCG; if there are only added statements but no deleted statements in the diff file, generate vSCG by extracting the same nodes and edges from the PDG of the vulnerable function and the pSCG of its corresponding patch function; Step 23: For the patch function that needs to be matched with the target function, find the added statements by analyzing the vulnerability and diff files, regard the added statements as patch statements, use the patch statements as slicing criteria, use program slicing technology to find the slice nodes in PDG, and then connect these nodes through data dependency and control dependency, and then use each node in the slice as the root node, expand its corresponding AST, and form a slice composite graph of the patch function, recorded as pSCG; if there are only deleted statements but no added statements in the diff file, generate pSCG by extracting the same nodes and edges from the PDG of the vulnerable function and the vSCG generated in step 22; Step 3: Using the sentence node embedding network composed of the CodeBERT model and the tree-type attention mechanism, the semantic and grammatical information of the FCG, vSCG, and pSCG sentence nodes are extracted respectively, and the initial sentence node embedding vector composed of the semantic feature vector and the grammatical feature vector is generated; Step 4: Use graph neural networks to learn representations of FCG, vSCG, and pSCG respectively, and learn the vector representation of each node; Step 5: Use the weighted graph matching method to calculate the similarity between FCG and vSCG, and between FCG and pSCG; Step 6: Using the label information and the similarity calculated in step 5, calculate the triplet loss function, and adjust the parameters of the sentence node embedding network and the graph matching model composed of the graph neural network and the weighted graph matching method according to the error back propagation until the response of the sentence node embedding network and the graph matching model to the input reaches the predetermined target range. The training is completed and a similar vulnerability detection model composed of the sentence node embedding network and the graph matching model is obtained; Step 7: Use the trained similar vulnerability detection model to detect similar vulnerabilities in the code.
2. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 1 is characterized in that The specific steps of step 3 are as follows: Step 31: For any statement node v in FCG, vSCG, or pSCG i , use the CodeBERT model trained and fine-tuned with vulnerability data to learn the dependencies between tokens in the sentence to obtain the node v i The corresponding semantic feature vector representation is recorded as Step 32: Generate node v using tree attention mechanism i The grammatical feature vector of Step 33: Concatenate the semantic feature vector and the grammatical feature vector and send them into a fully connected network to map them to the same semantic space, and finally obtain the initial embedding vector representation of the node.
3. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 2 is characterized in that The specific steps of step 32 are as follows: Step 321: Obtain a node sequence by depth-first traversal of the AST; Step 322: Use the word2vec model to obtain the digital vectors of all nodes, calculate the position code of the node and add it to the digital vector of the node; Step 323: extract the non-leaf node with multiple child nodes in the AST and use it as the root node of the subtree; Step 324: Use the subtree mask to hide the sibling nodes and parent nodes of the subtree root node, only calculate the attention value of the subtree root node itself and its child nodes, and add the attention values of the subtree root node itself and its child nodes to obtain the hidden vector representation of the subtree; Step 325: Use the soft attention mechanism to find out the importance of all AST subtrees, and then use these importances as weights for weighted summation to obtain the grammatical feature vector representation of the sentence.
4. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 2 is characterized in that The specific calculation formula of the tree-type attention mechanism is as follows: Q i =B i W Q ,K i =T i W K ,V i =T i W V Among them, Q i , K i 、V i Respectively represent the node v in the tree-type attention mechanism i The corresponding query vector, target vector and value vector; T i =[t i,1 ,t i,2 ,…,t i,m ]∈R m×d The node v i The corresponding syntax tree T i The node embedding matrix of T i The number of tree nodes contained in t i,m Indicates T i The feature vector corresponding to the mth tree node in B, d represents the vector dimension of the node; i =[b i,1 ,b i,2 ,…,b i,n ]∈R n×d Indicates T i The embedding matrix of the corresponding subtree root node, n is T i The number of neutron tree root nodes, b i,n Indicates T i The feature vector corresponding to the root node of the nth subtree in W Q , W K , W V is the learnable weight matrix; M i Then T i The corresponding subtree mask, S i =[s i,1 ,s i,2 ,…,s i,n ] T is the vector matrix of all subtrees obtained, where s i,j Represents the syntax tree T i The embedding vector of the jth subtree in ; u is a learnable random initialization vector, a i,j Indicates i,j The corresponding attention score, For statement node v i The final grammatical feature vector.
5. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 1 is characterized in that The specific steps of step 4 are as follows: Step 41: For Figure G G =(V G ,E G ,X G ,D G ) any node v i ∈V G , based on the information of neighbor nodes, the GNN model is used to update the state of the current node. The specific calculation formula is as follows: Among them, h i and i Indicates its corresponding hidden vector representation and output vector representation; and The nodes v are obtained after l-1 and l layers of GNN respectively. i The hidden vector representation of is node v j The hidden vector representation of v j Yes i Neighbor, v j ∈N(v i ), there is a path from v j to v i The edge of the eigenvector is d ji ,N(v i ) indicates v i The set of neighbor nodes; f is the propagation function of the GNN model, which is used to collect information about neighboring nodes to update the state of the current node; z is the output function, which is used to calculate the final output feature vector of the node; Step 42: Use the GNN model to obtain the output graph data structure of FCG, vSCG and pSCG: Among them, V F 、V V and V P for and The set of nodes contained in E F 、E V and E P for and The set of edges contained in O F , O V and O P for and The output feature vector set of the middle node, D F , D V and D P for and The set of eigenvectors for the edges.
6. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 5 is characterized in that The specific steps of step 5 are as follows: Step 51: and Combining into a bipartite graph Where V FV =V F ∪V V , Will and Also merged into a bipartite graph Where V FP =V F ∪V P , Step 52: For two bipartite graphs and The nodes in are matched pairwise, and the minimum matching cost is calculated according to the distance between the two matching nodes as the similarity of the two bipartite graphs; Step 53: Normalize the matching cost. The specific steps are as follows: create an empty graph φ with the same structure for each composite graph, set the feature vector of its nodes to 0 vector, and obtain the standardized matching cost C by comparing with the empty graph; Step 54: Calculate the similarity of the graphs using the matching cost.
7. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 6 is characterized in that In step 52, two nodes are calculated according to the following formula: and The matching cost between nodes and The matching cost between: in, Is a node and The matching cost between Is a node and The matching cost between Representation Node and The distance between Representation Node and The distance between and Represents the nodes in vSCG data dependency weight, control dependency weight and comprehensive weight; and It means that the node in pSCG data dependency weight, control dependency weight and comprehensive weight; and For Node and The corresponding output feature vector; In step 53, the specific calculation formula of the matching cost C is as follows: in, and They represent the matching costs between FCG and vSCG, and between FCG and pSCG, respectively; For Node and The matching cost between For Node and The matching cost between For Node and the matching cost between 0 vector nodes, For Node and the matching cost between 0 vector nodes, For Node and the matching cost between 0-vector nodes; In step 54, the calculation formula of the graph similarity is as follows: in, and Represent the graph similarity between FCG and vSCG, and between FCG and pSCG, respectively.
8. The similar vulnerability detection method based on tree-type attention and weighted graph matching according to claim 1 is characterized in that In step 7, when the following two conditions are met at the same time, it is determined that the target function is similar to the known vulnerability function: (1) The similarity between the target function and the vulnerability function exceeds a certain threshold; (2) The similarity between the target function and the vulnerability function exceeds its similarity with the patch function.
