Skip to content

Optimize the document of Quark Script CWE-502#769

Merged
zinwang merged 2 commits into
masterfrom
JerryTasi-patch-CWE502
Jun 25, 2025
Merged

Optimize the document of Quark Script CWE-502#769
zinwang merged 2 commits into
masterfrom
JerryTasi-patch-CWE502

Conversation

@JerryTasi

@JerryTasi JerryTasi commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

Detect CWE-502 in Android Application

This scenario seeks to find Deserialization of Untrusted Data in the APK file.

CWE-502: Deserialization of Untrusted Data

We analyze the definition of CWE-502 and identify its characteristics.

See CWE-502 for more details.

image

Code of CWE-502 in pivaa.apk

We use the pivaa.apk sample to explain the vulnerability code of CWE-502.

image

CWE-502 Detection Process Using Quark Script API

image

Let’s use the above APIs to show how the Quark script finds this vulnerability.

To begin with, we created a detection rule named deserializeData.json to identify behaviors that deserialize data.

Next, we retrieve the methods that interact with the deserialization API. Following this, we check if the methods match any APIs for verifying data.

If NO, it could imply that the APK deserializes the untrusted data, potentially leading to a CWE-502 vulnerability.

Quark Script CWE-502.py

image

from quark.script import runQuarkAnalysis, Rule

SAMPLE_PATH = "pivaa.apk"
RULE_PATH = "deserializeData.json"

ruleInstance = Rule(RULE_PATH)

result = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)

verificationApis = [
    ["Ljava/io/File;", "exists", "()Z"],
    ["Landroid/content/Context;", "getFilesDir", "()Ljava/io/File;"],
    ["Landroid/content/Context;", "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;"],
    ["Landroid/os/Environment;", "getExternalStorageDirectory", "()Ljava/io/File;"],
]

for dataDeserialization in result.behaviorOccurList:
    apis = dataDeserialization.getMethodsInArgs()
    caller = dataDeserialization.methodCaller
    if not any(api in apis for api in verificationApis):
        print(f"CWE-502 is detected in method, {caller.fullName}")

Quark Rule: deserializeData.json

image

{
    "crime": "Deserialize Data",
    "permission": [],
    "api": [

        {
            "class": "Ljava/io/ObjectInputStream;",
            "method": "<init>",
            "descriptor": "(Ljava/io/InputStream;)V"
        },
        {
            "class": "Ljava/io/ObjectInputStream;",
            "method": "readObject",
            "descriptor": "()Ljava/lang/Object;"
        }

    ],
    "score": 1,
    "label": []
}

Quark Script Result

$ python CWE-502.py
CWE-502 is detected in method, Lcom/htbridge/pivaa/handlers/ObjectSerialization; loadObject ()V

@codecov

codecov Bot commented Jun 25, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.73%. Comparing base (989197a) to head (a291b4d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #769   +/-   ##
=======================================
  Coverage   80.73%   80.73%           
=======================================
  Files          75       75           
  Lines        6265     6265           
=======================================
  Hits         5058     5058           
  Misses       1207     1207           
Flag Coverage Δ
unittests 80.73% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zinwang zinwang self-requested a review June 25, 2025 03:38
@zinwang zinwang added documentation Improvements or additions to documentation pr-processing-state-05 labels Jun 25, 2025

@zinwang zinwang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@zinwang zinwang merged commit cfee070 into master Jun 25, 2025
19 checks passed
@JerryTasi JerryTasi deleted the JerryTasi-patch-CWE502 branch July 1, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation pr-processing-state-05

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants