Vulnerability detection method based on cross-modal feature enhancement of source code and assembly code

By adopting the cross-modal feature enhancement method in software vulnerability detection, combined with the statement alignment and cross-attention mechanism of high-level language and assembly code, the problems of coarse granularity and difficulty in feature-level fusion in the existing technology are solved, and the efficiency of vulnerability detection is improved. Accuracy and granularity.

CN115577362BActive Publication Date: 2024-09-27HARBIN INST OF TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211105496.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-09
Publication Date
2024-09-27
Estimated Expiration
2042-09-09

AI Technical Summary

Technical Problem

Existing software vulnerability detection methods based on deep learning have problems such as coarse granularity at the high-level language source code and assembly code levels, inability to achieve feature-level fusion and location detection, and difficulty in aligning statement-level features of high-level languages ​​and assembly codes.

Method used

Using a cross-modal feature enhancement method based on source code and assembly code, by extracting syntax and semantic features of control dependence and data dependence, using cross-attention mechanisms and different deep learning networks for feature-level fusion, the statements of high-level language and assembly code are realized Alignment and features complement each other.

Benefits of technology

It improves the accuracy of software vulnerability detection, can more accurately capture vulnerability characteristics in the code, and achieves feature-level fusion and decision-level accurate detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115577362B
    Figure CN115577362B
Patent Text Reader

Abstract

The present invention discloses a vulnerability detection method based on cross-modal feature enhancement of source code and assembly code, the method extracts grammatical semantic features related to control dependency and data dependency from source code, extracts grammatical semantic features related to memory operation from assembly code, and then uses assembly code with high-level language program source code aligned with its statements to input into a cross-modal feature enhanced and fused bimodal representation learning model for software vulnerability detection. The method can perform representation learning on two program modes, high-level language source code and assembly code, utilize the statement alignment relationship between source code and assembly code, extract vulnerability-related semantic features in source code mode and assembly code mode respectively, and use different deep learning networks and cross-attention mechanisms to learn the semantic relevance between the two, and make full use of the feature complementarity of the two modal programs to perform feature-level fusion, thereby improving the accuracy of software vulnerability detection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of software vulnerability detection, and relates to a method for automatically detecting software vulnerabilities, and specifically to a software vulnerability detection method based on cross-modal feature enhancement of source code and assembly code. Background Art

[0002] Software vulnerabilities (also known as vulnerabilities) refer to defects in software that may be exploited by criminals during its life cycle. Once exploited, they will lead to serious hazards such as privacy leakage, illegal authorization, and extortion. Software vulnerability detection technology is an important method and means to reduce software security vulnerabilities and reduce software security risks. Program representation learning is the basis and key to software vulnerability detection. The process of using deep learning technology to learn the grammatical and semantic feature representation of code from the intermediate representation of the program is called code representation learning.

[0003] Currently, software vulnerability detection based on deep learning is either performed at the source code level written in a high-level language or at the assembly code level to learn the program representation and perform software vulnerability detection.

[0004] At the high-level language program level, most vulnerability detection methods based on deep learning use language models, treat the code as natural language text for word embedding, and then use deep neural networks (such as LSTM and GRU) to learn the representation of the code, automatically extract vulnerability features from the code, and send it to the classifier for vulnerability detection. At the assembly code level, most use RNN models to learn the representation of function-level assembly code, extract vulnerability features from the assembly code, and then send the hidden vector representation output by the model to the classifier for vulnerability detection. The assembly code obtained from the high-level language source code can be divided into forward and reverse. The reverse method is to use a decompilation tool or a computer simulator to capture the assembly instructions of the executable program in the order of execution. However, the compiler optimizes the program when compiling the program and deletes the code that is not covered by the execution. Therefore, the assembly code obtained by reverse engineering can only obtain the calling context tree (CCT) of the program, which loses the control flow information. Therefore, this method is suitable for vulnerability detection scenarios where the source code cannot be obtained. The forward method is to generate the complete program assembly code by the compiler when the program is compiled. The existing methods for vulnerability detection of forward generated assembly code do not consider the statement-level alignment of assembly code and source code, so when detecting software vulnerabilities, only function-level software vulnerability detection can be achieved. The detection granularity is coarse and it is impossible to detect and locate the source code location where the vulnerability occurs in the source code for decision-level fusion. It is difficult to achieve feature-level fusion, and only function-level granularity vulnerability detection can be achieved. For example, although the article "AVulnerability Detection System Based on Fusion of Assembly Code and Source Code" uses three modalities: high-level language source code, assembly code, and source code mixed with assembly code, it has the following shortcomings: First, only function-level vulnerability detection is achieved, and the detection granularity is coarse. Second, the three modalities use the same program representation learning model, and no deep learning model suitable for different modalities is designed according to the characteristics of different modalities to extract their complementary and unique vulnerability features. Third, the voting layer is used to perform decision-level fusion on the code vulnerability detection results of the three modalities, and the fusion method is too simple.

[0005] Currently, no method has been found to perform feature-level fusion of high-level language code and assembly code and detect vulnerabilities at the code segment granularity. The difficulty in performing feature-level fusion of high-level language and assembly language codes is that it is difficult to perform statement-level feature alignment and bimodal feature enhancement of assembly code and high-level language code. Summary of the invention

[0006] The purpose of the present invention is to provide a vulnerability detection method based on cross-modal feature enhancement of source code and assembly code. The method can perform representation learning on two program modalities, namely, high-level language source code and assembly code, and utilize the statement alignment relationship between source code and assembly code to extract vulnerability-related semantic features in the source code modality and assembly code modality respectively. Different deep learning networks and cross-attention mechanisms are used to learn the semantic correlation between the two, and the feature complementarity of the two modal programs is fully utilized for feature-level fusion, thereby improving the accuracy of software vulnerability detection.

[0007] The objective of the present invention is achieved through the following technical solutions:

[0008] A vulnerability detection method based on cross-modal feature enhancement of source code and assembly code, extracting grammatical semantic features related to control dependency and data dependency from source code, extracting grammatical semantic features related to memory operation from assembly code, and then using the assembly code aligned with the source code of a high-level language program and its statements to input into a cross-modal feature enhancement and fusion bimodal representation learning model composed of a source code representation learning network, an assembly code representation learning network and a cross-attention layer to perform software vulnerability detection, specifically comprising the following steps:

[0009] Step 1: Convert the high-level language program source code into assembly code with statement alignment and source code variable annotations;

[0010] Step 2: Use static analysis tools to generate an abstract syntax tree (AST) and a program dependency graph (PDG) from the source code of the high-level language program;

[0011] Step 3: Generate source code and assembly code slice code segments according to the source code slice criteria;

[0012] Step 4: Generate source code and assembly code slice code segments according to the assembly code slice criteria;

[0013] Step 5: Merge the source code slice code segment sets generated by step 3 and step 4, and mark the slice code segments containing vulnerability statements as "with vulnerability", and mark the remaining slice code segments as "without vulnerability", thereby forming a training data set of source code slice code segments; similarly, merge the assembly code slice code segment sets generated by step 3 and step 4, and mark the slice code segments containing vulnerability statements as "with vulnerability", and mark the remaining slice code segments as "without vulnerability", thereby forming a training data set of assembly code slice code segments;

[0014] Step 6: Use word2vec to embed the tokens in the source code slice code segment, obtain the initial vector representation of each statement in the source code slice code segment, send it to the sentence encoding network composed of CNN, obtain the hidden vector representation of each statement, and then send the hidden vector representation of each statement to the program encoding network composed of bidirectional GRU (Gate Recurrent Unit) to obtain the hidden vector representation of the source code slice code segment; similarly, use word2vec to embed the tokens in the assembly code slice code segment, obtain the initial vector representation of each statement in the assembly code slice code segment, and send it to the assembly code representation learning network composed of two layers of bidirectional GRU to obtain the hidden vector representation of the assembly code slice code segment;

[0015] Step 7: The hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment obtained in step 6 are enhanced using the cross-attention mechanism to generate a more accurate slice code segment vector representation, and then the hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment are weightedly aggregated and spliced ​​to obtain a hidden vector representation with cross-modal feature enhancement and fusion;

[0016] Step 8: The vector representation obtained in step 7 after cross-modal cross-attention feature fusion and enhancement is sent to the classifier composed of a fully connected layer (FCN) and Softmax, and the cross entropy loss (Cross Entropy Loss) is calculated according to the output result of the classifier and the actual label of the code segment. Back-propagation updates the parameters of the cross-modal feature enhancement and fusion bimodal representation learning model composed of the source code representation learning network, the assembly code representation learning network and the cross-attention layer until the training of the cross-modal feature enhancement and fusion bimodal representation learning model is completed;

[0017] Step 9: Use the trained cross-modal feature enhancement and fusion bimodal representation learning model and classifier network to detect vulnerabilities in the code to be tested.

[0018] Compared with the prior art, the present invention has the following advantages:

[0019] (1) Memory operation instructions are introduced as slicing criteria for the first time. The sliced ​​code vulnerability dataset generated based on the memory operation slicing technology finds the memory operation instructions related to the source code variables. The slices extracted for data flow related vulnerabilities are more accurate, making it more helpful for the code representation learning model to learn vulnerability related statements.

[0020] (2) Code compilation preprocessing information and debug trace information are used to achieve cross-modal code element alignment, generate assembly code with source code variable annotations, narrow the lexical gap between assembly code and source code, and assist the model to quickly integrate cross-modal semantics.

[0021] (3) Source code-based vulnerability detection can only focus on the data flow between variables, but it is difficult to capture memory-level data flow information. Assembly code can better capture vulnerability features related to data flow such as memory operations. The combined use of high-level language source code and assembly code data can complement each other's advantages and more comprehensively capture the vulnerability features of the code, thereby improving the accuracy of model detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 Schematic diagram of the overall process of the vulnerability detection method of the present invention.

[0023] Figure 2 Example of alignment of bimodal code statements.

[0024] Figure 3 A schematic diagram of generating bimodal code slicing code snippets according to source code slicing criteria.

[0025] Figure 4 A schematic diagram of generating a bimodal code slicing code segment according to an assembly code slicing criterion.

[0026] Figure 5 The process of enhancing vulnerability detection for cross-modal features. DETAILED DESCRIPTION

[0027] 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.

[0028] The present invention provides a vulnerability detection method based on cross-modal feature enhancement of source code and assembly code. First, the high-level programming language code to be detected is converted into statement-aligned assembly code to obtain statement-level aligned bimodal program code (such as Figure 2 As shown). Slice the high-level language program source code according to the source code slicing rules, and find the corresponding assembly code slicing code segment according to the obtained source code slicing code segment (as shown in Figure 3 Then, according to the assembly code with source code variable annotations, the source code segments corresponding to the memory operation instructions related to the source code variables are found, added to the source code slice code segment dataset, and the complete assembly code is found according to the statements in these source code segments (such as Figure 4As shown in the figure, the code is added to the slice code segment dataset of the assembly code. Then, according to the different statement features of the two modalities, different deep neural networks are designed to represent the two modal program slice code segments and learn them. The corresponding hidden vector representation is obtained, and the cross attention network is used for feature enhancement. The bimodal vector is then spliced ​​after attention weighted aggregation to complete feature fusion. Finally, the fused features are sent to the classifier composed of the fully connected layer (FCN) and Softmax to determine whether the test program contains vulnerabilities.

[0029] like Figure 1 As shown, the specific steps of the method are as follows:

[0030] Step 1: Convert the high-level language program source code into statement-aligned assembly code with source code variable annotations. The specific steps are as follows:

[0031] Step 1.1: Use the code compiler to debug the source code step by step, generate assembly code by statement, and align the output source code and assembly code by statement;

[0032] Step 1.2: Use the code compiler command line compilation method to generate assembly code with source code variable annotations;

[0033] Step 1.3: Fill the assembly code comments into the corresponding source code statements generated in step 1.1.

[0034] Step 2: Use static analysis tools to generate an abstract syntax tree (AST) and a program dependency graph (PDG) from the high-level language program source code.

[0035] Step 3: Generate source code and assembly code slice code segments according to the source code slice criteria. The specific steps for generating assembly code slices are as follows:

[0036] Step 3.1: According to the slicing criteria of the source code, extract the vulnerability candidate key points of the source code, traverse the program dependency graph obtained in step 2 forward and backward, obtain a set of bidirectional slicing statements, and generate a slicing code segment in the form of a high-level language;

[0037] Step 3.2: According to the statement correspondence between the assembly code and the high-level language source code obtained in step 1, find the assembly code block corresponding to each statement in the source code slice code segment, thereby obtaining the slice code segment in the form of assembly language.

[0038] Step 4: Generate source code and assembly code slice code segments according to the assembly code slice criteria. The specific steps are as follows:

[0039] Step 4.1: Extract memory operation instructions related to source code variables based on the statement alignment results of the source code and assembly code generated in step 1 and the source code variable annotations of the assembly code;

[0040] Step 4.2: Find the source code statement corresponding to the memory operation instruction as the slice code segment of the source code;

[0041] Step 4.3: According to the statements in the slice code segment generated in step 4.2, find the corresponding assembly instruction sequence as the slice code segment of the assembly code.

[0042] Step 5: Merge the same-modal datasets generated in step 3 and step 4 and mark the vulnerabilities to obtain the training datasets of source code slice code segments and assembly code slice code segments. The specific steps are as follows:

[0043] Step 5.1: merge the source code slice code segment sets generated in step 3 and step 4, and mark the slice code segments containing vulnerability statements as "with vulnerability", and mark the remaining slice code segments as "without vulnerability", thereby forming a training data set of source code slice code segments;

[0044] Step 5.2: Merge the assembly code slice code segment sets generated in step 3 and step 4, and mark the slice code segments containing the vulnerability statements as "with vulnerability", and mark the remaining slice code segments as "without vulnerability", thereby forming a training data set of assembly code slice code segments.

[0045] Step 6: Use word2vec to embed the tokens in the source code slice code segment, obtain the initial vector representation of each statement in the source code slice code segment, send it to the sentence encoding network composed of CNN, obtain the hidden vector representation of each statement, and then send the hidden vector representation of each statement to the program encoding network composed of bidirectional GRU (Gate Recurrent Unit) to obtain the hidden vector representation of the source code slice code segment; similarly, use word2vec to embed the tokens in the assembly code slice code segment, obtain the initial vector representation of each statement in the assembly code slice code segment, and send it to the assembly code representation learning network composed of two layers of bidirectional GRU to obtain the hidden vector representation of the assembly code slice code segment. The specific steps are as follows:

[0046] Step 6.1: Perform cross-modal joint word embedding training based on the multimodal corpus consisting of source code and assembly code, and represent the tokens in the source code and assembly code slices as word embeddings.

[0047] Step 6.2: Token feature extraction stage. The importance of different tokens is distinguished through the self-attention network. Given the token word embedding sequence T corresponding to the sentence, T is sent to the self-attention layer to generate the hidden vector representation of the token sequence. The specific calculation formula is as follows:

[0048] Q=W Q ·T;

[0049] K=W K ·T;

[0050] V=W V ·T;

[0051]

[0052] Among them, Q is the query sequence, (K, V) is the key-value pair sequence, and W Q , W K , W V is the learnable weight, H is the token hidden vector sequence, Attention() is the self-attention method, d k is the dimension of Q.

[0053] Step 6.3: Sentence feature extraction stage. For the token hidden vector sequence H obtained in step 6.2, first use multiple convolution layers with different convolution kernel sizes to capture local semantic features of different scales in the source code sentence, and then pass through the maximum pooling layer to concatenate the pooled feature vectors to obtain the vector representation of the source code sentence. The specific calculation formula is as follows:

[0054] H S1 =CNN k=4 (H);

[0055] H S2 =CNN k=5 (H);

[0056] H S3 =CNN k=6 (H);

[0057] H P1 =MaxPooling(H S1 );

[0058] H P2 =MaxPooling(H S2 );

[0059] H P3 =MaxPooling(H S3 );

[0060] s=Concat(H P1 ,H P2 ,H P3 );

[0061] Among them, CNN is a one-dimensional convolutional neural network, k is the convolution kernel size, HS1 , H S2 , H S3 They are the hidden vector representations obtained after the convolutional neural network with convolution kernel sizes of 4, 5, and 6, respectively. MaxPooling() is the maximum pooling layer, H P1 , H P2 , H P3 H S1 , H S2 , H S3 The result obtained after the maximum pooling layer, Concat() refers to the concatenation operation on the feature dimension, and s is the final vector representation of the source code sentence after concatenation.

[0062] Step 6.4: Code segment feature extraction stage. For source code program slice S = [s 1 ,s 2 ,...,s i ,...s n ], where s i is the final vector representation of the i-th source code statement obtained in step 6.3, n is the number of initial vector representations of source code statements in S, and the program slice representation composed of the final vector set of statements is sent to the program encoding network composed of bidirectional GRU to obtain the hidden representation P of the source code slice code segment C , the specific formula is as follows:

[0063] P C =BiGRU(S);

[0064] Among them, BiGRU() is a bidirectional GRU network, P C is the vector representation of source code program slices, and S is the vector representation of source code statements.

[0065] Step 6.5: The assembly code slice obtained in step 6.1 is represented as A = [a 1 ,a 2 ,...,a i ,...a m ], where a i is the initial vector representation of the i-th assembly statement, m is the number of initial vector representations of assembly statements in A, and it is sent to the assembly code representation learning model composed of two layers of bidirectional GRU to obtain the vector representation P of the assembly code slice code segment A , the formula is as follows:

[0066] P A =BiGRU(BiGRU(A));

[0067] Among them, BiGRU() is a bidirectional GRU network, P Ais the hidden representation of the assembly code program slice, and A is the representation of the assembly slice code segment.

[0068] Step 7: The hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment obtained in step 6 are enhanced using the cross-attention mechanism to generate a more accurate slice code segment vector representation. Then, the hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment are weightedly aggregated and spliced ​​to obtain a hidden vector representation with cross-modal feature enhancement and fusion. The specific steps are as follows:

[0069] Step 7.1: Slice the source code obtained in step 6 to obtain the hidden vector representation P C , and the hidden vector representation P of the assembly slice code segment A , use the cross-attention mechanism to perform feature enhancement, obtain the attention value of one modality for another modality, and obtain a feature-enhanced bimodal vector representation.

[0070] The cross-attention calculation formula of source code modality relative to assembly code modality is as follows:

[0071]

[0072]

[0073]

[0074]

[0075] The cross-attention calculation formula of the assembly code modality relative to the source code modality is as follows:

[0076]

[0077]

[0078]

[0079]

[0080] Among them, Q C is the source code modal query sequence, then (K A ,V A ) is the assembly code modal key-value pair sequence, Q A is the assembly code mode query sequence, at this time (K C ,V C ) is a sequence of source code modal key-value pairs, All are learnable weights, and after cross attention, the source code representation with feature enhancement is obtained in is the hidden vector representation of the feature-enhanced i-th source code statement, N is H C The number of hidden vector representations of source code statements after feature enhancement, and the assembly code representation after feature enhancement after cross attention in is the hidden vector representation of the jth assembly instruction after feature enhancement, M is H A The number of hidden vector representations of the assembly code statements after feature enhancement.

[0081] Step 7.2: After performing attention weighted aggregation on the bimodal code segment vector, concatenate it to obtain the fused feature vector. The specific steps are as follows: The hidden vector representation of the bimodal feature enhancement obtained at the previous moment is sent to the Attention weighted aggregation layer for attention weighted aggregation. i 、a j The weighted sum of the source code slice attention weighted aggregation vector representation x C and the vector representation x of the weighted aggregation of the encoded code slices A , and then spliced ​​x C 、x A Then we get the feature fusion vector x t ,in:

[0082] The source code modality attention weighted aggregation formula is as follows:

[0083]

[0084]

[0085]

[0086] The assembly code modal attention weighted aggregation formula is as follows:

[0087]

[0088]

[0089]

[0090] x t =Concat(x C ,x A );

[0091] in, for The vector representation after being mapped by the fully connected layer and the activation function tanh(), They are The transposed vector of are all trainable vectors.

[0092] Step 8: The vector representation obtained in step 7 after cross-modal cross-attention feature fusion and enhancement is sent to the classifier composed of a fully connected layer (FCN) and Softmax. The cross entropy loss is calculated based on the output of the classifier and the actual label of the code segment. The parameters of the bimodal representation learning model with cross-modal feature enhancement and fusion are updated by back propagation until the training of the bimodal representation learning model with cross-modal feature enhancement and fusion is completed. The specific steps are as follows:

[0093] Step 8.1: Fusion vector x obtained in step 7 t It means that it is sent to the fully connected layer and then passes through the Softmax function to obtain the predicted result vector y′.

[0094] Step 8.2: Use the label information of the sliced ​​code segment to calculate the cross entropy loss. The specific steps are as follows: Use the label value marked in step 5. When the label value is 0, it means no vulnerability, then initialize it to the vector [1,0]. When the label value is 1, it means there is a vulnerability, then initialize it to the vector [0,1]. Calculate the cross entropy loss with the prediction result vector given by the bimodal representation learning model of cross-modal feature enhancement and fusion. Finally, adjust the parameters of the bimodal representation learning model of cross-modal feature enhancement and fusion according to the error back propagation until the loss value no longer decreases, and the training ends. Among them:

[0095] The specific calculation formula of cross entropy loss is as follows:

[0096]

[0097] Among them, y′ is the model prediction result obtained in step 8.1, and y is the vulnerability label value of the slice code segment after vector initialization.

[0098] Step 9: Use the trained cross-modal feature enhancement and fusion bimodal representation learning model and classifier network to detect vulnerabilities in the test code.

[0099] Example:

[0100] like Figure 2As shown in the figure, it is an example of the alignment of source code and assembly code bimodal statements. The vulnerability of this program segment meets CWE (Common Weakness Enumeration) 121. The error type is Stack Overflow. This defect is caused by the fact that the C language does not have a built-in check mechanism to ensure that the data copied to the buffer can be larger than the size of the buffer. If the amount of data to be copied is larger than the buffer, it will overflow the buffer range. When the buffer overflows, other storage units will be overwritten, causing program crashes, etc., leading to unexpected consequences. Figure 2 It can be seen that the method in step 1 generates program code with aligned source code and assembly code statements, where each statement is preceded by the line number of the corresponding mode, and the source code line number is a number followed by a “:”. This type of line number is consistent with the source code mode program code line number. The assembly code line number is a pure number, and one source code statement generally corresponds to 0 to 20 assembly codes. In the assembly code, in addition to the regular assembly instruction part, there are also source code program variable comments corresponding to the registers, separated by “,” after the “#”. The statement where the source code vulnerability occurs is in line 6 of the code segment. The strncpy function is used to copy 80 characters of the str string to buf. Figure 2 Lines 18 to 23 show the assembly code mode of the source code vulnerability statement and the register comments related to the source code variables, achieving cross-modal statement alignment.

[0101] Depend on Figure 3 It can be seen that the process of finding the corresponding assembly code segment according to the C language source code slice code segment is an example of a slice of char*str under the slicing criterion for the example source code function parameter. After finding the function parameters that need to be tracked through the abstract syntax tree, and then finding the corresponding parameter dependency statement through the program dependency graph, after obtaining the source code modal program slice, the assembly code lines aligned with each source code statement are combined to obtain the assembly code modal program slice. In the source code slice, it can be seen that since there is no macro definition value of the third line of the C language code in the slice, the length of buf cannot be obtained according to the code statement in the slice. In the 9th line of the assembly instruction corresponding to the 4th line of the C language code, 40 is assigned to the tmp90 temporary register. The real buf length can be found in the assembly code mode. It can be seen that the vulnerability that cannot be detected in the source code mode can be found and detected in the assembly code mode.

[0102] Depend on Figure 4It can be seen that the memory operation slicing criteria also play their own advantages by slicing based on the parameter char*str. It can be seen that the comment part in the assembly code marks which registers have a cross-modal alignment relationship with the variable str. Str corresponds to the register [-56rbp]. The assembly code related to this variable is line 8 and line 14 respectively. The two instructions correspond to line 4 and line 6 of the C language source code respectively. These statements are combined into source code modal program slices. Then the assembly code lines aligned with each source code statement are combined to obtain the assembly code modal program slice. By Figure 4 It can be seen that in the bimodal program slicing based on str, the memory operation slicing criterion only slices one statement in the C language source code except the declaration, and this statement is the vulnerability statement, and the cause of the vulnerability can be seen in the assembly code mode. Using the memory operation slicing criterion, the slices extracted for vulnerabilities related to data flow are more accurate.

Claims

1. A vulnerability detection method based on cross-modal feature enhancement of source code and assembly code, characterized in that The method comprises the following steps: Step 1: Convert the high-level language program source code into assembly code with statement alignment and source code variable annotations; Step 2: Use static analysis tools to generate an abstract syntax tree and program dependency graph from the high-level language program source code; Step 3: Generate source code and assembly code slice code segments according to the source code slice criteria. The specific steps for generating assembly code slices are as follows: Step 3.1: According to the slicing criteria of the source code, extract the vulnerability candidate key points of the source code, traverse the program dependency graph obtained in step 2 forward and backward, obtain a set of bidirectional slicing statements, and generate a slicing code segment in the form of a high-level language; Step 3.2: According to the statement correspondence between the assembly code and the high-level language source code obtained in step 1, find the assembly code block corresponding to each statement in the source code slice code segment, so as to obtain the slice code segment in the assembly language form; Step 4: Generate source code and assembly code slice code segments according to the assembly code slice criteria. The specific steps are as follows: Step 4.1: Extract memory operation instructions related to source code variables based on the statement alignment results of the source code and assembly code generated in step 1 and the source code variable annotations of the assembly code; Step 4.2: Find the source code statement corresponding to the memory operation instruction as the slice code segment of the source code; Step 4.3: Find the corresponding assembly instruction sequence according to the statement in the slice code segment generated in step 4.2 as the slice code segment of the assembly code; Step 5: merge the source code slice code segment sets generated by step 3 and step 4, and mark the slice code segment containing the vulnerability statement as "with vulnerability", and mark the other slice code segments as "without vulnerability", so as to form a training data set of source code slice code segments; similarly, merge the assembly code slice code segment sets generated by step 3 and step 4, and mark the slice code segment containing the vulnerability statement as "with vulnerability", and mark the other slice code segments as "without vulnerability", so as to form a training data set of assembly code slice code segments; Step 6: Use word2vec to embed the tokens in the source code slice code segment, obtain the initial vector representation of each statement in the source code slice code segment, send it to the sentence encoding network composed of CNN, obtain the hidden vector representation of each statement, and then send the hidden vector representation of each statement to the program encoding network composed of bidirectional GRU to obtain the hidden vector representation of the source code slice code segment; Similarly, word2vec is used to embed the tokens in the assembly code slice code segment into words to obtain the initial vector representation of each statement in the assembly code slice code segment, which is then fed into the assembly code representation learning network consisting of two layers of bidirectional GRU to obtain the hidden vector representation of the assembly code slice code segment. Step 7: The hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment obtained in step 6 are enhanced using the cross-attention mechanism to generate a more accurate slice code segment vector representation. Then, the hidden vector representation of the source code slice code segment and the hidden vector representation of the assembly slice code segment are weightedly aggregated and spliced ​​to obtain a hidden vector representation with cross-modal feature enhancement and fusion. The specific steps are as follows: Step 7.1: Slice the source code obtained in step 6 to obtain the hidden vector representation P C , and the hidden vector representation P of the assembly slice code segment A , use the cross attention mechanism for feature enhancement, obtain the attention value of one modality for another modality, and obtain the feature-enhanced bimodal vector representation; The cross-attention calculation formula of source code modality relative to assembly code modality is as follows: The cross-attention calculation formula of the assembly code modality relative to the source code modality is as follows: Among them, Q C is the source code modal query sequence, then (K A ,V A ) is the assembly code modal key-value pair sequence, Q A is the assembly code mode query sequence, at this time (K C ,V C ) is a sequence of source code modal key-value pairs, All are learnable weights, and after cross attention, the source code representation with feature enhancement is obtained in is the hidden vector representation of the feature-enhanced i-th source code statement, N is H C The number of hidden vector representations of source code statements after feature enhancement, and the assembly code representation after feature enhancement after cross attention in is the hidden vector representation of the jth assembly instruction after feature enhancement, M is H A The number of hidden vector representations of the assembly code statements after feature enhancement in ; Step 7.2: After performing attention weighted aggregation on the bimodal code segment vector, concatenate it to obtain the fused feature vector. The specific steps are as follows: The hidden vector representation of the bimodal feature enhancement obtained at the previous moment is sent to the Attention weighted aggregation layer for attention weighted aggregation. i 、a j The weighted sum of the source code slice attention weighted aggregation vector representation x C and the vector representation x of the weighted aggregation of the encoded code slices A , and then spliced ​​x C 、x A Then we get the feature fusion vector x t ,in: The source code modality attention weighted aggregation formula is as follows: The assembly code modal attention weighted aggregation formula is as follows: x t =Concat(x C ,x A ); in, for The vector representation after being mapped by the fully connected layer and the activation function tanh(), They are The transposed vector of All are trainable vectors; Step 8: The vector representation obtained in step 7 after cross-modal cross-attention feature fusion and enhancement is sent to the classifier composed of a fully connected layer and a Softmax layer, and the cross entropy loss is calculated according to the output result of the classifier and the actual label of the code segment. The parameters of the bimodal representation learning model composed of the source code representation learning network, the assembly code representation learning network and the cross-attention layer are updated by back-propagation until the training of the bimodal representation learning model with cross-modal feature enhancement and fusion is completed; Step 9: Use the trained cross-modal feature enhancement and fusion bimodal representation learning model and classifier network to detect vulnerabilities in the code to be tested.

2. The vulnerability detection method based on cross-modal feature enhancement of source code and assembly code according to claim 1 is characterized in that The specific steps of step 1 are as follows: Step 1.1: Use the program compiler to debug the source code step by step, generate assembly code by statement, and align the output source code and assembly code by statement; Step 1.2: Use the code compiler command line compilation mode with specific parameters to generate assembly code with source code variable annotations; Step 1.3: Fill the assembly code comments into the corresponding source code statements generated in step 1.1.