Learn
This activity defines Mainactivity class to launch on startup, .
here signifies it is in same package.
It is considered main entry point with class launcher(launch this mainactivity class on startup).
Exported activities are activities that can be started by an app or adb.
ADB (Android Debug Bridge)
adb shell
drops us on android linux environment.adb push your_path android_path
to transfer files from your machine to android.adb pull android_path your_path
to fetch files from android machine to yours.adb install path_of_apk
to install apk on android machine.adb shell pm list packages
to list all installed packages on device.Note:
pm
is package manager,shell
allows us to run commands as if we were inside device shell.
adb shell pm list packages -3
to list only 3rd part apps.adb shell pm clear package_name
to clear all data of an app in phone but not uninstall it.adb shell dumpsys package package_name
to dump information about a package like permissions, activities exported, apk path etc.adb shell am start package_name/.activityname
to start an activity.Note:
am
is activity manager.
adb uninstall package_name
to uninstall the app.adb shell pm path package_name
to get full path of apk on android file system.adb shell pm list packages -3 -f
to get path of apk in same command.
If multiple emulators are running
adb devices -l
shows list
adb -s device_name subcommand
to insteract with that emulator.
or
adb subcommand -t transport_id
to interact with emulator transport id instead of big name.
ADB Logcat
adb logcat
shows logs from all the things in device.adb logcat -v brief
to see brief messgaes. It has multiple tagsadb logcat -v brief "MainActivity:V *:S"
we can filter logs specifically. this will only log verbose log of type mainactivity.
Android compilation process:
Java/Kotlin ------> .class files(using javac/kotlic)-------->classes.dex (using d8 compiler) it is android byte code.------------>machine code(using Dalvik virtual machine(older android)/ ART android run time)
Smali
Smali means assembly closely . Assembly language of dalvic bytecode.
baksmali
: disassembler for dalvik bytecode. thats's what apktool tool does, disassembled .dex
files to smali
files.
Apktool
Installation: https://apktool.org/docs/install/
apktool d path_tok_apk
to decompile an apk.
Recompiler and sign APK
create the apk
From inside the folder where all files and folders are created after decompilation run:
apktool b
this will drop a new apk file in dist
directory.
We can't install this file yet. Let's sign it first
Create a keystore:
keytool -genkey -v -keystore research.keystore -alias research_key -keyalg RSA -keysize 2048 -validity 10000
keystore research.keystore
file will contain our signing key research_key
.
Confirm with yes
on last question.
Sign the APK:
jarsigner -verbose -keystore research.keystore app_name.apk research_key
Troubleshooting Install Errors
INSTALL_PARSE_FAILED_NO_CERTIFICATES: There is still something wrong with the signature. Maybe you tried to install an unsigned apk or the chosen algorithm (eg. SHA1) gets rejected.
INSTALL_FAILED_INVALID_APK: Failed to extract native libraries
This error occurs with some versions of apktool if the app contains native-libraries. To fix it, edit the AndroidManifest.xml so that extractNativeLibs
is set to true
. Afterwards you need to repackage and re-sign your APK.
INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package package_name
signatures do not match previously installed version; ignoring!
You will get this message if a version of the app signed with a different key is installed on the device. The simple solution is to delete the existing app.
Failed parse during installPackageLI:
Targeting R+ (version 30 and above) requires the resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary'
This happens on newer apps, try this alternative method using zipalign
and apksigner
coming with the specific version build tools:
apksigner, zipalign are installed with Androistudio but you need to add build-tools/34.0.0
to SYSTEM PATH.
jarsigner and keytool are installed when you install JDK from internet.
Decompiling
Reflection feature in java/kotlin allows decompilation easy and in very high level. Unless app is obfuscated.
Reflection enables code to kind of "self introspect" at run time to resolve symbols [variable, class names etc]
Start Emulator without Android studio
If you just want run emulator and nit android studio. Use emulator
tool android studio. you will have to add it to your path.
emulator -list-avdsto list your mobile devices.
emulator -avd device_name
to start your device.
emulator -tcpdump file.pcap -avd device_name
to capture network dump of entire phone.
Note: you may need to disable wifi interface, as tcpdump might not be able to capture http packets on wifi interface.
Proxy
After configuring proxy in device, we need to install burp CA cert in android trusted certificated list to be able to perorm mitm with ssl traffic.
In latest android you can just browse to http://burpsuite
and download ca cert and install it by searching certificate in settings. This will install certificate in user trusted list which is not supported/trusted by most of the apps.
In older android device do not support der certificate. You will need to convert it to pem format first then install.
/system/etc/security/cacerts
directory has all system certificates.
/data/misc/user/0/cacerts-added/
directory has all user installed certificates.
In order to install our certificate as system cert we need root access.
root android studio emulator. (search rootavd)
have a rooted physical device
use genymotion, it ocmes with rooted emulators.
or use a non-google play image in android studio which allows you to be root.
Then
install certificate.
If you have a device with root access follow the following steps:
Install the proxy certificate as a regular user certificate
Ensure you are root (
adb root
), and execute the following commands inadb shell
:
This trick of adding your certificate to /system/etc/security/cacerts work till android 13 only after that android has made changes that it doesn't handle system certificate this way anymore.
Read this for android 14 fix and problem:
Here is script to automate this, once you have installed you certificate on Android 14 as user. And you have root access, run this bash script inside emulator:
Install split apks
In modern devices and latest playstore versions, I noticed that playstore install apps in split apks instead of one single base.apk file.
And for analysis or you want all install this same app on a device with no access to playstor. You will have to pull all apks one by one and install them at all.
adb shell pm path package_name
e.g.
You can now pull each apk and install them together using this command.
This will install the original APK on new device.
If some patching is done in APKs you can use objection to sign them
objection signapk *.apk
There is another option called SAI split APKs
installer app from playstore which is paid now. But you can use its older version for free from its github: https://github.com/Aefyr/SAI
Using this app you create a backup of your target app then export the backup in apks format.
Transfer this apks backup to second device and using the same SAI application import the backup and install the app.
Advanced Proxy:
We have seen that if we got root access on a device, we can install our ca cert in system trusted CA certificate list.
But, What if we can't get root on our device ? ? ?
In that case we can patch our target APK to trust user certificate too.
By default android 9 and above if application doesn't define CA trust configuration, ANdroid defaults to system certificate only.
But we can update this value to trust user certificate too.
In androidmanifest.xml check <application> tag.
If android:networkSecurityConfig="@xml/network_security_config"
is present in it which means application do already implement some custom config, we just have to update res/xml/network_security_config.xml
file.
or if not add `android:networkSecurityConfig="@xml/network_security_config"
to <application> tag among other entries and create `res/xml/network_security_config.xml file.
See we explicitly told system that this app trust "user" certificate.
Now Rebuild apk and sign the apk and install it which will alow you to intercept raffic without root access.
Note: In modern devices you will face problem of split apks and in that case
Fetch all split files as discussed earlier.
Most probably Androidmanifest.xml and rest of the config files will be base.apk file. So decompile the base.apk
Make required changes.
rebuild the base apk.
Now you have rebuilt base.apk and split apks. Use
Objection
tool to sign all apks
or you can maually sign and align 4 apks using zipalign and apksigner tool as discussed earlier. (You don't have to align other split apks as you didn't change them )
When APKs are ready you can perform
You should be go to got from here. But take it with some grain of salt.
This way of patching might break some things functionality wise or you might not be able to install at all and get some errors. As apktool decompilation and rebulding process is not flawless. It's your skill to find the solution of your problem.
Use VpnService API
Sometime you might not be able to intercept traffic from your application even if certificates and proxy are configured propelry. Because application might be trying to bypass system proxy settings. e.g
Direct Sockets: Some apps use low-level socket APIs (e.g., Socket
or OkHttp
) to establish direct connections, bypassing the HTTP proxy set at the system level.
Then we can use android vpnservice API
Android's VPNService API allows apps to intercept and redirect all network traffic at network level.
We can install this app and set proxy to our burp suite and set DNS to "system" setting. Using this we can capture traffic as it forces application to reroute traffic through our tunnel as this app registers itself as vpnservice which tells android device to send all raw tcp packets through this gateway and get rewritten by rethink app.
Dynamic Instrumentation
Install frida: https://frida.re/
Install Objection: https://github.com/sensepost/objection
If you are doing it in virtual environment and you are getting this error
Here is fix: pip install --upgrade setuptools
Hooking app with frida
There are two ways to got from here
Patching the apk with frida
Installing frida server on android and dynamically hooking app at runtime.
Method 1:
Go to the official Frida releases page: https://github.com/frida/frida/releases.
Download same architecture(android devicee) and version as frida-cli on your machine.
You can check your device architecture using:
Push frida server to android device
Give execute permission
strat the server
Use frida cli to interact with app.
List installed packages.
attach to a running app
Method 2:
To inject Frida into an APK we can use objection:
Objection will extract, patch, re-pack, align and sign the application, and so it's a very fast and easy way to get Frida running.
Note that the application will wait on launch for Frida to connect to it, so to start the application we have to run:
Frida REPL (Read-Eval-Print Loop)
This terminal you see is just JS interpreter which means it can run JS code.
Run a js script
anychanges to your script are auto reoloaded in memory. Which can be configured
Hooking Java Classes implementation
Jadx-gui provides option to copy class as frida snippet.
In Frida, the Java.perform
function is a wrapper that ensures your script is executed within the context of a valid Java Virtual Machine (VM) thread. This is essential because certain operations, such as interacting with Java classes or calling methods, require access to the Java VM, which might not be immediately available when your script is loaded.
When you call Java.perform
, Frida:
Schedules your callback to run in a valid Java thread.
Ensures the current thread is attached to the Java VM if it isn't already.
Executes your code within the context of the valid thread.
Example Usage
Without Java.perform
If you attempt to run Java API code directly without Java.perform
in REPL or using a script file:
This will likely throw an error because the current thread might not be attached to the Java VM:
With Java.perform
Using Java.perform
, the script will ensure the thread is correctly attached:
Frida Script to hook Activity
->onResume
function to track which Activity class we are in:
Note: frida doesn't replace original function, it just intercepts function calls and allows you to modifiy behviour of this call. You can always access original function with this
keyword.
Why You Can Still Access the Original Function:
Overriding vs. Replacing: You're overriding the function's implementation, not replacing it entirely. The original method still exists, and you're allowed to call it within your custom hook.
this
keyword: Thethis
keyword refers to the current instance of theInterceptionFragment
class, which means you're invoking the original function (with the altered argument) through the same object. This keeps the rest of the object context intact.
Frida-Trace
a tool from frida toolkit to trace function calls from of loaded classes in memory and its return values.
Search for java class included by -j
and ignore java classes included by -J
.
(Format: classname!methodname)
using frida we can trace function calls in native libraries too (JNI)
Note: If in frida-trace you dont' see any output after clicking button. RUn frida-trace again as it loads new things in memory which was not loaded previously.
example time:
using frida-trace or jadx we can identify functions being called for a challenge.
You can see if dice roll is not equal to 5 then we will lose. let's hook randomDice
funciton here to return always 5.
Which allows us to win.
Last updated