Skip to content

Commit 849c27f

Browse files
authored
Optimize CWE-22 document (ev-flow#633)
* Optimize CWE 22 document * Optimize CWE 22 document
1 parent e76f950 commit 849c27f

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

docs/source/quark_script.rst

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,22 +1658,38 @@ Quark Script Result
16581658
$ python3 CWE-489.py
16591659
CWE-489 is detected in pivaa.apk
16601660
1661-
Detect CWE-22 in Android Application (ovaa.apk and InsecureBankv2.apk )
1662-
-----------------------------------------------------------------------
1663-
This scenario seeks to find **the improper limitation of a pathname to a restricted directory ('Path Traversal')**. See `CWE-22 <https://cwe.mitre.org/data/definitions/22.html>`_ for more details.
1661+
Detect CWE-22 in Android Application
1662+
----------------------------------------
16641663

1665-
Let’s use `ovaa.apk <https://github.com/oversecured/ovaa>`_, `InsecureBankv2.apk <https://github.com/dineshshetty/Android-InsecureBankv2/releases>`_, and the above APIs to show how the Quark script finds this vulnerability.
1664+
This scenario seeks to find **the improper limitation of a pathname to a restricted directory (‘Path Traversal’)**.
16661665

1667-
First, we design a detection rule ``accessFileInExternalDir.json`` to spot behavior accessing a file in an external directory.
1666+
CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
1667+
=========================================================================================
16681668

1669-
Next, 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.
1669+
We analyze the definition of CWE-22 and identify its characteristics.
16701670

1671-
Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to check if there are any APIs in the caller method for string matching. If **NO**, the APK does not neutralize special elements within the argument, which may cause CWE-22 vulnerability.
1671+
See `CWE-22 <https://cwe.mitre.org/data/definitions/22.html>`_ for more details.
16721672

1673-
Quark Script CWE-22.py
1674-
=======================
1673+
.. image:: https://imgur.com/agRPwp8.png
1674+
1675+
Code of CWE-22 in ovaa.apk
1676+
===============================
1677+
1678+
We use the `ovaa.apk <https://github.com/oversecured/ovaa>`_ sample to explain the vulnerability code of CWE-22.
16751679

1676-
The Quark Script below uses ovaa.apk to demonstrate. You can change the ``SAMPLE_PATH`` to the sample you want to detect. For example, ``SAMPLE_PATH = InsecureBankv2.apk``.
1680+
.. image:: https://imgur.com/WFpfzFk.png
1681+
1682+
1683+
Quark Scipt: CWE-22.py
1684+
=========================
1685+
1686+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
1687+
1688+
First, we design a detection rule ``accessFileInExternalDir.json`` to spot behavior accessing a file in an external directory.
1689+
1690+
Next, 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.
1691+
1692+
Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to check if there are any APIs in the caller method for string matching. If NO, the APK does not neutralize special elements within the argument, which may cause CWE-22 vulnerability.
16771693

16781694
.. code-block:: python
16791695
@@ -1695,20 +1711,20 @@ The Quark Script below uses ovaa.apk to demonstrate. You can change the ``SAMPLE
16951711
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
16961712
16971713
for accessExternalDir in quarkResult.behaviorOccurList:
1698-
16991714
filePath = accessExternalDir.secondAPI.getArguments()[2]
17001715
17011716
if quarkResult.isHardcoded(filePath):
1702-
continue
1717+
continue
17031718
17041719
caller = accessExternalDir.methodCaller
17051720
strMatchingAPIs = [
1706-
api for api in STRING_MATCHING_API if quarkResult.findMethodInCaller(
1707-
caller, api)
1721+
api
1722+
for api in STRING_MATCHING_API
1723+
if quarkResult.findMethodInCaller(caller, api)
17081724
]
17091725
17101726
if not strMatchingAPIs:
1711-
print(f"CWE-22 is detected in method, {caller.fullName}")
1727+
print(f"CWE-22 is detected in method, {caller.fullName}")
17121728
17131729
Quark Rule: accessFileInExternalDir.json
17141730
=========================================
@@ -1734,23 +1750,14 @@ Quark Rule: accessFileInExternalDir.json
17341750
"label": []
17351751
}
17361752
1737-
17381753
Quark Script Result
17391754
======================
1740-
- **ovaa.apk**
17411755

17421756
.. code-block:: TEXT
17431757
17441758
$ python3 CWE-22.py
17451759
CWE-22 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
17461760
1747-
- **InsecureBankv2.apk**
1748-
1749-
.. code-block:: TEXT
1750-
1751-
$ python3 CWE-22.py
1752-
CWE-22 is detected in method, Lcom/android/insecurebankv2/ViewStatement; onCreate (Landroid/os/Bundle;)V
1753-
17541761
Detect CWE-23 in Android Application (ovaa.apk and InsecureBankv2.apk )
17551762
-----------------------------------------------------------------------
17561763
This scenario aims to demonstrate the detection of the **Relative Path Traversal** vulnerability using `ovaa.apk <https://github.com/oversecured/ovaa>`_ and `InsecureBankv2.apk <https://github.com/dineshshetty/Android-InsecureBankv2/releases>`_. See `CWE-23 <https://cwe.mitre.org/data/definitions/23.html>`_ for more details.

0 commit comments

Comments
 (0)