Skip to content

Commit cdceaba

Browse files
authored
Add Quark script showcase of detecting CWE-359 (#803)
* Optimize the document of Quark Script CWE-359 * Optimize the document of Quark Script CWE-359
1 parent 08e609e commit cdceaba

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

docs/source/quark_script.rst

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,3 +3378,130 @@ Quark Script Result
33783378
33793379
33803380
3381+
Detect CWE-359 in Android Application
3382+
--------------------------------------
3383+
3384+
This scenario aims to demonstrate the detection of the **Exposure of Private Personal Information to an Unauthorized Actor** vulnerability.
3385+
3386+
CWE-359: Exposure of Private Personal Information to an Unauthorized Actor
3387+
===========================================================================
3388+
3389+
We analyze the definition of CWE-359 and identify its characteristics.
3390+
3391+
See `CWE-359 <https://cwe.mitre.org/data/definitions/359.html>`_ for more details.
3392+
3393+
.. image:: https://i.postimg.cc/QxZcD3gb/image.png
3394+
3395+
Code of CWE-359 in ovaa.apk
3396+
============================
3397+
3398+
We use the `ovaa.apk <https://github.com/oversecured/ovaa>`_ sample to explain the vulnerability code of CWE-359.
3399+
3400+
.. image:: https://i.postimg.cc/LhKL2vvC/image.png
3401+
3402+
CWE-359 Detection Process Using Quark Script API
3403+
=================================================
3404+
3405+
.. image:: https://i.postimg.cc/8CB6ywzN/image.png
3406+
3407+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
3408+
3409+
To begin with, we create a detection rule named ``accessFileWithUnsafeUriPath.json`` to identify behavior that accesses a file with an unsafe path from ``Uri``.
3410+
3411+
Next, we use API ``methodInstance.methodCaller`` to retrieve the name of the caller that has this behavior.
3412+
3413+
Then, we use API ``quarkResultInstance.isHardcoded(argument)`` to check if the file path is hardcoded into the APK. If not, the file path is likely from external input.
3414+
3415+
After that, we use API ``getProviders(samplePath)`` and ``providerInstance.isExported()`` to check if there is any exported provider that matches the caller class name. If yes, any external application can access the behavior.
3416+
3417+
Finally, we use API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to search for any APIs in the caller method that are used to match strings.
3418+
3419+
If **NO** API is found, that implies the APK does not neutralize special elements within the argument, possibly resulting in a CWE-359 vulnerability.
3420+
3421+
Quark Script: CWE-359.py
3422+
=========================
3423+
3424+
.. image:: https://i.postimg.cc/76KT46zR/image.png
3425+
3426+
.. code-block:: python
3427+
3428+
from quark.script import Rule, runQuarkAnalysis, getProviders
3429+
3430+
SAMPLE_PATH = "ovaa.apk"
3431+
RULE_PATH = "accessFileWithUnsafeUriPath.json"
3432+
3433+
STRING_MATCHING_API = [
3434+
["Ljava/lang/String;", "contains", "(Ljava/lang/CharSequence)Z"],
3435+
["Ljava/lang/String;", "indexOf", "(I)I"],
3436+
["Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"],
3437+
["Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"],
3438+
[
3439+
"Ljava/lang/String;",
3440+
"replaceAll",
3441+
"(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;",
3442+
],
3443+
]
3444+
3445+
ruleInstance = Rule(RULE_PATH)
3446+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
3447+
3448+
exportedProviders = [
3449+
str(provider)
3450+
for provider in getProviders(SAMPLE_PATH)
3451+
if provider.isExported()
3452+
]
3453+
3454+
for behavior in quarkResult.behaviorOccurList:
3455+
caller = behavior.methodCaller
3456+
classNameInJavaFormat = caller.className.replace("/", ".")[1:-1]
3457+
filePath = behavior.secondAPI.getArguments()[2]
3458+
3459+
if quarkResult.isHardcoded(filePath):
3460+
continue
3461+
3462+
if classNameInJavaFormat not in exportedProviders:
3463+
continue
3464+
3465+
if not any(
3466+
quarkResult.findMethodInCaller(caller, api)
3467+
for api in STRING_MATCHING_API
3468+
):
3469+
print(f"CWE-359 is detected in method, {caller.fullName}")
3470+
3471+
Quark Rule: accessFileWithUnsafeUriPath.json
3472+
=============================================
3473+
3474+
.. image:: https://i.postimg.cc/kGDRgmFg/image.png
3475+
3476+
.. code-block:: json
3477+
3478+
{
3479+
"crime": "Access a File with an unsafe path from Uri",
3480+
"permission": [],
3481+
"api": [
3482+
{
3483+
"class": "Landroid/net/Uri;",
3484+
"method": "getLastPathSegment",
3485+
"descriptor": "()Ljava/lang/String;"
3486+
},
3487+
{
3488+
"class": "Ljava/io/File;",
3489+
"method": "<init>",
3490+
"descriptor": "(Ljava/io/File;Ljava/lang/String;)V"
3491+
}
3492+
],
3493+
"score": 1,
3494+
"label": []
3495+
}
3496+
3497+
Quark Script Result
3498+
====================
3499+
3500+
.. code-block:: TEXT
3501+
3502+
$ python3 CWE-359.py
3503+
CWE-359 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
3504+
3505+
3506+
3507+

0 commit comments

Comments
 (0)