# Exploiting a debuggeable application

## 利用可调试应用程序

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFT**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## **绕过root和可调试检查**

本文节选自文章[**https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0**](https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0)

### 使Android应用程序可调试并绕过检查的步骤

#### **使应用程序可调试**

内容基于<https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0>

1. **反编译APK：**

* 使用APK-GUI工具对APK进行反编译。
* 在\_android-manifest\_文件中插入`android:debuggable=true`以启用调试模式。
* 重新编译、签名和对修改后的应用程序进行zipalign。

2. **安装修改后的应用程序：**

* 使用命令：`adb install <application_name>`。

3. **检索包名：**

* 执行`adb shell pm list packages –3`以列出第三方应用程序并找到包名。

4. **设置应用程序等待调试器连接：**

* 命令：`adb shell am setup-debug-app –w <package_name>`。
* **注意：** 必须在启动应用程序之前每次运行此命令，以确保应用程序等待调试器。
* 要持久化，使用`adb shell am setup-debug-app –w -–persistent <package_name>`。
* 要删除所有标志，使用`adb shell am clear-debug-app <package_name>`。

5. **在Android Studio中准备调试：**

* 在Android Studio中导航至\_File -> Open Profile or APK\_。
* 打开重新编译后的APK。

6. **在关键Java文件中设置断点：**

* 在`MainActivity.java`（特别是在`onCreate`方法中）、`b.java`和`ContextWrapper.java`中设置断点。

#### **绕过检查**

应用程序在某些点会验证是否可调试，并检查是否存在指示设备已root的二进制文件。可以使用调试器修改应用程序信息、取消调试标志并更改搜索二进制文件的名称以绕过这些检查。

对于可调试检查：

1. **修改标志设置：**

* 在调试器控制台的变量部分中导航至：`this mLoadedAPK -> mApplicationInfo -> flags = 814267974`。
* **注意：** `flags = 814267974`的二进制表示是`11000011100111011110`，表示“Flag\_debuggable”已激活。

![https://miro.medium.com/v2/resize:fit:1400/1\*-ckiSbWGSoc1beuxxpKbow.png](https://miro.medium.com/v2/resize:fit:1400/1*-ckiSbWGSoc1beuxxpKbow.png)

这些步骤共同确保应用程序可以进行调试，并且可以使用调试器绕过某些安全检查，从而更深入地分析或修改应用程序的行为。

第2步涉及将标志值更改为814267972，其二进制表示为110000101101000000100010100。

## **利用漏洞**

演示使用一个包含按钮和文本视图的易受攻击应用程序。最初，应用程序显示“Crack Me”。目标是在运行时将消息从“Try Again”更改为“Hacked”，而无需修改源代码。

### **检查漏洞**

* 使用`apktool`对应用程序进行反编译，以访问`AndroidManifest.xml`文件。
* 在AndroidManifest.xml中存在`android_debuggable="true"`表明应用程序是可调试的且易受攻击。
* 值得注意的是，`apktool`仅用于检查可调试状态，而不修改任何代码。

### **准备设置**

* 过程涉及启动模拟器、安装易受攻击的应用程序，并使用`adb jdwp`识别正在侦听的Dalvik VM端口。
* JDWP（Java调试线协议）允许通过公开唯一端口调试在VM中运行的应用程序。
* 远程调试需要端口转发，然后将JDB附加到目标应用程序。

### **在运行时注入代码**

* 通过设置断点和控制应用程序流程来执行利用。
* 使用`classes`和`methods <class_name>`等命令来揭示应用程序的结构。
* 在`onClick`方法设置断点，并控制其执行。
* 使用`locals`、`next`和`set`命令来检查和修改局部变量，特别是将“Try Again”消息更改为“Hacked”。
* 使用`run`命令执行修改后的代码，成功实时更改应用程序的输出。

此示例演示了如何操纵可调试应用程序的行为，突显了更复杂利用的潜力，例如在应用程序上下文中获取设备上的shell访问权限。

### 参考资料

* <https://medium.com/@shubhamsonani/hacking-with-precision-bypass-techniques-via-debugger-in-android-apps-27fd562b2cc0>
* <https://resources.infosecinstitute.com/android-hacking-security-part-6-exploiting-debuggable-android-applications>

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFT**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/mobile-pentesting/android-app-pentesting/exploiting-a-debuggeable-applciation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
