Android Hooking: List Activities

Introduction

Android hooking is a technique used to intercept and modify the behavior of Android applications. It allows developers to gain control over the execution flow and manipulate various components of an app. In this article, we will focus on hooking activities in Android applications.

Activities are the fundamental building blocks of Android apps. They represent individual screens or user interfaces. By hooking activities, we can monitor their lifecycle events, modify their behavior, or even launch a different activity altogether.

Hooking Activities

To hook activities in an Android app, we can use a popular hooking framework called Xposed. Xposed provides a runtime environment that enables developers to inject code into the target application's process and intercept various events.

Setting up Xposed

First, we need to set up Xposed in our development environment. Follow these steps to get started:

  1. Download the Xposed framework from the official repository (
  2. Install the Xposed Installer app on your Android device or emulator.
  3. Launch the Xposed Installer app and go to the "Framework" section.
  4. Click on "Install/Update" to install the Xposed framework.
  5. Reboot your device or emulator to activate Xposed.

Writing a Hook

Once we have Xposed set up, we can start writing our hook to list activities in an Android app. In this example, we will intercept the onCreate() method of each activity and log its name.

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;

public class ActivityHooker implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (!lpparam.packageName.equals("com.example.targetapp"))
            return;

        XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                Activity activity = (Activity) param.thisObject;
                String activityName = activity.getClass().getName();
                Log.d("ActivityHooker", "Activity created: " + activityName);
            }
        });
    }
}

In the code above, we implement the IXposedHookLoadPackage interface provided by Xposed. Inside the handleLoadPackage() method, we check if the target application's package name matches our desired app (com.example.targetapp). Then, we use XposedHelpers.findAndHookMethod() to hook the onCreate() method of the Activity class. Finally, we log the name of the activity that is being created.

Building and Installing the Module

To compile our hook into a module, we need to create an Android project and add the Xposed framework as a dependency. Here are the steps to follow:

  1. Create a new Android project in your preferred IDE or use the command-line tools.
  2. Add the Xposed framework as a dependency in your project's build.gradle file:
dependencies {
    implementation 'de.robv.android.xposed:api:82'
}
  1. Copy the ActivityHooker class into your project's source folder.
  2. Build the project to generate an APK file.
  3. Install the APK file on your device or emulator.

Running and Verifying the Hook

Once the module is installed, launch the target application (com.example.targetapp). You should see log messages in your device's logcat console indicating the creation of activities:

D/ActivityHooker: Activity created: com.example.targetapp.MainActivity
D/ActivityHooker: Activity created: com.example.targetapp.SecondActivity

Congratulations! You have successfully hooked the activities in the target Android app.

Conclusion

Android hooking is a powerful technique that allows developers to manipulate the behavior of Android applications. In this article, we focused on hooking activities using the Xposed framework. We learned how to set up Xposed, write a hook to intercept the onCreate() method of activities, and log their names. By understanding and utilizing hooking techniques, developers can gain deeper insights into app behavior and customize it according to their needs.

With further exploration, you can use this knowledge to build more advanced hooks and modify other aspects of Android apps. Happy coding!