Transform code-based automatic auditing method

Code auditing is carried out through a deep learning model based on Transformer, and the code is converted into vectorized data structures using program dependency graphs and slicing technology, which realizes the automation and intelligent detection of code vulnerabilities, and solves the inefficiency caused by manual analysis in traditional methods and The problem of insufficient vulnerability detection improves security and reliability.

CN118886017AInactive Publication Date: 2024-11-01ELECTRIC POWER RES INST CHINA SOUTHERN POWER GRID CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410953680.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-16
Publication Date
2024-11-01
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Traditional code auditing methods rely on manual analysis, are time-consuming and labor-intensive, and are prone to omissions and errors, and cannot effectively detect vulnerabilities in the code, especially 0day vulnerabilities.

Method used

Using a deep learning model based on Transformer, the code is converted into vectorized data structures by obtaining the program dependency graph, slicing technology and Doc2Vec model of the program to be detected, and input it into the Transformer model for prediction, automatically generate vulnerability reports, and realize the automation of code vulnerabilities. and intelligent detection.

Benefits of technology

It improves the efficiency and accuracy of code auditing, can continuously optimize and iterate, adapt to new vulnerability modes, enhances the security and reliability of the system, and solves the problems of insufficient vulnerability detection and manual analysis errors in traditional methods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118886017A_ABST
    Figure CN118886017A_ABST
Patent Text Reader

Abstract

The invention provides a transform code-based automatic auditing method. The transform code-based automatic auditing method comprises the following steps of: generating a first program dependency graph of a to-be-detected program by using an open-source tool; slicing the to-be-detected program by using a slicing technology to obtain a plurality of first slices and corresponding first slice dependency graphs, and generating a first word library of the corresponding first slices; converting words and sentences in each first slice dependency graph into word vectors by adopting a target Doc2Vec model and based on the first word library to obtain a corresponding first vectorized data structure; and inputting all the first vectorized data structures into the target transformer model for prediction to obtain a corresponding prediction result, and automatically generating a vulnerability report of the to-be-detected program according to the prediction result and the first program dependency graph. According to the method, the auditing efficiency and accuracy can be improved, continuous optimization and iteration can be realized, and the safety and reliability of the system are further improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of code analysis, and more specifically, to a method for transformer-based automated code auditing, a device for transformer-based automated code auditing, a computer-readable storage medium, and an electronic device. Background Art

[0002] With the rapid development of information technology and the popularization of the Internet, attacks and vulnerabilities against software codes have become a major challenge in the field of network security. Traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors. Summary of the invention

[0003] The main purpose of this application is to provide a method based on transformer-based automated code auditing, a device based on transformer-based automated code auditing, a computer-readable storage medium and an electronic device, so as to at least solve the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0004] In order to achieve the above-mentioned purpose, according to one aspect of the present application, a method for transformer-based automated code auditing is provided, including: obtaining a program to be tested, and using an open source tool to generate a first program dependency graph of the program to be tested; slicing the program to be tested using a slicing technology to obtain multiple first slices and a first slice dependency graph corresponding to each first slice, and generating a first vocabulary of the corresponding first slice according to each first slice dependency graph; using a target Doc2Vec model and based on the first vocabulary, converting the words and sentences in each first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure; inputting all the first vectorized data structures into a target transformer model for prediction to obtain corresponding prediction results, and automatically generating a vulnerability report of the program to be tested according to the prediction results and the first program dependency graph.

[0005] Optionally, all of the first vectorized data structures are input into a target transformer model for prediction to obtain corresponding prediction results, and a vulnerability report of the program to be detected is automatically generated based on the prediction results and the first program dependency graph, including: all of the first vectorized data structures are input into a target transformer model for prediction to obtain corresponding prediction results, the prediction result is whether a vulnerability exists or not, and when the prediction result is that a vulnerability exists, the prediction result also includes the vulnerability type; when the prediction result is that a vulnerability exists, it is determined that the corresponding first slice has a vulnerability; based on the first program dependency graph and the first slice with a vulnerability, a vulnerable subroutine corresponding to the first slice in the program to be detected is determined; and a vulnerability report of the program to be detected is generated based on at least all of the vulnerable subroutines.

[0006] Optionally, before obtaining the program to be detected, the method also includes: obtaining a vulnerable program and using an open source tool to generate a second program dependency graph of the vulnerable program, wherein the vulnerable program is a published program containing vulnerabilities; slicing the vulnerable program using a slicing technique to obtain multiple second slices and second slice dependency graphs corresponding to each second slice, and generating a second vocabulary corresponding to each second slice dependency graph according to each second slice dependency graph; using a target Doc2Vec model and based on the second vocabulary, converting the words and sentences in each second slice dependency graph into word vectors to obtain a corresponding second vectorized data structure; inputting the vectorized data in the second vectorized data structure into an initial transformer model for training to obtain a trained target transformer model, wherein the target transformer model is a code audit automation model; and using the code audit automation module and the target transformer model to perform automated code audits on running applications in real time.

[0007] Optionally, using an open source tool to generate a second program dependency graph of the vulnerable program includes: using an open source tool to generate an initial program dependency graph of the vulnerable program; marking attributes of vulnerability nodes related to the vulnerability in the initial program dependency graph to obtain the second program dependency graph, wherein, when a node in the initial program dependency graph is the vulnerability node, the attribute type of the vulnerability node is set to 1.

[0008] Optionally, the vulnerable program is sliced ​​using slicing technology, including: performing forward slicing on all variables of the code in a first type of statement, the first type of statement including at least: system API call statements, array usage statements, pointer usage statements, and arithmetic operation statements; performing backward slicing on variables participating in conditional calculations in conditional statements, and performing forward slicing on all data-dependent variables; performing backward slicing on function variables participating in conditional calculations in function call statements whose return values ​​are not used, and performing forward slicing on all data-dependent function variables.

[0009] Optionally, the vectorized data in the second vectorized data structure is input into the initial transformer model for training to obtain a trained target transformer model, including: inputting the vectorized data in the second vectorized data structure into the initial transformer model for training, and marking the second slice containing vulnerabilities as corresponding vulnerability types, and marking the second slice not containing vulnerabilities as no vulnerabilities to obtain a trained transformer model; using cross-validation technology and parameter adjustment technology to optimize the trained transformer model to obtain the target transformer model.

[0010] Optionally, obtaining the program to be detected includes: obtaining an initial program and a compressed package of the initial program, wherein the compressed package is an unencrypted compressed package; determining a corresponding de-translation tool according to the type of the compressed package; and using the corresponding de-translation tool to de-translate the compressed package to obtain the program to be detected, wherein the program to be detected is a de-translated program.

[0011] According to another aspect of the present application, a device for transformer-based automated code auditing is provided, including: an acquisition unit, used to acquire a program to be detected, and use an open source tool to generate a first program dependency graph of the program to be detected; a first processing unit, used to slice the program to be detected using a slicing technology to obtain multiple first slices and a first slice dependency graph corresponding to each of the first slices, and generate a first vocabulary of the corresponding first slice according to each of the first slice dependency graphs; a second processing unit, used to adopt a target Doc2Vec model and convert sentences in each of the first slice dependency graphs into word vectors based on the first vocabulary to obtain a corresponding first vectorized data structure; a prediction unit, used to input all of the first vectorized data structures into a target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the program to be detected according to the prediction results and the first program dependency graph.

[0012] According to another aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium includes a stored program, wherein when the program is run, the device where the computer-readable storage medium is located is controlled to execute any one of the transformer-based automated code auditing methods.

[0013] According to another aspect of the present application, an electronic device is provided, comprising: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by the one or more processors, and the one or more programs include a method for executing any one of the transformer-based automated code auditing methods.

[0014] Applying the technical solution of the present application, the above transformer-based code automated auditing method first obtains the program to be detected, and uses an open source tool to generate the first program dependency graph of the program to be detected; then uses the slicing technology to slice the program to be detected, obtains multiple first slices and the first slice dependency graph corresponding to each first slice, and generates the first vocabulary of the corresponding first slice according to each first slice dependency graph; then uses the target Doc2Vec model and converts the words and sentences in each first slice dependency graph into word vectors based on the first vocabulary to obtain the corresponding first vectorized data structure; finally, all the first vectorized data structures are input into the target transformer model for prediction, and the corresponding prediction results are obtained, and the vulnerability report of the above program to be detected is automatically generated according to the prediction results and the first program dependency graph. This method uses deep learning and Transformer models to innovate traditional code auditing methods, fills the shortcomings of current security detection methods in coverage and 0day vulnerability detection, realizes the automation and intelligence of code vulnerability detection, and improves the ability to identify new vulnerabilities. It can not only improve the efficiency and accuracy of the audit, but also achieve continuous optimization and iteration. As historical data increases, the model can continuously learn and adapt to new vulnerability patterns, further improving the security and reliability of the system. It solves the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] The drawings constituting part of the present application are used to provide a further understanding of the present application. The exemplary embodiments and descriptions of the present application are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0016] Figure 1A hardware structure block diagram of a mobile terminal for executing a method for transformer-based code automated auditing provided in an embodiment of the present application is shown;

[0017] Figure 2 A flowchart of a method for automated auditing of transformer-based code is shown according to an embodiment of the present application;

[0018] Figure 3 The overall logic diagram of a transformer-based automated code auditing method provided according to an embodiment of the present application is shown;

[0019] Figure 4 A system architecture diagram of a transformer-based code audit automation method provided according to an embodiment of the present application is shown;

[0020] Figure 5 A flowchart of another method for automated transformer code auditing provided according to an embodiment of the present application is shown;

[0021] Figure 6 A structural block diagram of a transformer-based code audit automation device provided according to an embodiment of the present application is shown.

[0022] The above drawings include the following reference numerals:

[0023] 102, processor; 104, memory; 106, transmission device; 108, input and output devices. DETAILED DESCRIPTION

[0024] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0025] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0026] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present application described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0027] As introduced in the background technology, the prior art relies on training data, and the complexity of the twin network structure and parameter adjustment. In particular, the quality of the data set directly determines the final detection effect, which may result in the inability to detect new vulnerabilities. In order to solve the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors, the embodiments of the present application provide a method based on transformer code automation auditing, a transformer-based code auditing automation device, a computer-readable storage medium, and an electronic device.

[0028] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention.

[0029] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a mobile terminal as an example, Figure 1 1 is a hardware structure block diagram of a mobile terminal of a method for automated auditing of transformer code according to an embodiment of the present invention. Figure 1 As shown, the mobile terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the mobile terminal may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is for illustration only and does not limit the structure of the mobile terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown.

[0030] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the method based on transformer code automated auditing in the embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, the above method is implemented. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the mobile terminal via a network. Examples of the above networks include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof. The transmission device 106 is used to receive or send data via a network. The above network specific examples may include a wireless network provided by a communication provider of the mobile terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0031] In this embodiment, a method for automated auditing of transformer-based code running on a mobile terminal, a computer terminal or a similar computing device is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0032] Figure 2 1 is a flow chart of a method for quantifying the strength of a single renewable energy system through flexible direct current collection according to an embodiment of the present application. Figure 2 As shown, the method comprises the following steps:

[0033] Step S201, obtaining a program to be detected, and using an open source tool to generate a first program dependency graph of the program to be detected;

[0034] Specifically, the integrated system monitors the application in real time. CI / CD tool integration is used to trigger the automated audit process. When uploading the compressed package of the program to be tested, if it is Java, the jar package is uploaded, and the compressed package of the program to be tested is not encrypted. A real-time monitoring module is developed to perform behavioral analysis and security threat detection on the application.

[0035] Decompress and decompile. After the program obtains the compressed package, select the corresponding decompilation tool according to the type of compressed package, use the corresponding decompilation tool to decompile the compressed package, and obtain the corresponding decompilation program. Of course, there are also some tools that can directly generate PDG based on the compressed package, so this step can be omitted.

[0036] Generate PDG, use open source tools to generate the program dependency graph (PDG) of the source code, and save the generated PDG into the graph database.

[0037] Wherein, obtaining the program to be detected includes:

[0038] Step S2011, obtaining an initial program and a compressed package of the initial program, wherein the compressed package is an unencrypted compressed package;

[0039] Step S2012, determining a corresponding de-translation tool according to the type of the compressed package;

[0040] Step S2013, using the corresponding above-mentioned reverse translation tool to reversely translate the above-mentioned compressed package to obtain the above-mentioned program to be detected, and the above-mentioned program to be detected is a reverse translation program.

[0041] Specifically, by parsing HTML, the link of the corresponding version is finally obtained and downloaded and saved.

[0042] Before obtaining the program to be tested, it is necessary to build a target transformer model to audit the actual application program. That is, the above method also includes the following steps:

[0043] Step S301, obtaining a vulnerable program, and using an open source tool to generate a second program dependency graph of the vulnerable program, wherein the vulnerable program is a published program containing a vulnerability;

[0044] Specifically, the vulnerable program is a sample of the training module. All historical vulnerability information is obtained through the website https: / / nvd.nist.gov / vuln / full-listing and saved in json file format. After obtaining the software name and version number containing the vulnerability, the program package of the corresponding software version is obtained through the github website, where the URL is: https: / / github.com / vendor / software / tree / software_branch, where vendor is the software developer, software is the software name, and software_branch is the branch of the software. By parsing HTML, the link of the corresponding version is finally obtained and downloaded and saved.

[0045] The vulnerability types of the code related to the vulnerability obtained in step S301 are marked. Currently, the main marked types are: SQL injection, cross-site scripting attack (XSS) and buffer overflow, command injection, improper access control, sensitive information leakage, XML external entity (XXE) injection, remote code execution (RCE), and integer overflow.

[0046] Among them, the second program dependency graph of the above vulnerable program is generated using an open source tool, including:

[0047] Step S3011, using an open source tool to generate an initial program dependency graph of the vulnerable program;

[0048] Step S3012, attribute marking is performed on the vulnerability nodes related to the vulnerability in the initial program dependency graph to obtain the second program dependency graph, wherein, when the node in the initial program dependency graph is the vulnerability node, the attribute type of the vulnerability node is set to 1;

[0049] Specifically, since the model detection is performed on the slices after the code is sliced, the code location containing the vulnerability can be accurately located. That is, the program dependency graph (PDG) of the source code can be generated using open source tools, and the generated PDG can be saved in a graph database (such as Neo4j), and special attributes are marked on the nodes related to the vulnerability for subsequent processing. If the node is related to the vulnerability, the attribute type of the node is set to 1.

[0050] Among them, the tools for generating PDG include Joern (mainly for C / C++ languages), Antlr supports both Java and Python languages, and there are other similar open source tools. The graph database used in this experiment is neo4j.

[0051] A program dependency graph (PDG) is a graphical representation method used to show the control and data dependencies between statements in a program. PDG plays an important role in many areas of software engineering, such as compiler design, program analysis, optimization, debugging, and understanding complex program structures.

[0052] Among them, PDG consists of nodes and edges. A node represents a basic statement or expression in a program. An edge represents the dependency between nodes, including control dependency and data dependency.

[0053] The process of building a program dependency graph (PDG) is a sophisticated engineering process that combines static program analysis techniques and graph theory methods. Its core steps are rigorous and interrelated, as detailed below:

[0054] Step 1, source code preprocessing, including: lexical analysis, the program source code first goes through the lexical analysis stage, parsing the character stream into a sequence of grammatical symbols (tokens), which is the basic level for understanding the program structure; grammatical analysis, then, using the symbol sequence generated by the lexical analysis to construct an Abstract Syntax Tree (AST), which is an intuitive mapping of the program's logical structure and facilitates higher-level analysis and processing.

[0055] Step 2, Abstract Syntax Tree Conversion: Convert AST into an intermediate representation of a Control Flow Graph (CFG) to facilitate subsequent control flow and data flow analysis.

[0056] Step 3, control flow analysis, includes: basic block division, based on the source code or the intermediate code generated by the compiler, by identifying the instruction sequence that is executed sequentially and has a unique entry and no branch exit, the program is divided into basic blocks; CFG construction, based on the control transfer relationship between basic blocks, a control flow graph is created. In CFG, nodes represent basic blocks, and edges reflect the control transfer between different basic blocks, including conditional jumps, unconditional jumps, and the relationship between function calls and returns.

[0057] Step 4: Data flow analysis and identification, including: flow dependency analysis, determining the direct data flow dependencies between variables in the program; reverse dependency and output dependency analysis, analyzing complex data flows involving register renaming and memory operations, including WAR (Write-After-Read), RAW (Read-After-Write), WAW (Write-After-Write) and other dependency types.

[0058] Step 5, control dependency graph (CDG) construction, including: extracting control dependencies, extracting control dependency relationships from the existing control flow graph, and constructing a control dependency graph to clearly show how decision nodes in the program control flow determine the execution order of adjacent basic blocks.

[0059] Step 6: Data Dependency Graph (DDG) construction, including: deep data dependency analysis, analyzing the deep interaction between variables and expressions, accurately identifying and recording the data dependencies between them, and then building a data dependency graph. Each edge in the DDG expresses the value transfer dependency relationship between the nodes involved in data exchange.

[0060] Step 7, Program Dependency Graph (PDG) integration, including: PDG integration, merging the information of the Control Dependency Graph (CDG) and the Data Dependency Graph (DDG), and building a complete Program Dependency Graph (PDG). In addition to basic blocks, the node set of the PDG can also be refined to the expression level; the edge set shows the intricate control flow and data dependency network between nodes.

[0061] Step 8, PDG attribute tagging, attributes are marked on nodes in PDG to identify program elements related to specific goals (such as security vulnerabilities, code quality issues).

[0062] Step 9, PDG storage and query, stores the constructed PDG in a graph database or other suitable data structure to support efficient query and analysis.

[0063] Step 10, program slicing, uses PDG to slice the program, selects a specific node as the starting point of slicing, and generates a subgraph that only contains the program parts that have a direct impact on or are affected by the node.

[0064] Step 11, optimization and verification, includes: optimizing PDG to reduce redundant information and improve analysis efficiency; verifying the accuracy of PDG to ensure the correctness of dependencies.

[0065] Step 12, application and analysis, use PDG to understand and maintain the program, including code refactoring, defect location, performance optimization, etc.

[0066] Through the above-mentioned series of carefully designed and implemented specialized steps, PDG was successfully constructed. Its detailed structural view reveals the intricate control process and data flow within the program, greatly facilitating a series of activities in the field of software engineering, such as program analysis, optimization, and maintenance.

[0067] Among them, the applications of PDG include: program optimization, which performs optimization by identifying structures such as loops and conditional branches, such as loop unrolling and conditional elimination; debugging and testing, which helps developers understand the execution path of the program and perform effective debugging and testing; program understanding, which provides an intuitive structural display for complex programs to assist developers in understanding the working principles of the program; static analysis, which is used to detect potential errors, such as dead code and uninitialized variables.

[0068] By generating PDG, this embodiment can capture the dependencies in the program and provide a structured perspective for vulnerability detection. The generation of PDG helps to identify key paths and potential risk points in the code, making vulnerability detection more systematic and comprehensive.

[0069] Step S302, slicing the vulnerable program using a slicing technique to obtain a plurality of second slices and a second slice dependency graph corresponding to each of the second slices, and generating a second vocabulary corresponding to each of the second slices according to each of the second slice dependency graphs;

[0070] Among them, the slicing technology is used to slice the above vulnerable programs, including:

[0071] Step S3021, forward slicing is performed on all variables in the code of the first type of statements, where the first type of statements at least include: system API call statements, array use statements, pointer use statements, and arithmetic operation statements;

[0072] Step S3022, performing backward slicing on the variables participating in the conditional calculation in the conditional statement, and performing forward slicing on all variables that are data dependent;

[0073] Step S3023, performing backward slicing on the function variables participating in the condition calculation in the function call statement whose return value is not used, and performing forward slicing on all function variables that are data dependent.

[0074] Specifically, there are usually statements in the vulnerability code that are not related to the vulnerability, so the slicing technology is used to remove as much code as possible that is not related to the vulnerability.

[0075] When slicing, you need to select the slicing points of interest. The slicing methods used in this example include the following:

[0076] (1) For system API calls (when their return values ​​are used), array usage, pointer usage, and arithmetic operation statements, forward slicing is performed normally, that is, forward slicing is performed on all variables in the line of code.

[0077] (2) Return statement. The return statement is the last statement executed in the program execution flow. It has no dependency relationship with the remaining code execution after the return statement, so there is no need to perform forward slicing.

[0078] (3) Conditional statements. Conditional statements affect the execution flow of the program. Therefore, for conditional statements, we perform backward slicing on the variables involved in the conditional calculation, find out all the data dependencies of the conditional statements, and perform forward slicing on all the variables that are data dependent. Finally, we merge all the results into a complete slice dependency graph.

[0079] (4) Function call statements whose return values ​​are not used. The same slicing method as the conditional statement is used for the function parameters to obtain a more complete slicing dependency graph.

[0080] Slicing is performed from the program sensitive point to generate the corresponding slice dependency graph (Slice DependencyGraph, SDG). The nodes of SDG can be obtained by performing forward and backward slicing from the program sensitive point.

[0081] Slicing is a technique used in software engineering to analyze and understand programs. It simplifies the complexity of a program by identifying the code related to specific program points (such as vulnerability points or sensitive operations). Generating a slice dependency graph (SDG) is a key step in slice analysis, which can help developers understand which parts of the code have a direct impact on specific program points.

[0082] Program slicing is a sophisticated static program analysis technique used in software engineering to focus on code snippets that are closely related to specific execution points. The optimized program slicing process can be broken down into several key steps, aiming to accurately depict the key paths that affect program behavior, especially in security vulnerability analysis and code understanding. The specific steps include the following:

[0083] Step 1, System API call slicing: For all system-level APIs called in the program, especially those whose return values ​​are subsequently used, forward slicing analysis is performed. The core goal of this stage is to track how the API call results propagate along the control flow and affect the subsequent computing state.

[0084] Step 2, array and pointer operation slicing: Focus on array operations and forward slicing of pointer references, as they may cause significant changes in program state, which are likely to be directly related to potential vulnerabilities. By carefully tracking these operations, their potential impact on the global state of the program is revealed.

[0085] Step 3, arithmetic operation statement slicing: Perform forward slicing on the executed arithmetic operation expressions to clarify how these operations shape the program's data flow and possible control transfer.

[0086] Step 4, return statement processing: Since the return statement terminates the execution flow within the function and has no dependency on subsequent code, there is no need to perform forward slicing on such statements.

[0087] Step 5, conditional statement slicing: Given the decisive role of conditional statements in control flow, we first perform backward slicing on the variables involved in the conditional judgment to identify all data dependency sources that may affect the conditional results. Subsequently, we perform forward slicing on these data-dependent variables to fully construct a data dependency network.

[0088] Step 6, function call slicing: For those function calls whose return values ​​are not used, their parameters are also sliced ​​using a strategy similar to conditional statements. This step helps to discover the implicit impact of function calls on the global program state and incorporate these impacts into the slice dependency graph.

[0089] Step 7, build a slice dependency graph (SDG): Starting from the sensitive points in the custom program (such as suspected vulnerability locations), a detailed slice dependency graph (SDG) is constructed by combining forward and backward slicing techniques. The nodes of the graph represent independent code segments related to the sensitive points, and the nodes are connected by edges that reflect their internal dependencies.

[0090] Through the rigorous and optimized slicing process, developers can extract a detailed program dependency graph that clearly shows all relevant execution paths around specific sensitive points. This in-depth analysis is not only helpful for locating and fixing potential security issues, but also has core value for in-depth understanding of complex program logic, optimizing code structure and its functional implementation.

[0091] For the generated slice dependency graph, the code tokens are first renamed to a unified form, and the function names and variable names are renamed to "FUN_" and "VAR_" respectively, where the underscore "_" represents a number, starting from 0 and increasing to represent the function or variable in each slice dependency graph, reducing the impact of custom function and variable names on the training of the Doc2Vec model.

[0092] Tokens can be understood as operators in computer languages, which are slightly different in each computer language. For example, the commonly used operators in Java include ++, +=, / =, +, -, *, etc. Each slice converted into a sentence can be understood as an article, and the variable and method names in the code are understood as words.

[0093] Step S303, using the target Doc2Vec model and based on the second vocabulary, converting the words and sentences in the second slice dependency graph into word vectors to obtain a corresponding second vectorized data structure;

[0094] In the above steps, the Doc2Vec model is used to generate the initial vector of each node in the slice dependency graph. The Doc2Vec model is an unsupervised model that can convert a whole sentence of code into a vector of fixed length. It was proposed by Mikolov based on the Word2Vec model and has some advantages, such as not having to fix the sentence length and accepting sentences of different lengths as training samples.

[0095] Doc2Vec is an unsupervised learning model for generating document vector representations, proposed by Tomas Mikolov et al. based on Word2Vec. Its core idea is to capture the semantic information of a document by learning the co-occurrence relationship of words in the document and represent the document as a vector of fixed length. In the context of the Program Dependency Graph (PDG), Doc2Vec can be used to convert code snippets or entire programs into vectors, thereby supporting various vector-based analyses and applications.

[0096] Among them, the Doc2Vec model mainly has two architectures: Distributed Memory Model (DM) and Distributed Bag of Words Model (DBOW). The following are the basic principles of the two models:

[0097] First, for the distributed memory model (DM), the goal is to make the vectors of documents containing the same words closer. The principle is that the model infers the vector representation of the document by learning the context of the words in the document. Specifically, the model tries to optimize the objective function so that given a word in a document, the model can predict other words in its context.

[0098] Second, for the distributed bag of words model (DBOW), the goal is to make the document vector more useful than the word vector when predicting the context of a word. The principle is: Unlike DM, the DBOW model uses the document vector as the context, and by optimizing the objective function, given a word, the model can use the document vector to predict its context.

[0099] Implementing a Doc2Vec model typically involves the following steps:

[0100] Step 1: Data preprocessing: Segment the code and convert the source code into word sequences. Build a vocabulary and map each word to a unique ID.

[0101] Step 2, model training: Randomly initialize word vectors and document vectors. For each document (which can be a code snippet or an entire program in PDG), perform the following steps: Select a window size (such as 10) and determine the context of the current word (i.e., other words in the window). Use the current word and context words as input to learn document vectors and word vectors through the model.

[0102] Step 3, optimize the objective function: use optimization algorithms such as gradient descent to update the word vector and document vector according to the preset number of iterations.

[0103] Step 4, application of document vectors: After training, each document (code snippet or program) will have a fixed-length vector representation that can be used for clustering, classification or other machine learning tasks.

[0104] Doc2Vec implementation details include:

[0105] 1. Context selection: Select context words based on the window size, which can be a sliding window or a fixed window.

[0106] 2. Optimization algorithm: Use stochastic gradient descent (SGD) or other optimization algorithms to update the vector representation.

[0107] 3. Loss function: Depending on the selected Doc2Vec architecture (DM or DBOW), define the corresponding loss function.

[0108] 4. Vector dimension: Determines the dimension of document vectors and word vectors, which is usually a hyperparameter.

[0109] 5. Learning rate: Set the learning rate, which controls the step size of the gradient descent.

[0110] 6. Number of iterations: Determine the number of iterations for model training.

[0111] 7. Regularization: To prevent overfitting, L1 or L2 regularization can be introduced.

[0112] 8. Hyperparameter tuning: Select the optimal hyperparameter combination through methods such as cross-validation.

[0113] In summary, by applying the Doc2Vec model to code snippets and performing refined training and tuning, we can obtain high-quality code vector representations, which not only helps to gain in-depth insights into the semantic connections within the code, but also effectively supports a variety of vector-based software engineering tasks and intelligent applications.

[0114] Step S304, inputting the vectorized data in the second vectorized data structure into the initial transformer model for training to obtain a trained target transformer model, wherein the target transformer model is a code audit automation model;

[0115] The vectorized data in the second vectorized data structure is input into the initial transformer model for training to obtain a trained target transformer model, including:

[0116] Step S3041, inputting the vectorized data in the second vectorized data structure into the initial transformer model for training, marking the second slice containing the vulnerability as the corresponding vulnerability type, and marking the second slice not containing the vulnerability as no vulnerability, so as to obtain a trained transformer model;

[0117] Step S3042, using cross-validation technology and parameter adjustment technology to optimize the above-mentioned training transformer model to obtain the above-mentioned target transformer model.

[0118] Specifically, through cross-validation and parameter adjustment techniques, the performance of the model is optimized to improve the accuracy and generalization ability of vulnerability identification.

[0119] The slice dependency graph is converted into a vectorized data structure, the vectorized data is put into the neural network transformer model for training, and the trained model is saved. Since the model is a multi-classification model, the slices containing vulnerabilities are marked as the corresponding type, and the slices without vulnerabilities are marked as 0, that is, no vulnerability.

[0120] Step S305, using the above-mentioned code audit automation module and the above-mentioned target transformer model to perform automatic code audit on the running application in real time.

[0121] Specifically, we build a code audit system, and by adding a continuous integration and real-time monitoring module, we can seamlessly integrate the code audit automation tool through the module into the development workflow and provide real-time monitoring capabilities.

[0122] Continuous Integration: Code submission in the development workflow will automatically trigger the audit process. Through integration with CI / CD tools, each time the code is updated, the automated audit tool will perform a security check on the latest code to ensure that potential security issues can be discovered and resolved at an early stage.

[0123] Real-time monitoring: In order to provide real-time security protection, this patent will also implement a monitoring system that will analyze the behavior of running applications. Through real-time monitoring, the system can promptly detect and respond to potential security threats, thereby greatly improving the security of applications.

[0124] Step S202, slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs;

[0125] Specifically, by generating a program dependency graph (PDG) and combining it with slicing technology, we can accurately focus on code snippets that may contain vulnerabilities. The slicing strategy takes into account a variety of code structures, including system API calls, array and pointer operations, arithmetic operations, return statements, conditional statements, and function calls, forming a highly targeted and comprehensive slice dependency graph (SDG).

[0126] Step S203, using the target Doc2Vec model and based on the first vocabulary, converting the words and sentences in the first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure;

[0127] Specifically, the Word2Vec model is used to vectorize code slices, which reduces the personalized impact of code naming and converts complex code structures into vector forms that can be processed by the model.

[0128] Combined with the Doc2Vec model, it can flexibly handle code snippets of different lengths, further improving the model's ability to handle code structures.

[0129] The Transformer deep model is introduced for multi-classification training, so that the model can accurately identify multiple vulnerability types, including but not limited to common security issues such as SQL injection, XSS attack, buffer overflow, etc.

[0130] Step S204, input all the above-mentioned first vectorized data structures into the target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the above-mentioned program to be detected based on the above-mentioned prediction results and the above-mentioned first program dependency graph.

[0131] Specifically, the code audit automation tool is deeply integrated with the continuous integration and continuous deployment (CI / CD) process in modern software engineering practices to form a tightly coupled solution. This integrated design ensures that code audit activities are fully integrated into the development pipeline. Whenever developers submit code, the system can automatically trigger the audit mechanism to achieve real-time code review. This seamless connection greatly improves the agility and timeliness of security detection, which is conducive to revealing and repairing potential security risks at an early stage.

[0132] Among them, all the above-mentioned first vectorized data structures are input into the target transformer model for prediction to obtain corresponding prediction results, and a vulnerability report of the above-mentioned program to be detected is automatically generated according to the above-mentioned prediction results and the above-mentioned first program dependency graph, including:

[0133] Step S2041, inputting all the first vectorized data structures into the target transformer model for prediction, and obtaining a corresponding prediction result, wherein the prediction result is the presence of a vulnerability or the absence of a vulnerability. When the prediction result is the presence of a vulnerability, the prediction result also includes the vulnerability type;

[0134] Step S2042, when the prediction result indicates that there is a vulnerability, determining that the corresponding first slice has a vulnerability;

[0135] Step S2043, determining a vulnerable subroutine corresponding to the first slice in the program to be detected according to the first program dependency graph and the first slice having the vulnerability;

[0136] Step S2044, generating a vulnerability report of the program to be detected based on at least all of the vulnerability subroutines.

[0137] Specifically, the Transformer-based deep neural network model is innovatively applied to code vulnerability detection, which can process complex code structure and semantic information. Compared with traditional rule-based detection methods, deep learning models can better understand the inherent logic of the code and potential security threats. They can not only identify known vulnerability patterns, but also predict unknown vulnerabilities to a certain extent. They can better identify various common vulnerability patterns, such as SQL injection, cross-site scripting attacks (XSS), buffer overflows, etc.

[0138] It also includes a user interaction and feedback mechanism system module. In order to enhance user experience and continuously improve tools, this patent will introduce a user interaction and feedback mechanism system.

[0139] User interface design: We will design an intuitive and easy-to-use user interface so that even non-professional developers can easily use the tool for code audits. By simplifying the operation process and providing clear guidance, users can quickly get started and conduct security checks effectively.

[0140] Feedback mechanism: Establish a feedback system to encourage users to provide comments and suggestions during use. By collecting and analyzing user feedback, we can continuously optimize the functions and performance of the tool to better meet user needs and adapt to changing security challenges.

[0141] In summary, this embodiment builds an efficient, comprehensive and intelligent code audit system by combining deep learning and automation technology, which greatly improves the accuracy and real-time performance of code security detection, and has good usability and scalability, and has significant advantages for security control in the software development process.

[0142] The above transformer-based code automated auditing method of the present application first obtains the program to be detected, and uses an open source tool to generate the first program dependency graph of the program to be detected; then uses the slicing technology to slice the program to be detected, obtains multiple first slices and the first slice dependency graph corresponding to each first slice, and generates the first vocabulary of the corresponding first slice according to each first slice dependency graph; then uses the target Doc2Vec model and converts the words and sentences in each first slice dependency graph into word vectors based on the first vocabulary to obtain the corresponding first vectorized data structure; finally, all the first vectorized data structures are input into the target transformer model for prediction, and the corresponding prediction results are obtained, and the vulnerability report of the above program to be detected is automatically generated according to the prediction results and the first program dependency graph. This method uses deep learning and Transformer models to innovate traditional code auditing methods, fills the shortcomings of current security detection methods in coverage and 0day vulnerability detection, realizes the automation and intelligence of code vulnerability detection, and improves the ability to identify new vulnerabilities. It can not only improve the efficiency and accuracy of the audit, but also achieve continuous optimization and iteration. As historical data increases, the model can continuously learn and adapt to new vulnerability patterns, further improving the security and reliability of the system. It solves the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0143] In some instances, such as Figure 3 As shown, the specific implementation steps include the following:

[0144] Step 1: Sample acquisition, obtain vulnerability-related code, and mark the vulnerability type to which the code belongs.

[0145] Step 2: Generate a program dependency graph (PDG) of the source code using an open source tool, save the generated PDG to a graph database (e.g. Neo4j), and mark nodes related to the vulnerability with special attributes for subsequent processing. If the node is related to the vulnerability, set the attribute type of the node to 1.

[0146] Step 3: Slice from the program sensitive point to generate the corresponding slice dependency graph (SliceDependencyGraph, SDG). The nodes of SDG can be obtained by forward and backward slicing from the program sensitive point.

[0147] Step 4: Generate a vocabulary. For the generated slice dependency graph, first rename the code tokens to a unified form, rename the function name and variable name to "FUN_" and "VAR_" respectively, where the underscore "_" represents a number, starting from 0 and increasing to represent the function or variable in each slice dependency graph, reducing the impact of custom function and variable names on training the Doc2Vec model.

[0148] Step 5: Use the Doc2Vec model to generate the initial vectors for each node in the slice dependency graph.

[0149] Step 6: Convert the slice dependency graph into a vectorized data structure, put the vectorized data into the neural network transformer model for training, and save the trained model. Through cross-validation and parameter adjustment techniques, optimize the performance of the model and improve the accuracy and generalization ability of vulnerability identification.

[0150] Step 7: Build a code audit system. Add a continuous integration and real-time monitoring module to seamlessly integrate the code audit automation tool through the module into the development workflow and provide real-time monitoring capabilities.

[0151] in, Figure 4 This is a system architecture diagram based on the transformer code audit automation method. Figure 4 The system architecture diagram shown implements the above transformer-based code audit automation method.

[0152] In order to enable those skilled in the art to more clearly understand the technical solution of the present application, the implementation process of the method for automated transformer code auditing of the present application will be described in detail below in combination with specific embodiments.

[0153] This embodiment relates to a specific method based on transformer code automatic auditing, such as Figure 5 As shown, the following steps are included:

[0154] Step S1: Integrate system real-time monitoring. Integrate with CI / CD tools to trigger the automated audit process. When uploading the program compressed package, if it is Java, upload the jar package. Develop a real-time monitoring module to perform behavior analysis and security threat detection on the application.

[0155] Step S2: decompress and decompile. After the program obtains the compressed package, it selects the corresponding decompilation tool according to the type of the compressed package, and uses the corresponding decompilation tool to decompile the compressed package to obtain the corresponding decompilation program. Of course, there are also some tools that can directly generate PDG according to the compressed package, so this step can be omitted.

[0156] Step S3: Generate PDG, use open source tools to generate a program dependency graph (PDG) of the source code, and save the generated PDG into a graph database.

[0157] Step S4: SDG slicing, using the following slicing rules:

[0158] (1) For system API calls (when their return values ​​are used), array usage, pointer usage, and arithmetic operation statements, forward slicing is performed normally, that is, forward slicing is performed on all variables in the line of code.

[0159] (2) Return statement. The return statement is the last statement executed in the program execution flow. It has no dependency relationship with the remaining code execution after the return statement, so there is no need to perform forward slicing.

[0160] (3) Conditional statements. Conditional statements affect the execution flow of the program. Therefore, for conditional statements, we perform backward slicing on the variables involved in the conditional calculation, find out all the data dependencies of the conditional statements, and perform forward slicing on all the variables that are data dependent. Finally, we merge all the results into a complete slice dependency graph.

[0161] (4) Function call statements whose return values ​​are not used. The same slicing method as the conditional statement is used for the function parameters to obtain a more complete slicing dependency graph.

[0162] After slicing, a PDG will contain multiple slices, and then the slices will be predicted separately.

[0163] Step S5: Extract words and sentences. For the generated slice dependency graph, first rename the code tokens into a unified form, and rename the function name and variable name into the form of "FUN_" and "VAR_", respectively, where the underscore "_" represents a number, starting from the number 0 and increasing incrementally to represent each function or variable in the slice dependency graph.

[0164] Step S6: Convert word vectors, load the trained Doc2Vec model, and use the model to convert words and sentences into word vectors.

[0165] Step S7: Model prediction, first load the neural network transformer model, put the word vector into the model for prediction, if the prediction is a certain vulnerability type, then the slice is considered to contain a vulnerability. According to the slice, query the source code of this through the graph data, and finally locate it in the code and prompt it.

[0166] Step S8: Automatically generate a vulnerability audit report.

[0167] It should be noted that the steps shown in the flowcharts of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and that, although a logical order is shown in the flowcharts, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0168] The embodiment of the present application also provides an automated code auditing device based on transformer. It should be noted that the automated code auditing device based on transformer in the embodiment of the present application can be used to execute the method for automated code auditing based on transformer provided in the embodiment of the present application. The device is used to implement the above-mentioned embodiments and preferred implementation modes, and those that have been described will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware for a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceived.

[0169] The following is an introduction to the transformer-based code audit automation device provided in an embodiment of the present application.

[0170] Figure 6 Schematic diagram of a transformer-based code audit automation device according to an embodiment of the present application. Figure 6 As shown, the device includes: an acquisition unit 10, a first processing unit 20, a second processing unit 30 and a prediction unit 40, the acquisition unit 10 is used to acquire the program to be detected, and use an open source tool to generate a first program dependency graph of the program to be detected; the first processing unit 20 is used to slice the program to be detected using a slicing technology to obtain multiple first slices and a first slice dependency graph corresponding to each of the first slices, and generate a first vocabulary corresponding to the first slice according to each of the first slice dependency graphs; the second processing unit 30 is used to adopt a target Doc2Vec model and convert the words and sentences in each of the first slice dependency graphs into word vectors based on the first vocabulary to obtain a corresponding first vectorized data structure; the prediction unit 40 is used to input all of the first vectorized data structures into a target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the program to be detected according to the prediction results and the first program dependency graph.

[0171] The transformer-based code audit automation device of the present application includes: an acquisition unit, a first processing unit, a second processing unit and a prediction unit, wherein the acquisition unit is used to acquire the program to be detected, and use an open source tool to generate the first program dependency graph of the program to be detected; the first processing unit is used to slice the program to be detected using a slicing technique to obtain multiple first slices and a first slice dependency graph corresponding to each of the first slices, and generate a first vocabulary corresponding to the first slice according to each of the first slice dependency graphs; the second processing unit is used to use a target Doc2Vec model and convert the words and sentences in each of the first slice dependency graphs into word vectors based on the first vocabulary to obtain a corresponding first vectorized data structure; the prediction unit is used to input all of the first vectorized data structures into the target transformer model for prediction, obtain the corresponding prediction results, and automatically generate a vulnerability report of the program to be detected according to the prediction results and the first program dependency graph. The device uses deep learning and the Transformer model to innovate the traditional code audit method, fill the gaps in the current security detection means in terms of coverage and 0day vulnerability detection, realize the automation and intelligence of code vulnerability detection, and improve the ability to identify new vulnerabilities. It can not only improve the efficiency and accuracy of the audit, but also achieve continuous optimization and iteration. As historical data increases, the model can continuously learn and adapt to new vulnerability patterns, further improving the security and reliability of the system. It solves the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0172] In some examples, the prediction unit includes a first prediction module, a first determination module, a second determination module and a first processing module. The first prediction module is used to input all the above-mentioned first vectorized data structures into the target transformer model for prediction to obtain the corresponding prediction result. The above-mentioned prediction result is the presence of a vulnerability or the absence of a vulnerability. When the above-mentioned prediction result is the presence of a vulnerability, the above-mentioned prediction result also includes the vulnerability type; the first determination module is used to determine that the corresponding above-mentioned first slice has a vulnerability when the above-mentioned prediction result is the presence of a vulnerability; the second determination module is used to determine the vulnerability subroutine corresponding to the above-mentioned first slice in the above-mentioned program to be detected according to the above-mentioned first program dependency graph and the above-mentioned first slice with a vulnerability; the first processing module is used to generate a vulnerability report of the above-mentioned program to be detected based on at least all the above-mentioned vulnerability subroutines. It can not only identify known vulnerability patterns, but also predict unknown vulnerabilities to a certain extent, and can better identify various common vulnerability patterns.

[0173] In some instances, the device further includes a first acquisition module, a second processing module, a first conversion module, a third processing module and a fourth processing module. Before acquiring the program to be detected, the first acquisition module is used to acquire the vulnerable program and generate a second program dependency graph of the vulnerable program using an open source tool. The vulnerable program is a published program containing vulnerabilities. The second processing module is used to slice the vulnerable program using a slicing technique to obtain a plurality of second slices and a second slice dependency graph corresponding to each of the second slices, and generate a second vocabulary corresponding to each of the second slice dependency graphs. The first conversion module is used to use a target Doc2Vec model and convert the words and sentences in each of the second slice dependency graphs into word vectors based on the second vocabulary to obtain a corresponding second vectorized data structure. The third processing module is used to input the vectorized data in the second vectorized data structure into an initial transformer model for training to obtain a trained target transformer model. The target transformer model is a code audit automation model. The fourth processing module uses the code audit automation module and the target transformer model to perform automated code audits on running applications in real time. By adding a continuous integration and real-time monitoring module, the code audit automation tool that will be used through the module can be seamlessly integrated into the development workflow, and real-time monitoring capabilities can be provided.

[0174] In some examples, the first acquisition module includes a first generation module and a first marking module, the first generation module is used to generate the initial program dependency graph of the above-mentioned vulnerable program using an open source tool; the first marking module is used to attribute mark the vulnerability nodes related to the vulnerability in the above-mentioned initial program dependency graph to obtain the above-mentioned second program dependency graph, wherein, when the node in the above-mentioned initial program dependency graph is the above-mentioned vulnerability node, the attribute type of the above-mentioned vulnerability node is set to 1. In this way, the code location containing the vulnerability can be accurately located.

[0175] In some instances, the second processing module includes a first execution module, a second execution module and a third execution module, the first execution module is used to perform forward slicing on all variables in the code of the first type of statements, the above-mentioned first type of statements at least include: system API call statements, array use statements, pointer use statements and arithmetic operation statements; the second execution module is used to perform backward slicing on the variables participating in the conditional calculation in the conditional statement, and perform forward slicing on all variables that are data dependent; the third execution module is used to perform backward slicing on the function variables participating in the conditional calculation in the function call statement whose return value is not used, and perform forward slicing on all function variables that are data dependent. There are usually statements unrelated to the vulnerability in the vulnerability code, so the slicing technology is used to remove as much code unrelated to the vulnerability as possible.

[0176] In some instances, the third processing module includes a first training module and a first optimization module. The first training module is used to input the vectorized data in the second vectorized data structure into the initial transformer model for training, and mark the second slice containing vulnerabilities as the corresponding vulnerability type, and mark the second slice not containing vulnerabilities as non-vulnerable, so as to obtain a trained transformer model; the first optimization module is used to optimize the trained transformer model by using cross-validation technology and parameter adjustment technology to obtain the target transformer model. Through cross-validation and parameter adjustment and other technologies, the performance of the model is optimized to improve the accuracy and generalization ability of vulnerability identification.

[0177] In some examples, the acquisition unit includes a second acquisition module, a third determination module and a first translation module, the second acquisition module is used to acquire the initial program and the compressed package of the initial program, the compressed package is an unencrypted compressed package; the third determination module is used to determine the corresponding de-translation tool according to the type of the compressed package; the first translation module is used to de-translate the compressed package using the corresponding de-translation tool to obtain the program to be detected, the program to be detected is the de-translation program. By parsing HTML, the link of the corresponding version is finally obtained for downloading and saving.

[0178] The transformer-based code audit automation device includes a processor and a memory. The acquisition unit and the like are stored in the memory as program units, and the processor executes the program units stored in the memory to implement corresponding functions. The modules are all located in the same processor; or, the modules are located in different processors in any combination.

[0179] The processor contains a kernel, which calls the corresponding program unit from the memory. The kernel can be set to one or more, and the traditional code audit method usually relies on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0180] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.

[0181] An embodiment of the present invention provides a computer-readable storage medium, which includes a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to execute the method based on transformer code automated auditing.

[0182] Specifically, the methods based on transformer code automated auditing include:

[0183] Step S201, obtaining a program to be detected, and using an open source tool to generate a first program dependency graph of the program to be detected;

[0184] Step S202, slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs;

[0185] Step S203, using the target Doc2Vec model and based on the first vocabulary, converting the words and sentences in the first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure;

[0186] Step S204, input all the above-mentioned first vectorized data structures into the target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the above-mentioned program to be detected based on the above-mentioned prediction results and the above-mentioned first program dependency graph.

[0187] An embodiment of the present invention provides a processor, which is used to run a program, wherein the method based on transformer code automatic auditing is executed when the program is running.

[0188] Step S201, obtaining a program to be detected, and using an open source tool to generate a first program dependency graph of the program to be detected;

[0189] Step S202, slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs;

[0190] Step S203, using the target Doc2Vec model and based on the first vocabulary, converting the words and sentences in the first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure;

[0191] Step S204, input all the above-mentioned first vectorized data structures into the target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the above-mentioned program to be detected based on the above-mentioned prediction results and the above-mentioned first program dependency graph.

[0192] An embodiment of the present invention provides a device, the device including a processor, a memory, and a program stored in the memory and executable on the processor, and when the processor executes the program, at least the following steps are implemented:

[0193] Step S201, obtaining a program to be detected, and using an open source tool to generate a first program dependency graph of the program to be detected;

[0194] Step S202, slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs;

[0195] Step S203, using the target Doc2Vec model and based on the first vocabulary, converting the words and sentences in the first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure;

[0196] Step S204, input all the above-mentioned first vectorized data structures into the target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the above-mentioned program to be detected based on the above-mentioned prediction results and the above-mentioned first program dependency graph.

[0197] The devices in this article can be servers, PCs, PADs, mobile phones, etc.

[0198] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program for initializing at least the following method steps:

[0199] Step S201, obtaining a program to be detected, and using an open source tool to generate a first program dependency graph of the program to be detected;

[0200] Step S202, slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs;

[0201] Step S203, using the target Doc2Vec model and based on the first vocabulary, converting the words and sentences in the first slice dependency graph into word vectors to obtain a corresponding first vectorized data structure;

[0202] Step S204, input all the above-mentioned first vectorized data structures into the target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the above-mentioned program to be detected based on the above-mentioned prediction results and the above-mentioned first program dependency graph.

[0203] Obviously, those skilled in the art should understand that the above modules or steps of the present invention can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order than here, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.

[0204] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0205] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0206] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0207] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0208] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0209] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0210] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0211] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0212] From the above description, it can be seen that the above embodiments of the present application achieve the following technical effects:

[0213] 1) The above transformer-based code automated auditing method of the present application first obtains the program to be detected, and uses an open source tool to generate the first program dependency graph of the program to be detected; then uses the slicing technology to slice the program to be detected, obtains multiple first slices and the first slice dependency graph corresponding to each first slice, and generates the first vocabulary of the corresponding first slice according to each first slice dependency graph; then uses the target Doc2Vec model and converts the words and sentences in each first slice dependency graph into word vectors based on the first vocabulary to obtain the corresponding first vectorized data structure; finally, all the first vectorized data structures are input into the target transformer model for prediction, and the corresponding prediction results are obtained, and the vulnerability report of the above program to be detected is automatically generated according to the prediction results and the first program dependency graph. This method uses deep learning and Transformer models to innovate traditional code auditing methods, fills the shortcomings of current security detection methods in coverage and 0day vulnerability detection, realizes the automation and intelligence of code vulnerability detection, and improves the ability to identify new vulnerabilities. It can not only improve the efficiency and accuracy of the audit, but also achieve continuous optimization and iteration. As historical data increases, the model can continuously learn and adapt to new vulnerability patterns, further improving the security and reliability of the system. It solves the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0214] 2) The transformer-based code audit automation device of the present application includes: an acquisition unit, a first processing unit, a second processing unit and a prediction unit. The acquisition unit is used to acquire the program to be detected and generate the first program dependency graph of the program to be detected using an open source tool; the first processing unit is used to slice the program to be detected using a slicing technique to obtain multiple first slices and a first slice dependency graph corresponding to each of the first slices, and generate the first vocabulary of the first slice according to each of the first slice dependency graphs; the second processing unit is used to use the target Doc2Vec model and convert the words and sentences in each of the first slice dependency graphs into word vectors based on the first vocabulary to obtain the corresponding first vectorized data structure; the prediction unit is used to input all of the first vectorized data structures into the target transformer model for prediction, obtain the corresponding prediction results, and automatically generate the vulnerability report of the program to be detected according to the prediction results and the first program dependency graph. The device uses deep learning and the Transformer model to innovate the traditional code audit method, fill the gaps in the current security detection means in terms of coverage and 0day vulnerability detection, realize the automation and intelligence of code vulnerability detection, and improve the ability to identify new vulnerabilities. It can not only improve the efficiency and accuracy of the audit, but also achieve continuous optimization and iteration. As historical data increases, the model can continuously learn and adapt to new vulnerability patterns, further improving the security and reliability of the system. It solves the problem that traditional code auditing methods usually rely on manual analysis, which is not only time-consuming and labor-intensive, but also prone to omissions and errors.

[0215] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for automated auditing of transformer code. It is characterized in that include: Obtain a program to be detected, and use an open source tool to generate a first program dependency graph of the program to be detected; Slicing the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generating a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs; Using the target Doc2Vec model and based on the first vocabulary, converting each word or sentence in the first slice dependency graph into a word vector to obtain a corresponding first vectorized data structure; All of the first vectorized data structures are input into a target transformer model for prediction to obtain corresponding prediction results, and a vulnerability report of the program to be detected is automatically generated based on the prediction results and the first program dependency graph.

2. The method according to claim 1, It is characterized in that Inputting all the first vectorized data structures into the target transformer model for prediction, obtaining corresponding prediction results, and automatically generating a vulnerability report of the program to be detected according to the prediction results and the first program dependency graph, including: Inputting all the first vectorized data structures into the target transformer model for prediction, and obtaining a corresponding prediction result, wherein the prediction result is the presence of a vulnerability or the absence of a vulnerability. When the prediction result is the presence of a vulnerability, the prediction result also includes the vulnerability type; When the prediction result indicates that there is a vulnerability, determining that the corresponding first slice has a vulnerability; Determine, according to the first program dependency graph and the first slice having a vulnerability, a vulnerable subroutine corresponding to the first slice in the program to be detected; A vulnerability report of the program to be detected is generated based on at least all of the vulnerability subroutines.

3. The method according to claim 1, It is characterized in that Before acquiring the program to be detected, the method further includes: Obtain a vulnerable program, and use an open source tool to generate a second program dependency graph of the vulnerable program, wherein the vulnerable program is a published program containing a vulnerability; Slicing the vulnerable program using a slicing technique to obtain a plurality of second slices and a second slice dependency graph corresponding to each of the second slices, and generating a second vocabulary corresponding to each of the second slices according to each of the second slice dependency graphs; Using the target Doc2Vec model and based on the second vocabulary, converting the words and sentences in each of the second slice dependency graphs into word vectors to obtain a corresponding second vectorized data structure; Inputting the vectorized data in the second vectorized data structure into the initial transformer model for training to obtain a trained target transformer model, wherein the target transformer model is a code audit automation model; The code audit automation module and the target transformer model are used to perform automated code audit on the running application in real time.

4. The method according to claim 3, It is characterized in that Use an open source tool to generate a second program dependency graph of the vulnerable program, including: Generate an initial program dependency graph of the vulnerable program using open source tools; Attribute marking is performed on vulnerability nodes related to the vulnerability in the initial program dependency graph to obtain the second program dependency graph, wherein when a node in the initial program dependency graph is the vulnerability node, the attribute type of the vulnerability node is set to 1.

5. The method according to claim 3, It is characterized in that Use slicing technology to slice the vulnerable program, including: Perform forward slicing on all variables in the code of the first type of statements, wherein the first type of statements at least include: system API call statements, array use statements, pointer use statements, and arithmetic operation statements; Perform backward slicing on the variables involved in the conditional calculation in the conditional statement, and perform forward slicing on all variables that are data dependent; Backward slicing is performed on function variables involved in conditional calculations in function call statements whose return values ​​are not used, and forward slicing is performed on all function variables that are data dependent.

6. The method according to claim 3, It is characterized in that Inputting the vectorized data in the second vectorized data structure into the initial transformer model for training to obtain a trained target transformer model, including: Inputting the vectorized data in the second vectorized data structure into the initial transformer model for training, marking the second slices containing vulnerabilities as corresponding vulnerability types, and marking the second slices not containing vulnerabilities as non-vulnerable, so as to obtain a trained transformer model; The training transformer model is optimized by using cross-validation technology and parameter adjustment technology to obtain the target transformer model.

7. The method according to any one of claims 1 to 6, It is characterized in that Get the program to be tested, including: Obtaining an initial program and a compressed package of the initial program, wherein the compressed package is an unencrypted compressed package; Determining a corresponding de-translation tool according to the type of the compressed package; The compressed package is detranslated using the corresponding detranslation tool to obtain the program to be detected, and the program to be detected is a detranslation program.

8. A device for automated auditing of transformer code, It is characterized in that include: An acquisition unit, used for acquiring a program to be detected, and generating a first program dependency graph of the program to be detected using an open source tool; a first processing unit, configured to slice the program to be detected using a slicing technology to obtain a plurality of first slices and a first slice dependency graph corresponding to each of the first slices, and generate a first vocabulary corresponding to each of the first slices according to each of the first slice dependency graphs; A second processing unit is used to convert the words and sentences in each of the first slice dependency graphs into word vectors based on the first vocabulary using a target Doc2Vec model to obtain a corresponding first vectorized data structure; A prediction unit is used to input all of the first vectorized data structures into a target transformer model for prediction, obtain corresponding prediction results, and automatically generate a vulnerability report of the program to be detected based on the prediction results and the first program dependency graph.

9. A computer-readable storage medium, It is characterized in that The computer-readable storage medium includes a stored program, wherein when the program is run, the device where the computer-readable storage medium is located is controlled to execute the method based on transformer code automatic auditing as described in any one of claims 1 to 7.

10. An electronic device, It is characterized in that include: One or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by the one or more processors, and the one or more programs include a method for executing the transformer-based automated code auditing method described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • C source code vulnerability detection method based on Bert model and BiLSTM

    CN113420296A

  • Source code vulnerability detection method based on data dependence enhancement program slicing

    CN116702160A