repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
android-applications.mdMarkdown683 lines26.5 KiBsource raw download

title: "Android Application" section: "Methodology" sectionSlug: "methodology" sourcePath: "docs/methodology/android-applications.md" sourceUrl: "https://github.com/swisskyrepo/InternalAllTheThings/blob/203bb0c0b290/docs/methodology/android-applications.md" sha: "203bb0c0b290" isIndex: false#

Android Application#

Lab#

Extract APK#

ADB Method#

Connect to ADB shell and list/download packages. You might need to enable Developer mode and Debugging in order to connect with adb

adb shell pm list packages
adb shell pm path com.example.someapp
adb pull /data/app/com.example.someapp-2.apk

Stores#

Warning: Downloading APK files from unofficial stores can compromise your device's security. These sources often host malware and malicious software. Always use trusted and official app stores for downloads.

Download APK from Google Play using a 3rd Party:

Static Analysis#

Extract Contents From APK#

Search for strings flag,secret, the default string file is Resources/resources.arsc/res/values/strings.xml.

apktool d application.apk

Decompile Data as Java Code#

To reverse .odex you need to provide the /system/framework/arm, fortunately since we have the firmware we have it.

java -jar baksmali-2.3.4.jar x application.odex -d k107-mb-8.1/system/framework/arm -o application
apktool d application.apk 
apktool b rebuild_folder -o rebuilt.apk

Decompile Native Code#

Native library are represented as .so files. These libraries by default are included in the APK at the file path /lib/<cpu>/lib<name>.so or /assets/<custom_name>.

Use IDA, Radare2/Cutter or Ghidra to reverse them.

CPU Native Library Path
"generic" 32-bit ARM lib/armeabi/libcalc.so
x86 lib/x86/libcalc.so
x64 lib/x86_64/libcalc.so
ARMv7 lib/armeabi-v7a/libcalc.so
ARM64 lib/arm64-v8a/libcalc.so

:warning: The shared object file (.so) doesn't need to be embedded in the app.

Mobile Security Framework Static#

Mobile Security Framework (MobSF) is an automated, all-in-one mobile application (Android/iOS/Windows) pen-testing, malware analysis and security assessment framework capable of performing static and dynamic analysis.

Run MobSF/Mobile-Security-Framework-MobSF

Online Assets#

:warning: Uploading APKs to uncontrolled websites risks data leaks, malware, intellectual property theft, and privacy violations. Use trusted platforms only to ensure the security and integrity of your app.

React Native and Hermes#

Identify React Native app with index.android.bundle inside the assets folder

Hermes: pip install hbctool
╰─$ hbctool disasm index.android.bundle indexasm
[*] Disassemble 'index.android.bundle' to 'indexasm' path
[*] Hermes Bytecode [ Source Hash: 4013cb75f7e16d4474f5cf258edc45ee16585560, HBC Version: 74 ]
[*] Done

Flutter#

Indentify Flutter use in the MANIFEST.MF and search for libflutter.so.

APK Patching#

docker run --rm -v .:/pwd -it madsquirrels/apkpatcher -a base.apk

# Java dependencies
apt install -y default-jre

# sdktools dependendies installation
wget https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip
mkdir /usr/lib/android-sdk
cd /usr/lib/android-sdk
unzip commandlinetools-linux-6200805_latest.zip
mkdir cmdline-tools
mv tools/ cmdline-tools/
echo 'export ANDROID_SDK_ROOT=/usr/lib/android-sdk' >> ~/.bashrc

# installation of platform-tools
sdkmanager "platform-tools" "platforms;android-36" "build-tools;36.0.0" "emulator"

# install apkpatcher
pip install apkpatcher

Sign and Package APK#

Unpack Resources#

apkpatcher -a <apk> --only-unpack dir
apkpatcher -a <new_apk> --only-repack dir

Inject Proxy Certificate#

Accept user certificate

apkpatcher -a <apk> -e

Add a custom certificate

apkpatcher -a <apk> -c burp.der

Add Permissions#

apkpatcher -a <apk> --add-permissions <my_permission>

Debug Mode#

Inject the android:debuggable option into an application AndroidManifest.xml, allowing users to debug applications even when they are in release mode.

apkpatcher -a <apk>  --enable-debug

Set the application to be the debug target.

adb shell am set-debug-app --persistent

Start the application’s activity in debug mode.

adb shell am start -D -a android.intent.action.MAIN -n <package>/<Activity>

Then use gdbserver for Native Code or Java Debugger like jdb.

adb forward tcp:12345 jdwp:$(adb shell pidof <package> )
jdb -attach localhost:12345

Dynamic Analysis#

Dynamic analysis for Android malware involves executing and monitoring an app in a controlled environment to observe its behavior. This technique detects malicious activities like data exfiltration, unauthorized access, and system modifications. Additionally, it aids in reverse engineering app features, revealing hidden functionalities and potential vulnerabilities for better threat mitigation.

Burp Suite#

# Convert Burp certificate for Android
openssl x509 -inform DER -in burp.der -out burp.pem
openssl x509 -inform PEM -subject_hash_old -in burp.pem |head -1
mv burp.pem <hash output>.0

# Push the certificate in the AVD
emulator -list-avds
emulator -avd Pentesting_Device -writable-system
adb root
adb remount
adb push <hash>.0 /sdcard/

# Change the permissions
adb shell
mv /sdcard/<hash>.0 /system/etc/security/cacerts/
chmod 644 /system/etc/security/cacerts/<hash>.0
chown root:root /system/etc/security/cacerts/<hash>.0

Frida#

Download frida/frida from releases.

pip install frida-tools
unxz frida-server.xz
adb root # might be required
adb push frida-server /data/local/tmp/
adb shell "chmod 755 /data/local/tmp/frida-server"
adb shell "/data/local/tmp/frida-server &"

Inject frida inside the APK.

$ pip install frida-tools
$ frida --version
16.7.13

$ apkpatcher -a <apk> --download_frida_version <version>
$ apkpatcher -a <apk> --download_frida_version 16.7.13

Interesting Frida scripts:

Runtime Mobile Security#

Runtime Mobile Security (RMS) 📱🔥 - is a powerful web interface that helps you to manipulate Android and iOS Apps at Runtime

Requirements:

In case of issue with your favorite Browser, please use Google Chrome (fully supported).

Genymotion#

Genymotion is a robust Android emulator designed for developers, offering fast and reliable virtual devices for app testing. It features GPS, battery, and network simulation, enabling comprehensive testing and development

Android SDK emulator#

Android Virtual Device (AVD) without Google Play Store.

Mobile Security Framework Dynamic#

:warning: Dynamic Analysis will not work if you use MobSF docker container or setup MobSF inside a Virtual Machine.

Requirements:

Dynamic Analysis from MobSF grants you the following features:

Appium#

Appium is an open-source project and ecosystem of related software, designed to facilitate UI automation of many app platforms, including mobile (iOS, Android, Tizen), browser (Chrome, Firefox, Safari), desktop (macOS, Windows), TV (Roku, tvOS, Android TV, Samsung), and more!

Examples:

Flutter#

Repackage a Flutter Android application to allow Burp Suite proxy interception.

An alternative way to do it is using a rooted Android device with zygisk-reflutter.

SSL Pinning Bypass#

SSL certificate pinning in an APK involves embedding a server's public key or certificate directly into the app. This ensures the app only trusts specific certificates, preventing man-in-the-middle attacks by rejecting any certificates not matching the pinned ones, even if they are otherwise valid.

:warning: Android 9.0 is changing the defaults for Network Security Configuration to block all cleartext traffic.

Root Detection Bypass#

Common root detection techniques:

Common bypass:

Android Debug Bridge#

Android Debug Bridge (ADB) is a versatile command-line tool that enables communication between a computer and an Android device. It facilitates tasks like installing apps, debugging, accessing the device's shell, and transferring files, making it essential for developers and power users in Android development and troubleshooting.

USB Debugging#

./platform-tools/adb connect IP:PORT
./platform-tools/adb shell

Wireless Debugging#

One more step, you need to pair the devices using a code.

./platform-tools/adb pair IP:PORT CODE
./platform-tools/adb connect IP:PORT
./platform-tools/adb shell
Command Description
adb devices List devices
adb connect <IP>:<PORT> Connect to a remote device
adb install app.apk Install application
adb uninstall app.apk Uninstall application
adb root Restarting adbd as root
adb shell pm list packages List packages
adb shell pm list packages -3 Show third party packages
adb shell pm list packages -f Show packages and associated files
adb shell pm clear com.test.abc Delete all data associated with a package
adb pull <remote> <local> Download file
adb push <local> <remote> Upload file
adb shell screenrecord /sdcard/demo.mp4 Record video of the screen
adb shell am start -n com.test.abc Start an activity
adb shell am startservice Start a service
adb shell am broadcast Send a broadcast
adb logcat *:D Show log with Debug level
adb logcat -c Clears the entire log

Android Virtual Device#

An Android Virtual Device (AVD) is an emulator configuration that mimics a physical Android device. It allows developers to test and run Android apps in a simulated environment with specific hardware profiles, screen sizes, and Android versions, facilitating app testing without needing actual devices.

emulator -avd Pixel_8_API_34 -writable-system
Command Description
-tcpdump /path/dumpfile.cap Capture all the traffic in a file
-dns-server X.X.X.X Set DNS servers
-http-proxy X.X.X.X:8080 Set HTTP proxy
-port 5556 Set the ADB TCP port number

Unlock Bootloader#

Requirements:

Unlock the bootloader will wipe the userdata partition. On some device these methods will require a key to successfully unlock the bootloader.

Android XML#

pip install pyaxml

# To print Manifest in XML form
pyaxml-rs axml2xml -i AndroidManifest.xml

# To print resources.arsc in XML form
pyaxml-rs arsc2xml -i resources.arsc

# To convert XML to AXML
pyaxml-rs xml2axml -i AndroidManifest.xml -o AndroidManifest.axml

References#