Decompiling methods, electronic devices and media for Android applications

By obtaining the dex file from the APK file and generating the SMALI code, combined with the class address index table structure, the problems of slow decompilation speed and low file reading efficiency in the existing technology are solved, and efficient decompilation and file storage are achieved.

CN114564208BActive Publication Date: 2024-10-11HANGZHOU YUNSHEN TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210265102.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-17
Publication Date
2024-10-11
Estimated Expiration
2042-03-17

AI Technical Summary

Technical Problem

Due to the frequent access and modification of the file system, the decompilation technology of existing Android applications results in slow decompilation speed and low efficiency, and generates a large number of decompiled files, which causes low computer reading efficiency and consumes a large amount of computing resources.

Method used

By obtaining the target dex file from the APK file to be decompiled, read the class_def table one by one, decompile according to the dex file structure and Dalvik instruction format, generate SMALI code, and create the decompiled file according to the preset data storage structure, using Class address index table improves query efficiency.

Benefits of technology

It improves the decompilation speed of Android applications, generates decompiled files that are easy for computers to read, and reduces the time and resource consumption of computers reading decompiled files.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114564208B_ABST
    Figure CN114564208B_ABST
Patent Text Reader

Abstract

The present invention relates to a decompilation method, electronic device and medium of an Android application, the method comprising step S1, obtaining at least one target dex file from an APK file to be decompiled; step S2, obtaining a class_def table from the target dex file, reading each class_def in the class_def table one by one, and obtaining class decompilation data corresponding to each class_def; step S3, creating a decompilation file structure according to a preset data storage structure, storing each type of decompilation data obtained in the decompilation file structure in sequence, and generating a decompilation file, wherein the preset data storage structure is {first type of decompilation data, preset separator, second type of decompilation data, preset separator, ..., (M-1)th type of decompilation data, preset separator, Mth decompilation data}. The present invention improves the decompilation speed of the Android application, and can generate a decompilation file that is easy for a computer to read, thereby improving the speed at which the computer reads the decompilation file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a decompilation method, electronic device and medium for an Android application. Background Art

[0002] The development process of Android applications is usually based on multiple source files to generate an Android application package (APK). The existing decompilation technology of Android applications usually generates a directory tree based on the class name, and then generates multiple class files corresponding to the source files based on the directory tree. However, due to the large number of source files of APK, some of which can reach hundreds of thousands, it is necessary to continuously access and modify the file system during the decompilation process, and the decompilation speed is slow and the efficiency is low. In addition, due to the large number of decompiled files obtained by the existing decompilation technology, it is necessary to continuously access the file system based on the file address during the computer reading the class file, and the reading efficiency is low, and the computing resources consumed are large. It can be seen from this that how to improve the decompilation speed of Android applications and generate decompiled files that are easy for computers to read have become technical problems that need to be solved urgently. Summary of the invention

[0003] The purpose of the present invention is to provide a decompilation method, electronic device and medium for an Android application, which improves the decompilation speed of the Android application and can generate a decompiled file that is easy for a computer to read, thereby improving the speed at which the computer reads the decompiled file.

[0004] According to a first aspect of the present invention, a decompilation method for an Android application is provided, comprising:

[0005] Step S1, obtaining at least one target dex file from the APK file to be decompiled;

[0006] Step S2, obtaining a class_def table from the target dex file according to a preset dex file structure, reading each class_def in the class_def table one by one, and decompiling each class_def one by one according to the dex file structure and the Dalvik instruction format document, and then generating decompiled SMALI codes based on SMALI syntax as class decompilation data corresponding to each class_def, each class of decompilation data including basic description information, member variable description information and member method description information;

[0007] Step S3, create a decompilation file structure according to a preset data storage structure, store each type of decompilation data obtained in the decompilation file structure in turn, and generate a decompilation file, wherein the preset data storage structure is {first type of decompilation data, preset separator, second type of decompilation data, preset separator, ..., (M-1)th type of decompilation data, preset separator, Mth decompilation data}, wherein M is the total number of classes corresponding to the target dex file, the mth decompilation data includes the mth type basic description data segment, the mth type member variable description data segment, the mth type member method description data segment, the mth type member method description data segment includes a method signature data segment and a method code data segment, and the value of method m is 1 to M.

[0008] According to a second aspect of the present invention, there is provided an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being configured to execute the method described in the first aspect of the present invention.

[0009] According to a third aspect of the present invention, a computer-readable storage medium is provided, wherein the computer instructions are used to execute the method described in the first aspect of the present invention.

[0010] Compared with the prior art, the present invention has obvious advantages and beneficial effects. By means of the above technical solution, the decompilation method, electronic device and medium of an Android application provided by the present invention can achieve considerable technical advancement and practicality, and has wide industrial utilization value, and has at least the following advantages:

[0011] The present invention can place data obtained by decompiling one or more dex files into the same file, thereby improving the decompilation speed of Android applications, and can generate a decompiled file that is easy for a computer to read, thereby improving the speed at which the computer reads the decompiled file.

[0012] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the following specifically cites a preferred embodiment and describes it in detail with the accompanying drawings as follows. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] Figure 1 A flow chart of a decompilation method for an Android application provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0014] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined purpose of the invention, the following is a detailed description of the decompilation method of an Android application, the specific implementation mode of the electronic device and the medium and the effects thereof proposed by the present invention in combination with the accompanying drawings and preferred embodiments.

[0015] It should be mentioned before discussing the exemplary embodiments in more detail that some exemplary embodiments are described as processes or methods depicted as flow charts. Although the flow charts describe the steps as sequential processes, many of the steps therein can be implemented in parallel, concurrently or simultaneously. In addition, the order of the steps can be rearranged. The process can be terminated when its operation is completed, but can also have additional steps not included in the accompanying drawings. The process can correspond to a method, function, procedure, subroutine, subprogram, etc.

[0016] The embodiment of the present invention provides a decompilation method for an Android application program, such as Figure 1 As shown, including:

[0017] Step S1, obtaining at least one target dex (dalvik executable) file from the APK (Android application package) file to be decompiled;

[0018] APK is an application package file format used by the Android operating system to distribute and install mobile applications and middleware. Dalvik is one of the core components of the Android system and a virtual machine used to execute code. Dex files are the code format executed by the Dalvik virtual machine. There are one or more dex files in an APK. According to the specific decompilation requirements, one or more dex files can be selected from the APK file to be decompiled as the target dex file.

[0019] Step S2, obtaining a class_def table from the target dex file according to a preset dex file structure, reading each class_def in the class_def table one by one, and decompiling each class_def one by one according to the dex file structure and the Dalvik instruction format document, and then generating decompiled SMALI codes based on SMALI syntax as class decompilation data corresponding to each class_def, each class of decompilation data including basic description information, member variable description information and member method description information;

[0020] Among them, the class_def table is a known file structure in the dex file format. SMALI is an existing dex decompiled code syntax format. One target dex file corresponds to one class_def table, and one class_def table corresponds to multiple class_defs.

[0021] Step S3, create a decompilation file structure according to a preset data storage structure, store each type of decompilation data obtained in the decompilation file structure in turn, and generate a decompilation file, wherein the preset data storage structure is {first type of decompilation data, preset separator, second type of decompilation data, preset separator, ..., (M-1)th type of decompilation data, preset separator, Mth decompilation data}, wherein M is the total number of classes corresponding to the target dex file, the mth decompilation data includes the mth type basic description data segment, the mth type member variable description data segment, the mth type member method description data segment, the mth type member method description data segment includes a method signature data segment and a method code data segment, and the value of method m is 1 to M.

[0022] It should be noted that step S2 and step S3 are not strictly executed in sequence, but are executed synchronously. When a type of decompiled data is read and generated in step S2, the decompiled data will be stored according to the decompiled file structure in step S3 until all class_defs in all class_def tables in all target dex files are traversed. Each target dex file can independently set a decompiled file structure to generate an independent decompiled file. Multiple target dex files can also generate a decompiled file based on the same decompiled file structure, depending on the specific application requirements. The preset delimiter is a character or string that does not conflict with the SMALI syntax.

[0023] The embodiment of the present invention can place the data obtained by decompiling one or more dex files into the same file. During the decompilation process, only the corresponding files need to be operated, and the operating system does not need to repeatedly modify the file system table during the file writing process, thereby improving the decompilation speed of the Android application and being able to generate a decompiled file that is easy for the computer to read, thereby improving the speed at which the computer reads the decompiled file.

[0024] As an embodiment, the step S3 further includes:

[0025] Step S4, obtaining the decompiled file;

[0026] Step S5, read the decompiled files in sequence, namely, the first type of decompiled data, the second type of decompiled data, ... the Mth type of decompiled data, to generate source code corresponding to the target dex file.

[0027] It can be understood that the data decompiled from the same dex file is placed in the same file. There is no need to constantly access the retrieval system table. It is only necessary to query the file system once, and then directly read the data decompiled from the same dex file based on one file, which improves the efficiency of machine analysis of decompilation results.

[0028] There are multiple class_def classes obtained by decompiling an APK file, and some of them can even reach hundreds of thousands. Therefore, in some application scenarios, it is necessary to obtain the decompiled data of one or several target classes in a file. If the decompiled file is traversed directly from beginning to end, the efficiency is low. In order to improve the efficiency of obtaining the decompiled data corresponding to a single class, as an embodiment, the step S3, in the process of generating the decompiled file, further includes the following steps:

[0029] Step S31, create a class address index table structure, used to store the correspondence between the mth class primary key and the mth class address information, the mth class primary key is the class name of the mth class class_def, indicating the primary key corresponding to the mth decompiled data, and the mth class address indicates the starting address of the mth decompiled data in the decompiled file;

[0030] Step S32: when m=1, generating a first type of primary key based on the first type of basic description data segment, and determining the address information of the first type of decompiled data by using the initial address of the decompiled file;

[0031] For example, the initial address is set to the initial offset of the file as 0.

[0032] Step S33, when m>1, generate the mth class primary key based on the mth class basic description data segment, obtain the (m-1)th file offset corresponding to the preset delimiter connected to the end position of the (m-1)th decompiled data, and determine the next file address information of the (m-1)th file offset as the address information of the mth decompiled data, thereby generating a class address index table.

[0033] In the step S31, creating a class address index table structure may specifically include: step S311, creating the class address index table structure at the end of the decompiled file, that is, setting the class address index table structure directly at the end of the decompiled file, and sharing a folder with the decompiled data. Or step S312, creating the class address index table structure in another preset file outside the decompiled file; in this way, the decompiled data and the class address index table are respectively stored in a separate file. Or step S313, creating the class address index table structure in a preset database. The database may specifically be a Key-Value type database. The storage method of the class address index table is specifically selected based on factors such as the index file or write performance.

[0034] As an embodiment, step S32 or step S33 may include:

[0035] Step S321, directly determining the mth type of basic description data as the mth type of primary key;

[0036] Step S322: Store the m-th type primary key and the m-th type address in the address index table structure in sequence, where m=1, 2... or M.

[0037] Step S321-step S322 directly uses the class basic description data as the mth class primary key to improve the construction speed of the address index table.

[0038] Since the class basic description data is of variable length, in order to further improve the efficiency of querying the decompiled data of the target class, as an embodiment, in step S32 or step S33, the mth class primary key is generated based on the mth class basic description data segment, where m=1, 2... or M, including:

[0039] Step S331: performing a hash operation on the m-th type of basic description data based on a preset hash function to obtain a corresponding hash code as the m-th type of primary key;

[0040] The hash operation involved is an existing hash algorithm and will not be described in detail here.

[0041] Step S332: Store the m-th type primary key and the m-th type address in the address index table structure in sequence, where m=1, 2... or M.

[0042] Through steps S331 and S332, the primary keys of all classes can be converted into fixed-length primary keys, thereby improving the efficiency of querying the decompiled data of the target class.

[0043] On the premise that the address index table is also synchronously established in step S3, as an embodiment, the step S3 further includes:

[0044] Step S04: obtaining a target class primary key corresponding to the target class basic description data, traversing the address index table based on the target class primary key, and determining a target class address corresponding to the target class primary key;

[0045] Step S05: determine the target class address and the data between the first preset separator after the target class address as target class decompilation data.

[0046] As an embodiment, the step S32 or step S33 may further include:

[0047] Step S341: performing a hash operation on the m-th type of basic description data based on a preset hash function to obtain a corresponding hash code as the m-th type primary key;

[0048] Step S342, traverse the primary keys in the current class address index table structure, and insert the mth class primary key and the mth class address into the corresponding positions in the current address index table structure according to the preset sorting method, so that all primary keys in the current address index table structure are arranged according to the preset sorting method.

[0049] The preset sorting method may be from small to large, and it is understandable that the preset sorting method may also be from large to small.

[0050] Taking the preset sorting method as an example, the step S3 further includes:

[0051] Step S40: Obtain basic description data of the target class, perform a hash operation on the basic description data of the target class based on a preset hash function, and obtain a target hash code as a primary key of the target class;

[0052] Step S50: using the class address index table as the current target index area;

[0053] Step S60, taking the center position class primary key of the current target index area as the current center class primary key, comparing the target class primary key with the current center class primary key, if they are the same, determining the class address corresponding to the current center class primary key as the target class address, and executing step S80, otherwise, executing step S70;

[0054] Step S70: If the target class primary key is greater than the current center class primary key, the area from the current center class primary key to the end of the current target index area is updated as the current target index area, and the process returns to step S60; if the target class primary key is less than the current center class primary key, the area from the start of the current target index area to the current center class primary key is updated as the current target index area, and the process returns to step S60;

[0055] Step S80: determine the target class address and the data between the first preset separator after the target class address as target class decompiled data.

[0056] Based on steps S40 to S80, the efficiency of obtaining the decompiled data of the target class is further improved.

[0057] An embodiment of the present invention also provides an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are configured to execute the method described in the embodiment of the present invention.

[0058] The embodiment of the present invention further provides a computer-readable storage medium, wherein the computer instructions are used to execute the method described in the embodiment of the present invention.

[0059] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Although the present invention has been disclosed as a preferred embodiment as above, it is not used to limit the present invention. Any technician familiar with this profession can make some changes or modifications to equivalent embodiments of equivalent changes using the technical contents disclosed above without departing from the scope of the technical solution of the present invention. However, any simple modification, equivalent change and modification made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solution of the present invention still falls within the scope of the technical solution of the present invention.

Claims

1. A decompilation method for an Android application, characterized in that: include: Step S1, obtaining at least one target dex file from the APK file to be decompiled; Step S2, obtaining a class_def table from the target dex file according to a preset dex file structure, reading each class_def in the class_def table one by one, and decompiling each class_def one by one according to the dex file structure and the Dalvik instruction format document, and then generating decompiled SMALI codes based on SMALI syntax as class decompilation data corresponding to each class_def, each class of decompilation data including basic description information, member variable description information and member method description information; Step S3, create a decompilation file structure according to a preset data storage structure, store each type of decompilation data obtained in the decompilation file structure in turn, and generate a decompilation file, wherein the preset data storage structure is {first type of decompilation data, preset separator, second type of decompilation data, preset separator, ..., (M-1)th type of decompilation data, preset separator, Mth decompilation data}, wherein M is the total number of classes corresponding to the target dex file, the mth decompilation data includes the mth type basic description data segment, the mth type member variable description data segment, and the mth type member method description data segment, the mth type member method description data segment includes a method signature data segment and a method code data segment, and the value of method m is 1 to M.

2. The method according to claim 1, characterized in that: In the process of generating the decompiled file, step S3 further includes the following steps: Step S31, create a class address index table structure, used to store the correspondence between the mth class primary key and the mth class address information, the mth class primary key is the class name of the mth class class_def, indicating the primary key corresponding to the mth decompiled data, and the mth class address indicates the starting address of the mth decompiled data in the decompiled file; Step S32: when m=1, generating a first type of primary key based on the first type of basic description data segment, and determining the address information of the first type of decompiled data by using the initial address of the decompiled file; Step S33, when m>1, generate the mth class primary key based on the mth class basic description data segment, obtain the (m-1)th file offset corresponding to the preset delimiter connected to the end position of the (m-1)th decompiled data, and determine the next file address information of the (m-1)th file offset as the address information of the mth decompiled data, thereby generating a class address index table.

3. The method according to claim 2, characterized in that In step S31, creating a class address index table structure includes: Step S311, creating the class address index table structure at the end of the decompiled file; or, Step S312, creating the class address index table structure in another preset file other than the decompiled file; or, Step S313: Create the class address index table structure in a preset database.

4. The method according to claim 2, characterized in that: The step S32 or step S33 comprises: Step S321, directly determining the mth type of basic description data as the mth type of primary key; Step S322: Store the m-th primary key and the m-th address in the address index table structure in sequence, where m=1, 2... or M.

5. The method according to claim 2, characterized in that: In step S32 or step S33, generating the mth type primary key based on the mth type basic description data segment, where m=1, 2... or M, includes: Step S331: performing a hash operation on the m-th type of basic description data based on a preset hash function to obtain a corresponding hash code as the m-th type of primary key; Step S332: Store the m-th primary key and the m-th address in the address index table structure in sequence, where m=1, 2... or M.

6. The method according to claim 4 or 5, characterized in that: After step S3, the following steps are also included: Step S04: obtaining a target class primary key corresponding to the target class basic description data, traversing the address index table based on the target class primary key, and determining a target class address corresponding to the target class primary key; Step S05: determine the target class address and the data between the first preset separator after the target class address as target class decompilation data.

7. The method according to claim 2, characterized in that The step S32 or step S33 comprises: Step S341: performing a hash operation on the m-th type of basic description data based on a preset hash function to obtain a corresponding hash code as the m-th type of primary key; Step S342, traverse the primary keys in the current class address index table structure, and insert the mth class primary key and the mth class address into the corresponding positions in the current address index table structure according to the preset sorting method, so that all primary keys in the current address index table structure are arranged according to the preset sorting method.

8. The method according to claim 7, characterized in that The preset sorting method is sorting from small to large, and after step S3, the following further includes: Step S40: Obtain basic description data of the target class, perform a hash operation on the basic description data of the target class based on a preset hash function, and obtain a target hash code as a primary key of the target class; Step S50, using the class address index table as the current target index area; Step S60, taking the center position class primary key of the current target index area as the current center class primary key, comparing the target class primary key with the current center class primary key, if they are the same, determining the class address corresponding to the current center class primary key as the target class address, and executing step S80, otherwise, executing step S70; Step S70: If the target class primary key is greater than the current center class primary key, the area from the current center class primary key to the end of the current target index area is updated as the current target index area, and the process returns to step S60; if the target class primary key is less than the current center class primary key, the area from the start of the current target index area to the current center class primary key is updated as the current target index area, and the process returns to step S60; Step S80: determine the target class address and the data between the first preset separator after the target class address as target class decompiled data.

9. An electronic device, characterized in that: include: at least one processor; and, a memory communicatively coupled to the at least one processor; The memory stores instructions executable by the at least one processor, wherein the instructions are configured to execute the method according to any one of claims 1 to 8.

10. A computer-readable storage medium, characterized in that: Computer executable instructions are stored, and the computer executable instructions are used to execute the method of any one of the preceding claims 1-8.