Skip to content

Add MASTG-TEST-0264, MASTG-TEST-0265, MASTG-DEMO-0038, MASTG-DEMO-0039 #3246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0038/MASTG-DEMO-0038.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ title: Detecting StrictMode Uses with Frida
id: MASTG-DEMO-0038
code: [kotlin]
test: MASTG-TEST-0264
status: draft
note: This demo shows how to detect the use of StrictMode at runtime using Frida.
status: new
---

### Sample

This sample demonstrates the detection of `StrictMode` uses at runtime using Frida. The app enables a `StrictMode` policy to detect leaked SQLite objects and intentionally leaves a cursor unclosed to trigger the policy.

{{ ../MASTG-DEMO-0037/MastgTest.kt }}

### Steps

1. Install the app on a device (@MASTG-TECH-0005)
2. Make sure you have @MASTG-TOOL-0001 installed on your machine and the frida-server running on the device
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. Make sure you have @MASTG-TOOL-0001 installed on your machine and the frida-server running on the device
2. Make sure you have @MASTG-TOOL-0031 installed on your machine and the frida-server running on the device

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: shouldn't this (eventually?) be a Technique?

3. Run `run.sh` to spawn the app with Frida
4. Click the **Start** button
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
4. Click the **Start** button
4. Click the **Start** button in the app

5. Stop the script by pressing `Ctrl+C`

{{ run.sh # script.js }}

### Observation

The Frida script output reveals the runtime usage of `StrictMode`.

{{ output.txt }}

### Evaluation

The test fails because the Frida script output shows the runtime usage of `StrictMode`, specifically:

- `StrictMode.VmPolicy.Builder.penaltyLog`
- `StrictMode.setVmPolicy`
30 changes: 30 additions & 0 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0038/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

[+] Frida script loaded to detect StrictMode usage and penaltyLog calls.


[*] StrictMode.VmPolicy.Builder.penaltyLog() called


Backtrace:
android.os.StrictMode$VmPolicy$Builder.penaltyLog(Native Method)
android.os.StrictMode$VmPolicy$Builder.build(StrictMode.java:1226)
android.os.StrictMode.initVmDefaults(StrictMode.java:1522)
android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844)
android.app.ActivityThread.handleBindApplication(Native Method)
android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
android.os.Handler.dispatchMessage(Handler.java:106)

[*] StrictMode.setVmPolicy() called


Backtrace:
android.os.StrictMode.setVmPolicy(Native Method)
android.os.StrictMode.initVmDefaults(StrictMode.java:1522)
android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844)
android.app.ActivityThread.handleBindApplication(Native Method)
android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
android.os.Handler.dispatchMessage(Handler.java:106)
android.os.Looper.loopOnce(Looper.java:205)
Policy: [StrictMode.VmPolicy; mask=1082130464]
2 changes: 2 additions & 0 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0038/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
frida -U -f org.owasp.mastestapp -l ./script.js -o output.txt
40 changes: 40 additions & 0 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0038/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Java.perform(() => {

// Function to print backtrace with a configurable number of lines (default: 8)
function printBacktrace(maxLines = 8) {
let Exception = Java.use("java.lang.Exception");
let stackTrace = Exception.$new().getStackTrace().toString().split(",");

console.log("\nBacktrace:");
for (let i = 0; i < Math.min(maxLines, stackTrace.length); i++) {
console.log(stackTrace[i]);
}
}

// Hook StrictMode.setVmPolicy
let StrictMode = Java.use('android.os.StrictMode');

StrictMode.setVmPolicy.implementation = function (policy) {
console.log("\n[*] StrictMode.setVmPolicy() called\n");

// Java stack trace
printBacktrace();

console.log("Policy: " + policy);
this.setVmPolicy(policy);
};

// Hook StrictMode.VmPolicy.Builder.penaltyLog
let VmPolicyBuilder = Java.use('android.os.StrictMode$VmPolicy$Builder');

VmPolicyBuilder.penaltyLog.implementation = function () {
console.log("\n[*] StrictMode.VmPolicy.Builder.penaltyLog() called\n");

// Java stack trace
printBacktrace();

return this.penaltyLog();
};

console.log("\n[+] Frida script loaded to detect StrictMode usage and penaltyLog calls.\n");
});
29 changes: 27 additions & 2 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0039/MASTG-DEMO-0039.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ title: Detecting StrictMode PenaltyLog Usage with Semgrep
id: MASTG-DEMO-0039
code: [kotlin]
test: MASTG-TEST-0265
status: draft
note: This demo shows how to detect the use of StrictMode in the codebase using Semgrep.
status: new
---

### Sample

This sample demonstrates the detection of `StrictMode` uses at runtime using Frida. The app enables a `StrictMode` policy to detect leaked SQLite objects and intentionally leaves a cursor unclosed to trigger the policy.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This sample demonstrates the detection of `StrictMode` uses at runtime using Frida. The app enables a `StrictMode` policy to detect leaked SQLite objects and intentionally leaves a cursor unclosed to trigger the policy.
This sample demonstrates the static detection of `StrictMode` in the app using Semgrep. The app enables a `StrictMode` policy to detect leaked SQLite objects and intentionally leaves a cursor unclosed to trigger the policy.


{{ ../MASTG-DEMO-0037/MastgTest.kt # MastgTest_reversed.java }}

### Steps

Let's run @MASTG-TOOL-0110 rules against the sample code.

{{ ../../../../rules/mastg-android-strictmode.yml }}

{{ run.sh }}
Comment on lines +18 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a step missing? Since you run the semgrep rule against java code, I guess the app needs to be decompiled?

But since decompiling does not work so reliably, why not run a semgrep rule against the smali code?


### Observation

The output shows all usages of APIs related to `StrictMode.setVmPolicy`.

{{ output.txt }}

### Evaluation

The test fails because the output shows usages of `StrictMode` APIs, specifically:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we were only looking for the setVmPolicy, the text sounds a bit off?


- `StrictMode.setVmPolicy`
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.owasp.mastestapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.StrictMode;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;

/* compiled from: MastgTest.kt */
@Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\b\u0010\u0005\u001a\u00020\u0006H\u0002J\u0006\u0010\u0007\u001a\u00020\bJ\b\u0010\t\u001a\u00020\u0006H\u0002R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\n"}, d2 = {"Lorg/owasp/mastestapp/MastgTest;", "", "context", "Landroid/content/Context;", "(Landroid/content/Context;)V", "enableStrictMode", "", "mastgTest", "", "triggerSqliteCursorLeak", "app_debug"}, k = 1, mv = {1, 9, 0}, xi = 48)
/* loaded from: classes4.dex */
public final class MastgTest {
public static final int $stable = 8;
private final Context context;

public MastgTest(Context context) {
Intrinsics.checkNotNullParameter(context, "context");
this.context = context;
}

public final String mastgTest() {
enableStrictMode();
triggerSqliteCursorLeak();
System.gc();
return "SUCCESS!!\n\nSQL Cursor leaked.";
}

private final void enableStrictMode() {
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedClosableObjects().penaltyLog().build());
}

private final void triggerSqliteCursorLeak() {
SQLiteDatabase db = this.context.openOrCreateDatabase("test.db", 0, null);
Intrinsics.checkNotNullExpressionValue(db, "openOrCreateDatabase(...)");
db.execSQL("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");
db.execSQL("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
db.rawQuery("SELECT * FROM users", null);
}
}
13 changes: 13 additions & 0 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0039/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


┌────────────────┐
│ 1 Code Finding │
└────────────────┘

MastgTest_reversed.java
❯❱ rules.mastg-android-strictmode
[MASVS-RESILIENCE] Detected usage of StrictMode

29┆ StrictMode.setVmPolicy(new
StrictMode.VmPolicy.Builder().detectLeakedClosableObjects().penaltyLog().build());

2 changes: 2 additions & 0 deletions demos/android/MASVS-RESILIENCE/MASTG-DEMO-0039/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
NO_COLOR=true semgrep -c ../../../../rules/mastg-android-strictmode.yml ./MastgTest_reversed.java > output.txt
10 changes: 10 additions & 0 deletions rules/mastg-android-strictmode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
- id: mastg-android-strictmode
severity: WARNING
languages:
- java
metadata:
summary: This rule scans uses of StrictMode.
message: "[MASVS-RESILIENCE] Detected usage of StrictMode"
patterns:
- pattern: StrictMode.setVmPolicy(...)
21 changes: 19 additions & 2 deletions tests-beta/android/MASVS-RESILIENCE/MASTG-TEST-0264.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ id: MASTG-TEST-0264
type: [dynamic]
weakness: MASWE-0094
best-practices: []
status: draft
note: This test checks whether the app uses StrictMode by placing relevant hooks to detect the use of StrictMode APIs, such as StrictMode.setVmPolicy and StrictMode.VmPolicy.Builder.penaltyLog().
status: new
---

## Overview

This test checks whether the app uses `StrictMode` by dynamically analyzing the app's behavior and placing relevant hooks to detect the use of `StrictMode` APIs, such as `StrictMode.setVmPolicy` and `StrictMode.VmPolicy.Builder.penaltyLog`.

While `StrictMode` is useful for developers to log policy violations such as disk I/O or network operations in production apps, it can expose sensitive implementation details in the logs that could be exploited by attackers.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
While `StrictMode` is useful for developers to log policy violations such as disk I/O or network operations in production apps, it can expose sensitive implementation details in the logs that could be exploited by attackers.
While `StrictMode` is useful for developers to log policy violations such as disk I/O or network operations during development, it can expose sensitive implementation details in the logs that could be exploited by attackers.

I know we have the same text in the existing test, but I think it sounds a bit confusing.


## Steps

1. Run a dynamic analysis tool like @MASTG-TOOL-0039 and look for uses of `StrictMode` APIs.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Run a dynamic analysis tool like @MASTG-TOOL-0039 and look for uses of `StrictMode` APIs.
1. Run a dynamic analysis tool like @MASTG-TOOL-0031 and look for uses of `StrictMode` APIs.


## Observation

The output should show the runtime usage of `StrictMode` APIs.

## Evaluation

The test fails if the Frida script output shows the runtime usage of `StrictMode` APIs.
20 changes: 18 additions & 2 deletions tests-beta/android/MASVS-RESILIENCE/MASTG-TEST-0265.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ id: MASTG-TEST-0265
type: [static]
weakness: MASWE-0094
best-practices: []
status: draft
note: This test checks whether the app uses StrictMode APIs, which can expose sensitive implementation details in the logs.
status: new
---

## Overview

This test checks whether the app uses `StrictMode`, which while useful for developers to log policy violations such as disk I/O or network operations in production apps, can expose sensitive implementation details in the logs that could be exploited by attackers.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This test checks whether the app uses `StrictMode`, which while useful for developers to log policy violations such as disk I/O or network operations in production apps, can expose sensitive implementation details in the logs that could be exploited by attackers.
This test checks whether the app uses `StrictMode`. While useful for developers to log policy violations such as disk I/O or network operations during development, it can expose sensitive implementation details in the logs that could be exploited by attackers.


## Steps

1. Use @MASTG-TOOL-0110 to identify all instances of `StrictMode`
APIs.
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to reference a technique here?


## Observation

The output should identify all instances of `StrictMode` usage in the app.

## Evaluation

The test fails if the app uses `StrictMode` APIs.