Multi-modal binary similarity vulnerability detection method based on pre-training model
Through a multi-modal binary similarity vulnerability detection method based on a pre-trained model, pseudocode and code attribute graph sequences are used for replacement word detection and comparative learning, which solves the problems of compilation diversity and high false negative rate in the existing technology, and achieves Provides more accurate binary code vulnerability detection.
Patent Information
- Application Number
- CN202410987754.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-23
- Publication Date
- 2024-10-22
AI Technical Summary
Existing binary code vulnerability detection methods are inefficient and have a high false negative rate when faced with compilation of vulnerable codes with small diversity and differences, making it difficult to accurately identify them. Moreover, existing methods focus on code semantic information and ignore code structure information.
A multi-modal binary similarity vulnerability detection method based on a pre-trained model is used to generate pseudocode through disassembly, generate a code attribute graph sequence, and use the Electra-small model for replacement word detection, code attribute graph edge prediction and multi-modal comparison. Learn and train models to mitigate compilation diversity effects and improve vulnerability detection accuracy.
It effectively reduces the false negative rate, improves the stable representation of binary code, and combines semantic and structural information to enhance the accuracy of vulnerability detection.
Smart Images

Figure CN118821152A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of network security, and in particular is a multimodal binary similarity vulnerability detection method based on a pre-training model. Background Art
[0002] With the popularity of various open source communities, developers usually reuse third-party open source codes. However, while code reuse brings convenience to development, it also aggravates the spread of cloned vulnerabilities. Due to factors such as intellectual property protection, manufacturers usually do not disclose source code, so researchers have proposed many binary code vulnerability detection methods. For example, symbolic execution, taint analysis, fuzz testing and other technologies, these methods have been continuously developed and are now widely used in the field of vulnerability detection. However, these existing methods still have their shortcomings. For example, the efficiency of symbolic execution is limited by its path space search, constraint solving and parallel processing. Taint analysis has high requirements for the dynamic operation coverage of the program. The effect of fuzz testing is also closely related to the selection of seeds and mutation strategies.
[0003] With the continuous development of reverse engineering related technologies, binary code similarity detection (BCSD) technology has gradually been applied to the field of vulnerability detection. Traditional BCSD compares the similarity between two pieces of code by extracting features, which means that the effectiveness of related methods depends largely on the selected features. Today's BCSD relies more on deep learning technology, embedding assembly code as functions, building graphs or building gadgets, and calculating the similarity of assembly code in vector space. Limited by the differences in compilation methods for different target architectures, the same source code may be very different after compilation. Most of the current methods model assembly instructions. Due to differences in compiler types, compilation optimization levels, and target architectures, the compiled code, such as instruction order, register allocation, and other information, will be different. These differences will affect vulnerability detection, which requires a more stable method to represent binary code.
[0004] In addition, the difference between the code containing the vulnerability and its patch code is sometimes significant, and sometimes it is reflected in subtle details. For vulnerability codes with significant differences, existing methods have achieved good results, but for patch codes and vulnerability codes with small differences, existing methods are often difficult to accurately identify and show a high false negative rate. The current vulnerability detection methods using RNN, BLSTM or pre-trained NLP models pay more attention to the semantic information of the code, but still lack attention to the syntax and control flow information of the code. Therefore, the vulnerability detection method that combines code semantic information and code structure information still needs to be further studied. Summary of the invention
[0005] In order to overcome the shortcomings of the prior art, the present invention proposes a multimodal binary similarity vulnerability detection method based on a pre-trained model, which can alleviate the impact of compilation diversity on vulnerability detection and effectively reduce the false negative rate.
[0006] The technical solution to achieve the purpose of the present invention is: a multimodal binary similarity vulnerability detection method based on a pre-trained model, the steps are as follows:
[0007] Step 1, disassemble the binary file to generate assembly instructions, and decompile the assembly instructions to obtain pseudo code;
[0008] Step 2, generate a code attribute graph for the pseudocode, and convert the code attribute graph traversal into a code attribute graph sequence;
[0009] Step 3: Input the pseudocode and code attribute graph sequences into the Electra-small model for pre-training, and set three pre-training goals: replacement word detection, code attribute graph edge prediction, and multimodal contrastive learning;
[0010] Step 4: Adjust the pre-trained Electra-small model for the vulnerability detection task, and use the adjusted Electra-small model for vulnerability detection.
[0011] Preferably, step 1 disassembles the binary file to generate assembly instructions, and decompiles the assembly instructions to obtain pseudo code, including:
[0012] Disassemble and decompile the library files of multiple open source projects to obtain pseudocode, where each library is compiled using two compilers, GCC and Clang, including 32-bit and 64-bit versions of x86-64, ARM and MIPS, and different levels of compilation optimization;
[0013] The 55 CWE types in the JulietTest Suite dataset are compiled into binary files according to different target architectures and compilation priorities. The binary files are disassembled and decompiled to generate pseudocode. Finally, a total of about 50,000 files are used as the model fine-tuning experimental dataset.
[0014] Preferably, step 2 generates a corresponding code attribute graph for the pseudocode using the Joern method; and converts the code attribute graph into a code attribute graph sequence including control structure and data flow information.
[0015] Preferably, the Electra-small model is pre-trained. When performing code attribute graph edge prediction training, the input data is the code attribute graph sequence obtained in step 2; when performing replacement word detection and multimodal contrastive learning training, the input data is pseudocode and code attribute graph sequence.
[0016] Preferably, the specific method for pre-training replacement word detection on the Electra-small model is:
[0017] For the input pseudo code C = [C1, C2, ... C n ] and the code attribute graph sequence G = [G1, G2, ... G n ], using a small mask language model for partial masking MASK, corresponding to m C and m G The specific formula is and Where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively, and unif represents randomly selecting a number of numbers in the value range according to uniform distribution;
[0018] Use the data generator m C and m G Generate predicted words, and the prediction results are expressed as and The data generator uses C when inputting the results corrupt With G corrupt Replacement logo and The specific formula is:
[0019]
[0020] At the same time, a discriminator D is trained, whose input contains C corrupt With G corrupt The discriminator determines whether the words at all input positions are original inputs rather than predicted by the generator.
[0021] The loss function of the replacement word detection task is:
[0022]
[0023]
[0024] The logarithmic loss function is used, where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively, and y i represents the true label of the i-th word, represents the probability that the discriminator in the pseudocode predicts that the i-th word is the original word, represents the probability that the discriminator predicts the i-th word to be the original word in the code attribute graph sequence, and Represents the loss function value corresponding to C and G.
[0025] Preferably, the specific method for pre-training the Electra-small model to predict the attribute graph edge of the target code is:
[0026] Select some words G containing edge information in the code attribute graph input sequence * , use [MASK] instead of G * In pre-training, the model is required to predict the words that [MASK] may represent. The prediction results are expressed as The specific formula is:
[0027] The loss function for code attribute graph edge prediction is:
[0028]
[0029] Among them, |G * | indicates G * The number of categories, y i Represents G * The true label of the i-th category, Represents model predictions is the probability of the i-th category, Represents the loss function value.
[0030] Preferably, the specific method for pre-training the Electra-small model for multimodal contrastive learning is:
[0031] Construct the first type of positive sample pairs, specifically: regard the pseudo code as the original input, regard the code attribute graph sequence as a positive sample similar to it, adopt the cross-batch sampling strategy to obtain positive and negative samples, for two batches of training data batch1 = [X1, X2, ...X n ], Where Xi is the original sample, As a positive sample pair, the remaining data is used to form a negative sample set of size 2(n-1)
[0032] Use the Transformer encoder to map positive and negative samples into vector space to obtain positive sample pairs With negative samples The cosine similarity of the calculated vectors indicates the similarity between samples;
[0033] Exchange the pseudocode with the code attribute graph sequence, treat the code attribute graph sequence as the original input, treat the pseudocode as a positive sample, and repeat the above steps to construct the second type of positive sample pairs. With negative samples
[0034] Through contrastive learning, the similarity between the original input and its positive samples of different modalities is maximized, and the similarity between the original input and the negative samples is minimized. The loss function of multimodal contrastive learning is specifically:
[0035]
[0036]
[0037] Where exp represents an exponential function with the natural constant e as the base. Represents the calculation vector V i and The cosine similarity of Represents the calculation vector V i and The cosine similarity of n represents the number of original samples, 2(n-1) represents the size of the negative sample set, represents the loss function value of the first type of multimodal contrastive learning, represents the loss function value of the second type of multimodal contrastive learning, Represents the sum of two types of multimodal contrastive learning loss functions.
[0038] Compared with the prior art, the present invention has the following significant advantages: by using pseudocode as the research object, the model only needs to input pseudocode as the training object, without inputting assembly instructions of multiple different architectures, thereby alleviating the impact of cross-architecture and compilation diversity on binary vulnerability detection; and by comparing and learning the pseudocode with the code attribute graph sequence, the training model learns more comprehensive features to better represent the code for vulnerability detection.
[0039] The present invention is described in further detail below in conjunction with the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 This is the overall framework diagram of the present invention.
[0041] Figure 2 Schematic diagram of pre-trained target replacement word detection in the present invention.
[0042] Figure 3 Schematic diagram of the edge prediction of the pre-trained target code attribute graph in the present invention.
[0043] Figure 4 Schematic diagram of multimodal contrastive learning of pre-training targets in the present invention. DETAILED DESCRIPTION
[0044] The present invention will be further described below in conjunction with the accompanying drawings.
[0045] The present invention provides a multimodal binary similarity vulnerability detection method based on a pre-trained model, comprising the following steps:
[0046] Step 1: First, disassemble the binary file to generate assembly instructions, and then decompile it to obtain pseudo code as the experimental data set.
[0047] Disassemble and decompile the library files of multiple open source projects to get pseudocode. Each library is compiled using two compilers, GCC and Clang, including 32-bit and 64-bit versions of x86-64, ARM, and MIPS, and different levels of compilation optimization. Finally, a total of about 1.5 million files are used as pre-training datasets;
[0048] The 55 CWE types in the Juliet Test Suite dataset are compiled into binary files according to different target architectures and compilation priorities. The binary files are disassembled and decompiled to generate pseudocode. Finally, a total of about 50,000 files are used as the model fine-tuning experimental dataset.
[0049] Step 2: Use Joern to generate a code attribute graph (code attribute graph) for the pseudocode. In order to meet the input requirements of the model, the code attribute graph traversal is converted into a sequence.
[0050] Figure 1 It is an overall framework diagram for implementing the present invention. The present invention decompiles a binary file into pseudocode and generates a corresponding code attribute graph, and then converts the code attribute graph into a sequence containing structural information and inputs it into the model together with the pseudocode for pre-training.
[0051] Step 3, the present invention sets three pre-training goals for the model, including replacement word detection, code attribute graph edge prediction and multimodal contrastive learning, so that the model learns the universal features of the input on a large amount of data. At the same time, the three pre-training goals are set to update the model parameters, where the code attribute graph edge prediction input is only the code attribute graph sequence obtained in step 2, and the replacement word detection and multimodal contrastive learning input is the pseudo code and code attribute graph sequence.
[0052] Figure 2 This is a schematic diagram of the pre-training target replacement word detection in the present invention. The present invention trains two data generators in the replacement word detection target, a pseudo code generator and a code property graph sequence generator For the input pseudo code C = [C1, C2, ... C n ] and the code attribute graph sequence G = [G1, G2, ... G n ], using a small mask language model for partial masking MASK (respectively m C and m G ), the specific formula is and Where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively. unif represents randomly selecting a number of numbers in the value range according to uniform distribution. Taking i as an example, i is a random number from 1 to |C| and from 1 to |G| respectively. The i-th word C is randomly selected. i and G i After masking, it is represented as and Use the data generator m C and m G Generate predicted words, and the prediction results are expressed as and The data generator uses C when inputting the results corrupt With G corrupt Replacement logo and At the same time, a discriminator D is trained, whose input contains C corrupt With G corrupt The discriminator determines whether the words at all input positions are original inputs rather than predicted by the generator. This is a binary classification problem, so the loss function of the replacement word detection task is:
[0053]
[0054]
[0055] The logarithmic loss function is used, where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively, and y i represents the true label (0 or 1) of the i-th word, represents the probability that the discriminator in the pseudocode predicts that the i-th word is the original word, represents the probability that the discriminator predicts the i-th word to be the original word in the code attribute graph sequence, and Represents the loss function value.
[0056] Figure 3 The schematic diagram of the edge prediction of the pre-trained target code attribute graph in the present invention. In order to make the model pay attention to the edge information in the code attribute graph sequence, the present invention masks the edge information in the code attribute graph sequence and requires the model to predict. Since only the edge information is concerned, the masked language model is adopted to select some words G containing edge information in the code attribute graph input sequence. * , use [MASK] instead and ask the model to predict. The specific formula is: The loss function for code attribute graph edge prediction is:
[0057]
[0058] Among them, |G * | indicates G * The number of categories, y i Represents G * In the true label of the ith category (0 or 1), Representing model predictions is the probability of the i-th category, Represents the loss function value.
[0059] Figure 4 Schematic diagram of multimodal contrastive learning of pre-training target in the present invention. In order to make the model fully understand the semantic equivalence between pseudocode and code attribute graph sequence, the present invention first constructs the first type of positive sample pairs, regards the pseudocode as the original input, regards the code attribute graph sequence as a positive sample similar to it, and adopts the strategy of cross-batch sampling to obtain positive samples and negative samples. For two batches of training data batch1 = [X1, X2, ...X n ], Where Xi is the original sample, As a positive sample pair, the remaining data is used to form a negative sample set of size 2(n-1) Then use the Transformer encoder to map the positive and negative samples into the vector space to obtain the positive sample pair With negative samples Then calculate the cosine similarity of the vectors to represent the similarity between samples. Then exchange the pseudocode with the code attribute graph sequence, regard the code attribute graph sequence as the original input, and regard the pseudocode as a positive sample to repeat the above steps to construct the second type of positive sample pairs. Through contrastive learning, the similarity between the original input and its positive samples of different modalities is maximized, and the similarity between the original input and the negative samples is minimized. The loss function of multimodal contrastive learning is specifically:
[0060]
[0061]
[0062] Where exp represents an exponential function with the natural constant e as the base. Represents the calculation vector V i and The cosine similarity of Represents the calculation vector V i and The cosine similarity of n represents the number of original samples, 2(n-1) represents the size of the negative sample set, represents the loss function value of the first type of multimodal contrastive learning, represents the loss function value of the second type of multimodal contrastive learning, Represents the sum of two types of multimodal contrastive learning loss functions.
[0063] Step 4, after completing the unsupervised pre-training, fine-tune the model to make it suitable for binary code similarity vulnerability detection. The dataset used for fine-tuning is the fine-tuning dataset generated in step 1. Vulnerability detection is regarded as a binary classification task, and the model will classify the given input, that is, "there is a vulnerability" and "there is no vulnerability". The present invention fine-tunes the pre-trained model to make it suitable for binary code similarity vulnerability detection.
[0064] Although the present invention has been disclosed as above with preferred embodiments, it is not intended to limit the present invention. A person with ordinary knowledge in the technical field to which the present invention belongs may make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention shall be determined by the definition of the claims.
Claims
1. A multimodal binary similarity vulnerability detection method based on pre-trained models, It is characterized in that Here are the steps: Step 1, disassemble the binary file to generate assembly instructions, and decompile the assembly instructions to obtain pseudo code; Step 2, generate a code attribute graph for the pseudo code, and convert the code attribute graph traversal into a code attribute graph sequence; Step 3: Input the pseudocode and code attribute graph sequences into the Electra-small model for pre-training, and set three pre-training goals: replacement word detection, code attribute graph edge prediction, and multimodal contrastive learning; Step 4: Adjust the pre-trained Electra-small model for the vulnerability detection task, and use the adjusted Electra-small model for vulnerability detection.
2. According to the multimodal binary similarity vulnerability detection method based on pre-training model according to claim 1, It is characterized in that Step 1: Disassemble the binary file to generate assembly instructions, and decompile the assembly instructions to obtain pseudo code, including: Disassemble and decompile the library files of multiple open source projects to obtain pseudocode, where each library is compiled using two compilers, GCC and Clang, including 32-bit and 64-bit versions of x86-64, ARM and MIPS, and different levels of compilation optimization; The 55 CWE types in the Juliet Test Suite dataset are compiled into binary files according to different target architectures and compilation priorities. The binary files are disassembled and decompiled to generate pseudocode. Finally, a total of about 50,000 files are used as the model fine-tuning experimental dataset.
3. The multimodal binary similarity vulnerability detection method based on a pre-trained model according to claim 1, It is characterized in that Step 2 uses the Joern method to generate a corresponding code attribute graph for the pseudocode; converting the code attribute graph into a code attribute graph sequence including control structure and data flow information.
4. The multimodal binary similarity vulnerability detection method based on a pre-trained model according to claim 1, It is characterized in that Pre-train the Electra-small model. When performing code attribute graph edge prediction training, the input data is the code attribute graph sequence obtained in step 2; when performing replacement word detection and multimodal contrastive learning training, the input data is pseudocode and code attribute graph sequence.
5. The multimodal binary similarity vulnerability detection method based on a pre-trained model according to claim 1, It is characterized in that The specific method for pre-training replacement word detection for the Electra-small model is: For the input pseudo code C = [C 1 ,C 2 ,…C n ] and the code attribute graph sequence G = [G 1 ,G 2 ,…G n ], using a small mask language model for partial masking MASK, corresponding to m C and m G The specific formula is and Where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively, and unif represents randomly selecting a number of numbers in the value range according to uniform distribution; Use the data generator m C and m G Generate predicted words, and the prediction results are expressed as and The data generator uses C when inputting the results corrupt With G corrupt Replacement logo and The specific formula is: At the same time, a discriminator D is trained, whose input contains C corrupt With G corrupt The discriminator determines whether the words at all input positions are original inputs rather than generated by the generator's predictions. The loss function of the replacement word detection task is: The logarithmic loss function is used, where |C| and |G| represent the number of words in the pseudocode and code attribute graph sequences respectively, and y i represents the true label of the i-th word, represents the probability that the discriminator in the pseudocode predicts that the i-th word is the original word, represents the probability that the discriminator predicts the i-th word to be the original word in the code attribute graph sequence, and Represents the loss function value corresponding to C and G.
6. The multimodal binary similarity vulnerability detection method based on a pre-trained model according to claim 1, It is characterized in that The specific method for pre-training the Electra-small model to predict the attribute graph edge of the target code is as follows: Select some words G containing edge information in the code attribute graph input sequence * , use [MASK] instead of G * In pre-training, the model is required to predict the word that [MASK] may represent, and the prediction result is represented by G *masked , the specific formula is: The loss function for code attribute graph edge prediction is: Among them, |G * | indicates G * The number of categories, y i Represents G * In the true label of the i-th category, P(G i *masked ,G i * ) indicates that the model predicts G *masked is the probability of the i-th category, Represents the loss function value.
7. The multimodal binary similarity vulnerability detection method based on a pre-trained model according to claim 1, It is characterized in that The specific method for pre-training the Electra-small model for multimodal contrastive learning is: Construct the first type of positive sample pairs, specifically: regard the pseudo code as the original input, regard the code attribute graph sequence as a positive sample similar to it, adopt the cross-batch sampling strategy to obtain positive and negative samples, for the two batches of training data batch1 = [X 1 , X 2 , …X n ],batch2=[X 1 + , X 2 + , …X n + ], where Xi is the original sample, {X i , X i + } as a positive sample pair, and the remaining data constitutes a negative sample set X of size 2(n-1) i — ; The Transformer encoder is used to map positive and negative samples to the vector space to obtain positive sample pairs {V i , V i + } and negative samples V i — , the cosine similarity of the calculated vectors represents the similarity between samples; Exchange the pseudocode with the code attribute graph sequence, treat the code attribute graph sequence as the original input, treat the pseudocode as a positive sample, and repeat the above steps to construct the second type of positive sample pairs {V i + , V i } and negative samples V i — ; Through contrastive learning, the similarity between the original input and its positive samples of different modalities is maximized, and the similarity between the original input and the negative samples is minimized. The loss function of multimodal contrastive learning is: Where exp represents the exponential function with the natural constant e as the base, cossim(V i ,V i + ) represents the calculation vector V i and V i + The cosine similarity, cossim(V i ,V j - ) represents the calculation vector V i and V j - The cosine similarity of n represents the number of original samples, 2(n-1) represents the size of the negative sample set, represents the loss function value of the first type of multimodal contrastive learning, represents the loss function value of the second type of multimodal contrastive learning, Represents the sum of two types of multimodal contrastive learning loss functions.
