Skip to content

Commit a3abe84

Browse files
authored
Optimize the document of Quark Script CWE-73 (#735)
* Optimize the document of Quark Script CWE-73 * Optimize the document of Quark Script CWE-73
1 parent 1696ff6 commit a3abe84

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

docs/source/quark_script.rst

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,8 +2254,9 @@ Quark Script Result
22542254
CWE-925 is detected in method, Lcom/android/insecurebankv2/MyBroadCastReceiver;
22552255
22562256
2257+
22572258
Detect CWE-73 in Android Application
2258-
-------------------------------------
2259+
--------------------------------------
22592260

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

@@ -2266,36 +2267,43 @@ We analyze the definition of CWE-73 and identify its characteristics.
22662267

22672268
See `CWE-73 <https://cwe.mitre.org/data/definitions/73.html>`_ for more details.
22682269

2269-
.. image:: https://imgur.com/ES7xg5X.png
2270+
.. image:: https://imgur.com/I1C5yku.png
22702271

22712272
Code of CWE-73 in ovaa.apk
22722273
===========================
22732274

22742275
We use the `ovaa.apk <https://github.com/oversecured/ovaa>`_ sample to explain the vulnerability code of CWE-73.
22752276

2276-
.. image:: https://imgur.com/9oa1HIC.png
2277+
.. image:: https://imgur.com/gLJ6zWr.png
22772278

2278-
Quark Script: CWE-73.py
2279-
=======================
2279+
CWE-73 Detection Process Using Quark Script API
2280+
================================================
22802281

2281-
Let’s use the above APIs to show how Quark script find this vulnerability.
2282+
.. image:: https://imgur.com/zGjZHA1.png
22822283

2283-
First, we design a detection rule ``accessFileInExternalDir.json`` to spot behavior accessing a file in an external directory.
2284+
Let’s use the above APIs to show how Quark script finds this vulnerability.
22842285

2285-
Second, we use 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.
2286+
First, we design a detection rule ``useLastPathSegmentAsFileName.json`` to spot behavior that uses the last path segment as the file name.
22862287

2287-
Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to check if 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.
2288+
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.
22882289

2289-
.. code:: python
2290+
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.
2291+
2292+
Quark Script: CWE-73.py
2293+
========================
2294+
2295+
.. image:: https://imgur.com/EHrcCPg.png
2296+
2297+
.. code-block:: python
22902298
22912299
from quark.script import runQuarkAnalysis, Rule
22922300
22932301
SAMPLE_PATH = "ovaa.apk"
2294-
RULE_PATH = "accessFileInExternalDir.json"
2302+
RULE_PATH = "useLastPathSegmentAsFileName.json"
22952303
22962304
OPEN_FILE_API = [
22972305
"Landroid/os/ParcelFileDescriptor;", # Class name
2298-
"open", # Method name
2306+
"open", # Method name
22992307
"(Ljava/io/File; I)Landroid/os/ParcelFileDescriptor;" # Descriptor
23002308
]
23012309
@@ -2304,7 +2312,7 @@ Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod,
23042312
23052313
for accessExternalDir in quarkResult.behaviorOccurList:
23062314
filePath = accessExternalDir.secondAPI.getArguments()[2]
2307-
2315+
23082316
if quarkResult.isHardcoded(filePath):
23092317
continue
23102318
@@ -2313,20 +2321,22 @@ Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod,
23132321
23142322
if result:
23152323
print("CWE-73 is detected in method, ", caller.fullName)
2316-
2317-
Quark Rule: accessFileInExternalDir.json
2318-
=========================================
2324+
2325+
Quark Rule: useLastPathSegmentAsFileName.json
2326+
==============================================
2327+
2328+
.. image:: https://imgur.com/JxBdde0.png
23192329

23202330
.. code-block:: json
23212331
23222332
{
2323-
"crime": "Access a file in an external directory",
2333+
"crime": "Use the last path segment as the file name",
23242334
"permission": [],
23252335
"api": [
23262336
{
2327-
"class": "Landroid/os/Environment;",
2328-
"method": "getExternalStorageDirectory",
2329-
"descriptor": "()Ljava/io/File;"
2337+
"class": "Landroid/net/Uri;",
2338+
"method": "getLastPathSegment",
2339+
"descriptor": "()Ljava/lang/String;"
23302340
},
23312341
{
23322342
"class": "Ljava/io/File;",
@@ -2339,13 +2349,13 @@ Quark Rule: accessFileInExternalDir.json
23392349
}
23402350
23412351
Quark Script Result
2342-
=====================
2352+
====================
23432353

23442354
.. code-block:: TEXT
23452355
2346-
$ python CWE-73.py
2347-
CWE-73 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
2348-
2356+
$ python CWE-73.py
2357+
CWE-73 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
2358+
23492359
23502360
23512361
Detect CWE-78 in Android Application

0 commit comments

Comments
 (0)