Skip to content

Conversation

JerryTasi
Copy link
Collaborator

@JerryTasi JerryTasi commented Feb 26, 2025

Detect CWE-73 in Android Application

This scenario seeks to find External Control of File Name or Path in the APK file.

CWE-73 External Control of File Name or Path

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

See CWE-73 for more details.

image

Code of CWE-73 in ovaa.apk

We use the ovaa.apk sample to explain the vulnerability code of CWE-73.

image

CWE-73 Detection Process Using Quark Script API

image

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

First, we design a detection rule useLastPathSegmentAsFileName.json to spot behavior that uses the last path segment as the file name.

Second, we use the API methodInstance.getArguments() to get the argument for the file path and use quarkResultInstance.isHardcoded(argument) to check if the argument is hardcoded into the APK. If No, the argument is from external input.

Finally, we use Quark API quarkResultInstance.findMethodInCaller(callerMethod, targetMethod) to check if there are any APIs in the caller method for opening files. If YES, the APK performs file operations using external input as a path, which may cause CWE-73 vulnerability.

Quark Script: CWE-73.py

image

from quark.script import runQuarkAnalysis, Rule

SAMPLE_PATH = "ovaa.apk"
RULE_PATH = "useLastPathSegmentAsFileName.json"

OPEN_FILE_API = [
    "Landroid/os/ParcelFileDescriptor;",                   # Class name
    "open",                                                # Method name
    "(Ljava/io/File; I)Landroid/os/ParcelFileDescriptor;"  # Descriptor
]

ruleInstance = Rule(RULE_PATH)
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)

for accessExternalDir in quarkResult.behaviorOccurList:
    filePath = accessExternalDir.secondAPI.getArguments()[2]

    if quarkResult.isHardcoded(filePath):
        continue

    caller = accessExternalDir.methodCaller
    result = quarkResult.findMethodInCaller(caller, OPEN_FILE_API)

    if result:
        print("CWE-73 is detected in method, ", caller.fullName)

Quark Rule: useLastPathSegmentAsFileName.json

image

{
    "crime": "Use the last path segment as the file name",
    "permission": [],
    "api": [
        {
            "class": "Landroid/net/Uri;",
            "method": "getLastPathSegment",
            "descriptor": "()Ljava/lang/String;"
        },
        {
            "class": "Ljava/io/File;",
            "method": "<init>",
            "descriptor": "(Ljava/io/File;Ljava/lang/String;)V"
        }
    ],
    "score": 1,
    "label": []
}

Quark Script Result

$ python CWE-73.py
CWE-73 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;

@zinwang zinwang self-requested a review February 26, 2025 10:19
@zinwang zinwang self-assigned this Feb 26, 2025
@zinwang zinwang added documentation Improvements or additions to documentation pr-processing-state-05 labels Feb 26, 2025
Copy link

codecov bot commented Feb 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.68%. Comparing base (1696ff6) to head (2de1e08).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #735   +/-   ##
=======================================
  Coverage   80.68%   80.68%           
=======================================
  Files          75       75           
  Lines        6253     6253           
=======================================
  Hits         5045     5045           
  Misses       1208     1208           
Flag Coverage Δ
unittests 80.68% <ø> (ø)

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.

Copy link
Collaborator

@zinwang zinwang left a comment

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 a3abe84 into quark-engine:master Feb 26, 2025
18 checks passed
@JerryTasi JerryTasi deleted the patch-1 branch March 5, 2025 02:23
zinwang pushed a commit to zinwang/quark-engine that referenced this pull request Apr 1, 2025
* Optimize the document of Quark Script CWE-73

* Optimize the document of Quark Script CWE-73
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