Binary code clone detection method and system
Through the methods of self-supervised mask learning and supervised fine-tuning training, the pre-trained language model is used to detect code clones in binary files, which solves the problem of code clone detection in binary files in the existing technology and achieves efficient and accurate detection across platforms. , which improves software security.
Patent Information
- Application Number
- CN202410700489.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-31
- Publication Date
- 2024-08-30
AI Technical Summary
Existing technology is difficult to quickly and accurately detect code clones in binary files. Especially when the software source code is difficult to obtain, it becomes difficult to analyze potential security risks in binary executable programs, causing software security and stability to be affected. threaten.
The features of binary codes are automatically extracted through pre-trained language models, and self-supervised mask learning and supervised fine-tuning training methods are used to learn the execution semantics of binary functions to achieve binary code clone detection across platforms and compilation environments.
It achieves efficient and accurate binary code clone detection on different platforms and compilation environments, reduces feature engineering overhead, enhances the generalization ability of detection, and improves software security.
Smart Images

Figure CN118567715A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software technology, and in particular to a binary code clone detection method and system. Background Art
[0002] With the widespread popularity of open source projects and applications, more and more software developers tend to use open source code in their work to accelerate the software development process and improve software quality. However, the existence of this situation leads to a large number of identical or similar code fragments in different software products after they are released, which is called code clones. With the widespread existence of code cloning, the use of these open source codes inevitably introduces a series of security risks and potential vulnerabilities, which poses a severe challenge to the security and stability of software. Once the vulnerabilities in the open source code are exploited, they may cause system instability and information leakage, and even put the entire software system at risk. In order to ensure the security of software, how to quickly and accurately perform code clone detection from a large number of software projects to identify similar vulnerabilities has become a focus of the software security community. Since most of the software provided by most software service providers exists in the form of binary files, such as firmware images, it makes it extremely difficult to detect software components. When the software source code is difficult to obtain, in-depth analysis of the code referenced in the binary executable program to discover potential security risks is of vital significance to software security. Summary of the invention
[0003] The present invention provides a binary code clone detection method and system, which can not only automatically extract features for binary code clone detection through a pre-trained language model, saving a lot of feature engineering overhead, but also has strong generalization ability and can be applied to different platforms and compilation environments.
[0004] The present invention provides a binary code clone detection method, comprising: determining a static assembly code and a dynamic execution trajectory of a binary function based on the binary code; determining an unlabeled binary function data set according to the static assembly code and the dynamic execution trajectory; pre-training a language model according to the unlabeled binary function data set by adopting a self-supervised mask learning method to obtain a trained language model, so that the language model learns the execution semantics of the binary function; performing supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; and performing binary code clone detection according to the fine-tuned language model.
[0005] According to a binary code clone detection method provided by the present invention, the static assembly code and dynamic execution trajectory of a binary function are determined based on the binary code, including: splitting the binary code into code blocks with functions as units; running the binary function based on the code blocks to obtain the static assembly code and dynamic execution trajectory of the binary function; the dynamic execution trajectory is used to characterize the state value of the register when executing each instruction in the static assembly code.
[0006] According to a binary code clone detection method provided by the present invention, the determining of an unlabeled binary function data set based on the static assembly code and the dynamic execution trajectory includes: performing feature extraction on the static assembly code to obtain a feature sequence; performing numerical conversion on the feature sequence based on the static assembly code and the dynamic execution trajectory, and using the obtained numerical sequence as the unlabeled binary function data set.
[0007] According to a binary code clone detection method provided by the present invention, the feature extraction of the static assembly code to obtain a feature sequence includes: segmenting the static assembly code to obtain a segmented code; replacing the memory address in the segmented code with a preset word symbol to obtain the feature sequence; the feature sequence includes a code sequence, a value sequence, an instruction set architecture sequence, an optimization level sequence, an instruction position sequence and a word position sequence.
[0008] According to a binary code clone detection method provided by the present invention, the feature sequence is numerically converted according to the static assembly code and the dynamic execution trajectory, and the obtained numerical sequence is used as the unlabeled binary function data set, including: traversing the feature sequence; when a word in the feature sequence is a register symbol, according to the dynamic execution trajectory, the register state value is used as the conversion value corresponding to the word; when a word in the feature sequence is the preset word symbol, according to the static assembly code, the memory address is used as the conversion value of the word; when a word in the feature sequence is other symbols, the preset empty symbol is used as the conversion value of the word; the other symbols are symbols other than the register symbol and the preset word symbol.
[0009] According to a binary code clone detection method provided by the present invention, the language model is pre-trained in a self-supervised mask learning manner based on the unlabeled binary function data set to obtain a trained language model, including: inputting the numerical sequence into an embedding module to establish a word-to-index dictionary; the dictionary is an embedding matrix composed of word embedding vectors; the word embedding vector is a vector of digital mapping in the numerical sequence; based on the word-to-index dictionary, a coding module of a multi-head attention mechanism is used to mask the feature sequence input to the language model; training the language model based on the cross entropy of the predicted mask value and the true mask value predicted by the context of the masked part as a loss function, back-propagating and updating the language model to obtain the trained language model.
[0010] According to a binary code clone detection method provided by the present invention, the embedding module includes a coding layer, a gated recurrent unit and a fully connected layer; the inputting the numerical sequence into the embedding module to establish a word-to-index dictionary includes: converting each numerical value in the numerical sequence into a hexadecimal digital sequence; inputting the digital sequence into the coding layer to obtain an initial vector; inputting the initial vector into the gated recurrent unit to obtain an intermediate vector; inputting the intermediate vector into the fully connected layer to obtain a representation vector that integrates sequence information; performing maximum pooling on the representation vector, and using the pooled vector as the word embedding vector; and establishing the word-to-index dictionary based on the word embedding vector.
[0011] According to a binary code clone detection method provided by the present invention, the method performs supervised fine-tuning training on the trained language model according to a labeled binary function data set to obtain a fine-tuned language model, including: performing function-level fine-tuning training on the trained language model according to the labeled binary function data set when the information of the binary function source file is unknown to obtain the function-level fine-tuned language model; the function-level fine-tuning training is to perform maximum pooling on the word vectors of the binary function to obtain the embedded vector of the binary function; according to the labeled binary function data set when the information of the binary function source file is known, performing file-level fine-tuning training on the trained language model to obtain the file-level fine-tuned language model; the file-level fine-tuning training is to model all functions in the binary file as an implicit global graph, use a graph attention network to learn the attention coefficients between nodes, and obtain the embedded vector of the binary function by message passing and neighbor aggregation; the embedded vector of the binary function is used to calculate the cosine similarity of the binary code.
[0012] The present invention also provides a binary code clone detection system, comprising: a first determination module, used to determine the static assembly code and dynamic execution trajectory of the binary function based on the binary code; a second determination module, used to determine an unlabeled binary function data set according to the static assembly code and the dynamic execution trajectory; a model training module, used to pre-train a language model according to the unlabeled binary function data set by adopting a self-supervised mask learning method to obtain a trained language model, so that the language model learns the execution semantics of the binary function; a model fine-tuning module, used to perform supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; and a detection module, used to perform binary code clone detection according to the fine-tuned language model.
[0013] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the binary code cloning detection method described in any one of the above is implemented.
[0014] The present invention provides a method and system for detecting clones of binary codes, the method comprising: determining static assembly code and dynamic execution trajectory of binary functions based on binary codes; determining an unlabeled binary function data set according to the static assembly code and dynamic execution trajectory; pre-training a language model according to the unlabeled binary function data set using a self-supervised mask learning method to obtain a trained language model so that the language model learns the execution semantics of binary functions; performing supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; and performing binary code clone detection according to the fine-tuned language model. The present invention can not only automatically extract features for clone detection of binary codes through a pre-trained language model, thus saving a large amount of feature engineering overhead, but also has a strong generalization capability and can be applied to different platforms and compilation environments. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0016] Figure 1 It is a flow chart of a binary code clone detection method provided by the present invention.
[0017] Figure 2It is the machine code of the binary function provided by the present invention and the assembly code diagram generated by disassembly.
[0018] Figure 3 It is a schematic diagram of the register status when the 1st, 6th and 13th instructions in the assembly code are run, provided by the present invention.
[0019] Figure 4 It is a schematic diagram of word segmentation and value replacement processing of the code sequence provided by the present invention.
[0020] Figure 5 It is a schematic diagram of the preprocessed code sequence and numerical sequence provided by the present invention.
[0021] Figure 6 This is a diagram of the pre-trained model architecture provided by the present invention.
[0022] Figure 7 It is the input processing graph of the embedding layer provided by the present invention.
[0023] Figure 8 It is a schematic diagram of encoding numerical values in a numerical sequence provided by the present invention.
[0024] Fig. 9 This is a diagram of the fine-tuned model architecture provided by the present invention.
[0025] Fig.10 It is a structural schematic diagram of a binary code clone detection system provided by the present invention.
[0026] Fig.11 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0027] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0028] With the rapid development of the computer software industry, more and more software products are being released to meet the needs of people's lives and work, forcing the widespread reuse of code in the software development process to improve software development efficiency. However, the existence of this situation leads to a large number of identical or similar code fragments in different software products after release, which is called code cloning. With the widespread existence of code cloning, code fragments containing vulnerabilities may also be reused in various software products, eventually causing vulnerabilities to spread rapidly and cause serious impacts on software systems. Therefore, how to efficiently and accurately detect binary code cloning has become a problem that needs to be solved in this field.
[0029] As an important part of binary code analysis, binary function clone detection is often used as a core functional module and has a wide range of applications in plagiarism detection, malware tracking, digital forensics, and vulnerability detection. For example, in software reverse analysis, a binary function code library is built, and the binary function clone detection module is used to match statically linked unknown binary function fragments to binary functions in the database, thereby analyzing unknown binary functions. In addition, in detecting and patching vulnerabilities in third-party libraries, given a vulnerable function, the binary function clone detection module can find similar programs similar to the vulnerable function from a large number of third-party libraries, thereby helping developers locate and fix vulnerable code more quickly.
[0030] Please refer to Figure 1 , Figure 1 A schematic flow chart of a binary code clone detection method provided by the present invention.
[0031] The present invention provides a binary code clone detection method, comprising: 101: Determine the static assembly code and dynamic execution trajectory of the binary function based on the binary code; 102: Determine an unlabeled binary function data set based on static assembly code and dynamic execution trace; 103: Based on the unlabeled binary function dataset, a language model is pre-trained by self-supervised mask learning to obtain a trained language model so that the language model learns the execution semantics of the binary function; 104: Perform supervised fine-tuning training on the trained language model according to the labeled binary function dataset to obtain a fine-tuned language model; 105: Binary code clone detection based on fine-tuned language model.
[0032] The present invention provides a method for detecting clones of binary codes. The method takes functions in binary codes as research objects, and based on a pre-trained language model, takes the static assembly code and dynamic running trajectory of binary functions as input, and pre-trains the language model in a self-supervised manner on a large number of unlabeled binary function data sets to learn the execution semantics of the function. Then, supervised training is performed using labeled binary function data sets to fine-tune the language model parameters, so that the fine-tuned language model can achieve binary code clone detection under cross-architecture and cross-compilation optimization levels. The method of the present invention can not only automatically extract features for clone detection of binary codes through a pre-trained language model, thus saving a large amount of feature engineering overhead, but also has strong generalization capabilities and can be applied to different platforms and compilation environments.
[0033] Of course, in order to further improve the performance of the language model, a paradigm based on pre-training and fine-tuning can also be adopted. This can use labeled binary function data sets for supervised training and fine-tune the language model parameters so that the fine-tuned language model can achieve binary code clone detection across architectures and compilation optimization levels.
[0034] As a preferred embodiment, the static assembly code and dynamic execution trajectory of a binary function are determined based on the binary code, including: splitting the binary code into code blocks with functions as units; running the binary function based on the code blocks to obtain the static assembly code and dynamic execution trajectory of the binary function; the dynamic execution trajectory is used to characterize the state value of the register when executing each instruction in the static assembly code.
[0035] Please refer to Figure 2 , Figure 2 The present invention provides a machine code of a binary function and an assembly code diagram generated by disassembly.
[0036] Please refer to Figure 3 , Figure 3 A schematic diagram of register states when the present invention runs to the 1st, 6th, and 13th instructions in the assembly code.
[0037] In this embodiment, the binary code is preprocessed, disassembled to generate assembly code, and the binary function is run to obtain the execution trajectory of the function. Specifically, the binary code is split into code blocks based on functions, and the binary function is executed by the cross-architecture and cross-platform CPU simulator Kadabra to obtain the assembly code of each binary function. For example, the registers when running to the 1st, 6th, and 13th instructions in the static assembly code, the state values of all registers when executing each instruction are saved during the Kadabra execution process, so as to extract the numerical sequence later.
[0038] As a preferred embodiment, an unlabeled binary function data set is determined based on static assembly code and dynamic execution traces, including: extracting features from the static assembly code to obtain a feature sequence; performing numerical conversion on the feature sequence based on the static assembly code and the dynamic execution trace, and using the obtained numerical sequence as the unlabeled binary function data set.
[0039] As a preferred embodiment, feature extraction is performed on static assembly code to obtain a feature sequence, including: segmenting the static assembly code to obtain a segmented code; replacing the memory address in the segmented code with a preset word symbol to obtain a feature sequence; the feature sequence includes a code sequence, a value sequence, an instruction set architecture sequence, an optimization level sequence, an instruction position sequence and a word position sequence.
[0040] Please refer to Figure 4 , Figure 4 A schematic diagram of word segmentation and value replacement processing of the code sequence provided by the present invention.
[0041] Based on the above embodiment, six feature sequences are extracted from the static assembly code, including code sequence, value sequence, instruction set architecture sequence, optimization level sequence, instruction position sequence and word position sequence. After obtaining the assembly code of the binary function, the assembly code is segmented and the word Token is extracted. For the memory address values (such as ) is replaced by an additional word symbol 'hexvar', and the immediate value is replaced by the word symbol 'num', finally obtaining a code sequence consisting of code words.
[0042] As a preferred embodiment, the feature sequence is numerically converted according to the static assembly code and the dynamic execution trajectory, and the obtained numerical sequence is used as an unlabeled binary function data set, including: traversing the feature sequence; when the word in the feature sequence is a register symbol, according to the dynamic execution trajectory, the register state value is used as the conversion value of the corresponding word; when the word in the feature sequence is a preset word symbol, according to the static assembly code, the memory address is used as the conversion value of the word; when the word in the feature sequence is other symbols, the preset empty symbol is used as the conversion value of the word; the other symbols are symbols other than the register symbol and the preset word symbol.
[0043] Please refer to Figure 5 , Figure 5 A schematic diagram of the preprocessed code sequence and numerical sequence provided by the present invention.
[0044] In this embodiment, the feature sequence (eg, code sequence) is traversed, and if the current word is a register symbol (eg, rdi), the register state value of the corresponding instruction (eg, ) as the conversion value of the corresponding word, and add the register state value to the numerical sequence of the word at the same position. If the current word is a preset word symbol (such as 'hexvar' or 'num'), the corresponding memory address value (such as ) or an immediate value is added to the value sequence. For other word symbols that do not have corresponding values (other symbols, such as lea), the '$$' symbol is added to the value sequence to indicate that the value at that position is empty. Finally, a numerical sequence corresponding to the code sequence is obtained, and each word in the code sequence corresponds to the value at the same position in the numerical sequence.
[0045] As a preferred embodiment, according to an unlabeled binary function data set, a language model is pre-trained by self-supervised mask learning to obtain a trained language model, including: inputting a numerical sequence into an embedding module to establish a word-to-index dictionary; the dictionary is an embedding matrix composed of word embedding vectors; the word embedding vector is a vector of digital mapping in the numerical sequence; based on the word-to-index dictionary, an encoding module of a multi-head attention mechanism is used to mask the feature sequence of the input language model; the cross entropy of the predicted mask value and the true mask value predicted by the training language model based on the context of the masked part is used as the loss function, and the language model is updated by back propagation to obtain the trained language model.
[0046] Please refer to Figure 6 , Figure 6 A diagram of the pre-trained model architecture provided by the present invention.
[0047] Please refer to Figure 7 , Figure 7 This is an input processing diagram of the embedding module provided by the present invention.
[0048] In this embodiment, the code sequence, value sequence, instruction set architecture sequence, optimization level sequence, instruction position sequence and word position sequence are input into the embedding module, a word-to-index dictionary is established, all six sequences are converted into numerical sequences, and then the numbers in the numerical sequence are mapped to the initial word embedding vector. All word vectors constitute an embedding matrix, the matrix length is the number of words in the code sequence, and the width is the word vector dimension. The Encoder (encoding module) of the 12-layer Transformer is superimposed as the encoding module of the pre-trained language model. Each layer of the Encoder includes 8 multi-head attention mechanism modules, and the words at the same position in the code sequence and the value sequence are randomly masked at a ratio of 15%. The training model predicts the masked instruction code and 16 hexadecimal program state values based on the context of the masked part, and the cross entropy of the predicted mask value and the real mask value is used as the loss function. The model parameters are updated by back propagation, thereby completing the self-supervised pre-training on the feature sequence data set.
[0049] As a preferred embodiment, the embedding module includes a coding layer, a gated recurrent unit and a fully connected layer; the numerical sequence is input into the embedding module to establish a word-to-index dictionary, including: converting each numerical value in the numerical sequence into a hexadecimal digital sequence; inputting the digital sequence into the coding layer to obtain an initial vector; inputting the initial vector into the gated recurrent unit to obtain an intermediate vector; inputting the intermediate vector into the fully connected layer to obtain a representation vector that integrates sequence information; performing maximum pooling on the representation vector, and using the pooled vector as a word embedding vector; and establishing a word-to-index dictionary based on the word embedding vector.
[0050] Please refer to Figure 8 , Figure 8 A schematic diagram of encoding numerical values in a numerical sequence provided by the present invention.
[0051] In this embodiment, the numerical values in the numerical sequence are first converted into 16-bit hexadecimal numbers and cut into a sequence of single digits. The numerical sequence is input into the encoding layer to be encoded into an initial vector, and then input into the two-layer gated recurrent unit GRU (Gate Recurrent Unit), and each layer output of the GRU is spliced. After the transformation of the fully connected layer, the representation vector of the fused sequence information is obtained. The representation vector is subjected to maximum pooling (Mean Pooling), and the pooled vector is used as the embedding vector of the numerical value in the value sequence.
[0052] The input and output structure of GRU is: there is a current input x t , and the hidden state h passed down from the previous node t -1 , this hidden state contains the relevant information of the previous node. Combined with x t and h t-1 , GRU will get the output y of the current hidden node t and the hidden state h passed to the next node t .
[0053] The internal structure of GRU is: first pass the state h transmitted from the previous t-1 and the input x of the current node t To get two gate states, r controls the reset gate and z controls the update gate.
[0054] As a preferred embodiment, according to a labeled binary function data set, supervised fine-tuning training is performed on the trained language model to obtain a fine-tuned language model, including: according to the labeled binary function data set, when the information of the binary function source file is unknown, function-level fine-tuning training is performed on the trained language model to obtain a function-level fine-tuned language model; the function-level fine-tuning training is to perform maximum pooling on the word vectors of the binary function to obtain an embedding vector of the binary function; according to the labeled binary function data set, when the information of the binary function source file is known, file-level fine-tuning training is performed on the trained language model to obtain a file-level fine-tuned language model; the file-level fine-tuning training is to model all functions in the binary file as an implicit global graph, use the graph attention network to learn the attention coefficient between nodes, and obtain the embedding vector of the binary function by message passing and neighbor aggregation; the embedding vector of the binary function is used to calculate the cosine similarity of the binary code.
[0055] Please refer to Fig. 9 , Fig. 9 This is a diagram of the fine-tuned model architecture provided by the present invention.
[0056] In this embodiment, based on the trained pre-trained language model, function-level fine-tuning and file-level fine-tuning are performed respectively according to whether the file source information of the binary function is known.
[0057] Based on the labeled binary function dataset, when the information of the binary function source file is unknown, the trained language model is fine-tuned at the function level to obtain a function-level fine-tuned language model. Function-level fine-tuning performs maximum pooling on the word vectors of the binary function to generate the embedding vector of the binary function, that is, taking the maximum value of each dimension in the word vectors of all binary functions.
[0058] According to the labeled binary function dataset, when the information of the binary function source file is known, the trained language model is fine-tuned at the file level to obtain the file-level fine-tuned language model. File-level fine-tuning models all functions in the same binary file as an implicit global graph. The global graph includes several nodes, each of which corresponds to an embedding vector. The attention coefficients between nodes are learned using the Graph Attention Network. Through message passing and neighbor aggregation, the vector representations of neighbor nodes are weighted summed according to the attention coefficients, and the summed vector is used as the embedding vector of the binary function corresponding to the current node.
[0059] Finally, the cosine similarity of the binary codes is calculated based on the embedding vector of the binary function.
[0060] The following is a description of a binary code clone detection system provided by the present invention. The binary code clone detection system described below and the binary code clone detection method described above can be referenced to each other.
[0061] Please refer to Fig.10 , Fig.10 A schematic structural diagram of a binary code clone detection system provided by the present invention.
[0062] The present invention also provides a binary code clone detection system, comprising: a first determination module 1, used to determine the static assembly code and dynamic execution trajectory of the binary function based on the binary code; a second determination module 2, used to determine an unlabeled binary function data set according to the static assembly code and the dynamic execution trajectory; a model training module 3, used to pre-train a language model according to the unlabeled binary function data set by adopting a self-supervised mask learning method to obtain a trained language model, so that the language model learns the execution semantics of the binary function; a model fine-tuning module 4, used to perform supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; a detection module 5, used to perform binary code clone detection according to the fine-tuned language model.
[0063] Fig.11 An example of a physical structure diagram of an electronic device is shown in FIG. Fig.11 As shown, the electronic device may include: a processor 1101, a communication interface 1102, a memory 1103 and a communication bus 1104, wherein the processor 1101, the communication interface 1102 and the memory 1103 communicate with each other through the communication bus 1104. The processor 1101 may call the logic instructions in the memory 1103 to execute a binary code clone detection method, the method comprising: determining a static assembly code and a dynamic execution track of a binary function based on the binary code; determining an unlabeled binary function data set according to the static assembly code and the dynamic execution track; pre-training a language model according to the unlabeled binary function data set by using a self-supervised mask learning method to obtain a trained language model so that the language model learns the execution semantics of the binary function; performing supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; and performing binary code clone detection according to the fine-tuned language model.
[0064] In addition, the logic instructions in the above-mentioned memory 1103 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0065] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the binary code cloning detection method provided by the above methods, which method includes: determining the static assembly code and dynamic execution trajectory of the binary function based on the binary code; determining an unlabeled binary function data set according to the static assembly code and the dynamic execution trajectory; pre-training a language model based on the unlabeled binary function data set using self-supervised mask learning to obtain a trained language model so that the language model learns the execution semantics of the binary function; performing supervised fine-tuning on the trained language model based on the labeled binary function data set to obtain a fine-tuned language model; and performing binary code cloning detection based on the fine-tuned language model.
[0066] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a binary code cloning detection method provided by the above methods, the method comprising: determining a static assembly code and a dynamic execution trajectory of a binary function based on the binary code; determining an unlabeled binary function data set according to the static assembly code and the dynamic execution trajectory; pre-training a language model according to the unlabeled binary function data set using a self-supervised mask learning method to obtain a trained language model, so that the language model learns the execution semantics of the binary function; performing supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain a fine-tuned language model; and performing binary code cloning detection according to the fine-tuned language model.
[0067] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0068] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0069] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for detecting clones of binary codes, characterized in that: include: Determine static assembly code and dynamic execution trace of binary function based on binary code; Determine an unlabeled binary function data set according to the static assembly code and the dynamic execution trace; Pre-training a language model using a self-supervised mask learning method according to the unlabeled binary function data set to obtain a trained language model, so that the language model learns the execution semantics of the binary function; According to the labeled binary function data set, supervised fine-tuning training is performed on the trained language model to obtain a fine-tuned language model; Binary code clone detection is performed based on the fine-tuned language model.
2. The binary code clone detection method according to claim 1, characterized in that: The method of determining the static assembly code and dynamic execution trajectory of the binary function based on the binary code includes: Splitting the binary code into code blocks based on functions; Based on the code block, a binary function is run to obtain a static assembly code and a dynamic execution trace of the binary function; the dynamic execution trace is used to characterize the state value of the register when each instruction in the static assembly code is executed.
3. The binary code clone detection method according to claim 1, characterized in that: The step of determining an unlabeled binary function data set according to the static assembly code and the dynamic execution trace includes: Extracting features from the static assembly code to obtain a feature sequence; According to the static assembly code and the dynamic execution trajectory, the feature sequence is numerically converted, and the obtained numerical sequence is used as the unlabeled binary function data set.
4. The method for detecting clones of binary codes according to claim 3, characterized in that: The feature extraction of the static assembly code to obtain a feature sequence includes: Segmenting the static assembly code to obtain segmented code; The memory address in the segmented code is replaced with a preset word symbol to obtain the feature sequence; the feature sequence includes a code sequence, a value sequence, an instruction set architecture sequence, an optimization level sequence, an instruction position sequence and a word position sequence.
5. The method for detecting clones of binary codes according to claim 4, characterized in that: The step of performing numerical conversion on the feature sequence according to the static assembly code and the dynamic execution trace, and using the obtained numerical sequence as the unlabeled binary function data set, comprises: traversing the feature sequence; In the case where a word in the feature sequence is a register symbol, according to the dynamic execution trajectory, a register state value is used as a conversion value corresponding to the word; In the case where a word in the feature sequence is the preset word symbol, according to the static assembly code, a memory address is used as a conversion value of the word; In the case where the word in the feature sequence is other symbols, a preset empty symbol is used as the conversion value of the word; the other symbols are symbols other than the register symbol and the preset word symbol.
6. The method for detecting clones of binary codes according to claim 5, characterized in that: The method of pre-training a language model by self-supervised mask learning according to the unlabeled binary function data set to obtain a trained language model includes: Input the numerical sequence into an embedding module to establish a word-to-index dictionary; the dictionary is an embedding matrix composed of word embedding vectors; the word embedding vector is a vector mapping the numbers in the numerical sequence; Based on the word-to-index dictionary, an encoding module using a multi-head attention mechanism is used to mask the feature sequence input into the language model; The language model is trained by taking the cross entropy of the predicted mask value and the true mask value predicted based on the context of the mask part as the loss function, and back-propagating to update the language model to obtain the trained language model.
7. The method for detecting clones of binary codes according to claim 6, characterized in that: The embedding module includes an encoding layer, a gated recurrent unit, and a fully connected layer; The step of inputting the numerical sequence into an embedding module and establishing a dictionary of words to indices comprises: Convert each value in the numerical sequence into a hexadecimal numerical sequence; Inputting the digital sequence into the coding layer to obtain an initial vector; Inputting the initial vector into the gated recurrent unit to obtain an intermediate vector; Inputting the intermediate vector into the fully connected layer to obtain a representation vector of fused sequence information; Performing maximum pooling on the representation vector, and using the pooled vector as the word embedding vector; According to the word embedding vector, a dictionary of the word to index is established.
8. The method for detecting clones of binary codes according to any one of claims 1 to 7, characterized in that: The method of performing supervised fine-tuning training on the trained language model according to the labeled binary function data set to obtain the fine-tuned language model includes: According to the labeled binary function data set, when the information of the binary function source file is unknown, the trained language model is fine-tuned at the function level to obtain the language model after function-level fine-tuning; the function-level fine-tuning training is to perform maximum pooling on the word vectors of the binary function to obtain the embedding vector of the binary function; according to the labeled binary function data set, when the information of the binary function source file is known, the trained language model is fine-tuned at the file level to obtain the language model after file-level fine-tuning; the file-level fine-tuning training is to model all functions in the binary file as an implicit global graph, use the graph attention network to learn the attention coefficients between nodes, and obtain the embedding vector of the binary function by message passing and neighbor aggregation; the embedding vector of the binary function is used to calculate the cosine similarity of the binary code.
9. A binary code clone detection system, characterized in that: include: A first determination module, used to determine the static assembly code and dynamic execution trajectory of the binary function based on the binary code; A second determination module, configured to determine an unlabeled binary function data set according to the static assembly code and the dynamic execution trace; A model training module is used to pre-train a language model using a self-supervised mask learning method according to the unlabeled binary function data set to obtain a trained language model so that the language model learns the execution semantics of the binary function; A model fine-tuning module, used to perform supervised fine-tuning training on the trained language model according to a labeled binary function data set to obtain a fine-tuned language model; A detection module is used to perform binary code clone detection based on the fine-tuned language model.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the binary code clone detection method according to any one of claims 1 to 8 is implemented.
Citation Information
Cited By
Binary program variable type recovery method, model training method and device
CN119536799A
