Workbook · Easy · 3 hours
Android Fundamentals
Android architecture, ADB, APK analysis, and mobile application security.
On this page 5 sections
Android is the main computer for many people, and every mobile security test starts with the same platform facts. This workbook covers the Android architecture, the app sandbox, the permission model, APK signing and the tools that unpack and inspect an installed app. ADB, apktool and a careful read of AndroidManifest.xml carry most of the early work. The manifest alone tells a tester which components are exported, which permissions are requested and how wide the attack surface is. Set up a test device or an emulator, keep to apps you wrote or to practice targets like DIVA and MASTG, and the rest of the mobile track builds on top of this base.
What You'll Learn
- Name the layers of the Android architecture and the job of each
- Explain the app sandbox, the permission types and APK signing
- Set up a test device and use
adbto list, install, pull and inspect apps - Describe what is inside an APK and unpack one with standard tools
- Read an
AndroidManifest.xmland mark the settings that widen the attack surface
About This Workbook
For many people, a mobile device is the main computer. It holds their messages, bank apps, photos and work email. A defender or tester who cannot read an Android app cannot judge its risk.
This workbook gives you the base. You learn how Android is built, how it isolates apps, and how an app is packaged and described. Every later mobile item uses these ideas.
Legal note. Use only a test device or emulator that you own, and apps that you wrote or that are made for practice. Good practice apps are the OWASP MASTG apps, DIVA and InsecureBankv2. Do not analyse an app on a phone that belongs to another person. Do not break the terms of an app store or a company. Always work inside a written scope.
You need a Linux, macOS or Windows computer with about 10 GB of free disk space and Java 17 or newer. The commands below use Linux. When you finish, continue with the lab Analyze an Android APK Statically.
Chapter 1 — Platform Overview
Objectives
- Name the five layers of Android from kernel to apps
- Explain what the Android runtime and Zygote do
- Describe how apps talk to system services
- Start an emulator and connect with
adb
Explanation
Android is an operating system built on the Linux kernel. Google leads the open-source project (AOSP). Phone makers add their own code and apps on top. The system has layers. A flaw in a lower layer affects all the layers above it.
| Layer | What it contains | Why it matters for security |
|---|---|---|
| Apps | Your apps and the system apps (phone, settings, browser) | Many flaws that a tester finds are here |
| Java API framework | Activity, package and window managers; content providers | Enforces permissions on every API call |
| Native libraries and Android runtime (ART) | libc (Bionic), SQLite, WebView, ART | Runs the app code; memory bugs live here |
| Hardware abstraction layer (HAL) | Drivers for camera, radio, sensors, keystore | Keys can be kept in hardware here |
| Linux kernel | Process scheduling, memory, SELinux, Binder driver | The base of the sandbox |
The runtime. An app is written in Kotlin or Java. The build tool compiles it to DEX bytecode (classes.dex). The Android runtime (ART) runs this bytecode. ART compiles hot code to native code when you install the app and while it runs. The older runtime, Dalvik, was replaced by ART in Android 5.0. The file name dex and the tool name dalvikvm remain from that time.
Zygote. When the system starts, it starts one process called Zygote. Zygote loads the common framework classes once. To start an app, Android asks Zygote to fork itself. The new process already has the framework in memory, so apps start fast. Every app process is a child of Zygote.
System services. The central process is system_server. It hosts the services that apps use: the Activity Manager (starts and stops components), the Package Manager (installs apps and knows their permissions) and many others. An app does not call these services directly. It sends a message through Binder, the Android inter-process communication (IPC) system in the kernel. Binder tells the service the user ID (UID) of the caller. The service uses that UID to decide if the call is allowed. This is the base of the permission model that you learn in Chapter 2.
ADB. The Android Debug Bridge (adb) is the tool that connects your computer to a device. It has three parts: a client (the command you type), a server (a background process on your computer, port 5037) and a daemon (adbd, on the device). With adb you open a shell on the device, copy files, install apps and read logs. A real device must have Developer options and USB debugging turned on. An emulator has them on already.
Use a test device only. USB debugging gives a computer deep control of a device. Never turn it on for your daily phone, and never connect a test device to an account that holds real data.
Worked example
You build a test emulator and prove that adb works. The steps use the Android command-line tools, which come with Android Studio or as a separate download from Google.
Install the platform tools, the emulator and a system image. Choose an image with google_apis, not google_apis_playstore. The first type lets you run adb root. The second is a production build and blocks it.
sdkmanager "platform-tools" "emulator" "system-images;android-34;google_apis;x86_64"
avdmanager create avd -n lab34 -k "system-images;android-34;google_apis;x86_64"
emulator -avd lab34 -no-snapshot &Wait about one minute for the boot, then connect.
adb devicesList of devices attached
emulator-5554 deviceThe word device means the connection works. The word unauthorized means that you must accept the key prompt on a real device. The word offline means that the device is still starting.
Open a shell and ask who you are.
adb shell iduid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log) context=u:r:shell:s0You are the shell user. It has more rights than an app but fewer than root. Now read the system details.
adb shell getprop ro.build.version.release
adb shell getprop ro.product.cpu.abi
adb shell getprop ro.build.type14
x86_64
userdebugThe three lines are the Android version, the CPU type and the build type. A userdebug build allows adb root. A phone that you buy has the type user and does not.
Look at the processes. Zygote is the parent of the app processes.
adb shell ps -A | grep -E "zygote|system_server|com.android.settings"root 414 1 ... zygote64
system 812 414 ... system_server
system 2290 414 ... com.android.settingsLook at the second number on each line. It is the parent process ID. system_server and the Settings app both have parent 414, the Zygote process. This is the fork model from the explanation.
Last, run one more command that you need every day. It shows the log of the device and ends when you press Ctrl+C.
adb logcat -d -t 20The -d option prints the log and exits. The -t 20 option limits the output to the last 20 lines.
Practice questions
- Which layer enforces permissions on an API call, and how does it know who made the call?
- Why do apps start fast on Android?
adb devicesprintsunauthorized. What do you do?- Why does the example use an image with
google_apisand notgoogle_apis_playstore? - In the
psoutput, which number shows that two processes were both started by Zygote?
Answers
- The Java API framework (the system services in
system_server) enforces it. Binder gives the service the UID of the caller, and the service checks that UID against the permissions the app holds. - Zygote loads the framework once. Each app is a fork of Zygote, so it does not load the framework again.
- Unlock the device and accept the "Allow USB debugging?" prompt that shows the key of your computer. Run
adb devicesagain. - The
google_apisimage is a developer build. It allowsadb rootand easy inspection. The Play Store image is a production build that blocks root. - The parent process ID (
PPID), the second number. Both have the value of the Zygote process.
Chapter 2 — Security Model
Objectives
- Explain how the UID-based sandbox isolates apps
- Name the three layers of access control: Linux, SELinux and permissions
- Separate normal, dangerous and signature permissions
- Explain what APK signing proves and what it does not prove
Explanation
Android treats every app as untrusted. The system limits what an app can do. An app cannot read another app's data, and it cannot use the camera or contacts until the user agrees. Four ideas make this work.
1. The sandbox. When you install an app, Android gives it a unique Linux UID, for example u0_a145. The app process runs as that user. Files of the app are in a private directory, /data/data/<package>/ (the same place as /data/user/0/<package>/), and only that UID can read it. This is plain Linux file permission. Two apps cannot read each other's files because the UIDs differ. Apps with the same developer key can ask to share a UID. This is rare and it removes the isolation, so you note it when you see it in a manifest (android:sharedUserId).
2. SELinux. Android runs SELinux in enforcing mode. SELinux is a second set of rules that the kernel applies even to the root user. It labels every process and file. For example, an app runs in the domain untrusted_app. The policy says what this domain can open. If an attacker gets code execution in an app, SELinux still limits what the code can touch. When you run adb shell getenforce the answer is Enforcing.
3. Permissions. An app must declare each permission in its manifest. The types are:
| Type | Example | Who decides |
|---|---|---|
| Normal | INTERNET, VIBRATE | Granted at install, no question |
| Dangerous (runtime) | CAMERA, READ_CONTACTS, ACCESS_FINE_LOCATION | The user decides, at the moment of use (Android 6.0 and later) |
| Signature | BIND_ACCESSIBILITY_SERVICE and many system permissions | Granted only to apps signed with the same key as the app that defined the permission |
| Special | SYSTEM_ALERT_WINDOW, MANAGE_EXTERNAL_STORAGE | The user grants it in a settings screen |
Dangerous permissions are grouped. A user can also take a permission back at any time in Settings. A well-built app must keep working, in a reduced way, when the user says no.
4. Signing. Android only installs an APK that carries a valid signature. The developer signs with a private key and the APK contains the certificate. Signing gives two guarantees: the APK was not changed since it was signed, and an update must be signed with the same key as the installed version. It does not prove that the developer is honest. Anyone can make a key and sign malware. There are four schemes. v1 signs the files in the ZIP (META-INF/) and is weak against some tricks. v2 and v3 sign the whole file, and v3 supports key rotation. v4 is a separate file used for streaming installs. Modern apps use v2 or newer, with v1 only for old Android versions.
Two more controls matter. Verified boot checks that the system partitions are not changed. Scoped storage (Android 10 and later) limits access to shared storage such as /sdcard, so apps cannot read each other's files there. Apps must keep private data in their private directory, not on shared storage.
Worked example
You inspect the sandbox of a practice app. The package here is com.example.notes, a name that stands for any app you own.
List the third-party packages and find the one you need.
adb shell pm list packages -3package:com.example.notes
package:org.owasp.mastestappThe -3 option shows only apps that were installed by users, not system apps. Now find the UID of the app.
adb shell dumpsys package com.example.notes | grep -E "userId|versionName|flags=" userId=10145
versionName=1.4.2
flags=[ DEBUGGABLE HAS_CODE ALLOW_CLEAR_USER_DATA ALLOWED_PERMISSION_CHECK ]Look at DEBUGGABLE. A release app must not have this flag. It lets anyone with adb run commands as the app, read its private files and attach a debugger. It is acceptable here because this is a practice app. The UID 10145 is shown in the shell as u0_a145 (the app UID is 10000 plus 145).
Read the permissions in the same output.
adb shell dumpsys package com.example.notes | grep -A8 "requested permissions" requested permissions:
android.permission.INTERNET
android.permission.READ_CONTACTS
android.permission.WRITE_EXTERNAL_STORAGE
install permissions:
android.permission.INTERNET: granted=true
runtime permissions:
android.permission.READ_CONTACTS: granted=falseA notes app has no reason to read contacts. When you review an app, always ask: does each permission match a feature? A permission with no feature is a finding, even when no code uses it yet.
Last, prove that the sandbox works. A shell user cannot enter the private directory of an app.
adb shell ls /data/data/com.example.notesls: /data/data/com.example.notes: Permission deniedFor a debuggable app, run-as switches to the app UID and the same command works.
adb shell run-as com.example.notes lscache databases files shared_prefsThe folders databases and shared_prefs are where apps store data. Defenders check them for passwords and tokens in plain text. The lab Analyze an Android APK Statically shows how.
Practice questions
- What gives two apps on the same phone separate access to files?
- Why does Android use SELinux when the Linux user rules already exist?
- Sort these into normal, dangerous or signature:
INTERNET,CAMERA,READ_CONTACTS. - A developer says: "The app is signed, so it is safe." What is wrong?
adb shell ls /data/data/com.example.notesreturns "Permission denied". Is this a fault?
Answers
- A unique UID for each app. Each private directory belongs to its UID and other UIDs cannot read it.
- SELinux is mandatory access control. It limits even a process that runs as root and limits a compromised app to the files and calls its policy allows. User rules alone do not do this.
INTERNETis normal.CAMERAandREAD_CONTACTSare dangerous.- A signature proves that the APK has not changed and that updates come from the same key holder. It says nothing about what the code does. Malware is also signed.
- No. It is the sandbox working. The
shelluser has a different UID from the app.
Chapter 3 — App Structure
Objectives
- List the files in an APK and say what each file holds
- Unpack an APK with
unzip,aapt2andapktool - Name the four component types
- Read an
AndroidManifest.xmland mark risky settings
Explanation
The APK. An APK (Android Package) is a ZIP file. You can open it with any ZIP tool. A real device installs an app from an APK, or from a bundle (.aab) that the store turns into several APKs. The main parts are:
| Path in the APK | Content |
|---|---|
AndroidManifest.xml | The description of the app. It is stored as binary XML |
classes.dex (and classes2.dex ...) | The compiled code (DEX bytecode) |
resources.arsc | Compiled resources: strings, IDs, styles |
res/ | Layouts, images and other resource files |
assets/ | Raw files that the app reads at run time |
lib/<abi>/ | Native libraries (.so) for each CPU type, for example arm64-v8a |
META-INF/ | The v1 signature files. The v2 and later signatures are in a signing block inside the file |
Because strings, endpoints and keys can be in resources.arsc, assets/ and the DEX code, a review reads all of these.
The four components. An app is not one program with one start point. It is a set of components that the system can start. Each component must be declared in the manifest.
| Component | Job | Example |
|---|---|---|
| Activity | One screen with a user interface | Login screen |
| Service | Work with no screen, in the background | Music player, upload job |
| Broadcast receiver | Reacts to a system or app event | "Boot completed", "SMS received" |
| Content provider | Shares structured data with other apps | Contacts, a notes database |
Components talk through intents. An intent is a message that says "do this" (ACTION_VIEW, a URL, extra data). An explicit intent names the target component. An implicit intent names only an action and the system finds a component that has a matching intent filter.
The manifest. AndroidManifest.xml is the most important file for a security reviewer. It tells you the package name, the minimum and target Android versions, the permissions, every component and the app flags. You can answer many questions with no code review. The settings that matter most are these.
| Setting | Risky value | Why |
|---|---|---|
android:debuggable | true | Anyone with adb can run as the app and read its files |
android:allowBackup | true (the default) | adb backup and cloud backup can copy app data off the device |
android:usesCleartextTraffic | true | The app can send data over plain HTTP |
android:exported on a component | true | Any app on the device can start it |
<intent-filter> on a component | present | The component is exported by default on Android 11 and older |
android:targetSdkVersion | old value | The app avoids newer protections |
android:sharedUserId | present | The app shares a UID and the sandbox is weaker |
android:networkSecurityConfig | trusts user CAs or allows cleartext | The app is easier to intercept |
Since Android 12 (API 31) a component that has an intent filter must say android:exported clearly, or the app does not install. An exported component is attack surface. It must either check the caller (with a permission or by checking the data) or not be exported.
Worked example
You unpack the practice app and read its manifest. Assume notes.apk is a copy that you pulled from your own emulator. First find the file path and copy it to your computer.
adb shell pm path com.example.notes
adb pull /data/app/~~Zr9q1w==/com.example.notes-Ab3x==/base.apk notes.apkpackage:/data/app/~~Zr9q1w==/com.example.notes-Ab3x==/base.apk
/data/app/~~Zr9q1w==/com.example.notes-Ab3x==/base.apk: 1 file pulled, 0 skipped. 4.1 MB/s (3472816 bytes in 0.806s)The folder names after ~~ are random on each device. Copy the path that pm path shows you. List the content as a ZIP.
unzip -l notes.apkArchive: notes.apk
Length Date Time Name
--------- ---------- ----- ----
7412 1981-01-01 00:00 AndroidManifest.xml
2108344 1981-01-01 00:00 classes.dex
412880 1981-01-01 00:00 resources.arsc
1820 1981-01-01 00:00 assets/config.json
926704 1981-01-01 00:00 lib/arm64-v8a/libnative.so
1423 1981-01-01 00:00 META-INF/CERT.RSA
--------- -------The date 1981-01-01 is normal: build tools set a fixed date. Next, ask aapt2 for a summary. It reads the binary manifest for you.
aapt2 dump badging notes.apk | head -12package: name='com.example.notes' versionCode='12' versionName='1.4.2' compileSdkVersion='34'
sdkVersion:'24'
targetSdkVersion:'34'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.READ_CONTACTS'
application-label:'Notes'
launchable-activity: name='com.example.notes.MainActivity'Now check the signature and print the certificate.
apksigner verify --verbose --print-certs notes.apkVerifies
Verified using v1 scheme (JAR signing): true
Verified using v2 scheme (APK Signature Scheme v2): true
Verified using v3 scheme (APK Signature Scheme v3): true
Number of signers: 1
Signer #1 certificate DN: CN=Example Notes, O=Example
Signer #1 certificate SHA-256 digest: 9f2c...e41aRecord the SHA-256 digest. Two versions of one app must show the same digest. A different digest on a "new version" means a different publisher.
Decode the manifest to readable text with apktool.
apktool d notes.apk -o notes_out
cat notes_out/AndroidManifest.xml<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.notes">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application android:allowBackup="true" android:debuggable="true"
android:label="Notes" android:usesCleartextTraffic="true">
<activity android:exported="true" android:name="com.example.notes.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:exported="true" android:name="com.example.notes.ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<service android:exported="false" android:name="com.example.notes.SyncService"/>
<provider android:authorities="com.example.notes.provider" android:exported="true"
android:name="com.example.notes.NotesProvider"/>
</application>
</manifest>Read the file in this order and write down each finding.
- Application flags.
debuggable="true"is a high finding in a release build.allowBackup="true"andusesCleartextTraffic="true"are medium findings. - Permissions.
READ_CONTACTShas no matching feature in a notes app. - Exported components.
MainActivityis exported with theMAINandLAUNCHERfilter. This is required, because the launcher must start it.ShareActivityis exported on purpose: it receives text from other apps. Its code must treat the input as untrusted.SyncServiceis not exported, which is correct.NotesProvideris exported with no permission. Any app on the device can query the notes. This is the strongest finding. - Next step. Open the code of
NotesProviderand check which URIs it serves and whether it filters the query. That is static analysis, the subject of the lab Analyze an Android APK Statically.
You can prove the provider finding on your own emulator with one command. It sends a query to the provider as the shell user.
adb shell content query --uri content://com.example.notes.provider/notesRow: 0 _id=1, title=Shopping list, body=milk, bread
Row: 1 _id=2, title=WiFi, body=password is in the safeA process that is not the app has read the private notes. The fix is to set android:exported="false", or to protect the provider with a signature-level permission.
Keep a manifest checklist. For every app, write the answers to five questions: which flags are risky, which permissions have no feature, which components are exported, which of those have no permission, and what is the target SDK. This takes about ten minutes and finds useful leads.
Practice questions
- Why can you not read
AndroidManifest.xmlin a text editor afterunzip? - Name the four component types and give one job for each.
- In the example, why is the exported
MainActivitynot a finding? - Why is an exported content provider with no permission worse than an exported activity that shows a login screen?
- Which command prints the signing certificate digest of an APK?
allowBackup="true"is in a banking app. What is the risk?
Answers
- The manifest in the APK is compiled binary XML. Use
aapt2 dump badging,apkanalyzer manifest printorapktool dto read it. - Activity: a screen. Service: background work. Broadcast receiver: reacts to events. Content provider: shares data with other apps.
- The launcher must start it, so it must be exported. It is the normal start point and it holds no private data or actions.
- A provider gives direct access to data. Any app on the device can read, and sometimes write, the data with no user action. A login screen shows the user a form and does not return data to another app.
apksigner verify --verbose --print-certs app.apk.- The data of the app, which can include tokens and account data, can be copied off the device with
adb backup(on older Android versions) or through cloud backup. A banking app setsallowBackup="false"or excludes sensitive files.
Next steps
Pick a practice app, for example one of the OWASP MASTG apps, and complete the manifest checklist from Chapter 3 on your own emulator. Write the result in six lines. Then continue with the lab Analyze an Android APK Statically, which decompiles the code and finds secrets and insecure code. After that you can follow the labs in order: Android Dynamic Analysis and Android Malware Analysis.
Workbook complete. You can describe the Android layers, the sandbox and signing, run adb on a test device, unpack an APK and read a manifest for risk.
Sign in to track progress and send feedback.