A code processing method, terminal and storage medium based on instruction injection

By splitting and encrypting the code instructions in the DEX file during the encryption phase of the Android APP, and injecting decryption instructions at runtime, the problem of the original DEX file in the encrypted APP memory being easily accessible in traditional technology is solved, and the secure storage of the encrypted code is achieved. and execution.

CN112052461BActive Publication Date: 2024-09-27BEIJING ZHI YOU WANG AN TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010776482.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-08-05
Publication Date
2024-09-27
Estimated Expiration
2040-08-05

AI Technical Summary

Technical Problem

With traditional Android code processing technology based on command injection, after the encrypted APP is run, there is a continuous memory space in the memory to store the original DEX file. It is easy to obtain the original DEX file before encryption through memory DUMP, making the encryption unsafe.

Method used

In the encryption phase of the APP, the DEX file is parsed, the code instructions are split and encrypted and saved, and the binary instructions of the decrypted code are constructed and injected into the original instruction location. When the encrypted APP is running, the decryption instruction is executed to decrypt and restore the method instructions, and jump Go to the decrypted instruction memory to continue execution, ensuring that the decrypted instructions are stored randomly in the memory.

Benefits of technology

It ensures that the encrypted code is always stored dispersedly in the memory, avoids obtaining the original DEX file through memory DUMP, and improves the security of the encrypted code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112052461B
    Figure CN112052461B
Patent Text Reader

Abstract

The present invention discloses a code processing method, terminal and storage medium based on instruction injection, the method comprising: in the encryption stage of APP, parsing DEX file, splitting the DEX file, extracting the code instructions saved in the DEX file, and encrypting and saving; constructing binary instructions of decryption code, and injecting them into the original instruction position in the DEX file; when the encrypted APP runs to the encrypted method, executing the injected binary instructions, decrypting and restoring the method instructions, and jumping to the decrypted instruction memory, and continuing to execute the decrypted instructions. The present invention splits the complete DEX file during encryption, separates the code segment of the encryption method, and injects the decryption instruction into the original code segment, calls the encrypted method at runtime, first executes the injected decryption instruction, completes the decryption of the current execution method, and ensures that the encrypted method will be decrypted only when it is executed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code processing, and in particular to a code processing method, a terminal and a storage medium based on instruction injection. Background Art

[0002] The DEX file is a program run by the Android virtual machine. When developers release an Android APP, they first compile the Java code into class bytecode, and then convert the class bytecode into the DEX file format through the Dx tool.

[0003] Therefore, the protection of DEX files is crucial. The traditional Android code processing technology based on instruction injection mainly replaces the original DEX file with a shell DEX file to encrypt and hide the original DEX. When the encrypted APP is running, the shell DEX calls the system method to load the original DEX.

[0004] However, this traditional Android code processing technology based on instruction injection will leave a continuous memory in the memory to store the original DEX after the encrypted APP is running. Therefore, the original DEX file before encryption can be easily obtained through memory DUMP (backup file system, dump, that is, transferring the content of the memory at a certain moment into a file).

[0005] Therefore, the prior art still needs to be improved and developed. Summary of the invention

[0006] The main purpose of the present invention is to provide a code processing method, terminal and storage medium based on instruction injection, aiming to solve the problem in the prior art that it is easy to obtain the original DEX file before encryption after the code is encrypted.

[0007] To achieve the above object, the present invention provides a code processing method based on instruction injection, and the code processing method based on instruction injection comprises the following steps:

[0008] During the encryption phase of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved;

[0009] Construct binary instructions for the decryption code and inject them into the original instruction location in the DEX file;

[0010] When the encrypted APP runs to the encrypted method, it executes the injected binary instructions, decrypts and restores the method instructions, jumps to the decrypted instruction memory, and continues to execute the decrypted instructions.

[0011] Optionally, in the code processing method based on instruction injection, splitting the DEX file specifically includes: cutting binary instructions from the DEX file and saving them into a preset file.

[0012] Optionally, the code processing method based on instruction injection, wherein, in the encryption stage of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the code instructions are encrypted and saved, specifically including:

[0013] When encrypting the APP, the binary file of the DEX file is parsed by an encryption tool;

[0014] Traverse and obtain all methods in the DEX file, and parse the binary code project structure of the code segment corresponding to the method;

[0015] Extracting binary instruction data recorded in the code project structure;

[0016] Generate a unique identification ID based on the class name, method name and method signature extracted from the binary instruction data;

[0017] Associating the identification ID with the binary instruction data and encrypting and saving them;

[0018] Insert the constructed decryption engine call instruction into the designated field position in the code project structure.

[0019] Optionally, in the code processing method based on instruction injection, each method corresponds to a code project structure.

[0020] Optionally, the code processing method based on instruction injection, wherein when the encrypted APP runs to the encrypted method, the injected binary instructions are executed, the method instructions are decrypted and restored, and the decrypted instruction memory is jumped to continue to execute the decrypted instructions, including:

[0021] When the encrypted APP is running, the encrypted method is executed, and the injected decryption engine call instruction is executed;

[0022] The decryption engine calculates the ID by finding the class name, method name and method signature of the currently executing method through the method call stack;

[0023] Find the encrypted instruction location stored in the specified file by ID;

[0024] The decryption engine decrypts the corresponding instructions and restores the instructions to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instructions.

[0025] Optionally, the code processing method based on instruction injection, wherein the decryption engine decrypts the corresponding instruction and restores the instruction to the code project structure of the current method, the memory space pointed to by the field, completes the decryption and restoration of the instruction, and then further includes:

[0026] After the decrypted instruction is restored, the preset statement is executed, jumping to the decrypted and restored memory, and continuing to execute the logic of the original method.

[0027] In addition, to achieve the above-mentioned purpose, the present invention also provides a terminal, wherein the terminal includes: a memory, a processor, and an instruction injection-based code processing program stored in the memory and executable on the processor, and when the instruction injection-based code processing program is executed by the processor, the steps of the instruction injection-based code processing method as described above are implemented.

[0028] In addition, to achieve the above-mentioned purpose, the present invention also provides a storage medium, wherein the storage medium stores a code processing program based on instruction injection, and when the code processing program based on instruction injection is executed by a processor, the steps of the code processing method based on instruction injection as described above are implemented.

[0029] In the encryption stage of the APP, the present invention parses the DEX file, splits the DEX file, extracts the code instructions saved in the DEX file, and encrypts and saves them; constructs a binary instruction for the decryption code, and injects it into the original instruction position in the DEX file; when the encrypted APP runs to the encrypted method, executes the injected binary instruction, decrypts and restores the method instruction, jumps to the decrypted instruction memory, and continues to execute the decrypted instruction. During encryption, the present invention splits the complete DEX file, separates the code segment of the encryption method, and injects the decryption instruction into the original code segment. During operation, the encrypted method is called, the injected decryption instruction is first executed, and the decryption of the currently executed method is completed, ensuring that the encrypted method will only be decrypted when it is executed, and the decrypted instructions are randomly and dispersedly stored in the memory, ensuring the security of the encrypted code in the memory. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 It is a flow chart of a preferred embodiment of the code processing method based on instruction injection of the present invention;

[0031] Figure 2 It is a schematic diagram of a code encryption process in a preferred embodiment of a code processing method based on instruction injection of the present invention;

[0032] Figure 3 It is a schematic diagram of a code decryption process in a preferred embodiment of a code processing method based on instruction injection of the present invention;

[0033] Figure 4 Schematic diagram of the operating environment of a preferred embodiment of the terminal of the present invention. DETAILED DESCRIPTION

[0034] In order to make the purpose, technical solution and advantages of the present invention clearer and more specific, the present invention is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0035] The code processing method based on instruction injection described in the preferred embodiment of the present invention comprises the following steps:

[0036] like Figure 1 and Figure 2 As shown, in step S10, during the encryption stage of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved.

[0037] Specifically, splitting means cutting out the binary instructions of the DEX file from the DEX file, and saving them into the file ijiami3.ajm (i.e. the preset file), and parsing the Dex file according to the binary file structure of the Dex file; the process is as follows.

[0038] (1) Traverse the classdef table and find all classes;

[0039] (2) Parse the class_data_item information of the class and find the encode_method information of all methods of the class;

[0040] (3) Parse the encode_method information and find the code_item structure of the method;

[0041] (4) Find the storage location of insns binary instructions through code_item;

[0042] (5) Extract the binary instructions of the insns method, generate a unique id based on the class name, method name, and method signature, and save the extracted data and id relationship record data into ijiami3.ajm;

[0043] (6) Clear the data in the original insns so that no user code is seen after decompilation.

[0044] Furthermore, when encrypting the APP, the binary file of the DEX file is parsed by an encryption tool; all methods in the DEX file are traversed and obtained, and the binary code project structure of the code segment corresponding to the method is parsed; the binary instruction data recorded in the code project structure is extracted; a unique identification ID is generated according to the class name, method name and method signature in the extracted binary instruction data; the identification ID and the binary instruction data are associated and encrypted and saved; and the constructed decryption engine call instruction is inserted into the specified field position in the code project structure.

[0045] First, parse the DEX file and find the method code structure code_item. The insns field of code_item is where the method instructions that need to be split out and encrypted are stored. Then, generate a unique ID based on the current method, class name, method name, and method signature. This unique ID, together with the split instruction data, is encrypted and stored in ijiami3.ajm. Then, clear the current method and insns instruction data and inject the decryption engine calling code. Run this encrypted method and calculate the ID based on the method class name, method name, and method signature. Then, search for the encrypted instruction data in ijiami3.ajm based on the ID. Release the instruction to the memory pointed to by the insns of the current method code_item, thus completing the code restoration operation. Finally, use the goto statement to jump to the first line of the method instruction and re-execute the restored instruction.

[0046] like Figure 1 and Figure 3 As shown, step S20, construct the binary instructions of the decryption code and inject them into the original instruction position in the DEX file.

[0047] Through encryption tools, DEX is parsed according to the binary file structure of DEX, all methods in DEX are traversed, and the binary code_item structure of the code segment corresponding to the method is parsed. The insns field of the code_item structure stores the binary instruction data of the current method, and the binary instruction data recorded in the insns is extracted (the extracted instructions are stored in association with the ID generated according to the method class name, method name, and method signature. During runtime, the ID is recalculated, and then the instruction data split out and stored in ijiami3.ajm is searched in ijiami3.ajm according to the ID. The code_item of the current method is retrieved during runtime. The insns pointer of the code_item structure is the location where our code needs to be restored, so there is no need to record the location additionally. As long as we know which method the extracted instruction belongs to, we can find the location where the instruction should be stored according to the insns field of the method code_item structure).

[0048] A unique identification ID is generated based on the current method class name, method name, and method signature. The ID is associated with the extracted insns instruction data and stored encrypted in ijiami3.ajm. The constructed decryption engine call instruction is inserted into the insns field of the code_item structure.

[0049] It is simply to insert a line of call, that is, the code of the decryption engine. When executing this method, this line of code will be executed first to execute the function of the decryption engine. The decryption engine may obtain the method class name, method name, and method signature of the current method by running the stack, generate an ID, and use the ID to find the encrypted instructions in ijiami3.ajm, and then decrypt the instructions, restore the decrypted instructions to the current method, the insns of the code_item structure, the position, execute the goto statement, adjust to the first line of instructions pointed to by insns, and re-instruct the decrypted and restored instruction code.

[0050] like Figure 1 and Figure 3 As shown, in step S30, when the encrypted APP runs to the encrypted method, the injected binary instructions are executed, the method instructions are decrypted and restored, and the decrypted instruction memory is jumped to continue to execute the decrypted instructions.

[0051] Specifically, when the encrypted APP is running, the encrypted method is executed, and the injected decryption engine call instruction is executed; the decryption engine finds the class name, method name and method signature of the currently executing method through the method call stack to calculate the ID; the encrypted instruction location stored in the specified file is found through the ID; the decryption engine decrypts the corresponding instruction, and restores the instruction to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instruction.

[0052] Furthermore, after step S30, the method further includes: the decrypted instruction restoration is completed, a preset statement is executed, the decrypted and restored memory is jumped to, and the logic of the original method is continued to be executed.

[0053] That is to say, when the encrypted APP is running, the encrypted method is executed, and the injected instruction to call the decryption engine is executed first; the decryption engine finds the class name, method name, and method signature of the currently executing method through the method call stack to calculate the ID; then the encrypted instruction location stored in ijiami3.ajm is found through the ID; the decryption engine decrypts the corresponding instruction and restores the instruction to the memory space pointed to by the insns field of the current method code_item structure to complete the decryption and restoration of the instruction; after the decryption instruction restoration is completed, the goto statement is executed to jump to the decrypted and restored memory to continue executing the logic of the original method.

[0054] Since the method instructions are cleared during encryption and a decryption engine call instruction is inserted, the decryption engine will be called when the method is running. After decryption, the decryption engine restores the instructions to the DEX loaded into the memory by the system. The current method corresponds to the memory pointed to by the insns field of the code_item structure. In order for the system to execute the restored instructions next, a goto instruction is needed to modify the current PC (program counter, used to indicate the memory address of the next machine instruction to be executed) to point to the decrypted code just now, and continue to execute the original method instructions restored by decryption.

[0055] The present invention proposes an encryption method for Android Java code based on instruction injection. In the encryption stage of APP, a DEX file is parsed, the DEX file is split, the code instructions saved in the DEX are extracted, and the code instructions are encrypted and saved; a binary instruction of the decryption code is separately constructed and injected into the original instruction position in the DEX; when the encrypted APP is running, when the encrypted method is run, the injected decryption instruction is first executed to decrypt and restore the method instruction, and then jump to the decrypted instruction memory to continue to execute the decrypted instruction, so that it can be ensured that the APP will decrypt the corresponding method only when running to a certain method, and the original DEX is always scattered in the memory, so that the original DEX dumped in the memory is still in an encrypted state of lacking code segments.

[0056] That is to say, the present invention splits the complete DEX file during encryption, separates the code segment of the encryption method, and injects the decryption instruction into the original code segment; during operation, the encrypted method is called, and the injected decryption instruction is first executed to complete the decryption of the currently executed method, ensuring that the encrypted method will only be decrypted when it is executed, and the decrypted instructions are randomly and dispersedly stored in the memory, ensuring the security of the encrypted code in the memory. That is, the method code in the DEX file is encrypted separately, and the corresponding decryption operation is performed only when the encrypted method is executed. The encrypted DEX file is always stored in a dispersed manner in the memory, avoiding the acquisition of the original DEX by memory DUMP.

[0057] Furthermore, if Figure 4 As shown, based on the above-mentioned code processing method based on instruction injection, the present invention also provides a terminal accordingly, and the terminal includes a processor 10, a memory 20 and a display 30. Figure 4 Only some components of the terminal are shown, but it should be understood that it is not required to implement all of the components shown, and more or fewer components may be implemented instead.

[0058] The memory 20 may be an internal storage unit of the terminal in some embodiments, such as a hard disk or memory of the terminal. The memory 20 may also be an external storage device of the terminal in other embodiments, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the terminal. Further, the memory 20 may also include both an internal storage unit of the terminal and an external storage device. The memory 20 is used to store application software and various types of data installed in the terminal, such as the program code of the installation terminal. The memory 20 may also be used to temporarily store data that has been output or is to be output. In one embodiment, a code processing program 40 based on instruction injection is stored on the memory 20, and the code processing program 40 based on instruction injection can be executed by the processor 10, thereby realizing the code processing method based on instruction injection in the present application.

[0059] In some embodiments, the processor 10 may be a central processing unit (CPU), a microprocessor or other data processing chip, used to run the program code stored in the memory 20 or process data, such as executing the code processing method based on instruction injection.

[0060] In some embodiments, the display 30 may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED (Organic Light-Emitting Diode) touch device, etc. The display 30 is used to display information on the terminal and to display a visual user interface. The components 10-30 of the terminal communicate with each other via a system bus.

[0061] In one embodiment, when the processor 10 executes the code processing program 40 based on instruction injection in the memory 20, the following steps are implemented:

[0062] During the encryption phase of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved;

[0063] Construct binary instructions for the decryption code and inject them into the original instruction location in the DEX file;

[0064] When the encrypted APP runs to the encrypted method, it executes the injected binary instructions, decrypts and restores the method instructions, jumps to the decrypted instruction memory, and continues to execute the decrypted instructions.

[0065] The splitting of the DEX file specifically includes: cutting binary instructions from the DEX file and saving them into a preset file.

[0066] In the encryption stage of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved, which specifically includes:

[0067] When encrypting the APP, the binary file of the DEX file is parsed by an encryption tool;

[0068] Traverse and obtain all methods in the DEX file, and parse the binary code project structure of the code segment corresponding to the method;

[0069] Extracting binary instruction data recorded in the code project structure;

[0070] Generate a unique identification ID based on the class name, method name and method signature extracted from the binary instruction data;

[0071] Associating the identification ID with the binary instruction data and encrypting and saving them;

[0072] Insert the constructed decryption engine call instruction into the designated field position in the code project structure.

[0073] Among them, each method corresponds to a code project structure.

[0074] When the encrypted APP runs to the encrypted method, the injected binary instructions are executed, the method instructions are decrypted and restored, and the decrypted instruction memory is jumped to continue to execute the decrypted instructions, including:

[0075] When the encrypted APP is running, the encrypted method is executed, and the injected decryption engine call instruction is executed;

[0076] The decryption engine calculates the ID by finding the class name, method name and method signature of the currently executing method through the method call stack;

[0077] Find the encrypted instruction location stored in the specified file by ID;

[0078] The decryption engine decrypts the corresponding instructions and restores the instructions to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instructions.

[0079] The decryption engine decrypts the corresponding instruction and restores the instruction to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instruction, and then further includes:

[0080] After the decrypted instruction is restored, the preset statement is executed, jumping to the decrypted and restored memory, and continuing to execute the logic of the original method.

[0081] The present invention also provides a storage medium, wherein the storage medium stores a code processing program based on instruction injection, and when the code processing program based on instruction injection is executed by a processor, the steps of the code processing method based on instruction injection as described above are implemented.

[0082] In summary, the present invention provides a code processing method, terminal and storage medium based on instruction injection, the method comprising: in the encryption stage of APP, parsing DEX file, splitting the DEX file, extracting the code instructions saved in the DEX file, and encrypting and saving; constructing binary instructions of decryption code, and injecting them into the original instruction position in the DEX file; when the encrypted APP runs to the encrypted method, executing the injected binary instructions, decrypting and restoring the method instructions, and jumping to the decrypted instruction memory, and continuing to execute the decrypted instructions. The present invention splits the complete DEX file during encryption, separates the code segment of the encryption method, and injects the decryption instruction into the original code segment. During operation, the encrypted method is called, the injected decryption instruction is first executed, and the decryption of the currently executed method is completed, ensuring that the encrypted method will only be decrypted when it is executed, and the decrypted instructions are randomly and dispersedly stored in the memory, ensuring the security of the encrypted code in the memory.

[0083] Of course, those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing related hardware (such as a processor, a controller, etc.) through a computer program, and the program can be stored in a computer-readable storage medium, and the program can include the processes of the above-mentioned method embodiments when executed. The storage medium can be a memory, a disk, an optical disk, etc.

[0084] It should be understood that the application of the present invention is not limited to the above examples. For ordinary technicians in this field, improvements or changes can be made based on the above description. All these improvements and changes should fall within the scope of protection of the claims attached to the present invention.

Claims

1. A code processing method based on instruction injection, characterized in that: The code processing method based on instruction injection includes: During the encryption phase of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved; In the encryption stage of the APP, the DEX file is parsed, the DEX file is split, the code instructions saved in the DEX file are extracted, and the encrypted instructions are saved, which specifically includes: When encrypting the APP, the binary file of the DEX file is parsed by an encryption tool; Traverse and obtain all methods in the DEX file, and parse the binary code project structure of the code segment corresponding to the method; Extracting binary instruction data recorded in the code project structure; Generate a unique identification ID based on the class name, method name and method signature extracted from the binary instruction data; Associating the identification ID with the binary instruction data and encrypting and saving them; Inserting the constructed decryption engine call instruction into a designated field position in the code project structure; The splitting of the DEX file specifically includes: cutting the binary instructions from the DEX file and saving them into a preset file; Construct binary instructions for the decryption code and inject them into the original instruction location in the DEX file; When the encrypted APP runs to the encrypted method, it executes the injected binary instructions, decrypts and restores the method instructions, jumps to the decrypted instruction memory, and continues to execute the decrypted instructions; When the encrypted APP runs to the encrypted method, the injected binary instructions are executed, the method instructions are decrypted and restored, and the decrypted instruction memory is jumped to continue to execute the decrypted instructions, including: When the encrypted APP is running, the encrypted method is executed, and the injected decryption engine call instruction is executed; The decryption engine calculates the ID by finding the class name, method name and method signature of the currently executing method through the method call stack; Find the encrypted instruction location stored in the specified file by ID; The decryption engine decrypts the corresponding instruction and restores the instruction to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instruction; Implement separate encryption of method codes in DEX files. The corresponding decryption operation will be performed only when the encrypted method is executed. The encrypted DEX files are always stored in a dispersed manner in the memory, avoiding the acquisition of the original DEX through memory dump. The decryption engine decrypts the corresponding instruction and restores the instruction to the code project structure of the current method and the memory space pointed to by the field, completing the decryption and restoration of the instruction, and then further includes: After the decrypted instruction is restored, the preset statement is executed, the current program counter is modified, the memory address of the first execution is obtained, and the decrypted and restored memory is jumped to continue executing the logic of the original method according to the memory address.

2. The code processing method based on instruction injection according to claim 1, characterized in that: Each method corresponds to a code project structure.

3. A terminal, characterized in that: The terminal includes: a memory, a processor, and an instruction injection-based code processing program stored in the memory and executable on the processor. When the instruction injection-based code processing program is executed by the processor, the steps of the instruction injection-based code processing method as described in any one of claims 1 to 2 are implemented.

4. A storage medium, characterized in that: The storage medium stores a code processing program based on instruction injection, and when the code processing program based on instruction injection is executed by a processor, the steps of the code processing method based on instruction injection as described in any one of claims 1-2 are implemented.