Skip to content

Commit 5d8d2ad

Browse files
authored
Add quark script for CWE-88 (ev-flow#503)
* Update quark_script.rst * Update quark_script.rst * Update quark_script.rst * Update quark_script.rst * Update quark_script.rst * Update quark_script.rst * Update quark_script.rst * Update quark_script.rst
1 parent af6a293 commit 5d8d2ad

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

docs/source/quark_script.rst

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,3 +1739,96 @@ Quark Script Result
17391739
17401740
$ python CWE-338.py
17411741
CWE-338 is detected in Lcom/htbridge/pivaa/EncryptionActivity$2; onClick (Landroid/view/View;)V
1742+
1743+
1744+
1745+
Detect CWE-88 in Android Application (Vuldroid.apk)
1746+
------------------------------------------------------
1747+
1748+
This scenario seeks to find **Improper Neutralization of Argument Delimiters in a Command**. See `CWE-88 <https://cwe.mitre.org/data/definitions/88.html>`_ for more details.
1749+
1750+
Let‘s use this `APK <https://github.com/jaiswalakshansh/Vuldroid>`_ and the above APIs to show how the Quark script finds this vulnerability.
1751+
1752+
First, we design a detection rule ``ExternalStringsCommands.json`` to spot on behavior using external strings as commands.
1753+
1754+
Next, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to check if any APIs in the caller method for string matching.
1755+
1756+
If NO, the APK does not neutralize special elements within the argument, which may cause CWE-88 vulnerability.
1757+
1758+
If YES, check if there are any delimiters used in string matching for a filter. If NO, the APK does not neutralize special elements within the argument, which may cause CWE-88 vulnerability.
1759+
1760+
1761+
Quark Script CWE-88.py
1762+
=======================
1763+
1764+
The Quark Script below uses Vuldroid.apk to demonstrate.
1765+
1766+
.. code-block:: python
1767+
1768+
from quark.script import runQuarkAnalysis, Rule
1769+
1770+
SAMPLE_PATH = "Vuldroid.apk"
1771+
RULE_PATH = "ExternalStringCommand.json"
1772+
1773+
1774+
STRING_MATCHING_API = [
1775+
["Ljava/lang/String;", "contains", "(Ljava/lang/CharSequence)Z"],
1776+
["Ljava/lang/String;", "indexOf", "(I)I"],
1777+
["Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"],
1778+
["Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"],
1779+
["Ljava/lang/String;", "replaceAll",
1780+
"(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;"],
1781+
]
1782+
1783+
delimiters = [' ', ';', '||', '|', ',', '>', '>>', '`']
1784+
1785+
ruleInstance = Rule(RULE_PATH)
1786+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
1787+
1788+
for ExternalStringCommand in quarkResult.behaviorOccurList:
1789+
1790+
caller = ExternalStringCommand.methodCaller
1791+
1792+
strMatchingAPIs = [
1793+
api for api in STRING_MATCHING_API if
1794+
quarkResult.findMethodInCaller(caller, api)
1795+
]
1796+
1797+
if not strMatchingAPIs or \
1798+
any(dlm not in strMatchingAPIs for dlm in delimiters):
1799+
print(f"CWE-88 is detected in method, {caller.fullName}")
1800+
1801+
1802+
Quark Rule: ExternalStringCommand.json
1803+
=========================================
1804+
1805+
.. code-block:: json
1806+
1807+
{
1808+
"crime": "Using external strings as commands",
1809+
"permission": [],
1810+
"api": [
1811+
{
1812+
"class": "Landroid/content/Intent;",
1813+
"method": "getStringExtra",
1814+
"descriptor": "(Ljava/lang/String;)Ljava/lang/String"
1815+
},
1816+
{
1817+
"class": "Ljava/lang/Runtime;",
1818+
"method": "exec",
1819+
"descriptor": "(Ljava/lang/String;)Ljava/lang/Process"
1820+
}
1821+
],
1822+
"score": 1,
1823+
"label": []
1824+
}
1825+
1826+
1827+
Quark Script Result
1828+
======================
1829+
- **Vuldroid.apk**
1830+
1831+
.. code-block:: TEXT
1832+
1833+
$ python3 CWE-88.py
1834+
CWE-88 is detected in method, Lcom/vuldroid/application/RootDetection; onCreate (Landroid/os/Bundle;)V

0 commit comments

Comments
 (0)