Shellcode code and comment generation method based on dual learning
Through the method based on dual learning, combined with the Transformer model and Adjust_QKNorm normalization, the challenge of shellcode code and comment generation is solved, the generation performance is significantly improved, the problem of shellcode code and comment generation in the existing technology is solved, and the software development and Maintenance efficiency.
Patent Information
- Application Number
- CN202210041705.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-14
- Publication Date
- 2024-08-20
- Estimated Expiration
- 2042-01-14
AI Technical Summary
Automatically generating shellcodes and generating comments for them are challenging tasks, especially since they are usually written in assembly language, and existing technologies lack effective methods to identify the attack intent of shellcodes and generate high-quality code or comments.
Using a method based on dual learning, shellcode code generation and annotation generation are formalized as dual tasks, using a shallow Transformer model for synchronous learning, and designing the Adjust_QKNorm normalization method to adapt to low resource tasks, combined with rule-based repair components. Improve the accuracy of automatic code generation.
In the same corpus, the performance of shellcode code and annotation generation is significantly improved compared to the baseline method. The high-quality annotations and codes generated can improve the work efficiency of developers, thereby improving software quality.
Smart Images

Figure CN114546397B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a shellcode code and annotation generation method based on dual learning. Background Art
[0002] Sellcode is a small piece of code written in assembly language. Malicious shellcode can perform DDoS attacks, data theft, or run malware against the target system. However, shellcode with good intentions can be used to identify vulnerabilities and then repair the system by fixing them. Therefore, researchers hope to use shellcode to discover security vulnerabilities in software, and shellcode code generation technology has become an active research topic.
[0003] However, automatically generating shellcodes is a challenging task because they are usually written in assembly language. In addition, summarizing the functional description of shellcodes (i.e., generating comments for shellcodes) is also a challenge because identifying the attack intent of these shellcodes is difficult for users who are not familiar with shellcodes and network information security. And, due to the small size of shellcode corpora, the automatic generation and summarization of shellcodes can be regarded as low-resource tasks, which makes solving these two tasks more challenging.
[0004] Automated methods for code generation and comment generation tasks are very important for software development and maintenance. The generated high-quality comments and code can help improve developer productivity and thus improve software quality. However, generating high-quality code or comments is time-consuming and error-prone. Previous studies on automatic code generation and comment generation have focused on code snippets in specific programming languages (such as Java, Python, C#). Recently, there have also been some studies aimed at studying code generation and comment generation methods for specific software components (such as Bash, SQL, and smart contracts). According to the survey, there is currently only one study on shellcode code generation, which is the first attempt to use neural machine translation to solve the generation task. In addition, there is no study on generating comments for shellcode.
[0005] How to solve the above challenges becomes the subject faced by the present invention. Summary of the invention
[0006] The object of the present invention is to provide a shellcode code and annotation generation method based on dual learning, which can automatically generate shellcode according to natural language description, and generate corresponding natural language description for shellcode, that is, code annotation.
[0007] The idea of the present invention is as follows: the present invention proposes a shellcode code and comment generation method based on dual learning, and utilizes the duality between the original task, i.e., mapping from domain X to domain Y, and the dual task, i.e., mapping from domain Y to domain X, to improve the performance of the two tasks, that is, formalizing shellcode code generation and shellcode comment generation into dual tasks, using a shallow Transformer for model construction, and designing a normalization method Adjust_QKNorm to adapt to such low-resource tasks. Finally, in order to alleviate the problem of out-of-vocabulary, a rule-based repair component is proposed to improve the performance of automatic code generation. The method of the present invention achieves better performance than the baseline method in the same corpus.
[0008] The present invention is achieved by the following measures: a shellcode code and annotation generation method based on dual learning, which includes the following steps:
[0009] (1) Collect shellcode corpora from 2000 to 2020 on ShellStorm and Exploit Database, and set their format to <code, comment>. Since the method of the present invention only uses one model to learn the dual task, the corpus is directly converted by exchanging input and output, and the two tasks are distinguished by adding prefixes. Specifically, the following steps are included:
[0010] (1-1) For the shellcode generation task, add the prefix “ShellCodeGen:” to its input data;
[0011] (1-2) For the shellcode comment generation task, add the prefix “ShellCodeSum:” to its input data;
[0012] (2) Considering the characteristics of shellcode generation and comment generation tasks, they are formalized as dual tasks. A shallow Transformer is used for synchronous learning to achieve knowledge sharing and improve the performance and generalization ability of the training model. The specific steps include:
[0013] (2-1) Through the embedding layer and position encoding, Transformer can transform the input sequence X input =(prefix,x1,x2,…,x n ) is converted to vector X emb , and feed this vector back to the encoder layer;
[0014] (2-2) The vector X emb Input to the multi-head attention layer;
[0015] (2-3) The attention function is calculated based on three matrices simultaneously: Q (query), K (key) and V (value). Considering that the insensitivity of cosine similarity to value may lead to errors in the final result, Adjust_QKNorm with zero mean adaptation is proposed to improve the self-attention calculation so that Transformer can adapt to low-resource tasks, taking into account both the numerical difference information of the matrix and the direction of the matrix.
[0016] (2-4) Use residual connection and layer normalization to make the matrix X emb and X atten The operation dimensions are consistent, and the hidden layers in the network are normalized to a standard normal distribution, which speeds up the training and convergence of the model.
[0017] (2-5) Then, through the feedforward layer and two linear mapping layers, the activation function is used to generate the vector X hidden , the hidden semantic vector is obtained through residual connection and layer normalization;
[0018] (2-6) In the decoder part, Transformer uses an autoregressive mechanism to infer its next output based on the output of the encoder part. Each decoder layer performs an attention operation on the final hidden state (i.e., the output of the encoder), and the rest of the operation is the same as the encoder part. Finally, the output of the decoder is sent to a fully connected neural network, which can predict the probability of the next word through a softmax layer;
[0019] (3) In order to alleviate the problem of out-of-vocabulary, a rule-based repair component is proposed to improve the accuracy of the automatic generation of shellcode by the method of the present invention. Specifically, the following steps are included:
[0020] The assembly statements in the shellcode consist of labels, instructions, and operands. The values that appear in the operand positions are divided into three categories (i.e., registers, addresses, and values). If an inconsistency is found in the address when generating the shellcode based on the natural language description, the correct address is extracted from the comment and used to update the generated shellcode.
[0021] As a further optimization scheme of a shellcode code and annotation generation method based on dual learning provided by the present invention, Adjust_QKNorm is used in the step (2-3) to improve the self-attention calculation. The existing QKNorm method converts the self-attention calculation from the dot product to the cosine similarity to adapt the Transformer to low-resource tasks. The cosine similarity is mainly based on the similarity of the direction (that is, the closer the direction between the two vectors, the higher the cosine similarity value). Therefore, QKNorm is insensitive to the numerical difference between the two vectors. In order to consider the numerical difference information in the self-attention calculation, the present invention designs a method for adjusting QKNorm. Specifically comprising the following steps:
[0022] (1) From the corresponding average values of matrices Q and K (i.e. and ) minus the value of the last dimension so that the sum of all elements in this dimension is equal to zero;
[0023] (2) Then perform L2 normalization on Q and K;
[0024] (3) The result of the dot product is expressed as the cosine similarity calculation of Q and K, and does not need to be divided by
[0025] (4) Train the model by multiplying it with a learnable parameter g;
[0026] As a further optimization scheme of the shellcode code and annotation generation method based on dual learning provided by the present invention, a rule-based repair component is used in step (3) to improve the performance of the automatic shellcode generation task, that is, when the generated shellcode and the annotation have inconsistent values at the operand position, the correct value is extracted from the annotation and the generated shellcode is updated using the value, thereby improving the accuracy of the automatic shellcode generation.
[0027] The parameters of the shellcode code and comment generation method based on dual learning are set as follows:
[0028]
[0029] Compared with the prior art, the present invention has the following beneficial effects:
[0030] 1. The present invention proposes a shellcode code and comment generation method based on dual learning. The method can automatically generate shellcode according to natural language description, and generate corresponding natural language description for shellcode, i.e. code comment. This is a completely data-driven method. Considering the characteristics of shellcode code generation and shellcode comment generation tasks, they are formalized as dual tasks, and then a shallow Transformer is used for synchronous learning.
[0031] 2. The present invention also proposes a normalization method Adjust_QKNorm to improve the self-attention calculation to adapt to this task in low-resource scenarios.
[0032] 3. In order to alleviate the problem of out-of-vocabulary, the present invention proposes a rule-based repair component to improve the accuracy of automatic shellcode generation by the method of the present invention. Compared with the advanced benchmark methods in the field of code automatic generation and code comment generation, the method can achieve better performance on both tasks, which is beneficial to software development and maintenance. The generated high-quality comments and codes can help improve the work efficiency of developers and thus improve software quality. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0034] Figure 1 A system framework diagram of a shellcode code and annotation generation method based on dual learning provided by the present invention. DETAILED DESCRIPTION
[0035] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.
[0036] Example 1
[0037] See also Figure 1 As shown, the present invention provides a shellcode code and annotation generation method based on dual learning, which specifically includes the following contents:
[0038] (1) Collect shellcode corpora from 2000 to 2020 on ShellStorm and Exploit Database, and set their format to <code, comment>. Since the method of the present invention only uses one model to learn the dual task, the corpus is directly converted by exchanging the input and output, and the two tasks are distinguished by adding prefixes: for the shellcode code generation task, the prefix "ShellCodeGen:" is added to its input data; for the shellcode comment generation task, the prefix "ShellCodeSum:" is added to its input data. Table 1 shows the statistical information of the corpus.
[0039] Table 1
[0040]
[0041]
[0042] Comment length statistics
[0043]
[0044] (2) Considering the characteristics of shellcode generation and comment generation tasks, they are formalized as dual tasks. A shallow Transformer is used for synchronous learning to achieve knowledge sharing and improve the performance and generalization ability of the training model. The specific steps include:
[0045] (2-1) Through the embedding layer and position encoding, Transformer can transform the input sequence X input =(prefix,x1,x2,…,x n ) is converted to vector X emb , and feed this vector back to the encoder layer;
[0046] (2-2) The vector X emb Input to the multi-head attention layer;
[0047] (2-3) The attention function is calculated based on three matrices: Q (query), K (key) and V (value). Considering that the insensitivity of cosine similarity to value may lead to errors in the final result, Adjust_QKNorm with zero mean adaptation is proposed to improve the self-attention calculation so that Transformer can adapt to low-resource tasks, considering both the numerical difference information of the matrix and the direction of the matrix. The specific steps include the following:
[0048] (2-3-1) From the corresponding average values of matrices Q and K (i.e. and ) minus the value of the last dimension so that the sum of all elements in this dimension is equal to zero;
[0049] (2-3-2) Then perform L2 normalization on Q and K;
[0050] (2-3-3) The result of the dot product is expressed as the cosine similarity calculation of Q and K, and does not need to be divided by
[0051] (2-3-4) Train the model by multiplying it with a learnable parameter g;
[0052] (2-4) Use residual connection and layer normalization to make the matrix X emb and X atten The operation dimensions are consistent, and the hidden layers in the network are normalized to a standard normal distribution, which speeds up the training and convergence of the model.
[0053] (2-5) Then, through the feedforward layer and two linear mapping layers, the activation function is used to generate the vector X hidden , the hidden semantic vector is obtained through residual connection and layer normalization;
[0054] (2-6) In the decoder part, Transformer uses an autoregressive mechanism to infer its next output based on the output of the encoder part. Each decoder layer performs an attention operation on the final hidden state (i.e., the output of the encoder), and the rest of the operation is the same as the encoder part. Finally, the output of the decoder is sent to a fully connected neural network, which can predict the probability of the next word through a softmax layer;
[0055] (3) In order to alleviate the problem of out-of-vocabulary, a rule-based repair component is proposed to improve the accuracy of the automatic generation of shellcode by the method of the present invention. Specifically, the following steps are included:
[0056] The assembly statements in the shellcode consist of labels, instructions, and operands. The values that appear in the operand positions are divided into three categories (i.e., registers, addresses, and values). If an inconsistency is found in the address when generating the shellcode based on the natural language description, the correct address is extracted from the comment and used to update the generated shellcode.
[0057] (4) The parameter settings of the shellcode code and comment generation method based on dual learning are shown in Table 2:
[0058] Table 2
[0059]
[0060] (5) The proposed method and six existing benchmark methods based on information retrieval and deep learning are evaluated on the same corpus, and four evaluation indicators (i.e., BLEU, ROUGE-L, METEOR, and ACC) widely used in neural machine translation and code generation research are used to automatically evaluate the above two tasks. Table 3 shows the comparison results of the proposed method with the benchmark methods in two different tasks.
[0061] Table 3
[0062]
[0063] Shellcode generation
[0064]
[0065]
[0066] Experiments show that the shellcode code and annotation generation method based on dual learning proposed in the present invention has achieved better performance in all indicators compared with the advanced benchmark methods based on information retrieval and deep learning, that is, the method proposed in the present invention is superior to the benchmark methods in both Shellcode annotation generation and Shellcode code generation tasks. Specifically, for the shellcode annotation generation task, in terms of BLEU, the method of the present invention can improve the performance by 3.913% to 12.380%; for ROUGE-L, the method of the present invention can improve the performance by 1.998% to 13.438%; for METEOR, the method of the present invention can improve the performance by 2.188% to 6.102%. For the shellcode code generation task, in terms of BLEU, the method of the present invention can improve the performance by 8.013% to 28.240%; for ROUGE-L, the method of the present invention can improve the performance by 3.132% to 15.472%; for ACC, the method of the present invention can improve the performance by 9.0625% to 35.625%. The significant performance improvements of the two tasks demonstrate that the proposed method is highly competitive.
[0067] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A shellcode code and annotation generation method based on dual learning, characterized in that: The following steps are involved: (1) Collect shellcode corpora from 2000 to 2020 on ShellStorm and Exploit Database, set their format to <code, comment>, use a model to learn dual tasks, transform the corpus by exchanging input and output, and distinguish the two tasks by adding prefixes; (2) The two tasks of shellcode comment generation and shellcode code generation are formalized as a dual learning problem, and a shallow Transformer is used for synchronous learning to share knowledge and improve the performance and generalization ability of the training model; (3) Propose a normalization method Adjust_QKNorm to improve self-attention calculation, making Transformer adaptable to low-resource tasks; (4) Propose a rule-based repair component to improve the accuracy of automatically generated shellcode; In step (3), Adjust_QKNorm is used to improve the self-attention calculation, which specifically includes the following steps: (3-1) The attention function is calculated simultaneously based on three matrices: Q (query), K (key) and V (value). The value of the last dimension is subtracted from the corresponding average of matrices Q and K so that the sum of all elements in this dimension is equal to zero; (3-2) Then perform L2 normalization on Q and K; (3-3) The result obtained by the dot product is expressed as the cosine similarity calculation of Q and K, and does not need to be divided by √dk; (3-4) Train the model by multiplying it with a learnable parameter g; In step (4), a rule-based repair component is proposed, and the specific contents are as follows: The assembly statements in the shellcode consist of labels, instructions, and operands. The values that appear at the operand positions are divided into three categories. When the generated shellcode and the comments have inconsistent values at the operand positions, the correct value is extracted from the comments and used to update the generated shellcode, thereby improving the accuracy of automatically generated shellcode. If an inconsistency in the address is found when generating shellcode based on the natural language description, the correct address is extracted from the comments and used to update the generated shellcode.
2. The shellcode code and annotation generation method based on dual learning according to claim 1 is characterized in that: In step (1), two tasks are distinguished by adding prefixes, which specifically includes the following steps: (2-1) For the shellcode generation task, add the prefix "ShellCodeGen:" to its input data; (2-2) For the shellcode comment generation task, add the prefix "ShellCodeSum:" to its input data.
Citation Information
Patent Citations
Method for intelligent construction of place name annotated corpus based on interactive and iterative learning
AU2020103654A4
Decision-based code annotation generation method fusing information retrieval and deep learning
CN113743064A
Automatic generation of code documentation
WO2021231049A1
AU4548689A
