Code matching method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202210405253.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-18
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2042-04-18
AI Technical Summary
然而,由于不同伪代码或源代码间广泛存在特征串复用,以及对作用一致的高度语义化建模采用不同编写方式等问题,代码特征串匹配方法的误匹配率高,准确性较差,无法满足逆向分析要求
[0023]本申请的技术方案通过获取待识别程序对应的待识别伪代码;基于待识别伪代码在源代码特征库中进行相似代码搜索,得到伪代码特征对应的多条候选源代码;将多条候选源代码分别与待识别伪代码进行组合,得到多个代码对;对多个代码对进行分类识别处理,得到分类识别结果,分类识别结果表征多个代码对中的候选源代码与待识别伪代码间的同源概率;基于分类识别结果从多条候选源代码中确定出与待识别程序匹配的目标源代码。通过该技术方案,基于源代码特征和伪代码特征进行代码理解、匹配及分类识别,可以对整体代码进行宏观语义的相似性判断,不受代码文本语言、细节语法和复杂匹配规则的限制,能够实现伪代码和源代码间的跨语言文本精准匹配,并且,先基于代码特征进行候选源代码筛选,然后进行候选源代码与待识别伪代码间的分类识别,能够兼顾召回准确率和分类准确率的优化,显著提高代码匹配精度,降低系统部署难度。
Smart Images

Figure CN116992440B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and in particular to a code matching method, apparatus, electronic device, and storage medium. Background Technology
[0002] Reverse engineering is one of the important tasks in the field of software security. In the process of reverse engineering, it is necessary to analyze the program code, such as analyzing the specific meaning of the program's machine language. Currently, reverse engineering tools are usually used to convert the program's machine language into pseudocode. However, pseudocode lacks key information such as variable names and has poor readability. Therefore, it is more desirable to directly obtain the source code corresponding to the program's machine language in order to obtain detailed code information.
[0003] Related techniques typically extract code feature strings with anti-compilation optimization properties from machine language such as binary code and source code, such as strings, exported functions, string arrays, and global constant arrays, and then perform matching between binary code and source code based on these feature strings. However, due to the widespread reuse of feature strings between different pseudocodes or source code, and the use of different writing styles for highly semantic modeling with consistent functions, code feature string matching methods suffer from high mismatch rates and poor accuracy, failing to meet the requirements of reverse engineering. Therefore, an improved code matching scheme is needed to enhance matching accuracy and the reliability of reverse engineering results. Summary of the Invention
[0004] This application provides a code matching method, apparatus, device, and storage medium that can significantly improve the accuracy and reliability of code matching.
[0005] On the one hand, this application provides a code matching method, the method comprising:
[0006] Obtain the pseudocode corresponding to the program to be identified;
[0007] Based on the pseudocode to be identified, a similar code search is performed in the source code feature library to obtain multiple candidate source codes corresponding to the pseudocode to be identified.
[0008] The candidate source codes are combined with the pseudocode to be identified to obtain multiple code pairs;
[0009] The multiple code pairs are classified and identified to obtain classification and identification results, which represent the homology probability between the candidate source code in the multiple code pairs and the pseudocode to be identified.
[0010] Based on the classification and recognition results, the target source code that matches the program to be identified is determined from the multiple candidate source codes.
[0011] On the other hand, a code matching device is provided, the device comprising:
[0012] Pseudocode acquisition module: used to acquire the pseudocode to be identified for the program to be identified;
[0013] Similar code search module: used to perform similar code search in the source code feature library based on the pseudocode to be identified, and obtain multiple candidate source codes corresponding to the pseudocode to be identified;
[0014] Code combination module: used to combine the multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs;
[0015] Classification and recognition module: used to classify and recognize the multiple code pairs to obtain classification and recognition results, wherein the classification and recognition results characterize the homology probability between the candidate source code in the multiple code pairs and the pseudocode to be identified;
[0016] Source code determination module: used to determine the target source code that matches the program to be identified from the multiple candidate source codes based on the classification and recognition results.
[0017] On the other hand, a computer device is provided, the device including a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the code matching method as described above.
[0018] On the other hand, a computer-readable storage medium is provided, wherein at least one instruction or at least one program is stored therein, the at least one instruction or the at least one program being loaded and executed by a processor to implement the code matching method as described above.
[0019] On the other hand, a server is provided, the server including a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the code matching method as described above.
[0020] On the other hand, a terminal is provided, the terminal including a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the code matching method as described above.
[0021] On the other hand, a computer program product or computer program is provided, which includes computer instructions that, when executed by a processor, implement the code matching method described above.
[0022] The code matching method, apparatus, device, storage medium, server, terminal, computer program, and computer program product provided in this application have the following technical effects:
[0023] The technical solution of this application obtains the pseudocode corresponding to the program to be identified; searches for similar code in a source code feature library based on the pseudocode to be identified to obtain multiple candidate source codes corresponding to the pseudocode features; combines the multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs; performs classification and recognition processing on the multiple code pairs to obtain classification and recognition results, which represent the homology probability between the candidate source codes in the multiple code pairs and the pseudocode to be identified; and determines the target source code that matches the program to be identified from the multiple candidate source codes based on the classification and recognition results. This technical solution, based on source code features and pseudocode features, performs code understanding, matching, and classification and recognition, enabling macro-level semantic similarity judgment of the overall code, unrestricted by code text language, detailed syntax, and complex matching rules. It achieves accurate cross-language text matching between pseudocode and source code. Furthermore, by first screening candidate source codes based on code features and then performing classification and recognition between the candidate source codes and the pseudocode to be identified, it can optimize both recall accuracy and classification accuracy, significantly improving code matching accuracy and reducing system deployment difficulty. Attached Figure Description
[0024] To more clearly illustrate the technical solutions and advantages in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0025] Figure 1 This is a schematic diagram of an application environment provided in an embodiment of this application;
[0026] Figure 2 This is a flowchart illustrating a code matching method provided in an embodiment of this application;
[0027] Figure 3 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0028] Figure 4 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0029] Figure 5 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0030] Figure 6This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0031] Figure 7 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0032] Figure 8 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0033] Figure 9 This is a schematic diagram of the framework of a code refinement model provided in an embodiment of this application;
[0034] Figure 10 This is a schematic diagram of another code refinement model provided in the embodiments of this application;
[0035] Figure 11 This is a flowchart illustrating another code matching method provided in an embodiment of this application;
[0036] Figure 12 This is a schematic diagram of the framework of a code matching device provided in an embodiment of this application;
[0037] Figure 13 This is a hardware structure block diagram of an electronic device for a code matching method provided in an embodiment of this application. Detailed Implementation
[0038] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0039] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or server that comprises a series of steps or sub-modules is not necessarily limited to those steps or sub-modules explicitly listed, but may include other steps or sub-modules not explicitly listed or inherent to such processes, methods, products, or devices.
[0040] Before providing a further detailed description of the embodiments of this application, the nouns and terms involved in the embodiments of this application will be explained, and the nouns and terms involved in the embodiments of this application shall be interpreted as follows.
[0041] Source code, also known as source program, refers to an uncompiled text file written according to a specific programming language specification. It represents a series of human-readable computer language instructions. In modern programming languages, source code can appear in the form of books or magnetic tapes, but the most common format is a text file. This typical format is designed to be compiled into a computer program. The ultimate goal of computer source code is to translate human-readable text into binary instructions that a computer can execute. This process is called compilation and is accomplished by a compiler.
[0042] IDA (Interactive Disassembler): The IDA decompiler and debugger is an interactive, programmable, scalable, multiprocessor decompiler hosted on Windows, Linux, or Mac OS X that can convert binary code into pseudocode.
[0043] Machine language is a programming language or instruction code that a machine can directly recognize without translation. Each opcode has corresponding circuitry within the computer to execute it. In other words, it refers to a programming language or instruction code that a machine can directly understand and accept without translation. Machine language uses absolute addresses and absolute opcodes. Different computers have their own machine languages, i.e., instruction sets. From a user perspective, machine language is the lowest-level language.
[0044] Binary code: Code consisting of two basic characters, 0 and 1. Code element: A "bit" of binary code. Codeword: Any combination of N code elements is called a codeword.
[0045] Pseudocode, also known as virtual code, is a high-level method for describing algorithms. It is not a physical programming language; it may combine the syntax and reserved words of multiple programming languages, and even utilize natural language. It specifies the function of the algorithm in a programming language-like written form.
[0046] Neural networks: A neural network is a computational model with interconnected layers of nodes, whose hierarchical structure is similar to that of neurons in the brain. Neural networks can learn from data to be trained to recognize patterns, classify data, and predict future events.
[0047] BERT: Bidirectional Encoder Representations from Transformers, is a pre-training technique for natural language processing that can be used for vector modeling of sequential inputs.
[0048] TEXTCNN (Text Convolutional Neural Networks): A class of sequential input vector modeling algorithms that combine convolutional neural networks.
[0049] Recall: Searching for multiple similar pieces of content from a massive amount of data, resulting in a large number of content items.
[0050] Fine sorting: Refined sorting of multiple similar contents found on the recall side to obtain the most similar contents.
[0051] Artificial intelligence (AI) is the theory, methods, technology, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to achieve optimal results. In other words, AI is a comprehensive technology within computer science that attempts to understand the essence of intelligence and produce a new kind of intelligent machine that can react in a way similar to human intelligence. AI studies the design principles and implementation methods of various intelligent machines, enabling them to possess the functions of perception, reasoning, and decision-making.
[0052] Artificial intelligence (AI) is a comprehensive discipline encompassing a wide range of fields, including both hardware and software technologies. Fundamental AI technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interactive systems, and mechatronics. AI software technologies primarily include computer vision, speech processing, natural language processing, and machine learning / deep learning.
[0053] Computer vision (CV) is the science that studies how to enable machines to "see." More specifically, it refers to machine vision, which uses cameras and computers to replace human eyes in recognizing and measuring targets, and then performs image processing to create images more suitable for human observation or transmission to instruments. As a scientific discipline, computer vision studies related theories and technologies, attempting to build artificial intelligence systems capable of extracting information from images or multidimensional data. Computer vision technologies typically include image processing, image recognition, image semantic understanding, image retrieval, OCR, video processing, video semantic understanding, video content / behavior recognition, 3D object reconstruction, 3D technology, virtual reality, augmented reality, simultaneous localization and mapping (SLAM), and common biometric recognition technologies such as facial recognition and fingerprint recognition.
[0054] Natural Language Processing (NLP) is an important field within computer science and artificial intelligence. It studies the theories and methods for enabling effective communication between humans and computers using natural language. NLP is a science that integrates linguistics, computer science, and mathematics. Therefore, research in this field involves natural language—the language people use in daily life—and thus it has a close relationship with linguistic research. NLP techniques typically include text processing, semantic understanding, machine translation, question answering, and knowledge graphs.
[0055] In recent years, with the research and progress of artificial intelligence technology, artificial intelligence technology has been widely used in many fields. The solutions provided in the embodiments of this application involve artificial intelligence technologies such as machine learning / deep learning and natural language processing, which are specifically illustrated through the following embodiments.
[0056] Please see Figure 1 , Figure 1 This is a schematic diagram of an application environment provided in an embodiment of this application, such as... Figure 1 As shown, the application environment may include at least terminal 01 and server 02. In practical applications, terminal 01 and server 02 can be directly or indirectly connected via wired or wireless communication, and this application does not impose any restrictions on this.
[0057] In this application embodiment, server 02 can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms.
[0058] Specifically, cloud technology refers to a hosting technology that unifies hardware, software, and network resources within a wide area network (WAN) or local area network (LAN) to achieve data computation, storage, processing, and sharing. Cloud technology can be applied to various fields, such as medical cloud, cloud IoT, cloud security, cloud education, cloud conferencing, AI cloud services, cloud applications, cloud calling, and cloud social networking. Based on the cloud computing business model, cloud technology distributes computing tasks across a resource pool composed of numerous computers, enabling various application systems to obtain computing power, storage space, and information services as needed. The network providing these resources is called the "cloud." From the user's perspective, the resources in the "cloud" are infinitely scalable, readily available, on-demand, expandable, and pay-as-you-go. As a provider of basic cloud computing capabilities, a cloud resource pool (referred to as a cloud platform, generally called IaaS (Infrastructure as a Service)) platform is established, deploying various types of virtual resources within the pool for external customers to choose from. The cloud resource pool mainly includes: computing devices (virtualized machines containing operating systems), storage devices, and network devices.
[0059] Based on logical function, a PaaS (Platform as a Service) layer can be deployed on top of the IaaS layer, and a SaaS (Software as a Service) layer can be deployed on top of the PaaS layer. Alternatively, SaaS can be deployed directly on top of IaaS. PaaS is a platform for running software, such as databases and web containers. SaaS refers to various types of business software, such as web portals and bulk SMS senders. Generally speaking, SaaS and PaaS are upper layers compared to IaaS.
[0060] Specifically, the server 02 mentioned above may include physical devices, such as network communication submodules, processors, and memory, and may also include software running on the physical devices, such as applications.
[0061] Specifically, terminal 01 may include physical devices such as smartphones, desktop computers, tablets, laptops, digital assistants, augmented reality (AR) / virtual reality (VR) devices, smart voice interaction devices, smart home appliances, smart wearable devices, and in-vehicle terminal devices, and may also include software running on the physical devices, such as applications.
[0062] In this embodiment, terminal 01 can be used to send code matching instructions and the program to be identified to server 02, so that server 02 can perform the corresponding code matching operation. Server 02 can be used to provide code matching services to obtain target source code that matches the program to be identified. It is understood that terminal 01 can also be used to decompile the machine language text of the program to be identified to obtain pseudocode to be identified, and send the pseudocode to be identified to server 02 to trigger the code matching service. Specifically, server 02 can also be used to provide model training services for a first pre-trained model and a second pre-trained model to obtain a code recall model and a code ranking model, and can also be used to store source code feature libraries, sample code sets, recall training sample sets, and ranking training sample sets, etc.
[0063] Furthermore, it is understandable that Figure 1 The example shown is merely an application environment for one code matching method, which may include more or fewer nodes; this application does not impose any limitations on this.
[0064] The application environment involved in this application embodiment, or the terminal 01 and server 02 in the application environment, can be a distributed system formed by connecting clients and multiple nodes (any form of computing device accessing the network, such as servers and user terminals) through network communication. The distributed system can be a blockchain system, which can provide the aforementioned code matching service and data storage service, etc.
[0065] The following describes a code matching method based on the aforementioned application environment. Embodiments of this application can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, and assisted driving. Please refer to... Figure 2 , Figure 2This is a flowchart illustrating a code matching method provided in an embodiment of this application. This specification provides method operation steps as shown in the embodiments or flowcharts, but based on conventional or non-inventive labor, more or fewer operation steps may be included. The order of steps listed in the embodiments is merely one possible execution order among many and does not represent the only execution order. In actual system or server product execution, the method can be executed sequentially according to the embodiments or drawings, or in parallel (e.g., in a parallel processor or multi-threaded processing environment). Specifically, as... Figure 2 As shown, the method may include the following steps S201-S209.
[0066] S201: Obtain the pseudocode corresponding to the program to be identified.
[0067] In this embodiment, the pseudocode to be identified can be obtained by decompiling the machine language text of the program to be identified. Machine language is a programming language or instruction code that a machine can directly recognize, such as binary code. Decompiling machine language can yield corresponding pseudocode, such as using a decompilation tool like IDA. Specifically, the machine language text of the program to be identified can be compiled from the entire program source file, or from a single business source file within the program file, or from functional code segments within the business source file.
[0068] S203: Based on the pseudocode to be identified, perform a similar code search in the source code feature library to obtain multiple candidate source codes corresponding to the pseudocode to be identified.
[0069] In this embodiment, the source code feature library includes multiple source code features corresponding to machine language text. These source code features are obtained by feature extraction from the source code. Specifically, source code can be vectorized and represented based on a source code representation model to achieve feature extraction. More specifically, a code retrieval model can be used to extract features from the pseudocode to be identified, obtaining code features to be identified. Based on these features, similar code retrieval is performed on the source code features in the source code feature library to obtain multiple candidate source code features. The source code corresponding to these candidate features is then identified as the candidate source for search, achieving source code retrieval. This source code representation model can be the same as the code retrieval model, i.e., based on the same sample training set and training task; or they can be different, such as having the same model architecture but based on different sample training sets and training tasks.
[0070] In practical applications, joint source code retrieval can be achieved through more than one code retrieval model. Please refer to the relevant documentation. Figure 3 S203 may include the following steps S2031-S2033.
[0071] S2031: Based on at least one code recall model, feature extraction is performed on the pseudocode to be identified to obtain at least one pseudocode feature.
[0072] S2032: Based on at least one pseudocode feature, perform a similar code search in the source code feature library to obtain the initial source code corresponding to at least one pseudocode feature.
[0073] S2033: Deduplicate the initial source code corresponding to at least one pseudocode feature to obtain multiple candidate source codes.
[0074] In a specific embodiment, in the case of joint recall, at least two code recall models and at least two source code feature libraries are set up, with a one-to-one correspondence between the source code feature libraries and the code recall models. Based on each code recall model, features are extracted from the pseudocode to be identified, obtaining pseudocode features corresponding to each code recall model. Then, for each pseudocode feature, similar code is searched in its corresponding source code feature library, and the initial source code recalled by different code recall models in the corresponding source code feature libraries is deduplicated to obtain multiple candidate source codes. Specifically, the source code features in the source code feature library are obtained by feature extraction from the source code using the corresponding code recall model.
[0075] For example, a first code retrieval model extracts features from the pseudocode to be identified, obtaining first pseudocode features. The source code feature library corresponding to the first code retrieval model searches for source code features similar to the first pseudocode features to obtain first source code features that satisfy a first similarity condition. The source code to which the first source code features belong is determined as the first initial source code. A second code retrieval model extracts features from the pseudocode to be identified, obtaining second pseudocode features. The source code feature library corresponding to the second code retrieval model searches for source code features similar to the second pseudocode features to obtain second source code features that satisfy a first similarity condition. The source code corresponding to the second source code features is determined as the second initial source code. The first and second initial source codes are deduplicated to obtain multiple candidate source codes. In some embodiments, the first code retrieval model can be built based on BERT, and the second code retrieval model can be built based on TextCNN.
[0076] In this way, by setting up multiple code recall models and matching source code feature libraries, the pseudocode to be identified is mapped to different feature spaces for code semantic understanding and extraction, improving the comprehensiveness of semantic extraction. Joint recall in source code libraries of different feature spaces can optimize the recall coverage, make up for the recall rate problem of a single model, avoid source code omissions, and benefit the model accuracy and model performance of code ranking model.
[0077] In some embodiments, S2032 may specifically include: performing similarity processing on at least one pseudocode feature and source code features in the source code feature library to obtain feature similarity results; selecting initial source code features from the source code feature library based on the feature similarity results; and using the source code corresponding to the initial source code features as the initial source code.
[0078] Specifically, the feature similarity result includes the feature similarity between each source code feature and pseudocode feature in the source code feature library. In the case of joint recall, for each pseudocode feature, the similarity is calculated between each source code feature in the corresponding source code feature library and the pseudocode feature to obtain the feature similarity between each source code feature and pseudocode feature. Then, based on the feature similarity, the source code features in the corresponding source code feature library are sorted, and a first preset number of source code features with the highest feature similarity are selected as the initial source code features. The first preset number can be, for example, 50; or, source code features with feature similarity greater than or equal to the preset similarity are selected as the initial source code features. Accordingly, satisfying the first similarity condition can specifically mean that the feature similarity of the source code feature is ranked before the preset ranking or the feature similarity is greater than or equal to the preset similarity.
[0079] It should be noted that for different code retrieval models, the same first preset quantity or preset similarity can be set, or different first preset quantities or preset similarities can be set respectively.
[0080] S205: Combine multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs.
[0081] In this embodiment of the application, each candidate source code and the pseudocode to be identified are combined into a code pair, which is used as the input for subsequent classification and identification processing, so as to identify whether the candidate source code and the pseudocode to be identified in each code pair are from the same source.
[0082] S207: Perform classification and recognition processing on multiple code pairs to obtain classification and recognition results.
[0083] In this embodiment, the classification and recognition result represents the homology probability between candidate source code and pseudocode to be identified in multiple code pairs. The homology probability represents the probability that the candidate source code and pseudocode to be identified belong to the same machine language text. Specifically, the classification and recognition result includes the classification label and predicted classification probability for each code pair in the multiple code pairs. The classification label includes homology and non-homology labels, representing whether the candidate source code and pseudocode to be identified are homologous. The homology label can be 1, and the non-homology label can be 0. The classification probability represents the probability that the classification label of the code pair is a homology label. Specifically, each code pair can be classified and recognized based on a code ranking model to obtain the above classification label and homology probability. The code ranking model can be obtained by training a first pre-trained model under constraints of positive and negative sample pair recognition based on positive sample code pairs, negative sample code pairs, and corresponding sample pair labels.
[0084] In some embodiments, the candidate source code and the pseudocode to be identified in each code pair are concatenated to obtain the target concatenated code, which is then used as input to the code ranking model.
[0085] S209: Based on the classification and recognition results, determine the target source code that matches the program to be identified from multiple candidate source codes.
[0086] Specifically, the target source code can correspond to the overall program source file of the program to be identified, the business source file, or a functional code segment within the business source file. The target source code can be used for code de-analysis, assisting engineers in implementing risk control in scenarios such as security.
[0087] In summary, code understanding, matching, and classification based on source code and pseudocode features can perform macro-level semantic similarity judgments on the overall code, without being limited by code text language, detailed syntax, or complex matching rules. It can achieve accurate cross-language text matching between pseudocode and source code. Furthermore, by first screening candidate source code based on code features and then classifying and identifying the candidate source code and the pseudocode to be identified, it can optimize both recall accuracy and classification accuracy, significantly improving code matching accuracy and reducing system deployment difficulty.
[0088] In this embodiment, the target source code is a candidate source code that is from the same source as the code to be identified and whose similarity satisfies the second similarity condition. Accordingly, please refer to... Figure 4 S209 may include the following steps S2091-S2093.
[0089] S2091: Based on the classification and recognition results, select the target candidate source code that meets the preset classification conditions from multiple candidate source codes.
[0090] S2092: Sort the target candidate source code based on the classification and recognition results to obtain the similarity ranking results.
[0091] S2093: Based on the similarity ranking results, select the target source code from the candidate source code.
[0092] As mentioned above, the classification and recognition results include the classification label and classification probability of each code pair. If the preset classification conditions are met, the classification label can be a homologous label, that is, candidate source code whose classification result is homologous to the pseudocode to be identified is selected as the target candidate source code.
[0093] Furthermore, the selected target candidate source code is sorted based on classification probability, and the target candidate source code with the highest classification probability is selected as the target source code. This second preset number can be, for example, 10. In some embodiments, the target candidate source code with the highest classification probability at a preset percentage can also be selected as the target source code. This preset percentage can be, for example, the top 10% of all target candidate source code. In other embodiments, target candidate source code with a classification probability higher than a preset probability can also be selected as the target source code. Correspondingly, the second similarity condition can be: the classification probability of the target candidate source code is ranked before a preset ranking or the classification probability is greater than or equal to the preset probability.
[0094] Therefore, the first step is to identify candidate source code with the same source tag, and then the target source code is selected based on the classification probability, so as to avoid mismatching candidate source code with non-same source tags and improve the accuracy of code matching.
[0095] Based on some or all of the above implementation methods, in some embodiments, the machine language text of the program to be identified originates from a single business source file or a functional code segment within a business source file in the overall program file. Accordingly, before S201, the method may further include a step of acquiring the machine language text of the program to be identified. In some cases, this acquisition step specifically includes: acquiring the target machine language text corresponding to the program source file; segmenting the target machine language text based on the business source file information of the program file to obtain the machine language text to be identified and text position information corresponding to each business source file; or segmenting the target machine language text based on the business source file information and functional module information of the program source file to obtain the machine language text to be identified and corresponding text position information corresponding to each functional module. Wherein, the business source file information indicates the correspondence between text blocks in the business source file and the target machine language text; the functional module information indicates the correspondence between functional modules in a single business source file and text blocks in the target machine language text; and the text position information characterizes the position of the machine language text to be identified within the target machine language text, such as the text block order.
[0096] Furthermore, after obtaining the target source code, the target source code of each machine language text to be identified is concatenated based on the above text location information to obtain the source code text corresponding to the target machine language text.
[0097] In other cases, this acquisition step specifically includes: acquiring the target machine language text corresponding to the program file; decompiling the target machine language text to obtain the target pseudocode text; segmenting the target pseudocode text based on the business source file information of the program source file to obtain the pseudocode to be identified and its corresponding position information for each business source file; or segmenting the target pseudocode text based on the business source file information and functional module information of the program source file to obtain the pseudocode to be identified and its corresponding position information for each functional module. Here, the position information represents the position of the pseudocode to be identified within the target pseudocode text. It can be understood that, based on the position information of the pseudocode to be identified and the correspondence between the target pseudocode text and the target machine language text, the text block corresponding to the pseudocode to be identified within the target machine language text can be determined.
[0098] Furthermore, after obtaining the target source code, the target source code is concatenated based on the location information of the pseudocode to be identified to obtain the source code text corresponding to the target machine language text.
[0099] In this way, splitting program files based on business source file information or functional module information, and then identifying and matching them in segments, can reduce the difficulty of model learning and improve the model matching accuracy and robustness.
[0100] Based on some or all of the above embodiments, please refer to the embodiments of this application. Figure 5 The method may also include the following code recall model construction steps S301-S303.
[0101] S301: Obtain the recall training sample set, which includes sample pseudocode, as well as the positive sample source code and negative sample source code corresponding to the sample pseudocode.
[0102] S303: Using sample pseudocode, and the corresponding positive and negative sample source code as input to the first pre-trained model, the first pre-trained model is trained under the constraint of classifying positive and negative sample source code based on a self-supervised contrastive learning method to obtain a code recall model.
[0103] In practical applications, positive sample source code and sample pseudocode are homologous, while negative sample source code and sample pseudocode are not homologous. Sample pseudocode and its corresponding positive and negative sample source codes form sample triplets, which serve as input to the first pre-trained model for iterative training using comparative learning. During training, the first pre-trained model extracts features and classifies the sample pseudocode, positive sample source code, and negative sample source code, obtaining a first feature distance between the sample pseudocode and the positive sample source code, and a second feature distance between the sample pseudocode and the negative sample source code. The goal of model loss optimization is to narrow the first feature distance and widen the second feature distance until the training convergence condition is met, such as reaching a preset number of iterations. In some embodiments, the feature distances can be obtained based on the cosine similarity method. The first pre-trained model can be built based on BERT or TextCNN, and the loss function can be a triplet loss function.
[0104] In practical applications, please refer to Figure 6 S301 may specifically include the following steps S3011-S3013.
[0105] S3011: Obtain the sample code set, which includes sample source code and at least one sample pseudocode that matches the sample source code.
[0106] In a specific embodiment, at least one sample pseudocode matching the sample source code corresponds to machine language text of at least one compilation type. Specifically, at least one sample pseudocode corresponding to the same sample source code is obtained by decompiling machine language text of at least one compilation type, or by decompiling the same machine language text based on at least one decompilation type. It is understood that for the same sample source code, the machine language text obtained through different compilation types is different, and consequently, the sample pseudocode obtained by decompiling it is also different, or the sample pseudocode obtained by the same machine language text through different decompilation types is also different. For example, the text content of machine language text compiled on different machine platforms or different machine models is different, and the text content of sample pseudocode obtained by decompiling the same machine language text on different machine platforms or different machine models is also different.
[0107] S3012: Using sample source code that matches at least one sample pseudocode as positive sample source code and sample source code that does not match at least one sample pseudocode as negative sample source code, triple sampling is performed on the sample code set to obtain multiple sample triples.
[0108] S3013: Construct a recall training sample set based on multiple sample triples.
[0109] In a specific embodiment, the matching sample source code and sample pseudocode are the same source code, and each sample source code in the sample code set can have multiple matching sample pseudocodes. Specifically, for each sample pseudocode in the sample code set, its matching sample source code is taken as a positive sample, and a non-matching sample source code is sampled as a negative sample, forming a sample triplet, thus obtaining a recall training sample set including multiple sample triplets. In this way, pseudocode expansion based on different compilation or decompilation types improves the coverage of different types of pseudocode text, thereby optimizing the semantic understanding comprehensiveness and model performance of the recall model and the ranking model. Furthermore, using pseudocode as a sampling source for triplet matching can increase the number of triplet samples, which is beneficial for training data augmentation and model training effect.
[0110] For example, 10,000 lines of source code used in the business are selected as sample source code, and the pseudocode corresponding to the binary code text compiled on 10 machine platforms is selected as sample pseudocode to form a sample code set, with a sample pseudocode number of 10*10,000 lines. Triple sampling is performed on each sample pseudocode in this sample code set, with 10*10,000 sample triples corresponding to the same source code.
[0111] Based on some or all of the above embodiments, please refer to the embodiments of this application. Figure 7 The method may also include the following code ranking model construction steps S401-S403.
[0112] S401: Obtain the fine-ranking training sample set, which includes positive sample code pairs, negative sample code pairs, and corresponding sample pair labels.
[0113] S403: Using positive and negative sample code pairs as inputs to the second pre-trained model and sample pair labels as the expected outputs, the second pre-trained model is trained with constraints on positive and negative sample pair classification to obtain the code ranking model.
[0114] In practical applications, positive sample code pairs consist of sample pseudocode and matching sample source code, with the sample pair label being a homologous label, marked as 1. Negative sample pairs consist of sample pseudocode and non-matching sample source code, with the sample pair label being a non-homologous label, marked as 0. During training, the second pre-trained model iteratively trains to extract features and classify the sample pseudocode and sample source code in the sample code pairs. The loss function can be cross-entropy loss. Specifically, the input to the second pre-trained model can be a concatenated sequence of sample pseudocode and sample source code. In some embodiments, the second pre-trained model can be built based on the BERT model, in which case the input is "[CLS]sample source code[SEP]sample is code", and the output is the sample pair label 1 or 0. Thus, the code recall model is used for initial recall of similar code, and the code ranking model is used for classification and identification of candidate source code. The code matching process is broken down into steps, and the code recall model and code ranking model are trained and optimized separately. The recall accuracy and classification accuracy are optimized in parallel, reducing the performance requirements of the model and the difficulty of system deployment in a single step, and improving the robustness of the system.
[0115] In practical applications, please refer to Figure 8 S401 may specifically include the following steps S4011-S4013.
[0116] S4011: Based on the code recall model and each sample source code in the sample code set, recall the sample pseudocode in the sample code set to obtain the reference pseudocode set corresponding to each sample source code.
[0117] S4012: Combine each sample source code with the sample pseudocode in the reference pseudocode set to obtain positive sample code pairs and negative sample code pairs.
[0118] S4013: Construct a fine-ranking training sample set based on positive and negative sample code pairs.
[0119] In a specific embodiment, based on the code recall model trained as described above, each sample source code is used as a recall anchor point to recall sample pseudocode within the sample code set. This yields sample pseudocode identified by the code recall model as similar to the sample source code, resulting in a reference pseudocode set. This code recall process is similar to the aforementioned step S203. Thus, using sample source code as a recall anchor point enables the recall of pseudocode from the same source across multiple compilation types, increasing the proportion of positive sample pairs. This facilitates the expansion and balancing of training data, increases the number of effective code pairs in the finely ranked training sample set, and optimizes the model training effect.
[0120] Furthermore, each sample pseudocode in the reference pseudocode set is combined with the corresponding sample source code to form a sample code pair. If the sample source code and the sample pseudocode are the same source code, they are marked as positive sample code pairs, i.e., homologous labels; otherwise, they are marked as negative sample code pairs, i.e., non-homologous labels, thus obtaining the fine-ranking training sample set.
[0121] For example, the sample code set includes 10,000 source codes and 10 * 10,000 pseudocode samples belonging to different machine platforms. Similar code recall is performed using the 10,000 source codes. For each source code sample, a reference pseudocode set of 50 pseudocode samples is recalled. This reference pseudocode set includes positive pseudocode samples from the same source as the source code sample but belonging to different machine platforms, and negative pseudocode samples from different sources. The source code sample and the positive pseudocode sample are combined to form correctly matched positive sample code pairs, labeled as 1. The source code sample and the negative pseudocode sample are combined to form non-matching negative sample code pairs that are difficult for the code recall model to distinguish, labeled as 0. This generates a refined ranking training sample set, which is used to iteratively train the second pre-trained model to obtain the refined code ranking model.
[0122] Based on some or all of the above implementation methods, in some embodiments, the code ranking model performs classification and recognition processing based on the text features of code pairs. Please refer to [link / reference]. Figure 9 The system includes a word segmentation module, a feature extraction module, a classification and recognition module, and an output layer. Specifically, S207 can include: combining the pseudocode to be identified and the candidate source code; concatenating each code pair to obtain the target concatenated code, which serves as input to the code ranking model; then, performing word segmentation on the target concatenated code based on the word segmentation module to obtain a concatenated word segmentation sequence; subsequently, extracting features from the concatenated word segmentation sequence based on the feature extraction module to obtain a concatenated feature sequence, which includes the pseudocode word segmentation feature sequence of the pseudocode to be identified and the source code word segmentation feature sequence of the candidate source code; further, performing classification and recognition processing on the concatenated feature sequence based on the classification and recognition module, specifically by performing similarity processing on the pseudocode word segmentation feature sequence and the source code word segmentation feature sequence to obtain the feature distance between the candidate source code and the pseudocode to be identified; determining the classification probability of their common origin based on the feature distance; if the classification probability is higher than a preset probability, determining its classification label as 1, otherwise the classification label as 0; and finally, outputting the above classification probability and classification label through the output layer.
[0123] In other embodiments, the code ranking model performs classification and recognition processing based on the multimodal features of code pairs; please refer to [reference needed]. Figure 10It includes a word segmentation module, a feature extraction module, a feature fusion module, a classification and recognition module, and an output layer. The feature extraction module includes a word segmentation feature extraction submodule and an image feature extraction submodule. Accordingly, S207 can specifically include the following steps S2071-S2075.
[0124] S2071: Extract text features from the pseudocode to be identified and the candidate source code in multiple code pairs to obtain pseudocode text features and source code text features.
[0125] Specifically, the word segmentation module performs word segmentation and position extraction on each code pair to obtain pseudocode word segmentation sequences and source code word segmentation sequences, as well as the position information of the pseudocode word segments and source code word segments. Then, the word segmentation feature extraction submodule extracts features from the pseudocode word segmentation sequences and their position information, as well as the source code word segmentation sequences and their position information, to obtain pseudocode text features and source code text features. The position information of the pseudocode word segments includes their position within the pseudocode to be identified, and the position information of the source code word segments includes their position within the candidate source code.
[0126] S2072: Extract image features from the pseudocode to be identified and the candidate source code in multiple code pairs to obtain pseudocode image features and source code image features.
[0127] In one embodiment, the pseudocode image and the source code image can be extracted as a whole based on the image feature extraction submodule to obtain pseudocode image features and source code image features.
[0128] In another embodiment, the pseudocode image features are obtained by splicing the first segmentation image features of the pseudocode segmentation in the pseudocode to be identified, and the source code image features are obtained by splicing the second segmentation image features of the source code segmentation in the candidate source code.
[0129] Correspondingly, in some cases, S2072 may specifically include: converting each pseudocode word in the pseudocode word segmentation sequence into an image format to obtain a pseudocode word segmentation image; converting each source code word in the source code word segmentation sequence into an image format to obtain a source code word segmentation image; performing feature extraction on the pseudocode word segmentation image based on the image feature extraction submodule to obtain the first word segmentation image feature of each pseudocode word; and performing feature extraction on the source code word segmentation image to obtain the second word segmentation image feature of each source code word.
[0130] In other cases, S2072 may specifically include: acquiring the pseudocode image corresponding to the pseudocode to be identified and the source code image corresponding to the candidate source code. The pseudocode image and source code image can be image-based data of the pseudocode to be identified and the candidate source code, which can be acquired through scanning, photography, or format conversion. Character recognition and word segmentation are performed on the pseudocode image and source code image to obtain pseudocode word segments and their positions in the pseudocode image, and to obtain source code word segments and their positions in the source code image. Character recognition can be performed using OCR methods, and word segmentation can be performed by a word segmentation module. The aforementioned pseudocode word segmentation positions and source code word segmentation positions include the vertex coordinates of the corresponding word, as well as the width and height of the word segment.
[0131] Furthermore, based on the pseudocode segmentation position and the source code segmentation position, a first region image of each pseudocode segmentation is segmented from the pseudocode image, and a second region image of each source code segmentation is segmented from the source code image; feature extraction is performed on the first region image to obtain the first segmentation image features of each pseudocode segmentation, and feature extraction is performed on the second region image to obtain the second image features of each source code segmentation.
[0132] Furthermore, based on the pseudocode segmentation position order described above, the first segmentation image features of each pseudocode segmentation are concatenated to obtain pseudocode image features; based on the source code segmentation position order described above, the second segmentation image features of each source code segmentation are concatenated to obtain source code image features.
[0133] S2073: Fuse pseudocode text features and pseudocode image features to obtain pseudocode fused features.
[0134] S2074: The source code text features and source code image features are fused to obtain source code fused features.
[0135] Specifically, the feature fusion module fuses pseudocode text features and pseudocode image features, such as by adding them together, to obtain pseudocode fused features. It also fuses source code text features and pseudocode image features to obtain source code fused features.
[0136] S2075: Classify and identify the pseudocode fusion features and source code fusion features corresponding to multiple code pairs to obtain the classification and identification results.
[0137] Specifically, pseudocode fusion features and source code fusion features can be concatenated and input into the classification and recognition module to obtain the classification and recognition results, and the above classification probabilities and classification labels can be output through the output layer.
[0138] Thus, by combining text segmentation features and image features for classification and recognition, and by performing semantic understanding of pseudocode and source code based on multimodal semantic information, the impact of code segmentation errors on model performance and classification results can be reduced, thereby improving the accuracy of code matching.
[0139] In other embodiments, when classification and recognition are performed based on multimodal features, the feature extraction module of the code ranking model further includes a positional feature embedding submodule. Accordingly, the positional feature embedding submodule performs feature embedding processing on the pseudocode segmentation order, pseudocode segmentation position, source code segmentation order, and source code segmentation position to obtain the first segmentation position feature of each pseudocode segment and the second segmentation position feature of each source code segment. Then, based on the pseudocode segmentation order, the first segmentation position features are concatenated to obtain the first positional feature of the pseudocode to be identified; and based on the source code segmentation order, the second segmentation position features are concatenated to obtain the second positional feature of the candidate source code.
[0140] Furthermore, the feature fusion module performs feature fusion on pseudocode text features, pseudocode image features, and the first position feature to obtain pseudocode fused features, and performs feature fusion on source code text features, source code image features, and the second position feature to obtain source code fused features, which are then input into the classification and recognition module to obtain classification and recognition results.
[0141] In this way, combining text, image, and location features for classification feature recognition further improves the accuracy and comprehensiveness of semantic understanding, thereby enhancing model performance and accuracy.
[0142] Based on some or all of the above implementation methods, in some embodiments, during the word segmentation process of source code and pseudocode, a private corpus set and a word segmentation set are constructed. The private corpus set includes source code text of various business types, as well as pseudocode text corresponding to different compilation or decompilation types. The word segmentation set includes commonly used words in source code and pseudocode. The word segmentation processing module is trained in a targeted manner with the above corpus set and word segmentation set, which can reduce the error rate of code word segmentation, accelerate the convergence speed of the second pre-trained model, and optimize the model effect of the code ranking model.
[0143] Specifically, the word segmentation module can be built based on the Byte Pair Encoding (BPE) algorithm, the text feature extraction submodule can be built based on the BERT model, and the image feature extraction module can be used to perform convolution processing on images to obtain image features. It can be built based on convolutional neural networks (CNNs), such as ResNet.
[0144] The following combination Figure 11This application describes the code matching process. In this embodiment, the first code recall model is the BERT recall model, the second code recall model is the TextCNN recall model, and the code ranking model is the BERT ranking model. The code matching process includes the following steps:
[0145] S1. Obtain the source code to be matched.
[0146] S2. Based on the BERT recall model, feature extraction is performed on the source code, that is, each source code to be matched is vectorized to obtain the first source code feature. Based on the TextCNN recall model, feature extraction is performed on the source code to obtain the second source code feature. Then, the first source code feature library constructed from the first pseudocode feature and the second source code feature library constructed from the second pseudocode feature are obtained.
[0147] S3. Obtain the binary pseudocode to be identified, which is obtained by decompiling the binary text to be identified.
[0148] S4. Based on the BERT recall model and the TextCNN recall model respectively, the binary pseudocode in S3 is extracted to obtain the first pseudocode feature and the second pseudocode feature.
[0149] The first pseudocode feature shares the same feature space as the first source code feature, and the second pseudocode feature shares the same feature space as the second source code feature. For example, the first pseudocode feature and the first source code feature can be 768-dimensional embedding sequences, and the second pseudocode feature and the second source code feature can be 256-dimensional embedding sequences.
[0150] S5. Based on the first pseudocode features, perform similar code recall in the first source code feature library to obtain a first number of first candidate features, and use the source code corresponding to the first candidate features as the first initial source code.
[0151] S6. Based on the second pseudocode features, perform similar code recall in the second source code feature library to obtain a second number of second candidate features, and use the source code corresponding to the second candidate features as the second initial source code.
[0152] The similarity between source code features and pseudocode features can be measured by comparing the spatial distance between feature vectors to achieve similar code recall. This process is the same as step S203 described above. For example, both the first and second quantities can be 50.
[0153] S7. Perform deduplication on the first and second initial source codes to obtain multiple candidate source codes.
[0154] S8. Combine the candidate source code with the pseudocode to be identified to obtain multiple code pairs.
[0155] For example, after deduplication, 70 candidate source codes are obtained, resulting in 70 code pairs.
[0156] S9. Input multiple code pairs into the BERT fine-ranking model for classification and recognition, achieve fine-ranking matching, and obtain the target source code that matches correctly.
[0157] For example, the top 1 code pairs by classification probability can be selected, and their candidate source code can be used as the target source code. Alternatively, the top 10 code pairs by classification probability can be selected to obtain 10 target source codes. The classification probability can be the similarity score between the pseudocode to be identified and the candidate source code in the code pair, or a mapping value of that similarity score. Specifically, the BERT model described above can be configured with 12 encoder layers and 12 decoder layers.
[0158] The code matching method of this application will be introduced below in conjunction with specific application scenarios.
[0159] In one embodiment, the program to be identified is a cheat program for the target game. Here, a cheat program can refer to an unauthorized third-party cheat program that needs to be detected for any risky calls or modifications. Specifically, the binary code text of the cheat program to be identified is obtained, and the binary code text is decompiled to obtain the pseudocode to be identified.
[0160] In some cases, features are extracted from the pseudocode of the cheat program to be identified based on both the BERT recall model and the TextCNN recall model to obtain first pseudocode features and second pseudocode features. Based on the first pseudocode features, similar code is recalled in the first source code feature library corresponding to the BERT recall model to obtain a first number of first initial source codes. Based on the second pseudocode features, similar code is recalled in the second source code feature library corresponding to the TextCNN recall model to obtain a second number of second initial source codes. The first number of first initial source codes and the second number of second initial source codes are deduplicated to obtain multiple candidate source codes. Further, these multiple candidate source codes are combined with the pseudocode of the cheat program to be identified to obtain multiple code pairs, which are used as input to the BERT fine-ranking model to obtain classification results. Based on the classification probabilities in the classification results, the source codes in the top M code pairs are selected as the target source codes. Then, the target source code is traversed and matched based on pre-stored risk identification information to locate risks. Source code segments containing or matching pre-stored risk identification information are selected and identified as risky code segments for targeted security defense and cheat detection. The pre-stored risk identification information includes one or more of the following: target source keywords, target source strings, and target source code segments, providing key information indicating risky calls, commands, or program modifications.
[0161] In other cases, based on pre-stored risk identification information, the pseudocode to be identified is traversed and matched to locate risks, and pseudocode segments that include or match pre-stored risk identification information are selected. The selected pseudocode segments are identified as risky pseudocode segments, and then the above-mentioned steps of feature extraction, similar code recall, code pair combination, classification and identification and fine sorting are performed on the risky pseudocode segments to obtain the target source code corresponding to the risky pseudocode segments for targeted security defense and cheat detection.
[0162] In another embodiment, the program to be identified is a game program. Specifically, the binary code text of the game program to be identified is obtained, and the binary code text is decompiled to obtain pseudo-code to be identified. The pseudo-code to be identified is then subjected to the aforementioned steps of feature extraction, similar code retrieval, code pair combination, classification and identification, and fine sorting to obtain the target source code that matches the program to be identified. Compliance detection is performed on the target source code based on a non-compliance information database to obtain compliance detection results. The non-compliance information database includes one or more of the following: target source keywords, target source strings, and target source code segments, used to indicate key information of non-compliant instructions or code segments. Compliance detection includes traversing and matching the target source code based on the non-compliance information database. The compliance detection results characterize whether there are non-compliant code segments in the game program to be identified and the location information of the non-compliant code segments in the target source code, such as whether there are code instructions corresponding to unauthorized acquisition of classified information, terrorist content, and unauthorized calls.
[0163] In summary, this application establishes a step-by-step matching process for code recall and fine-grained ranking. It breaks down the matching task flow by first using a code recall model for multi-path recall and result merging, and then fine-grained ranking of the recalled similar source code to obtain the most similar target source code. This improves recall and code coverage, reduces the probability of missing correct source code, and allows for separate training of the code recall model and the code fine-grained ranking model. This approach balances the separation and parallel optimization of recall and matching accuracy, improving model performance in modules and reducing the feature extraction requirements, training difficulty, and system deployment complexity of individual modules. Furthermore, the method effectively models the semantic similarity between source code and pseudocode, enabling matching and understanding of semantic code text without being limited by detailed syntactic similarity, strict text consistency, or complex matching rules. It can achieve cross-language text matching of source code and pseudocode, and during the reverse engineering stage, it can effectively retrieve the code regions of machine language text and their source code execution logic.
[0164] This application also provides a code matching device 600, such as... Figure 12 As shown, Figure 12 The diagram shows a structural schematic of a code matching device provided in an embodiment of this application. The device may include the following modules.
[0165] Pseudocode acquisition module 10: Used to acquire the pseudocode to be identified corresponding to the program to be identified;
[0166] Similar code search module 20: used to search for similar code in the source code feature library based on the pseudocode to be identified, and obtain multiple candidate source codes corresponding to the pseudocode to be identified;
[0167] Code combination module 30: used to combine multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs;
[0168] Classification and recognition module 40: used to classify and recognize multiple code pairs to obtain classification and recognition results. The classification and recognition results represent the homology probability between candidate source code and pseudocode to be identified in multiple code pairs.
[0169] Source code determination module 50: used to determine the target source code that matches the program to be identified from multiple candidate source codes based on the classification and recognition results.
[0170] In some embodiments, the similar code search module 20 may include:
[0171] Pseudocode feature extraction submodule: used to extract features from the pseudocode to be identified based on at least one code recall model, and obtain at least one pseudocode feature;
[0172] Similar code search submodule: used to search for similar code in the source code feature library based on at least one pseudocode feature, and obtain the initial source code corresponding to at least one pseudocode feature;
[0173] The deduplication submodule is used to deduplicatize the initial source code corresponding to at least one pseudocode feature to obtain multiple candidate source codes.
[0174] In some embodiments, the apparatus may further include:
[0175] The first sample set acquisition module is used to acquire the recall training sample set, which includes sample pseudocode and the corresponding positive and negative sample source code.
[0176] The first training module is used to train the first pre-trained model by taking sample pseudocode and the corresponding positive and negative sample source code as input. It uses a self-supervised contrastive learning method to perform constrained training on the first pre-trained model to classify positive and negative sample source code, thereby obtaining a code recall model.
[0177] In some embodiments, the first sample set acquisition module may include:
[0178] Sample code set acquisition submodule: used to acquire sample code set, which includes sample source code and at least one sample pseudocode that matches the sample source code, wherein the at least one sample pseudocode that matches the sample source code corresponds to machine language text of at least one compilation type;
[0179] The triplet sampling submodule is used to sample the sample code set by taking the sample source code that matches at least one sample pseudocode as the positive sample source code and the sample source code that does not match at least one sample pseudocode as the negative sample source code, and to obtain multiple sample triplets.
[0180] The first sample set construction submodule is used to construct a recall training sample set based on multiple sample triples.
[0181] In some embodiments, the apparatus may further include:
[0182] The second sample set acquisition module is used to acquire the fine-ranking training sample set, which includes positive sample code pairs, negative sample code pairs, and corresponding sample pair labels.
[0183] The second training module is used to train the second pre-trained model with positive and negative sample code pairs as inputs and sample pair labels as expected outputs, thereby obtaining a code ranking model.
[0184] Accordingly, the classification and recognition module 40 can be specifically used to: classify and recognize multiple code pairs based on the code fine sorting model to obtain classification and recognition results.
[0185] In some embodiments, the second sample set acquisition module may include:
[0186] Sample Recall Submodule: Used to recall sample pseudocode from the sample code set based on the code recall model and the source code of each sample in the sample code set, and obtain the reference pseudocode set corresponding to each sample source code;
[0187] Sample code combination submodule: used to combine each sample source code with the sample pseudocode in the reference pseudocode set to obtain positive sample code pairs and negative sample code pairs;
[0188] The second sample set construction submodule is used to construct a finely ranked training sample set based on positive and negative sample code pairs.
[0189] In some embodiments, the similar code search submodule may include:
[0190] Similarity processing unit: used to perform similarity processing on at least one pseudocode feature and source code features in the source code feature library to obtain feature similarity results;
[0191] Initial source code feature filtering unit: used to filter initial source code features from the source code feature library based on feature similarity results;
[0192] Initial source code determination unit: used to determine the source code corresponding to the initial source code characteristics as the initial source code.
[0193] In some embodiments, the source code determination module 50 may include:
[0194] Target candidate source code filtering submodule: used to filter target candidate source code that meets preset classification conditions from multiple candidate source codes based on classification recognition results;
[0195] Candidate source code sorting submodule: used to sort the target candidate source code based on the classification and recognition results to obtain the similarity ranking results;
[0196] Target source code filtering submodule: Used to filter out a first preset number of target source codes from the candidate source codes based on the similarity ranking results.
[0197] In some embodiments, the classification and recognition module 40 may include:
[0198] The text feature extraction submodule is used to extract text features from the pseudocode to be identified and the candidate source code in multiple code pairs, and obtain the pseudocode text features and the source code text features.
[0199] Image feature extraction submodule: used to extract image features from the pseudocode to be identified and the candidate source code in multiple code pairs, to obtain pseudocode image features and source code image features;
[0200] The first fusion submodule is used to fuse pseudocode text features and pseudocode image features to obtain pseudocode fused features.
[0201] The second fusion submodule is used to fuse the source code text features and source code image features to obtain source code fused features.
[0202] The classification and recognition submodule is used to classify and recognize the pseudocode fusion features and source code fusion features corresponding to multiple code pairs, and obtain the classification and recognition results.
[0203] It should be noted that the above-described device embodiments and method embodiments are based on the same implementation methods.
[0204] This application provides a code matching device. The scheduling device can be a terminal or a server, including a processor and a memory. The memory stores at least one instruction or at least one program. The at least one instruction or at least one program is loaded and executed by the processor to implement the code matching method provided in the above method embodiments.
[0205] Memory is used to store software programs and modules. The processor executes various functional applications and code matching by running the software programs and modules stored in memory. Memory can primarily include a program storage area and a data storage area. The program storage area stores the operating system, application programs required for functionality, etc.; the data storage area stores data created based on device usage, etc. Furthermore, memory can include high-speed random access memory, and can also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. Accordingly, memory can also include a memory controller to provide the processor with access to the memory.
[0206] The methods and embodiments provided in this application can be executed in electronic devices such as mobile terminals, computer terminals, servers, or similar computing devices. Figure 13 This is a hardware structure block diagram of an electronic device using a code matching method provided in an embodiment of this application. For example... Figure 13 As shown, the electronic device 900 can vary significantly due to different configurations or performance. It may include one or more Central Processing Units (CPUs) 910 (CPUs 910 may include, but are not limited to, microprocessors such as MCUs or programmable logic devices such as FPGAs), a memory 930 for storing data, and one or more storage media 920 (e.g., one or more mass storage devices) for storing application programs 923 or data 922. The memory 930 and storage media 920 may be temporary or persistent storage. The program stored in the storage media 920 may include one or more modules, each module including a series of instruction operations on the electronic device. Furthermore, the CPU 910 may be configured to communicate with the storage media 920 and execute a series of instruction operations in the storage media 920 on the electronic device 900. The electronic device 900 may also include one or more power supplies 960, one or more wired or wireless network interfaces 950, one or more input / output interfaces 940, and / or one or more operating systems 921, such as Windows Server. TM Mac OS X TM Unix TM Linux™, FreeBSD™, etc.
[0207] The input / output interface 940 can be used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of the electronic device 900. In one example, the input / output interface 940 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the input / output interface 940 may be a radio frequency (RF) module used for wireless communication with the Internet.
[0208] Those skilled in the art will understand that Figure 13 The structure shown is for illustrative purposes only and does not limit the structure of the electronic device described above. For example, the electronic device 900 may also include... Figure 13 The more or fewer components shown, or having the same Figure 13 The different configurations shown.
[0209] Embodiments of this application also provide a computer-readable storage medium, which can be disposed in an electronic device to store at least one instruction or at least one program related to implementing a code matching method in the method embodiment. The at least one instruction or the at least one program is loaded and executed by the processor to implement the code matching method provided in the above method embodiment.
[0210] Optionally, in this embodiment, the storage medium may be located at at least one of the multiple network servers in a computer network. Optionally, in this embodiment, the storage medium may include, but is not limited to, various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0211] According to one aspect of this application, a computer program product or computer program is provided, comprising computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the methods provided in the various alternative implementations described above.
[0212] As can be seen from the embodiments of the code matching method, apparatus, device, server, terminal, storage medium, and program product provided in this application, the technical solution of this application obtains the pseudocode to be identified corresponding to the program to be identified; performs a similar code search in the source code feature library based on the pseudocode to be identified to obtain multiple candidate source codes corresponding to the pseudocode features; combines the multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs; performs classification and identification processing on the multiple code pairs to obtain classification and identification results, the classification and identification results representing the homology probability between the candidate source codes in the multiple code pairs and the pseudocode to be identified; and determines the target source code that matches the program to be identified from the multiple candidate source codes based on the classification and identification results. This technical solution enables code understanding, matching, and classification based on source code and pseudocode features. It allows for macro-level semantic similarity assessment of the entire code, unrestricted by code text language, detailed syntax, or complex matching rules. It achieves accurate cross-language text matching between pseudocode and source code. Furthermore, by first filtering candidate source code based on code features and then classifying the candidate source code with the pseudocode to be identified, it can optimize both recall accuracy and classification accuracy, significantly improving code matching precision and reducing system deployment difficulty.
[0213] It should be noted that the order of the embodiments described above is merely for descriptive purposes and does not represent the superiority or inferiority of the embodiments. Furthermore, the above description focuses on specific embodiments of this application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims can be performed in a different order than that shown in the embodiments and still achieve the desired results. Additionally, the processes depicted in the drawings do not necessarily require a specific or sequential order to achieve the desired results. In some implementations, multitasking and parallel processing are also possible or may be advantageous.
[0214] The various embodiments in this application are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the device, equipment, and storage medium embodiments are basically similar to the method embodiments, so the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0215] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware, or by a program instructing the relevant hardware to implement them. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.
[0216] The above are merely preferred embodiments of this application and are not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A code matching method, characterized in that, The method includes: Obtain the pseudo-code to be identified corresponding to the program to be identified, wherein the pseudo-code to be identified is obtained by decompiling the machine language text of the program to be identified; Based on the pseudocode to be identified, a similar code search is performed in the source code feature library to obtain multiple candidate source codes corresponding to the pseudocode to be identified. The candidate source codes are combined with the pseudocode to be identified to obtain multiple code pairs; The multiple code pairs are classified and identified to obtain classification and identification results. The classification and identification results represent the homology probability between the candidate source code and the pseudocode to be identified in the multiple code pairs. The homology probability represents the probability that the candidate source code and the pseudocode to be identified belong to the same machine language text. Based on the classification and recognition results, the target source code that matches the program to be identified is determined from the multiple candidate source codes; The step of searching for similar code in the source code feature library based on the pseudocode to be identified to obtain multiple candidate source codes corresponding to the pseudocode to be identified includes: Based on at least two code recall models, feature extraction is performed on the pseudocode to be identified to obtain pseudocode features corresponding to each code recall model; Based on the pseudocode features, similar code searches are performed in the corresponding source code feature library to obtain the initial source code corresponding to the pseudocode features. The source code feature library and the code recall model are set in a one-to-one correspondence. The source code features in the source code feature library are obtained by extracting features from the source code to be matched by the corresponding code recall model. Different source code feature libraries correspond to the same source code to be matched. The initial source code is deduplicated to obtain the multiple candidate source codes; Before obtaining the pseudocode to be identified corresponding to the program to be identified, the method further includes a step of obtaining the machine language text of the program to be identified. The obtaining step includes: obtaining the target machine language text corresponding to the program source file; segmenting the target machine language text based on the business source file information of the program source file to obtain the machine language text to be identified and the corresponding text position information for each business source file; or segmenting the target machine language text based on the business source file information and functional module information of the program source file to obtain the machine language text to be identified and the corresponding text position information for each functional module; wherein, the business source file information indicates the correspondence between the business source file and the text blocks in the target machine language text, the functional module information indicates the correspondence between the functional module in a single business source file and the text blocks in the target machine language text, and the text position information represents the position of the machine language text to be identified in the target machine language text; After obtaining the target source code, the method further includes: concatenating the target source code of the machine language text to be recognized based on the text location information to obtain the source code text corresponding to the target machine language text.
2. The method according to claim 1, characterized in that, The source code feature library includes source code features corresponding to multiple machine language texts, and the source code features are obtained by feature extraction from the source code.
3. The method according to claim 1, characterized in that, The method further includes: Obtain a recall training sample set, which includes sample pseudocode and the positive and negative sample source codes corresponding to the sample pseudocode; Using the sample pseudocode, and the corresponding positive and negative sample source codes as input to the first pre-trained model, the first pre-trained model is trained with constraints on positive and negative sample source code classification based on a self-supervised contrastive learning method to obtain the code recall model.
4. The method according to claim 3, characterized in that, The acquisition of the recall training sample set includes: Obtain a sample code set, the sample code set including sample source code and at least one sample pseudocode that matches the sample source code, wherein the at least one sample pseudocode that matches the sample source code corresponds to machine language text of at least one compilation type; The sample code set is sampled in triplets by taking the sample source code that matches the at least one sample pseudocode as the positive sample source code and the sample source code that does not match the at least one sample pseudocode as the negative sample source code, to obtain multiple sample triplets. The recall training sample set is constructed based on the multiple sample triples.
5. The method according to claim 1, characterized in that, The method further includes: Obtain a fine-ranking training sample set, which includes positive sample code pairs, negative sample code pairs, and corresponding sample pair labels; Using the positive sample code pairs and the negative sample code pairs as input to the second pre-trained model, and the sample pair labels as the expected output, the second pre-trained model is subjected to constraint training for positive and negative sample pair classification to obtain the code ranking model. The classification and recognition process for the multiple code pairs, resulting in classification and recognition results, includes: The multiple code pairs are classified and identified based on the code ranking model to obtain the classification and identification results.
6. The method according to claim 5, characterized in that, The acquisition of the fine-ranking training sample set includes: Based on the code recall model and each sample source code in the sample code set, the sample pseudocode in the sample code set is recalled to obtain a reference pseudocode set corresponding to each sample source code. Each sample source code is combined with the sample pseudocode in the reference pseudocode set to obtain positive sample code pairs and negative sample code pairs; The fine-ranking training sample set is constructed based on the positive sample code pairs and the negative sample code pairs.
7. The method according to any one of claims 2-6, characterized in that, The step of performing similar code searches in the corresponding source code feature library based on the pseudocode features to obtain the initial source code corresponding to the pseudocode features includes: The pseudocode features and the source code features in the source code feature library are subjected to similarity processing to obtain feature similarity results; Initial source code features are selected from the source code feature library based on the feature similarity results. The source code corresponding to the initial source code feature is used as the initial source code.
8. The method according to any one of claims 1-6, characterized in that, The step of determining the target source code that matches the program to be identified from the multiple candidate source codes based on the classification and recognition results includes: Based on the classification and recognition results, target candidate source code that meets the preset classification conditions is selected from the multiple candidate source codes; Based on the classification and recognition results, the target candidate source code is sorted to obtain a similarity ranking result; Based on the similarity ranking results, the target source code is selected from the target candidate source code.
9. The method according to any one of claims 1-6, characterized in that, The classification and recognition process for the multiple code pairs, resulting in classification and recognition results, includes: For each code pair, word segmentation and position extraction are performed to obtain pseudocode word segmentation sequence and source code word segmentation sequence, as well as position information of pseudocode word segmentation and source code word segmentation. The position information of pseudocode word segmentation includes the pseudocode word segmentation order in the pseudocode to be identified, and the position information of source code word segmentation includes the source code word segmentation order in the candidate source code. Feature extraction is performed on the pseudocode word segmentation sequence and the positional information of the pseudocode word segmentation, as well as the source code word segmentation sequence and the positional information of the source code word segmentation, to obtain pseudocode text features and source code text features; Image feature extraction is performed on the pseudocode to be identified and the candidate source code in the multiple code pairs to obtain pseudocode image features and source code image features; Feature embedding processing is performed on the pseudocode word segmentation order, pseudocode word segmentation position, source code word segmentation order, and source code word segmentation position to obtain the first word segmentation position feature and the second word segmentation position feature of each pseudocode word segmentation. The first word segmentation position features are concatenated based on the pseudocode word segmentation order to obtain the first position feature of the pseudocode to be identified. The second word segmentation position features are concatenated based on the source code word segmentation order to obtain the second position feature of the candidate source code. The pseudocode text features, the pseudocode image features, and the first position features are fused together to obtain pseudocode fusion features; The source code text features, the source code image features, and the second location features are fused to obtain source code fused features; The pseudocode fusion features and source code fusion features corresponding to the multiple code pairs are classified and identified to obtain the classification and identification results.
10. A code matching device, characterized in that, The device includes: Pseudocode acquisition module: used to acquire the pseudocode to be identified corresponding to the program to be identified. The pseudocode to be identified is obtained by decompiling the machine language text of the program to be identified. Similar code search module: used to perform similar code search in the source code feature library based on the pseudocode to be identified, and obtain multiple candidate source codes corresponding to the pseudocode to be identified; Code combination module: used to combine the multiple candidate source codes with the pseudocode to be identified to obtain multiple code pairs; Classification and recognition module: used to classify and recognize the multiple code pairs to obtain classification and recognition results. The classification and recognition results represent the homology probability between the candidate source code and the pseudocode to be recognized in the multiple code pairs. The homology probability represents the probability that the candidate source code and the pseudocode to be recognized belong to the same machine language text. Source code determination module: used to determine the target source code that matches the program to be identified from the multiple candidate source codes based on the classification and recognition results; The step of searching for similar code in the source code feature library based on the pseudocode to be identified to obtain multiple candidate source codes corresponding to the pseudocode to be identified includes: Based on at least two code recall models, feature extraction is performed on the pseudocode to be identified to obtain pseudocode features corresponding to each code recall model; Based on the pseudocode features, similar code searches are performed in the corresponding source code feature library to obtain the initial source code corresponding to the pseudocode features. The source code feature library and the code recall model are set in a one-to-one correspondence. The source code features in the source code feature library are obtained by extracting features from the source code to be matched by the corresponding code recall model. Different source code feature libraries correspond to the same source code to be matched. The initial source code is deduplicated to obtain the multiple candidate source codes; Before obtaining the pseudocode to be identified corresponding to the program to be identified, the method further includes a step of obtaining the machine language text of the program to be identified. The obtaining step includes: obtaining the target machine language text corresponding to the program source file; segmenting the target machine language text based on the business source file information of the program source file to obtain the machine language text to be identified and the corresponding text position information for each business source file; or segmenting the target machine language text based on the business source file information and functional module information of the program source file to obtain the machine language text to be identified and the corresponding text position information for each functional module; wherein, the business source file information indicates the correspondence between the business source file and the text blocks in the target machine language text, the functional module information indicates the correspondence between the functional module in a single business source file and the text blocks in the target machine language text, and the text position information represents the position of the machine language text to be identified in the target machine language text; After obtaining the target source code, the method further includes: concatenating the target source code of the machine language text to be recognized based on the text location information to obtain the source code text corresponding to the target machine language text.
11. A computer-readable storage medium, characterized in that, The storage medium stores at least one instruction or at least one program segment, which is loaded and executed by a processor to implement the code matching method as described in any one of claims 1-9.
12. A computer device, characterized in that, The device includes a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the code matching method as described in any one of claims 1-9.
13. A computer program product or computer program, characterized in that, The computer program product or computer program includes computer instructions that, when executed by a processor, implement the code matching method as described in any one of claims 1-9.
Citation Information
Patent Citations
Malicious code family classification method based on feature expression enhancement
CN113468527A