Android APK repacking

  • Author:ZERO-A-ONE
  • Date:2022-07-16

Recently, some projects need to use the repacking of APK. Let's record some steps in detail for future use.

0x1 Unpack

For repackaging, we definitely need to unpack the APK first, the tool I use here is apktool. The installation of this tool is very simple, just use the package management tool that comes with Ubuntu

$ sudo apt install apktool

Let's take an APK as an example and unpack it

$ apktool d -f -o ./result/ ./com.gaurav.avnc_12.apk
I: Using Apktool 2.6.1 on com.gaurav.avnc_12.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /home/syc/.local/share/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...
I: Copying META-INF/services directory

We can obtain the following directory structure

total 48
drwxr-xr-x  10 syc syc 4096 Jul 16 17:10 .
drwxr-xr-x   3 syc syc 4096 Jul 16 17:10 ..
-rw-r--r--   1 syc syc 2728 Jul 16 17:10 AndroidManifest.xml
drwxr-xr-x   3 syc syc 4096 Jul 16 17:10 META-INF
-rw-r--r--   1 syc syc  949 Jul 16 17:10 apktool.yml
drwxr-xr-x   3 syc syc 4096 Jul 16 17:10 assets
drwxr-xr-x   8 syc syc 4096 Jul 16 17:10 kotlin
drwxr-xr-x   6 syc syc 4096 Jul 16 17:10 lib
drwxr-xr-x   3 syc syc 4096 Jul 16 17:10 original
drwxr-xr-x 163 syc syc 4096 Jul 16 17:10 res
drwxr-xr-x   8 syc syc 4096 Jul 16 17:10 smali
drwxr-xr-x   3 syc syc 4096 Jul 16 17:10 unknown

Among them, resall the decompiled resources are stored under the folder, and smaliall the decompiled codes are stored under the folder, which are the files AndroidManifest.xmlafter decompilation and restorationmanifest

What is worth mentioning here is smalithe folder. If you enter this folder, you will find that its directory structure srcis almost the same as that in our source code. The main difference is that all java files have become smali files. . The smali file is actually the real source code, but its syntax is completely different from java. It is somewhat similar to the syntax of assembly, which is the register language used by the Android virtual machine.

0x2 repacking

Let's repackage the decompiled Decompile folder into APK now. It's actually very simple, just execute the command

$ apktool b ./result
I: Using Apktool 2.6.1
I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building apk file...
I: Copying unknown files/dir...
I: Built apk...

Where b is the meaning of build, which means that we want to resultpackage the folder into an APK file

After the compilation is successful, resulttwo new folders will be generated under the folder, buildand dist, which diststore the newly generated APK file

total 5756
drwxr-xr-x  2 syc syc    4096 Jul 16 17:15 .
drwxr-xr-x 12 syc syc    4096 Jul 16 17:15 ..
-rw-r--r--  1 syc syc 5882018 Jul 16 17:15 com.gaurav.avnc_12.apk

0x3 signature

In fact, the repackaged APK is not signed and needs to be signed before it can be installed on the Android terminal.

Each published application has its own unique legal ID, which is the signature of the application itself. The signature can ensure the consistency of your software upgrades. Applications with the same signature can be overwritten and installed, and inconsistent signatures cannot be shared. Data, that is, the installation cannot be overwritten, which can prevent others from tampering and stealing the developer's application, causing damage to the developer's interests

For the APK signature, we need to divide it into two steps:

  • create a key
  • Sign the APK with a key

3.1 create key

To create a key, you need to use the keytool tool, which we can install directly using the package management tool. Using the generated key to sign the apk uses jarsigner, the same as the installation method above

$ sudo apt install keytool jarsigner

An example command to generate a key is as follows:

keytool -genkey -alias watson.keystore -keyalg RSA -validity 40000 -keystore watson.keystore

illustrate:

  • genkey: generate a key
  • alias watson.keystore: key alias watson.keystore
  • keyalg RSA: Use the RSA algorithm to encrypt the signature
  • validity 40000: the certificate is valid for 4000 days
  • keystore watson.keystore: Generate the storage path of the key, you can generate it to the specified path (if you do not specify an absolute path, the key file will be generated in the current directory)

Here alias and keystore have the same name, but they can be completely different

total 5760
drwxr-xr-x  2 syc syc    4096 Jul 16 17:30 .
drwxr-xr-x 12 syc syc    4096 Jul 16 17:15 ..
-rw-r--r--  1 syc syc 5882018 Jul 16 17:15 com.gaurav.avnc_12.apk
-rw-r--r--  1 syc syc    2253 Jul 16 17:30 watson.keystore

3.2 Sign with key

The signature command format is as follows

jarsigner -verbose -keystore watson.keystore -signedjar signed.apk com.gaurav.avnc_12.apk watson.keystore

illustrate:

  • verbose: output signature details

  • keystore watson.keystore: Use the absolute path of the key, which is the key generated in the first step

    signedjar Decompile_signed.apk Decompile.apk watson.keystore: Official signature, the three parameters are the APK file signed.apk generated after signing, the APK to be signed com.gaurav.avnc_12.apk and the keystore (that is, our above alias) watson.keystore

...
>>> Signer
    X.509, CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown
    [trusted certificate]

jar signed.

Warning:
The signer's certificate is self-signed.

0x4 zipalign (compression alignment) optimization

The signed APK file can now be installed on the mobile phone, but before that, Android strongly recommends that we perform an alignment operation on the signed APK file, because this can make our program run faster in the Android system. The alignment operation uses the zipalign tool

We can install it directly using the package manager

$ sudo apt install zipalign 

The command format is as follows:

zipalign -v 4 signed.apk aligned.apk

In this way, zipalign can align the uncompressed data in the apk file on a 4-byte boundary (generally 4 bytes is a good performance value), so that the android system can use the function to read the file, which can be mmap()read Get higher performance on resources

Aligning on a 4-byte boundary means that the compiler reads 4 bytes as a unit of data resources. Therefore, the CPU can access variables more efficiently and quickly than before without alignment.

The root of alignment: the Davlik virtual machine in the android system uses its own proprietary format DEX. The structure of DEX is compact. In order to make the runtime performance better, it can be further optimized with "alignment", but the size will generally vary. Increase

After the alignment operation is completed, an aligned APK file will be generated in the same directory

total 17384
drwxr-xr-x  2 syc syc    4096 Jul 16 18:04 .
drwxr-xr-x 12 syc syc    4096 Jul 16 17:15 ..
-rw-r--r--  1 syc syc 5950698 Jul 16 18:04 aligned.apk
-rw-r--r--  1 syc syc 5882018 Jul 16 17:15 com.gaurav.avnc_12.apk
-rw-r--r--  1 syc syc 5950401 Jul 16 18:01 signed.apk
-rw-r--r--  1 syc syc    2253 Jul 16 17:30 watson.keystore

reference site

  • https://blog.csdn.net/huaxun66/article/details/52288969?spm=1001.2014.3001.5506

Guess you like

Origin blog.csdn.net/kelxLZ/article/details/125823384
Recommended