A cross-platform binary code similarity detection method based on semantic space alignment
Through contrastive learning and local sensitive hashing technology, the problem of difficult semantic expression in cross-platform binary code similarity detection is solved, semantic alignment and similarity detection under different platforms are achieved, and it is suitable for vulnerability detection and intellectual property protection to ensure software Safety.
Patent Information
- Application Number
- CN202111384834.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-22
- Publication Date
- 2024-10-25
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Existing cross-instruction architecture binary code similarity detection methods cannot effectively express the semantics of binary codes, and are not suitable for basic block embedding under different platforms, resulting in difficulty in similarity matching of the same source code under different platforms.
Using Contrastive Learning technology and locality-sensitive hashing technology in computer vision, we build a cross-platform binary code text function library, train the Bert model for semantic embedding, and use locality-sensitive hashing to quickly search the database to achieve semantic alignment and alignment under different platforms. Similarity detection.
It realizes the identification of the same semantics under different platforms, improves the accuracy of binary code similarity detection, is suitable for vulnerability detection and intellectual property protection, and ensures software security and copyright protection.
Smart Images

Figure CN114115894B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the fields of vulnerability detection, copyright disputes, malware analysis, etc., and specifically relates to a cross-platform binary code similarity detection method based on semantic space alignment. Background Art
[0002] Previous studies on binary code similarity across instruction architectures usually require manual selection of binary code features for basic block embedding, which not only requires professional knowledge, but also contains little embedded information and cannot fully express the semantics of the binary code, such as Gemini and Genius.
[0003] To solve the above problems, methods such as SAFE, Asm2vec, and PalmTree apply static word representation methods to binary code. These methods combine NLP techniques and input the contents of basic blocks into the model in a standardized manner, greatly improving the information capacity of basic block embedding. However, these methods are not suitable for cross-instruction architecture basic block embedding because different instruction architectures have independent semantic representation methods.
[0004] For the same source file, when compiled on different platforms, the minimum granularity similarity that can be obtained is at the function level. Therefore, starting from the function, the present invention maps the functions on different instruction platforms to different embedded vector spaces, and then uses the Contrastive Learning technology in computer vision to achieve the recognition capability of the same semantics on different platforms. Summary of the invention
[0005] The present invention applies Contrastive Learning, a computer vision technology, to binary similarity analysis, and proposes a cross-platform binary code similarity detection method based on semantic space alignment to solve the similarity matching problem of the same source code on different platforms, such as x86, ARM and other platforms. At the same time, the local sensitive hashing technology is used to build a fast search database to quickly search for semantically similar embeddings of binary codes.
[0006] The present invention is achieved through the following scheme:
[0007] A cross-platform binary code similarity detection method based on semantic space alignment:
[0008] The method specifically comprises the following steps:
[0009] Step 1: Build a cross-platform binary code text function library; compile each text function in the open source library through a compiler, and obtain the disassembled text code through different optimization options;
[0010] Step 2: Use the Bert model to train a single platform code semantic embedding model; enable the trained Bert model to recognize platform code semantics;
[0011] Step 3: Training of semantic alignment model based on Contrastive Learning;
[0012] Step 4: Build a fast search database based on local sensitive hashing technology; after converting high-dimensional vectors into low-dimensional vectors, use matching methods to obtain similar vector results; evaluate the experimental model by analyzing the matching results.
[0013] Furthermore, in step one,
[0014] After obtaining the disassembled text code, the semantic content is further extracted and the code is standardized;
[0015] Replace strings and large constants with special tags, and add compilation configuration information to function tags.
[0016] Furthermore, in step 2,
[0017] Train two neural network models so that the neural network models can obtain the semantic embedding of platform code.
[0018] The MLM masked language model training task in the neural network model is used to train a language model to predict the masked tags in the instruction; the MLM masked language model training task is a training task recently proposed in NLP;
[0019] The CWP context window training task in the neural network model infers the semantics of words or instructions by predicting whether two instructions appear together in the sliding window of the control flow; and because the data dependency or def-use relationship between instructions will not be tampered with by compiler optimization, the UDP def-use prediction training task is designed;
[0020] The trained Bert model can map the code semantics under any platform architecture in step one to the semantic space of the platform architecture, and the semantic spaces of different platforms are independent of each other.
[0021] Furthermore, in step three,
[0022] Contrastive Learning can learn generalized semantic features without supervision;
[0023] The same source code has different semantic embeddings on different platforms, and all embedded semantics constitute a parallel corpus data set.
[0024] in For source code 1 Semantic embedding on x86 platform, For source code 1 Semantic embedding under the arm platform, For source code n Semantic embedding on x86 platform, For source code n Semantic embedding under the arm platform,
[0025] The pair is trained in the input model to obtain the aligned semantic embedding model.
[0026] Furthermore, in step 4,
[0027] The cosine distance is used to measure the similarity of the embedded code, and the local sensitive hashing uses a random projection method to convert high-dimensional vectors into low-dimensional vectors;
[0028] Define k as the low-dimensional vector dimension, ε as the error,
[0029] For 0<ε<1, n∈N, we have
[0030]
[0031] Then for any set A={x 1 , ..., x n ∈R d}, there exists a mapping ∏:R d →R k , for all i≠j, we have
[0032]
[0033] Fixing a tolerable ε, we can always reduce R d →R k ,
[0034] Construct the transformation matrix by Gaussian random projection, for the high-dimensional vector X l×d , through the transfer matrix M d×k , get the low-dimensional vector Y l×k , the formula is: X l×d M d×k =Y l×k .
[0035] Furthermore, the construction process of the Gaussian random projection construction conversion matrix is:
[0036] Generate the transfer matrix, M, through the standard normal distribution 1,j ~N(0,1),
[0037] Calculate the transformed low-dimensional vector
[0038]
[0039] in Used to reduce the impact of high-dimensional to low-dimensional distance.
[0040] Beneficial effects of the present invention
[0041] The present invention solves the similarity matching problem of the same source code under different platforms, such as x86, ARM and other platforms, and realizes the recognition of the same semantics under different platforms.
[0042] In reality, there are a lot of open source code reuse situations. If there are vulnerabilities in these codes, it will affect a large number of software using the code. The present invention can be applied to the detection of homologous vulnerability codes, so as to find the disclosed vulnerabilities in a certain software and ensure the security of the software. The present invention can also be applied to the field of intellectual property protection, to find the reuse of unauthorized code in the code and protect the intellectual property rights of the software owner. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a schematic diagram of the training task of the present invention;
[0044] Figure 2 It is the overall framework of the Contrastive Learning model of the present invention. DETAILED DESCRIPTION
[0045] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments; based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0046] Combination Figure 1 to Figure 2 ;
[0047] A cross-platform binary code similarity detection method based on semantic space alignment:
[0048] The method specifically comprises the following steps:
[0049] Step 1: Build a cross-platform binary code text function library; select a suitable open source library (such as openssl, binutils, etc.), compile each text function in the open source library through a compiler, and obtain the disassembled text code through different optimization options; its compilation configuration is shown in Table 1;
[0050] platform Compiler Optimization options System Architecture X86 / ARM / MIPS GCC / Clang / MSV O0 / O1 / O2 / O3 Windows / Linux
[0051] Table 1 Configuration table
[0052] Step 2: Use the Bert model to train a single platform code semantic embedding model; enable the trained Bert model to recognize platform code semantics;
[0053] Step 3: Training of semantic alignment model based on Contrastive Learning;
[0054] Step 4: Build a fast search database based on local sensitive hashing technology; after converting high-dimensional vectors into low-dimensional vectors, use matching methods to obtain similar vector results; evaluate the experimental model by analyzing the matching results.
[0055] In step one, after obtaining the disassembled text code, the semantic content is further extracted and the code is normalized; strings and large constants are replaced with special tags, in order to know the compilation configuration of the function, and the compilation configuration information is added to the function tag.
[0056] In step 2, its biggest feature is that it abandons the traditional RNN and CNN, and converts the distance between two words at any position into 1 through the Attention mechanism, effectively solving the thorny long-term dependency problem in NLP. At the same time, by extracting relational features at different levels, it achieves a more comprehensive effect of reflecting the semantics of the sentence. It has superiority in understanding semantics.
[0057] In the model training phase, two neural network models are trained so that the neural network models can obtain the semantic embedding of the platform code. The specific process is as follows: Figure 1 As shown;
[0058] The MLM masked language model training task in the neural network model is used to train a language model to predict the masked tags in the instruction; the MLM masked language model training task is a training task recently proposed in NLP;
[0059] The CWP context window training task in the neural network model infers the semantics of words or instructions by predicting whether two instructions appear together in the sliding window of the control flow; and because the data dependency (or def-use relationship) between instructions is clearly defined and will not be tampered with by compiler optimization, the UDP def-use prediction training task is designed accordingly;
[0060] The trained Bert model already has the basic ability to recognize the semantics of a certain platform code, and can map the code semantics under any platform architecture in step one to the semantic space of the platform architecture, and the semantic spaces of different platforms are independent of each other.
[0061] In step three,
[0062] Contrastive Learning can learn generalized semantic features without supervision. It is a technique in machine learning that is used to learn generalized features without supervision. It only needs to input which data are similar and which data are not similar, and then it can learn well. The MoCo used here was proposed by He et al. in 2020 to solve computer vision problems. In computer vision, for the same photo of a cat, we can stretch, change colors, and other operations to obtain different photos of cats, but their semantics are all cats. Through the Contrastive Learning technology, we can know the characteristics of cats. This is the same source code as the problem we are going to solve. After different changes, its semantics are the same, so it can be applied to our problem. The overall model architecture is as follows: Figure 2 shown.
[0063] The same source code has different semantic embeddings on different platforms, and all embedded semantics constitute a parallel corpus data set.
[0064] in For source code 1 Semantic embedding on x86 platform, For source code 1 Semantic embedding under the arm platform, For source code n Semantic embedding on x86 platform, For source code n Semantic embedding under the arm platform;
[0065] The pairs (each pair of parallel corpus data in the parallel corpus data set) are input into the model for training to obtain the aligned semantic embedding model. The Bert model fully obtains the semantics of the code and solves the semantic embedding problems of different compilers and different compilation options. Therefore, the semantic alignment model only needs to focus on solving problems on different platforms, which can effectively improve the accuracy of the model.
[0066] In step 4, local sensitive hashing is a better method for neighbor search of high-dimensional massive data. The basic idea of local sensitive hashing is that after two adjacent data in the high-dimensional data space are mapped to the low-dimensional data space, there is a high probability that they are adjacent; and two data that were originally non-adjacent will also have a high probability of being non-adjacent in the low-dimensional space. Through this one-to-one mapping, we can find adjacent data points in the low-dimensional data space and avoid searching in the high-dimensional data space, because it is very time-consuming in the high-dimensional space.
[0067] The cosine distance is used to measure the similarity of the embedded code, and the local sensitive hashing uses a random projection method to convert high-dimensional vectors into low-dimensional vectors;
[0068] The Random Rrojection mapping is defined as follows:
[0069] Click X 1 , ..., X n ∈R d , d is a large number, by constructing a mapping ∏: R d →R k , where k<<d, so that
[0070] ||x i -x j ||≈||∏(x i )-∏(x j )||
[0071] According to the Johnson-Lindenstrauss lemma, k is defined as the dimension of the low-dimensional vector, ε is the error,
[0072] For 0<ε<1, n∈N, we have
[0073]
[0074] Then for any set A={x 1 , ..., x n ∈R d}, there exists a mapping ∏:R d →R k , for all i≠j, we have
[0075]
[0076] Fixing a tolerable ε, we can always reduce R d →R k ,
[0077] Construct the transformation matrix by Gaussian random projection, for the high-dimensional vector X l×d, through the transfer matrix M d×k , get the low-dimensional vector Y l×k , the formula is: X l×d M d×k =Y l×k .
[0078] The construction process of the Gaussian random projection conversion matrix is as follows:
[0079] Generate the transfer matrix, M, through the standard normal distribution 1,j ~N(0,1),
[0080] Calculate the transformed low-dimensional vector
[0081]
[0082] in Used to reduce the impact of high-dimensional to low-dimensional distance.
[0083] The above is a detailed introduction to the cross-platform binary code similarity detection method based on semantic space alignment proposed in the present invention, and the principles and implementation methods of the present invention are explained. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A cross-platform binary code similarity detection method based on semantic space alignment, Features: The method specifically comprises the following steps: Step 1: Build a cross-platform binary code function library; compile each function in the open source library through a compiler, and obtain the disassembled text code through different optimization options; Step 2: Use the Bert model to train a single platform code semantic embedding model; enable the trained Bert model to recognize platform code semantics; Train two neural network models so that the neural network models can obtain the semantic embedding of platform code. The MLM masked language model training task in the neural network model is used to train a language model to predict the masked tags in the instruction; the MLM masked language model training task is a training task recently proposed in NLP; The CWP context window training task in the neural network model infers the semantics of words or instructions by predicting whether two instructions appear together in the sliding window of the control flow; and because the data dependency or def-use relationship between instructions will not be tampered with by compiler optimization, the UDP def-use prediction training task is designed; The trained Bert model can map the code semantics under any platform architecture in step 1 to the semantic space of the platform architecture, and the semantic spaces of different platforms are independent of each other; Step 3: Training of semantic alignment model based on Contrastive Learning; Contrastive Learning can learn generalized semantic features in an unsupervised manner; The same source code has different semantic embeddings on different platforms, and all embedded semantics constitute a parallel corpus data set. in For source code 1 Semantic embedding on x86 platform, For source code 1 Semantic embedding under the arm platform, For source code n Semantic embedding on x86 platform, For source code n Semantic embedding under the arm platform, Train the pair input model to obtain the aligned semantic embedding model; Step 4: Build a fast search database based on local sensitive hashing technology; after converting high-dimensional vectors into low-dimensional vectors, use matching methods to obtain similar vector results; evaluate the experimental model by analyzing the matching results.
2. The method according to claim 1, Features: In step one, After obtaining the disassembled text code, the semantic content is further extracted and the code is standardized; Replace strings and large constants with special tags, and add compilation configuration information to function tags.
3. The method according to claim 2, Features: In step four, The cosine distance is used to measure the similarity of the embedded code, and the local sensitive hashing uses a random projection method to convert high-dimensional vectors into low-dimensional vectors; Define k as the low-dimensional vector dimension, ε as the error, For 0<ε<1,n∈N, we have Then for any set A={x 1 ,...,x n ∈R d }, there exists a mapping Π:R d →R k , for all i≠j, we have Fixing a tolerable ε, we can always reduce R d →R k , Construct the transfer matrix by Gaussian random projection, for the high-dimensional vector X 1×d , through the transfer matrix M d×k , get the low-dimensional vector Y 1×k , the formula is: X 1×d M d×k =Y 1×k .
4. The method according to claim 3, Features: The construction process of the Gaussian random projection transfer matrix is as follows: Generate the transfer matrix, M, through the standard normal distribution 1,j ~N(0,1), Calculate the transformed low-dimensional vector in Used to reduce the impact of high-dimensional to low-dimensional distance.
