Lab · Medium · 1.5 hoursFree account
Analyze an Android APK Statically
Decode an APK, review its manifest, then search the source for secrets and weak code.
On this page 16 sections
- ·What is static analysis?
- ·Prerequisites
- ·Lab setup
- 1Create a work folder and hash the APK
- 2Look inside the APK as a ZIP file
- Locked: Read the app identity, permissions and signature
- Locked: Decode the manifest and resources with apktool
- Locked: Review the manifest for attack surface
- Locked: Decompile the code to Java with jadx
- Locked: Search for secrets, insecure storage and weak crypto
- Locked: Trace one finding from the manifest to the code
- Locked: Record your findings and map the attack surface
- Locked: Check
- Locked: Clean-up
- Locked: Defender notes
- Locked: Command reference
An APK is a ZIP with a binary manifest, compiled DEX code and resources. Many flaws of a mobile app live in those files and never at run time: a hardcoded key, an exported component with no caller check, a backup flag that lets anyone copy the data, a password sent in clear text. Unpack the APK with apktool, decompile the code with jadx, search for secrets with grep, then record each finding with its file, its risk and its fix.
Lab Objectives
- Explain what static analysis finds in an Android app and what it cannot find
- Unpack an APK and name its main parts (manifest,
classes.dex, resources, signature) - Decode the manifest with apktool and list the exported components, permissions and risky flags
- Decompile the app to Java with jadx and search the source for hardcoded secrets and insecure code
- Trace one finding from the manifest to the code that handles it
- Record each finding with its file path, risk and fix
What is static analysis?
Static analysis means you study an app without running it. You read its files, its manifest and its decompiled code. You do not need a phone or an emulator.
This method is fast and safe. It also shows you the whole app, not only the screens you tap. It cannot show runtime values, server replies or code that the app downloads later. The Android Dynamic Analysis lab covers those.
An Android app ships as an APK (Android Package). An APK is a ZIP file. Inside it are the compiled code, the resources and a binary manifest. Tools turn this back into readable text and Java.
Terms that this lab uses:
- Manifest: the file
AndroidManifest.xml. It declares the components, the permissions and the app settings. - Activity: one screen of the app.
- Service: code that runs in the background and has no screen.
- Broadcast receiver: code that reacts to a message from the system or from another app.
- Content provider: a component that shares app data through a URI.
- Intent: a message that starts a component or sends data to it. An intent-filter in the manifest lists the intents that a component accepts.
- Exported component: a component that other apps can start. It is exported when it sets
android:exported="true". It is also exported by default when it has an intent-filter and the app targets Android 11 or lower. - DEX: the compiled code format of Android. The file is
classes.dex. - Smali: a readable text form of DEX code.
Prerequisites
- Read the Android Fundamentals workbook for background on the Android platform.
- Use the Kali Linux machine from the Kali Linux Environment Setup lab.
- Know how to run
grep,unzipandcdin a terminal.
Lab setup
You need one Kali Linux machine with internet access for the first download. You do not need an Android device.
| Item | Purpose |
|---|---|
unzip | Lists and extracts the APK as a ZIP file |
aapt | Reads the package name, SDK levels and permissions |
apksigner | Shows the signing certificate |
apktool | Decodes the manifest and resources to text, and the code to smali |
jadx | Decompiles the code to Java |
Install the tools:
sudo apt update
sudo apt install -y unzip aapt apksigner apktool jadxCheck the tool versions:
apktool --version
jadx --version
aapt version
apksigner --versionRecord the versions in your notes. Flags can differ between versions. Run a tool with --help if a flag in this lab fails.
Legal note. Analyse only apps you built, apps you have written permission to test, or training apps such as DIVA. An APK from a store is still someone else's software. Check its licence and terms before you decompile it. Do not upload a client's APK to an online scanner, because that shares their code with a third party. This lab never installs or runs the app. Never install DIVA on a personal phone.
Target. This lab uses DIVA (Damn Insecure and Vulnerable App). DIVA is an Android app that its authors built to be insecure, for training. Download the APK only from the link in the README of the payatu/diva-android project on GitHub. Save it as diva-beta.apk. Step 1 records its hash.
Step 1: Create a work folder and hash the APK
A hash proves which file you analysed. Record it first. A report that lacks the hash cannot be checked later.
Create a folder. Move the APK into it. Run sha256sum and keep the result.
The mv command below assumes that you saved the APK in ~/Downloads. Use the folder where you saved the file if it is not there.
mkdir -p ~/android-static-lab
cd ~/android-static-lab
mv ~/Downloads/diva-beta.apk .
sha256sum diva-beta.apk
ls -l diva-beta.apkExample output:
3f9a...e1c2 diva-beta.apk
-rw-r--r-- 1 kali kali 1494872 Nov 18 09:12 diva-beta.apkYour hash and file size can differ. The hash must stay the same each time you run the command on this file.
Why it matters. The hash is your evidence. If the hash changes, the file changed.
Step 2: Look inside the APK as a ZIP file
An APK is a ZIP archive. Listing it shows the parts before you decode anything.
List the APK contents
Run unzip -l on the APK. Do not extract it yet.
unzip -l diva-beta.apk | head -n 20Example output:
Archive: diva-beta.apk
Length Date Time Name
--------- ---------- ----- ----
5712 2016-01-01 00:00 AndroidManifest.xml
1076228 2016-01-01 00:00 classes.dex
...
38208 2016-01-01 00:00 resources.arsc
...
1385 2016-01-01 00:00 META-INF/MANIFEST.MF
1438 2016-01-01 00:00 META-INF/CERT.SF
1007 2016-01-01 00:00 META-INF/CERT.RSAFind these entries in your output.
| Entry | Meaning |
|---|---|
AndroidManifest.xml | The app declaration. It is binary XML, so a text editor cannot read it. |
classes.dex | The compiled code (Dalvik bytecode). Large apps have classes2.dex and more. |
resources.arsc | The compiled resource table, which includes strings. |
res/ and assets/ | Layouts, images and raw files. Look here for config files and keys. |
lib/ | Native libraries (.so files). jadx cannot decompile these. |
META-INF/ | The signature files. |
Why it matters. Native libraries and extra .dex files are extra places to search. Note which ones exist.
Sign in to continue
11 more sections are for signed-in members. It is free: sign in with Google.