Android: A method to combat decompilation tools
First, let's take a look at the format of the dex file:
Class_defs structure:
(1) class_idx describes the specific class type. The value is an index of type_ids. The value must be of the class type, not the array type or basic type.
(2) access_flags describes the class access types, such as public, final, and static. In the dex-format.html, "access_flags Definitions" has a specific description.
(3) superclass_idx, which describes the supperclass type. The value format is the same as class_idx.
(4) interfaces_off. The value is an offset address and points to the interfaces of the class. The data structure to be pointed to is type_list. If the class does not have interfaces, the value is 0.
(5) source_file_idx indicates the information of the source code file. The value is an index of string_ids. If this item is missing, the value is assigned NO_INDEX = 0 xffff ffff.
(6) annotions_off. The value is an offset address. It points to the comment of the class in the data area in the format of annotations_direcotry_item. If no content exists, the value is 0.
(7) class_data_off. The value is an offset address. It points to the data used by the class in the data area in the format of class_data_item. If no content exists, the value is 0. This structure contains a lot of information, such as the field, method, and Execution Code in the method of the class, which is described in a large part later in class_data_item.
(8) static_value_off is an offset address that points to a list in the data area in the format of encoded_array_item. If no content exists, the value is 0.
Focus on (5), which indicates the name of the source file. Therefore, a feasible solution is to add a class that is not available in the root of the project to dex and modify source_file_idx. Because source_file_idx is not used, no error will be reported during running, however, when the compilation tool performs static parsing, it will not parse and report an error!
Next, let's take a look at the specific components of the header:
(1) magic value
These eight bytes are constants. To enable the. dex file to be recognized, it must appear at the beginning of the. dex file.
(2) checksum and signature
4-byte file verification code. It uses the alder32 algorithm to verify all file regions except maigc and checksum, and is used to check file errors.
Use the hash algorithm of SHA-1 to remove all the remaining file regions except magic, checksum, and signature. This is used to uniquely identify this file.
As shown in the preceding structure, if source_file_idx is modified, both signature and checksum must be modified.
The procedure is as follows:
(1) After the project source code is packaged into a jar package (obfuscation), add a useless class x.
(2) convert the new jar package to dex
(3) Modify source_file_idx of class x. x in dex.
(4) recalculate signature.
(5) recalculate the checksum.
(6) Update dex.
(7) install the package signature normally.