Python byte code protection method based on operation code and control flow joint confusion

The joint obfuscation of opcodes and control flows in Python bytecode protection enhances security by increasing complexity and reducing reverse engineering, addressing the limitations of existing methods.

CN119066631AActive Publication Date: 2024-12-03UNIV OF SCI & TECH BEIJING

Patent Information

Application Number
CN202411123131.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-15
Publication Date
2024-12-03
Estimated Expiration
2044-08-15

AI Technical Summary

Technical Problem

Existing Python code protection methods, such as local compilation and source code obfuscation, are inadequate in preventing reverse engineering and decompilation, leading to insufficient security for Python programs.

Method used

A Python bytecode protection method involving joint obfuscation of opcodes and control flows, including opcode replacement, merging, synonymous instruction replacement, loop expansion, and opaque predicate insertion, to enhance code complexity and obfuscation.

Benefits of technology

The method significantly increases the complexity of Python bytecode, making it harder to reverse engineer and decompile, while maintaining the correctness of the original business logic and reducing the overhead in time and space.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119066631A_ABST
    Figure CN119066631A_ABST
Patent Text Reader

Abstract

The invention provides a Python byte code protection method and device based on operation code and control flow joint obfuscation, and relates to the technical field of Python code obfuscation protection. The Python byte code protection method based on operation code and control flow joint confusion comprises the following steps: acquiring a Python operation code file; an operation code replacement method is adopted for processing, and a new Python operation code is obtained through an operation code combination method; performing instruction insertion according to the new Python operation code, and running a Python operation code file by adopting a recompiled Python interpreter to obtain a preliminarily confused Python byte code file; carrying out replacement processing by adopting a synonymous instruction replacement method to obtain a confused byte code sequence; processing the obfuscated byte code sequence by adopting a method of inserting an opaque predicate to obtain an obfuscated Python byte code instruction list; and processing the instruction list by adopting an instruction pattern adding method to obtain a final confused byte code sequence. By adopting the embodiment of the invention, the time and space expenditure required for executing the Python byte code file can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of Python code obfuscation protection, and in particular to a Python bytecode protection method and device based on combined obfuscation of operation code and control flow. Background Art

[0002] As an interpreted language, Python has good cross-platform and portability. Commercial software designers usually release software in the form of unencrypted source code. As software security issues continue to escalate, in recent years, program publishers have used local compilation technology to package and release Python code to achieve the purpose of software protection.

[0003] The local compilation technology is a process of compiling the Python source program and the interpreter together into the local machine code on the target running platform. The local compilation technology improves the efficiency of program execution and optimizes resource allocation by directly converting the source code into the machine code of the target platform. However, the local compilation technology requires the compiled binary file to be released together with the dynamic link library, which leads to an increase in the storage space occupied by the target program, thus bringing about a non-negligible negative space overhead. Another protection method is code obfuscation technology, which mainly uses some open source tools to perform obfuscation at the source code level, such as Pyobfuscate, which can delete comments and document strings, change indentation, and rename function / class names / variable names. The reasonable use of open source tools can provide a certain degree of protection for Python program source code. However, its protection ability has obvious limitations. Source code obfuscation only performs some simple semantic information obfuscation. Shallow modifications such as variable names and class names reduce the readability of the source code. As a dynamic interpreted language, Python cannot completely hide the code logic at the source code level. The bytecode file corresponding to the obfuscated program is very easy to be reverse engineered and decompiled. It has obvious deficiencies in the field of Python code protection. Summary of the invention

[0004] In order to solve the technical problems of insufficient protection in the prior art and easy cracking by decompilation tools and manual static analysis technology, the embodiment of the present invention provides a Python bytecode protection method and device based on joint obfuscation of opcodes and control flows. The technical solution is as follows:

[0005] On the one hand, a Python bytecode protection method based on joint obfuscation of opcodes and control flows is provided, the method is implemented by a Python bytecode protection device based on joint obfuscation of opcodes and control flows, and the method includes:

[0006] S1. Get Python opcode file;

[0007] S2. According to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest occurrence frequency, and merge them into a new Python opcode;

[0008] S3, inserting instructions according to the new Python opcode, using the recompiled Python interpreter to run the Python opcode file, and obtaining a Python bytecode file after preliminary obfuscation;

[0009] S4. According to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used to perform replacement processing, and a loop instruction is used to perform loop expansion to obtain an obfuscated bytecode sequence;

[0010] S5. Using an opaque predicate insertion method, the obfuscated bytecode sequence is processed to obtain an obfuscated Python bytecode instruction list;

[0011] S6. Using an instruction garnishing method to process the obfuscated Python bytecode instruction list to obtain a final obfuscated bytecode sequence.

[0012] Optionally, the opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in the source code declaration file to be unrelated to function calls is selected as the target object of the replacement operation.

[0013] Optionally, the step S2 processes the original opcode sequence of the Python opcode file using an opcode replacement method according to the Python opcode file, extracts the opcode subsequence with the highest occurrence frequency through an opcode merging method, and merges them into a new Python opcode, including:

[0014] S21. According to the Python opcode file, the original opcode is processed by an opcode replacement method, and the opcode instructions are classified by a group random obfuscation strategy to obtain multiple groups;

[0015] S22. For each group, rearrange the order of the operation codes in a random manner to obtain an obfuscated operation code value mapping relationship;

[0016] S23, counting the high-frequency operation code sequences according to the obfuscated operation code value mapping relationship;

[0017] S24. According to the high-frequency operation code sequence, the high-frequency operation code sequence is merged by using the peephole optimization technology to obtain a new Python operation code.

[0018] Optionally, the step S4 uses a synonymous instruction replacement method to replace the initially obfuscated Python bytecode file, performs loop expansion through loop instructions, and obtains an obfuscated bytecode sequence, including:

[0019] According to the Python bytecode file after preliminary obfuscation, instruction traversal, conditional branch instructions and function call instructions are performed on the Python bytecode file after preliminary obfuscation, and code blocks with a degree of semantic equivalence of program execution are selected to replace the original opcode; loop instructions are used to modify the code logic structure of the original program by loop expansion to obtain an obfuscated bytecode sequence.

[0020] Optionally, the instruction embellishment method of S6 includes: a superposition jump method and a false loop method.

[0021] Optionally, the step S6 uses an instruction garnishing method to process the obfuscated Python bytecode instruction list, including:

[0022] S61. According to the obfuscated Python bytecode instruction list, a basic block in the original program is split using a superposition jump method to obtain a first sub-block and a second sub-block;

[0023] S62, creating a new false block set, and establishing a jump link relationship between the false basic block in the false block set and the first sub-block and the second sub-block;

[0024] S63, constructing a basic block set of a loop structure; dividing the basic block set of the original program into a first basic block and a second basic block according to logic;

[0025] S64. According to the first basic block and the second basic block, embed a basic block set with a loop structure between the first basic block and the second basic block.

[0026] On the other hand, a Python bytecode protection device based on joint obfuscation of opcodes and control flows is provided, and the device is applied to a Python bytecode protection method based on joint obfuscation of opcodes and control flows, and the device comprises:

[0027] A first acquisition unit is used to acquire a Python operation code file;

[0028] A processing unit is used to process the original opcode sequence of the Python opcode file by using an opcode replacement method according to the Python opcode file, and extract the opcode subsequence with the highest occurrence frequency by using an opcode merging method, and merge them into a new Python opcode;

[0029] A second acquisition unit is used to insert instructions according to the new Python opcode, and use the recompiled Python interpreter to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation;

[0030] A third acquisition unit is used to perform replacement processing using a synonymous instruction replacement method according to the Python bytecode file after the preliminary obfuscation, and perform loop expansion through loop instructions to obtain an obfuscated bytecode sequence;

[0031] A fourth acquisition unit is used to process the obfuscated bytecode sequence by inserting an opaque predicate method to obtain an obfuscated Python bytecode instruction list;

[0032] The fifth acquisition unit is used to process the obfuscated Python bytecode instruction list by using an instruction decoration method to obtain a final obfuscated bytecode sequence.

[0033] Optionally, the opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in the source code declaration file to be unrelated to function calls is selected as the target object of the replacement operation.

[0034] Optionally, the processing unit is used to:

[0035] According to the Python opcode file, the original opcode is processed by an opcode replacement method, and the opcode instructions are classified by a group random obfuscation strategy to obtain multiple groups;

[0036] For each group, the opcode order is rearranged in a random manner to obtain the obfuscated opcode-value mapping relationship;

[0037] According to the mapping relationship between the obfuscated opcode values, the opcode sequences with high frequency are counted;

[0038] According to the high-frequency operation code sequences, the peephole optimization technology is used to merge the high-frequency operation code sequences to obtain new Python operation codes.

[0039] Optionally, the third acquiring unit is used to:

[0040] According to the Python bytecode file after preliminary obfuscation, instruction traversal, conditional branch instructions and function call instructions are performed on the Python bytecode file after preliminary obfuscation, and code blocks with a degree of semantic equivalence of program execution are selected to replace the original opcode; loop instructions are used to modify the code logic structure of the original program by loop expansion to obtain an obfuscated bytecode sequence.

[0041] Optionally, the instruction embellishment method includes: a superposition jump method and a false loop method.

[0042] Optionally, the fifth acquiring unit is used to:

[0043] According to the obfuscated Python bytecode instruction list, the basic block in the original program is split using the superposition jump method to obtain the first sub-block and the second sub-block;

[0044] Creating a new false block set, and establishing a jump link relationship between a false basic block in the false block set and the first sub-block and the second sub-block;

[0045] Construct a set of basic blocks of a loop structure; divide the original program basic block set into a first basic block and a second basic block according to logic;

[0046] According to the first basic block and the second basic block, a basic block set with a loop structure is embedded between the first basic block and the second basic block.

[0047] On the other hand, a Python bytecode protection device based on joint obfuscation of opcodes and control flows is provided, and the Python bytecode protection device based on joint obfuscation of opcodes and control flows comprises: a processor; a memory, wherein computer-readable instructions are stored on the memory, and when the computer-readable instructions are executed by the processor, any one of the above-mentioned Python bytecode protection methods based on joint obfuscation of opcodes and control flows is implemented.

[0048] On the other hand, a computer-readable storage medium is provided, in which at least one instruction is stored, and the at least one instruction is loaded and executed by a processor to implement any one of the above-mentioned Python bytecode protection methods based on joint obfuscation of opcodes and control flows.

[0049] The beneficial effects brought about by the technical solution provided by the embodiment of the present invention include at least:

[0050] The embodiment of the present invention first obtains a Python opcode file; according to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest frequency of occurrence, and merge them into a new Python opcode; secondly, instructions are inserted according to the new Python opcode, and a recompiled Python interpreter is used to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation; finally, according to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used to perform replacement processing, and a loop is expanded through a loop instruction to obtain an obfuscated bytecode sequence; an opaque predicate insertion method is used to process the obfuscated bytecode sequence to obtain an obfuscated Python bytecode instruction list; an instruction flower method is used to process the obfuscated Python bytecode instruction list to obtain a final obfuscated bytecode sequence.

[0051] The obfuscated and protected Python bytecode file obtained by the embodiment of the present invention can improve the McCabe complexity and Henderson-Sellars complexity compared with the ordinary bytecode file, and can improve the labor cost of reverse analysis; in the decompilation scenario, the embodiment of the present invention can reduce the number of successful reverse engineers and can restrict static analysis methods; the embodiment of the present invention can improve the time and space overhead required for the execution of Python bytecode files; when the embodiment of the present invention is used to obfuscate the program, the correctness of the original business logic can be ensured, and the security barrier of the source code assets can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0052] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0053] Figure 1 This is an overall framework diagram of Python bytecode protection based on joint obfuscation of opcodes and control flows provided by an embodiment of the present invention;

[0054] Figure 2 This is a flow chart of the design idea of ​​an operation code obfuscation method based on frequency clustering and sequence merging provided by an embodiment of the present invention;

[0055] Figure 3 It is a flow chart of a Python bytecode protection method based on joint obfuscation of opcode and control flow provided by an embodiment of the present invention;

[0056] Figure 4 This is a schematic diagram of operation code replacement classification provided by an embodiment of the present invention;

[0057] Figure 5 This is a frequency statistics principle diagram of a parameterized instruction combination provided by an embodiment of the present invention;

[0058] Figure 6 is a schematic diagram of a merging example provided by an embodiment of the present invention;

[0059] Figure 7 It is a principle diagram of a synonymous instruction replacement algorithm provided by an embodiment of the present invention;

[0060] Figure 8 This is a schematic diagram of a control flow after inserting a false loop instruction provided by an embodiment of the present invention;

[0061] Fig. 9 It is a block diagram of a Python bytecode protection device based on joint obfuscation of opcodes and control flows provided by an embodiment of the present invention;

[0062] Fig.10 It is a structural schematic diagram of a Python bytecode protection device based on joint obfuscation of opcodes and control flows provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0063] The technical solution of the present invention is described below in conjunction with the accompanying drawings.

[0064] In the embodiments of the present invention, words such as "exemplarily" and "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design described as "example" in the present invention should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of the word "example" is intended to present the concept in a specific way. In addition, in the embodiments of the present invention, the meaning expressed by "and / or" can be both, or it can be either of the two.

[0065] In the embodiments of the present invention, "image" and "picture" can sometimes be used interchangeably. It should be noted that when the difference between them is not emphasized, the meanings they intend to express are the same. "of", "corresponding, relevant" and "corresponding" can sometimes be used interchangeably. It should be noted that when the difference between them is not emphasized, the meanings they intend to express are the same.

[0066] In the embodiments of the present invention, sometimes the subscripts such as W 1 It may be written in non-subscript form such as W1. When the difference is not emphasized, the meaning is the same.

[0067] In order to make the technical problems, technical solutions and advantages to be solved by the present invention more clear, a detailed description will be given below with reference to the accompanying drawings and specific embodiments.

[0068] The embodiment of the present invention provides a Python bytecode protection method based on combined obfuscation of opcodes and control flows. The method can be implemented by a Python bytecode protection device based on combined obfuscation of opcodes and control flows. The Python bytecode protection device based on combined obfuscation of opcodes and control flows can be a terminal or a server. Figure 1 The invention discloses an overall framework diagram of Python bytecode protection based on joint obfuscation of opcode and control flow provided by an embodiment of the present invention; in a feasible implementation mode, Cpython source code is obtained, the Cpython source code is parsed, and the parsed result is obtained; the opcode modification engine operation includes: opcode replacement and opcode merging; wherein the opcode replacement includes grouping design operation and random replacement operation; wherein the opcode merging includes: instruction insertion operation and compiler optimization operation; through the opcode modification engine processing, a reconstructed bytecode sequence is obtained; the reconstructed bytecode operation sequence is subjected to synonymous instruction replacement, opaque predicate insertion and instruction decoration processing to obtain an obfuscated bytecode sequence; wherein the synonymous instruction replacement includes: an operation replacement step and a loop expansion step; the opaque predicate insertion includes: a false branch insertion step; the instruction decoration includes: a superposition jump step and a false loop step.

[0069] like Figure 2 The figure shows a flow chart of the design idea of ​​an opcode obfuscation method based on frequency clustering and sequence merging provided by an embodiment of the present invention. In a feasible implementation manner, the specific operation steps of the opcode obfuscation method based on frequency clustering and sequence merging include: finding replaceable opcode instructions, modifying opcode mapping relationships, opcode frequency statistics, extracting mergable instruction combinations, adding new instruction execution processes, and recompiling to generate an interpreter.

[0070] like Figure 3 The figure is a flow chart of a Python bytecode protection method based on joint obfuscation of opcodes and control flows provided by an embodiment of the present invention. The processing flow of the method may include the following steps:

[0071] S1. Get the Python opcode file.

[0072] S2. According to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest occurrence frequency and merge them into a new Python opcode.

[0073] Optionally, an opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in the source code declaration file to be unrelated to function calls is selected as the target object of the replacement operation.

[0074] In a feasible implementation, in the actual opcode replacement, the opcode portion corresponding to the code value less than 90 and the operation portion of the opcode corresponding to the code value greater than 90 can be randomly replaced; wherein, the opcodes for the program control flow transformation operation include: CALL_FUNCTION, CALL_FUNCTION_KW, and CALL_FUNCTION_EX; wherein, the opcode operation involves a complex process of calling a function and processing a function return value, involving related operations of the virtual machine stack such as data in and out of the stack, function calls, and popping of return values.

[0075] Among them, the function call instruction involves the dynamic characteristics of the program, including: passing a variable number of parameters and using keyword parameters; the function call instruction will introduce complex program logic when executed; in order to simplify the replacement process, the function call instruction is not included in the instruction set scope of opcode replacement.

[0076] Among them, Figure 4 The figure shows a schematic diagram of opcode replacement classification provided by an embodiment of the present invention; in a feasible implementation manner, the Python opcode sequence is divided into a scrambled part and an unscrambled part, wherein the unscrambled part includes: OPCODEs that are not explicitly declared, OPCODEs used as function parameters, and OPCODEs used as special markers.

[0077] Optionally, the specific implementation steps of S2 may include S21-S24:

[0078] S21. According to the Python opcode file, the original opcode is processed by an opcode replacement method, and the opcode instructions are classified by a group random obfuscation strategy to obtain multiple groups;

[0079] Among them, the group random obfuscation strategy classifies and groups the opcode instructions; among them, the opcodes in the group are correlated.

[0080] S22. For each group, rearrange the order of the operation codes in a random manner to obtain an obfuscated operation code value mapping relationship;

[0081] In order to ensure the coherence of the overall program logic, the relative order between groups remains unchanged, ensuring the strength of obfuscation while maintaining the original control flow characteristics of the program. The order between groups of G1, G2, and G3 remains relatively consistent, and the opcode sequence within each group is randomly shuffled.

[0082] Among them, the obfuscation method can achieve good obfuscation effect while ensuring parameter characteristics, and can improve software complexity.

[0083] Among them, the instructions in the Python virtual machine are divided into three groups, including: opcodes without parameters, with corresponding code values ​​1-89, which can be randomly shuffled internally; opcodes with parameters and unrelated to function calls, with corresponding code values ​​90-130; opcodes with parameters and related to function calls, with corresponding code values ​​143-165.

[0084] In a feasible implementation, the specific steps of grouping the opcode instruction sequence according to the instruction grouping method in the Python virtual machine include:

[0085] (1) Extract the legal opcode value sequences, namely A1, A2 and A3;

[0086] Where A1=[1, 2, 3, 4, 5, 6, …, 86, 87, 89];

[0087] Among them, A2=[90, 91, 92, 93, 94, 95, …, 125, 126, 130];

[0088] Among them, A3=[143, 144, 145, 146, 147, 148, …, 163, 164, 165].

[0089] (2) In each group, the order of the code values ​​is randomly shuffled to obtain a shuffled sequence of operation code values, including:

[0090] Among them, A1=[11, 83, 22, 15, 48, 69, …, 68, 26, 9];

[0091] Among them, A2=[112, 98, 125, 92,117, 105, …, 126, 102, 130];

[0092] Among them, A3=[163, 148, 145, 152, 150, 144, …, 149, 157, 161].

[0093] (3) Write the new code value mapping relationship into the header file that saves the mapping relationship.

[0094] S23, counting the high-frequency operation code sequences according to the obfuscated operation code value mapping relationship;

[0095] In a feasible implementation, 1957 Python source code files are selected as a test set, and the instruction combinations with the highest frequency of occurrence are counted as objects to be merged.

[0096] Among them, in Python 3.9.6 version, it is stipulated that each opcode can only have one parameter; for the opcode sequence objects to be merged obtained by frequency analysis, if they are all instructions with parameters, one of the parameters will be fixed and explicitly written into the execution logic of the new instruction; if it is an instruction without parameters, the newly inserted instruction does not need to perform parameter processing.

[0097] In a feasible implementation, the process of frequency statistics of the parameterless instruction combination includes: establishing a two-dimensional statistical matrix of size 166×166 , the matrix elements The value of represents the number of occurrences of a pair of consecutive instructions consisting of a pair of instructions with the first opcode value i and the second opcode value j in the test set code file, using two index pointers to retrieve two operation instructions, wherein the two index pointers include: the first index pointer first and the second index pointer next; when the two index pointers appear in the same basic block as an opcode sequence, the corresponding array elements are incremented. Among them, for opcodes with parameters, according to the Python 3.9.6 interpreter, each instruction occupies two bytes, and the opcode and operand each occupy one byte; among them, in order to ensure that the opcode execution logic characteristics do not change after the addition of instructions, the merged new opcode can only contain one parameter.

[0098] In a feasible implementation manner, the process of counting the frequency of instruction combinations with parameters includes: fixing the parameters of the first operation code, and for the frequency counting part of the operation code with parameters, according to the frequency counting step of the instruction combination without parameters, counting the instruction combination with the highest frequency as the object of the merge operation; wherein, if Figure 5 The figure is a frequency statistics principle diagram of a parameterized instruction combination provided by an embodiment of the present invention. In a feasible implementation mode, It records the frequency of occurrence of the parameter value of the ith parameter of the instruction pair numbered m when it is n. In the embodiment of the present invention, only the parameter values ​​in the range of 0-10 are counted; wherein, in the actual Python bytecode instructions, many opcode parameters correspond to the index subscript values ​​of the constant table or the variable table, and the index value usually starts from 0 and has a small upper limit. For example, the instruction (LOAD_CONST, 0) indicates loading data with an index value of 0 from the constant table, and its frequency of occurrence is much higher than the frequency of occurrence of the instruction (LOAD_CONST, 20). Among them, the statistical method adopted in the embodiment of the present invention conforms to the statistical characteristics in actual scenarios. For example, The value represents the instruction combination numbered 1, that is, the frequency of the instruction when the corresponding parameter of the first instruction of (100, 100) is 0. It represents the frequency of occurrence of the corresponding instruction pair when the parameter corresponding to the second instruction of the fifth instruction combination (124, 124) is 3.

[0099] S24. According to the high-frequency operation code sequence, the high-frequency operation code sequence is merged by using the peephole optimization technology to obtain a new Python operation code.

[0100] Among them, in the statistical analysis process of instructions with parameters, the target of sequence extraction is the instruction sequence contained in the co_code attribute inside a single PyCodeObject structure; among them, the instruction sequence comes from the same program basic block and meets the column merging requirements.

[0101] By analyzing the source code, a virtual machine stack operation corresponding to a newly inserted instruction can be designed for an instruction with parameters or an instruction without parameters.

[0102] In one feasible implementation, the modification and expansion process of the opcode set uses a peephole optimization technique to identify opcode subsequences that appear frequently and are arranged consecutively in the same basic block. , merge the recognized opcode subsequences into a new opcode , and add custom The virtual machine stack operation corresponding to the instruction makes the inserted instruction and the instruction sequence execution effect equivalent; Figure 6 The figure shows a schematic diagram of a merging example provided by an embodiment of the present invention. In a feasible implementation manner, taking the instruction to be merged (124 0, 100 X) as an example, the corresponding opcode sequence is (LOAD_FAST 0, LOAD_CONST X), and the virtual machine stack operation includes: loading a constant with index X from a constant pool, creating a function object with 0 as a parameter; merging the instruction combination into a new instruction LOAD_FAST_ZERO_LOAD_CONST; and selecting 166 as the code value corresponding to the newly added opcode.

[0103] In a feasible implementation, the embodiment of the present invention uses peephole optimization technology to perform pattern matching of newly inserted code; the position where a specified continuous bytecode sequence appears in the Python bytecode file can be replaced with the newly inserted composite opcode.

[0104] Among them, the peephole optimization technology can be used for program execution, and two-step continuous operations can be replaced by more compact instructions; CPython provides a peephole optimizer, located in the Python / peephole.c file, which can improve the execution efficiency of the program by simplifying the code during the compilation process; by modifying the original peephole optimization strategy, the rule matching can recognize the inserted merged bytecode and execute it, making the bytecode obfuscated.

[0105] Among them, the opcode merging and insertion technology is used to increase the difference in opcode sets between the customized interpreter and the official interpreter, which can make the obfuscated Python bytecode sequence more complex.

[0106] S3. Insert instructions according to the new Python opcode, use the recompiled Python interpreter to run the Python opcode file, and obtain a Python bytecode file after preliminary obfuscation.

[0107] In a feasible implementation manner, the specific implementation process of instruction insertion includes:

[0108] (1) Add the definition of the insertion opcode in the source code of Python 3.9.6 version. The corresponding Lib / opcode.py file has the following definition command: def_op('LOAD_FAST_ZERO_LOAD_CONST', 166). In the Python virtual machine, the execution of the opcode sequence is controlled by a switch-case structure, which is responsible for reading the corresponding opcode and its parameters one by one and matching them to the corresponding case statement. The case statement block defines the relevant virtual machine stack operations.

[0109] (2) Analyze the operating principle of the Python virtual machine stack and define the virtual machine stack operation corresponding to the insertion instruction in the corresponding case statement block;

[0110] (3) Add the new code to the switch-case structure to complete the addition of the new instruction.

[0111] S4. According to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used for replacement processing, and loop instructions are used for loop expansion to obtain an obfuscated bytecode sequence.

[0112] Optionally, the specific implementation process of S4 may include:

[0113] According to the Python bytecode file after preliminary obfuscation, instruction traversal, conditional branch instructions and function call instructions are performed on the Python bytecode file after preliminary obfuscation, and code blocks with a degree of semantic equivalence of program execution are selected to replace the original opcode; loop instructions are used to modify the code logic structure of the original program by loop expansion to obtain an obfuscated bytecode sequence.

[0114] Among them, there are three types of instructions that control the program control flow in the Python virtual stack: conditional branch instructions, loop control instructions, and function call instructions;

[0115] Among them, Figure 7 The figure shows a schematic diagram of a synonymous instruction replacement algorithm provided by an embodiment of the present invention; in a feasible implementation manner, in the Python opcode sequence, the instructions involving program control flow transformation include: conditional branch instructions, function call instructions and loop instructions; the opcode instructions are read one by one through a traversal operation, and for conditional branch instructions and function call instructions, they are replaced with equivalent program blocks through code reorganization, conditional branch adjustment, function call statement reconstruction and data operation conversion techniques, and for loop instructions, the code logic structure of the source program is modified using loop unrolling technology.

[0116] S5. Use the opaque predicate insertion method to process the obfuscated bytecode sequence and obtain the obfuscated Python bytecode instruction list.

[0117] Among them, as shown in Figure 1, the obfuscation principle of inserting opaque predicate technology is shown.

[0118]

[0119] In one feasible approach, the obfuscated bytecode instruction sequence is initialized to an empty list; each instruction in the original bytecode instruction sequence is traversed, and if the instruction is<FOR_ITER> When the current instruction is a branch instruction, check whether it is a branch instruction. If the current instruction is a branch instruction, use the opaque predicate generation function to generate an opaque predicate, insert the generated opaque predicate into the obfuscated bytecode instruction sequence, and update the target address to make the inserted opaque predicate instruction effective; if it is not a branch instruction, directly insert the current instruction in the original bytecode instruction sequence into the obfuscated bytecode instruction sequence.

[0120] S6. Use the instruction garnishing method to process the obfuscated Python bytecode instruction list to obtain the final obfuscated bytecode sequence.

[0121] Optionally, the instruction decoration method of S6 includes: a superposition jump method and a false loop method.

[0122] In a feasible implementation manner, the specific implementation steps of the superposition jump method include:

[0123] (1) The basic block set in the original program is recorded as ;

[0124] (2) For any basic block Implement the split operation and divide it into two sub-blocks: the first sub-block and the second sub-block ;

[0125] (3) Create a new set of fake blocks , under the premise of strictly maintaining the original execution semantics of the program, establish a set of false blocks Inner false basic block and first sub-block and the second sub-block

[0126] The jump link relationship between them.

[0127] In a feasible implementation manner, the specific implementation steps of the superposition jump method include:

[0128] (1) Construct a set of basic blocks representing loop structures , the original program basic block set ;

[0129] (2) The basic block set of the original program is Each basic block in the program is divided into two new basic blocks according to the principle of logical division that does not affect the program execution result, including: the first basic block and the second basic block ; In the process of segmentation, it is necessary to ensure that the original semantics of the program is completely preserved;

[0130] (3) Between the two new basic blocks after segmentation, embed The pseudo-loop structure basic block represented by the collection; The pseudo loop structure basic blocks represented by the collection can increase the complexity of the control flow and increase the difficulty of reverse engineering the program.

[0131] Optionally, the specific implementation process of S6 may include S61-S64:

[0132] S61. According to the obfuscated Python bytecode instruction list, a basic block in the original program is split using a superposition jump method to obtain a first sub-block and a second sub-block;

[0133] S62, creating a new false block set, and establishing a jump link relationship between the false basic block in the false block set and the first sub-block and the second sub-block;

[0134] S63, constructing a basic block set of a loop structure; dividing the basic block set of the original program into a first basic block and a second basic block according to logic;

[0135] S64. According to the first basic block and the second basic block, embed a basic block set with a loop structure between the first basic block and the second basic block.

[0136] In a feasible implementation, Figure 8 The figure shows a control flow diagram after inserting a false loop instruction provided by an embodiment of the present invention, wherein a basic block set loopBBs of a loop structure is inserted between a starting code block startBB and a redundant code block bogusBB; wherein the design of the loop instruction is highly flexible, and in practical applications, a more complex alternative form that is closely related to the source program can be selected.

[0137] Among them, the inserted superposition jump instructions are similar to the original instructions in the original block in structure, but different in function. The inserted redundant code blocks have no actual execution meaning, and the codes do not correspond to specific function functions, and are meaningless instructions. Among them, the false loop instructions can simulate and disguise the original logic by selecting and using the existing variables in the original basic block as loop control conditions. The superposition jump method and the false loop method can enhance the interference effect on reverse engineering without changing the program function, and can increase the difficulty and complexity of reverse engineering.

[0138] Among them, the method of replacing Python bytecode sequences with synonymous instructions, inserting opaque predicates and adding instructions can improve the obfuscation strength of the control flow, make the original bytecode sequence fully cooperate with the false instruction part, and improve the hiding ability of the bytecode sequence control flow obfuscation; the method of replacing Python bytecode sequences with synonymous instructions, inserting opaque predicates and adding instructions can act on the real program semantics of the bytecode sequence and destroy the established pattern recognition rules of the deobfuscator.

[0139] Among them, the file is used to add flower instructions, which can hide the similarity between the inserted instructions and the original instruction sequence, and can improve the protection effect.

[0140] In a feasible implementation, the embodiment of the present invention randomly obfuscates the code value mapping relationship of the Python opcode sequence, obtains the opcode pairs with high frequency in Python basic operations, merges the contents of the opcode sequence composed of multiple opcodes, inserts them into the Python virtual machine definition as a new instruction, expands the original opcode set, and uses the peephole optimization technology to perform compilation optimization processing; the embodiment of the present invention adds a merge instruction through the statistical analysis of the graph of the opcode set, changes the length and structure of the original opcode sequence, and increases the obfuscation degree of the bytecode file. The use of the embodiment of the present invention can improve the time consumption rate and the space consumption change rate. The embodiment of the present invention can improve the obfuscation strength and elasticity of the bytecode sequence and has a small time negative overhead within an acceptable range without introducing additional space negative overhead.

[0141] The embodiment of the present invention first obtains a Python opcode file; according to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest frequency of occurrence, and merge them into a new Python opcode; secondly, instructions are inserted according to the new Python opcode, and a recompiled Python interpreter is used to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation; finally, according to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used to perform replacement processing, and a loop is expanded through a loop instruction to obtain an obfuscated bytecode sequence; an opaque predicate insertion method is used to process the obfuscated bytecode sequence to obtain an obfuscated Python bytecode instruction list; an instruction flower method is used to process the obfuscated Python bytecode instruction list to obtain a final obfuscated bytecode sequence.

[0142] The obfuscated and protected Python bytecode file obtained by the embodiment of the present invention can improve the McCabe complexity and Henderson-Sellars complexity compared with the ordinary bytecode file, and can improve the labor cost of reverse analysis; in the decompilation scenario, the embodiment of the present invention can reduce the number of successful reverse engineers and can restrict static analysis methods; the embodiment of the present invention can improve the time and space overhead required for the execution of Python bytecode files; when the embodiment of the present invention is used to obfuscate the program, the correctness of the original business logic can be ensured, and the security barrier of the source code assets can be improved.

[0143] Fig. 9 The present invention is a block diagram of a Python bytecode protection device based on combined obfuscation of opcodes and control flows according to an exemplary embodiment. The device is used in a Python bytecode protection method based on combined obfuscation of opcodes and control flows. Fig. 9 The device includes a first acquisition unit 310, a processing unit 320, a second acquisition unit 330, a third acquisition unit 340, a fourth acquisition unit 350 and a fifth acquisition unit 360. Wherein:

[0144] A first acquisition unit 310 is used to acquire a Python operation code file;

[0145] The processing unit 320 is used to process the original opcode sequence of the Python opcode file by using the opcode replacement method according to the Python opcode file, and extract the opcode subsequence with the highest occurrence frequency by using the opcode merging method, and merge them into a new Python opcode;

[0146] The second acquisition unit 330 is used to insert instructions according to the new Python opcode, and use the recompiled Python interpreter to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation;

[0147] The third acquisition unit 340 is used to perform replacement processing using a synonymous instruction replacement method according to the Python bytecode file after the preliminary obfuscation, and perform loop expansion through loop instructions to obtain an obfuscated bytecode sequence;

[0148] A fourth acquisition unit 350 is used to process the obfuscated bytecode sequence by inserting an opaque predicate method to obtain an obfuscated Python bytecode instruction list;

[0149] The fifth acquisition unit 360 is used to process the obfuscated Python bytecode instruction list using an instruction decoration method to obtain a final obfuscated bytecode sequence.

[0150] Optionally, the opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in the source code declaration file to be unrelated to function calls is selected as the target object of the replacement operation.

[0151] Optionally, the processing unit 320 is configured to:

[0152] According to the Python opcode file, the original opcode is processed by an opcode replacement method, and the opcode instructions are classified by a group random obfuscation strategy to obtain multiple groups;

[0153] For each group, the opcode order is rearranged in a random manner to obtain the obfuscated opcode-value mapping relationship;

[0154] According to the mapping relationship between the obfuscated opcode values, the opcode sequences with high frequency are counted;

[0155] According to the high-frequency operation code sequences, the peephole optimization technology is used to merge the high-frequency operation code sequences to obtain new Python operation codes.

[0156] Optionally, the third obtaining unit 340 is configured to:

[0157] According to the Python bytecode file after preliminary obfuscation, instruction traversal, conditional branch instructions and function call instructions are performed on the Python bytecode file after preliminary obfuscation, and code blocks with a degree of semantic equivalence of program execution are selected to replace the original opcode; loop instructions are used to modify the code logic structure of the original program by loop expansion to obtain an obfuscated bytecode sequence.

[0158] Optionally, the instruction embellishment method includes: a superposition jump method and a false loop method.

[0159] Optionally, the fifth acquiring unit 360 is configured to:

[0160] According to the obfuscated Python bytecode instruction list, the basic block in the original program is split using the superposition jump method to obtain the first sub-block and the second sub-block;

[0161] Creating a new false block set, and establishing a jump link relationship between a false basic block in the false block set and the first sub-block and the second sub-block;

[0162] Construct a set of basic blocks of a loop structure; divide the original program basic block set into a first basic block and a second basic block according to logic;

[0163] According to the first basic block and the second basic block, a basic block set with a loop structure is embedded between the first basic block and the second basic block.

[0164] The embodiment of the present invention first obtains a Python opcode file; according to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest frequency of occurrence, and merge them into a new Python opcode; secondly, instructions are inserted according to the new Python opcode, and a recompiled Python interpreter is used to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation; finally, according to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used to perform replacement processing, and a loop is expanded through a loop instruction to obtain an obfuscated bytecode sequence; an opaque predicate insertion method is used to process the obfuscated bytecode sequence to obtain an obfuscated Python bytecode instruction list; an instruction flower method is used to process the obfuscated Python bytecode instruction list to obtain a final obfuscated bytecode sequence.

[0165] The obfuscated and protected Python bytecode file obtained by the embodiment of the present invention can improve the McCabe complexity and Henderson-Sellars complexity compared with the ordinary bytecode file, and can improve the labor cost of reverse analysis; in the decompilation scenario, the embodiment of the present invention can reduce the number of successful reverse engineers and can restrict static analysis methods; the embodiment of the present invention can improve the time and space overhead required for the execution of Python bytecode files; when the embodiment of the present invention is used to obfuscate the program, the correctness of the original business logic can be ensured, and the security barrier of the source code assets can be improved.

[0166] Fig.10 : is a schematic diagram of the structure of a Python bytecode protection device based on combined obfuscation of opcodes and control flows provided by an embodiment of the present invention, such as Fig.10 As shown, the Python bytecode protection device based on the joint obfuscation of opcode and control flow may include the above Fig. 9 The Python bytecode protection device based on the joint obfuscation of opcode and control flow is shown. Optionally, the Python bytecode protection device 410 based on the joint obfuscation of opcode and control flow may include a first processor 2001 .

[0167] Optionally, the Python bytecode protection device 410 based on joint obfuscation of opcodes and control flows may further include a memory 2002 and a transceiver 2003 .

[0168] The first processor 2001, the memory 2002 and the transceiver 2003 may be connected via a communication bus.

[0169] Combine the following Fig.10The components of the Python bytecode protection device 410 based on the joint obfuscation of opcodes and control flows are specifically introduced as follows:

[0170] The first processor 2001 is the control center of the Python bytecode protection device 410 based on the joint obfuscation of the opcode and the control flow, and can be a processor or a general term for multiple processing elements. For example, the first processor 2001 is one or more central processing units (CPUs), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present invention, such as one or more microprocessors (digital signal processors, DSPs), or one or more field programmable gate arrays (field programmable gate arrays, FPGAs).

[0171] Optionally, the first processor 2001 can perform various functions of the Python bytecode protection device 410 based on the joint obfuscation of operation code and control flow by running or executing the software program stored in the memory 2002 and calling the data stored in the memory 2002.

[0172] In a specific implementation, as an embodiment, the first processor 2001 may include one or more CPUs, such as Fig.10 CPU0 and CPU1 are shown in FIG.

[0173] In a specific implementation, as an embodiment, the Python bytecode protection device 410 based on the joint obfuscation of opcodes and control flows may also include multiple processors, such as Fig.10 The first processor 2001 and the second processor 2004 are shown in FIG. Each of these processors may be a single-core processor (single-CPU) or a multi-core processor (multi-CPU). The processor here may refer to one or more devices, circuits, and / or processing cores for processing data (e.g., computer program instructions).

[0174] The memory 2002 is used to store the software program for executing the solution of the present invention, and is controlled to be executed by the first processor 2001. The specific implementation method can refer to the above method embodiment, which will not be repeated here.

[0175] Optionally, the memory 2002 may be a read-only memory (ROM) or other types of static storage devices that can store static information and instructions, a random access memory (RAM) or other types of dynamic storage devices that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical disc, laser disc, optical disc, digital versatile disc, Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 2002 may be integrated with the first processor 2001, or may exist independently, and may be connected to the first processor 2001 through the interface circuit ( Fig.10 (not shown) is coupled to the first processor 2001, which is not specifically limited in this embodiment of the present invention.

[0176] The transceiver 2003 is used to communicate with a network device or a terminal device.

[0177] Optionally, the transceiver 2003 may include a receiver and a transmitter ( Fig.10 The receiver is used to implement a receiving function, and the transmitter is used to implement a sending function.

[0178] Optionally, the transceiver 2003 may be integrated with the first processor 2001, or may exist independently, and may be protected by an interface circuit ( Fig.10 (not shown) is coupled to the first processor 2001, which is not specifically limited in this embodiment of the present invention.

[0179] It should be noted that Fig.10 The structure of the Python bytecode protection device 410 based on the joint obfuscation of opcodes and control flows shown in the figure does not constitute a limitation on the router. The actual knowledge structure recognition device may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0180] In addition, the technical effects of the Python bytecode protection device 410 based on the joint obfuscation of opcodes and control flows can refer to the technical effects of the Python bytecode protection method based on the joint obfuscation of opcodes and control flows described in the above method embodiment, which will not be repeated here.

[0181] It should be understood that the first processor 2001 in the embodiment of the present invention may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.

[0182] It should also be understood that the memory in the embodiments of the present invention may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic random access memory (DRAM), synchronous DRAM (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct rambus RAM (DR RAM).

[0183] The above embodiments can be implemented in whole or in part by software, hardware (such as circuits), firmware or any other combination. When implemented by software, the above embodiments can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server or data center to another website, computer, server or data center by wired (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that contains one or more available media sets. The available medium can be a magnetic medium (for example, a floppy disk, a hard disk, a tape), an optical medium (for example, a DVD), or a semiconductor medium. The semiconductor medium can be a solid-state hard disk.

[0184] It should be understood that the term "and / or" in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. A and B can be singular or plural. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship, but it may also indicate an "and / or" relationship. Please refer to the context for specific understanding.

[0185] In the present invention, "at least one" means one or more, and "plurality" means two or more. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple.

[0186] It should be understood that in various embodiments of the present invention, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.

[0187] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0188] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described equipment, devices and units can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0189] In the several embodiments provided by the present invention, it should be understood that the disclosed devices, apparatuses and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0190] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0191] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0192] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, and other media that can store program codes.

[0193] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A Python bytecode protection method based on joint obfuscation of opcode and control flow, It is characterized in that The method comprises: S1. Get Python opcode file; S2. According to the Python opcode file, an opcode replacement method is used to process the original opcode sequence of the Python opcode file, and an opcode merging method is used to extract the opcode subsequence with the highest occurrence frequency, and merge them into a new Python opcode; S3, inserting instructions according to the new Python opcode, using the recompiled Python interpreter to run the Python opcode file, and obtaining a Python bytecode file after preliminary obfuscation; S4. According to the Python bytecode file after preliminary obfuscation, a synonymous instruction replacement method is used to perform replacement processing, and a loop instruction is used to perform loop expansion to obtain an obfuscated bytecode sequence; S5. Using an opaque predicate insertion method, the obfuscated bytecode sequence is processed to obtain an obfuscated Python bytecode instruction list; S6. Using an instruction garnishing method to process the obfuscated Python bytecode instruction list to obtain a final obfuscated bytecode sequence.

2. The Python bytecode protection method based on combined obfuscation of opcode and control flow according to claim 1, It is characterized in that The opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in a source code declaration file to be unrelated to a function call is selected as a target object of the replacement operation.

3. The Python bytecode protection method based on combined obfuscation of opcode and control flow according to claim 1, It is characterized in that The S2 processes the original opcode sequence of the Python opcode file by using an opcode replacement method according to the Python opcode file, extracts the opcode subsequence with the highest occurrence frequency by using an opcode merging method, and merges them into a new Python opcode, including: S21. According to the Python opcode file, the original opcode is processed by an opcode replacement method, and the opcode instructions are classified by a group random obfuscation strategy to obtain multiple groups; S22. For each group, rearrange the order of the operation codes in a random manner to obtain an obfuscated operation code value mapping relationship; S23, counting the high-frequency operation code sequences according to the obfuscated operation code value mapping relationship; S24. According to the high-frequency operation code sequence, the high-frequency operation code sequence is merged by using the peephole optimization technology to obtain a new Python operation code.

4. The Python bytecode protection method based on combined obfuscation of opcode and control flow according to claim 1, It is characterized in that The S4 uses a synonymous instruction replacement method to replace the Python bytecode file after the preliminary obfuscation, performs loop expansion through loop instructions, and obtains an obfuscated bytecode sequence, including: According to the Python bytecode file after preliminary obfuscation, instruction traversal, conditional branch instructions and function call instructions are performed on the Python bytecode file after preliminary obfuscation, and code blocks with a degree of semantic equivalence of program execution are selected to replace the original opcode; loop instructions are used to modify the code logic structure of the original program by loop expansion to obtain an obfuscated bytecode sequence.

5. The Python bytecode protection method based on combined obfuscation of opcode and control flow according to claim 1, It is characterized in that The instruction decoration method of S6 includes: a superposition jump method and a false loop method.

6. The Python bytecode protection method based on combined obfuscation of opcode and control flow according to claim 5, It is characterized in that The S6 uses an instruction garnishing method to process the obfuscated Python bytecode instruction list, including: S61. According to the obfuscated Python bytecode instruction list, a basic block in the original program is split using a superposition jump method to obtain a first sub-block and a second sub-block; S62, creating a new false block set, and establishing a jump link relationship between the false basic block in the false block set and the first sub-block and the second sub-block; S63, constructing a basic block set of a loop structure; dividing the basic block set of the original program into a first basic block and a second basic block according to logic; S64. According to the first basic block and the second basic block, embed a basic block set with a loop structure between the first basic block and the second basic block.

7. A Python bytecode protection device based on joint obfuscation of opcodes and control flows, the Python bytecode protection device based on joint obfuscation of opcodes and control flows is used to implement the Python bytecode protection method based on joint obfuscation of opcodes and control flows as claimed in any one of claims 1 to 6, It is characterized in that The device comprises: A first acquisition unit is used to acquire a Python operation code file; A processing unit is used to process the original opcode sequence of the Python opcode file by using an opcode replacement method according to the Python opcode file, and extract the opcode subsequence with the highest occurrence frequency by using an opcode merging method, and merge them into a new Python opcode; A second acquisition unit is used to insert instructions according to the new Python opcode, and use the recompiled Python interpreter to run the Python opcode file to obtain a Python bytecode file after preliminary obfuscation; A third acquisition unit is used to perform replacement processing using a synonymous instruction replacement method according to the Python bytecode file after the preliminary obfuscation, and perform loop expansion through a loop instruction to obtain an obfuscated bytecode sequence; A fourth acquisition unit is used to process the obfuscated bytecode sequence by inserting an opaque predicate method to obtain an obfuscated Python bytecode instruction list; The fifth acquisition unit is used to process the obfuscated Python bytecode instruction list by using an instruction decoration method to obtain a final obfuscated bytecode sequence.

8. The Python bytecode protection device based on combined obfuscation of opcode and control flow according to claim 7, It is characterized in that The opcode replacement method is used to divide the opcode set into an instruction set with parameters and an instruction set without parameters based on the code value 90; wherein, an instruction set that is explicitly declared in a source code declaration file to be unrelated to a function call is selected as a target object of the replacement operation.

9. A Python bytecode protection device based on joint obfuscation of opcodes and control flows. It is characterized in that The Python bytecode protection device based on joint obfuscation of opcode and control flow includes: processor; A memory having computer-readable instructions stored thereon, wherein when the computer-readable instructions are executed by the processor, the method according to any one of claims 1 to 6 is implemented.

10. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores program codes, which can be called by a processor to execute the method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Python code obfuscation method based on control flow and appearance obfuscation

    CN106778101A

  • Python byte code reverse prevention method

    CN110378085A

  • Program control flow hiding method and system, and application

    CN112527307A

  • Complex control flow obfuscation method based on LLVM compiling optimization stage

    CN115438318A

Cited By

  • Python byte code obfuscation method and system based on dynamic screening and random replacement

    CN120805108A

  • A python bytecode obfuscation method and system based on dynamic screening and random replacement

    CN120805108B