← Back to home
Android APK Reversing Smali dex2jar Apktool
Advanced

Android Reversing: Binary Patching (Smali)

Mar 15, 2022

Reverse Engineering on Android consists of disassembling an application (APK) to study its logic, extract secrets, or modify its behavior (Patching). In this guide, we will cover patching vulnerabilities in InsecureBankv2 and bypassing checks in the famous KGB Messenger CTF.

1. The Reversing Lifecycle

Java/Kotlin code is compiled into a bytecode format called DEX. We have two paths:

2. Patching the AndroidManifest

# 1. Disassemble the APK:
apktool d InsecureBankv2.apk -o Source

# 2. Patch AndroidManifest.xml (Change android:exported to "false").

# 3. Rebuild the APK:
apktool b Source -o Patched.apk

# 4. Sign the new APK (Android requires signatures):
apksigner sign --ks my-release-key.keystore Patched.apk

3. KGB Messenger CTF: Smali Logic Bypass

The app checks if the device's locale is "Russia". If not, it exits. We need to patch the binary.

Step 1: Identify the Check in JADX

if (!locale.equals("RU")) { System.exit(0); }

Step 2: Patch the Smali Code

We find the conditional branch in MainActivity.smali:

invoke-virtual {v0, v1}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z
move-result v0
if-eqz v0, :cond_0   # "If Equal to Zero" (If not equal, jump to exit)

The Patch: Change if-eqz to if-nez (If Not Equal to Zero). The logic inverts: the app will only exit if the device IS in Russian. Rebuild, sign, and install.

πŸ”΄ Smali Patching Simulator

You have infiltrated the assembly code (Smali) of a banking Malware. The app performs a security check: it verifies if the device is "Rooted". Show your Reversing skills by indicating how to manipulate the instructions to bypass this defense.

>_ START CTF 27 CHALLENGE

4. Smali Instruction Glossary