Android System Securing, Hardening, & Privacy

Workshop: Bypassing and Emulating Android Permission Prompts for Automated Security Testing

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction

Automated security testing of Android applications is a critical component of modern DevSecOps pipelines. However, a significant hurdle often encountered by security researchers and developers alike is the unpredictable nature of Android’s runtime permission prompts. These system-level dialogs, designed to safeguard user privacy, can introduce flakiness and complexity into automated tests, making it challenging to ensure consistent and reliable security assessments. This workshop delves into expert-level strategies for effectively bypassing and emulating Android permission prompts, allowing for seamless and robust automated security testing without compromising the integrity of your test suite or requiring manual intervention.

We will explore practical techniques, from leveraging Android Debug Bridge (ADB) commands to manipulate permission states directly, to employing advanced mocking frameworks within your test code to simulate permission grants. Our focus is on customizing Android’s permission model for specific testing use cases, enabling testers to gain full control over the testing environment and focus on the application’s security logic rather than battling UI prompts.

Android’s Runtime Permission Landscape

Before Android 6.0 (API Level 23), permissions were granted at install time. With the introduction of runtime permissions, applications targeting API Level 23 or higher must explicitly request sensitive permissions (e.g., Camera, Location, Contacts) from the user at the point of use. This change dramatically enhanced user privacy but introduced a new layer of complexity for automated testing.

Key aspects of this model include:

  • User Consent: Users must explicitly grant or deny permissions through system dialogs.
  • Permission Groups: Permissions are often categorized into groups (e.g., ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION are in the Location group). Granting one permission in a group often grants others in the same group.
  • targetSdkVersion Impact: Apps with targetSdkVersion < 23 behave as if they are pre-M, meaning permissions are granted at install time. However, running such an app on a post-M device might still have limitations if the user manually revokes permissions.

Traditional UI automation tools like Appium or UI Automator can interact with these dialogs, but their system-level nature makes them prone to timing issues, unexpected UI variations across device manufacturers, and general flakiness, rendering test results unreliable.

Strategy 1: Bypassing Prompts via ADB Shell Commands

The most direct and widely used method for bypassing runtime permission prompts in automated tests is through the Android Debug Bridge (ADB). ADB provides powerful shell commands to interact with a connected device or emulator, including the ability to grant or revoke application permissions programmatically.

The pm grant and pm revoke Utility

The package manager (pm) utility within ADB allows direct manipulation of application permissions. This is invaluable for setting up a test environment without triggering UI prompts.

To grant a permission:

adb shell pm grant <PACKAGE_NAME> <PERMISSION_NAME>

For example, to grant camera and contacts permissions to an app with the package name com.example.myapp:

adb shell pm grant com.example.myapp android.permission.CAMERA adb shell pm grant com.example.myapp android.permission.READ_CONTACTS

To revoke a permission (useful for cleanup or testing permission denial scenarios):

adb shell pm revoke <PACKAGE_NAME> <PERMISSION_NAME>

For instance, revoking camera permission:

adb shell pm revoke com.example.myapp android.permission.CAMERA

Integrating adb into Automated Test Workflows

This approach seamlessly integrates into continuous integration (CI) pipelines and various testing frameworks. A typical workflow would involve:

  1. Build and Install: Compile your application’s APK and install it on the target device/emulator.
  2. Grant Permissions: Before executing your test suite, use adb shell pm grant for all necessary permissions.
  3. Execute Tests: Run your automated tests, which will now find the required permissions already granted.
  4. Cleanup (Optional but Recommended): After tests, you might revoke permissions using adb shell pm revoke to restore a clean state or test permission denial scenarios.

Here’s a simplified bash script example for a pre-test setup:

#!/bin/bash PACKAGE_NAME="com.example.myapp" # List of permissions to grant PERMISSIONS=("android.permission.CAMERA" "android.permission.READ_CONTACTS" "android.permission.ACCESS_FINE_LOCATION") echo "Installing $PACKAGE_NAME..." adb install app-debug.apk if [ $? -ne 0 ]; then echo "App installation failed. Exiting." exit 1 fi echo "Granting permissions for $PACKAGE_NAME..." for PERM in "${PERMISSIONS[@]}"; do echo "  Granting $PERM..." adb shell pm grant $PACKAGE_NAME $PERM if [ $? -ne 0 ]; then echo "    Failed to grant $PERM. Continuing..." else echo "    $PERM granted." fi done echo "Permissions granted. Proceeding with tests." # Your test execution command goes here # e.g., adb shell am instrument -w -r -e debug false com.example.myapp.test/androidx.test.runner.AndroidJUnitRunner # Optional: Revoke permissions after tests for PERM in "${PERMISSIONS[@]}"; do # adb shell pm revoke $PACKAGE_NAME $PERM done

Limitations: While powerful, adb shell pm grant primarily works for runtime permissions. Some system-level permissions or `appops` (application operations) might require root access or specific device configurations.

Strategy 2: Emulating Permission States with Mocking Frameworks

For unit and isolated integration tests, directly manipulating device permissions might be overkill or impossible (e.g., in a JVM-based test environment without an actual Android device). Here,

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner