Backdooring/patch APKs
n this laboratory, we are going to add a toast message on the target APK. This approach can be also used to backdoor the APK with something including the frida gadget.
πŸ’‰ Frida's Gadget Injection on Android: No Root, 2 Methods
Alexander Fadeev's Blog
Releases Β· frida/frida
GitHub
​

1. apktool - decode and compile APK

The first task is the decoding of the target APK. We can do this using the tool: apktool.
Apktool - A tool for reverse engineering 3rd party, closed, binary Android apps.
First, we need to access the folder and we have installed the apktool.jar, in my case: C:\Tools\android.
Tip: First time, we can remove the -r option to decode all the resource and analyze the Android-Manifest.xml file.
1
cd C:\Tools\android
2
PS C:\Tools\android> java -jar .\apktool.jar decode -r 'C:\tmp\xxxx_2.7.3.apk'
3
I: Using Apktool 2.5.0 onxxxx_2.7.3.apk
4
I: Copying raw resources...
5
I: Baksmaling classes.dex...
6
I: Baksmaling classes2.dex...
7
I: Copying assets and libs...
8
I: Copying unknown files...
9
I: Copying original files...
10
I: Copying META-INF/services directory
11
PS C:\Tools\android>
Copied!

2. Add the payload

After getting the source-code (smali), the frist step is to analyze the Android-Manifest.xml file to discover the laucher activity.
We got it!
πŸ€“
Let's opening the smali file: com.xxx.xxx.v2.ui.splashscreen.SplashscreenActivity.smali
After that, we need to find the "onCreate()" method (our target).
Here, we can add the code below, a toast message that will be presented when the activity starts.
1
const/4 v0, 0x1
2
const-string v1, "Backdoored like a baws"
3
invoke-static {p0, v1, v0}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
4
move-result-object v0
5
invoke-virtual {v0}, Landroid/widget/Toast;->show()V
Copied!

3. Build the APK

After adding the target payload, we need to build the APK with the changes. Inside the created folder, we can seethe apktool.yml file. It is necessary to proceed. So, we need to execute the akptool build process from this path.
1
(inside the decompiled APK folder (root))
2
java -jar ..\apktool.jar build -o c:\tmp\backdoored.apk
3
​
4
I: Using Apktool 2.5.0
5
I: Checking whether sources has changed...
6
I: Smaling smali folder into classes.dex...
7
I: Checking whether sources has changed...
8
I: Smaling smali_classes2 folder into classes2.dex...
9
I: Checking whether resources has changed...
10
I: Copying raw resources...
11
I: Copying libs... (/lib)
12
I: Copying libs... (/kotlin)
13
I: Copying libs... (/META-INF/services)
14
I: Building apk file...
15
I: Copying unknown files/dir...
16
I: Built apk...
17
​
Copied!

4. Create keystore with self-signed cert

The next step is to create a .keystore to sign the APK. For this, we will use the keytool.exe from JRE.
It's important to save some details, including the alias ("baws") and the typed password ("123456").
1
cd C:\Program Files\Java\jre1.8.0_251\bin
2
keytool.exe -genkey -v -keystore c:\tmp\.keystore -keyalg RSA -keysize 2040 -validity 365 -alias baws
3
4
(...)
5
Generating 2,040 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 365 days
6
for: CN=123, OU=123, O=123, L=123, ST=123, C=123
7
[Storing c:\tmp\.keystore]
Copied!
As observed, the .keystore file was created on the c:\tmp folder.

5. Sign the APK file

Here, you need to use the alias created when you created the keystore file (baws, in this case). The password was "123456".
1
cd C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin
2
jarsigner.exe -sigalg SHA1withRSA -digestalg SHA1 c:\tmp\backdoored.apk baws -keystore C:\tmp\.keystore
3
​
4
(...)
5
Enter Passphrase for keystore:
6
jar signed.
7
​
8
Warning:
9
The signer's certificate is self-signed.
Copied!

6. Align the APK with zipalign

After sign an APK, if you got some erros such as "Failed to extract native libraries, res=-2]", probably you need to align the APK.
Before aligning it, you can try to install it:
1
.\adb.exe install C:\tmp\backdored.apk
2
Performing Streamed Install
3
adb: failed to install C:\tmp\backdored.apk: Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]
Copied!
Attention: You must use zipalign at one of two specific points in the app building process, depending on the app signing tool you use:
    If you use apksigner, only run zipalign before signing the apk file. If you sign your APK using apksigner and make other changes to the APK, the signature will be invalidated.
    If you use jarsigner, only run zipalign after signing the APK file.
zipalign Β |Β  Android Developers
Android Developers
As I used jarsigner, my last step is align the APK before installing it.
1
zipalign -p -f -v 4 backdored.apk backdored-aligned.apk
Copied!
Mobexler - Mobile Application Penetration Testing Platform
Now, it's time to test it
1
.\adb.exe install C:\tmp\backdored-aligned.apk
Copied!
Of course, the APK was modified, you need to install it anyway.
When the installation process ends, it's prompted to send the app for a security scan. You should pick "DON'T SEND".
Gotcha!
😎
​
Last modified 3mo ago