Skip to content

Commit 4ea67e5

Browse files
authored
Add Quark Script CWE-73 (ev-flow#514)
* Add Quark Script CWE-73 * Add Quark Script CWE-73 * Correct the syntax errors. * Fix the error of double quotation * Correct errors discovered after merging.
1 parent 0713ab3 commit 4ea67e5

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/source/quark_script.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,3 +1913,79 @@ Quark Script Result
19131913
$ python CWE-925.py
19141914
CWE-925 is detected in method, Lowasp/sat/agoat/ShowDataReceiver;
19151915
CWE-925 is detected in method, Lcom/android/insecurebankv2/MyBroadCastReceiver;
1916+
1917+
Detect CWE-73 in Android Application (ovaa.apk)
1918+
---------------------------------------------------
1919+
1920+
This scenario seeks to find **External Control of File Name or Path**. See
1921+
`CWE-73 <https://cwe.mitre.org/data/definitions/73.html>`__ for more
1922+
details.
1923+
1924+
First, we design a detection rule ``accessFileInExternalDir.json`` to spot behavior accessing a file in an external directory.
1925+
1926+
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.
1927+
1928+
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.
1929+
1930+
Quark Script CWE-73.py
1931+
=======================
1932+
1933+
.. code:: python
1934+
1935+
from quark.script import runQuarkAnalysis, Rule
1936+
1937+
SAMPLE_PATH = "ovaa.apk"
1938+
RULE_PATH = "accessFileInExternalDir.json"
1939+
1940+
OPEN_FILE_API = [
1941+
"Landroid/os/ParcelFileDescriptor;", # Class name
1942+
"open", # Method name
1943+
"(Ljava/io/File; I)Landroid/os/ParcelFileDescriptor;" # Descriptor
1944+
]
1945+
1946+
ruleInstance = Rule(RULE_PATH)
1947+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
1948+
1949+
for accessExternalDir in quarkResult.behaviorOccurList:
1950+
filePath = accessExternalDir.secondAPI.getArguments()[2]
1951+
1952+
if quarkResult.isHardcoded(filePath):
1953+
continue
1954+
1955+
caller = accessExternalDir.methodCaller
1956+
result = quarkResult.findMethodInCaller(caller, OPEN_FILE_API)
1957+
1958+
if result:
1959+
print("CWE-73 is detected in method, ", caller.fullName)
1960+
1961+
Quark Rule: accessFileInExternalDir.json
1962+
=========================================
1963+
1964+
.. code-block:: json
1965+
1966+
{
1967+
"crime": "Access a file in an external directory",
1968+
"permission": [],
1969+
"api": [
1970+
{
1971+
"class": "Landroid/os/Environment;",
1972+
"method": "getExternalStorageDirectory",
1973+
"descriptor": "()Ljava/io/File;"
1974+
},
1975+
{
1976+
"class": "Ljava/io/File;",
1977+
"method": "<init>",
1978+
"descriptor": "(Ljava/io/File;Ljava/lang/String;)V"
1979+
}
1980+
],
1981+
"score": 1,
1982+
"label": []
1983+
}
1984+
1985+
Quark Script Result
1986+
=====================
1987+
1988+
.. code-block:: TEXT
1989+
1990+
$ python CWE-73.py
1991+
CWE-73 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;

0 commit comments

Comments
 (0)