Skip to content

Commit db2d36d

Browse files
authored
Optimize the document of Quark Script CWE-256 (#804)
1 parent cdceaba commit db2d36d

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

docs/source/quark_script.rst

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,3 +3505,108 @@ Quark Script Result
35053505
35063506
35073507
3508+
Detect CWE-256 in Android Application
3509+
--------------------------------------
3510+
3511+
This scenario seeks to find **Plaintext Storage of a Password**.
3512+
3513+
CWE-256: Plaintext Storage of a Password
3514+
=========================================
3515+
3516+
We analyze the definition of CWE-256 and identify its characteristics.
3517+
3518+
See `CWE-256 <https://cwe.mitre.org/data/definitions/256.html>`_ for more details.
3519+
3520+
.. image:: https://i.postimg.cc/rpydts5T/image.png
3521+
3522+
Code of CWE-256 in ovaa.apk
3523+
============================
3524+
3525+
We use the `ovaa.apk <https://github.com/oversecured/ovaa>`_ sample to explain the vulnerability code of CWE-256.
3526+
3527+
.. image:: https://i.postimg.cc/RhtqzHx7/image.png
3528+
3529+
CWE-256 Detection Process Using Quark Script API
3530+
=================================================
3531+
3532+
.. image:: https://i.postimg.cc/X7PzpBFM/image.png
3533+
3534+
First, we define a detection rule ``putStrAndCommit.json`` to identify behaviors that store information using ``SharedPreferences.Editor``.
3535+
3536+
Next, we call ``behaviorInstance.getParamValues()`` to retrieve all parameter values associated with this behavior. We then check whether any parameter contains keywords that suggest it is being used as a password (e.g., ``password``, ``pswd``, or ``passwd``).
3537+
3538+
Finally, we use ``behaviorInstance.isArgFromMethod(targetMethod)`` to verify whether the ``doFinal`` method for encryption is applied on the second argument ``value``. (Note: this Quark Script API checks all arguments, not just a specific one. Therefore, the API returns ``True`` even if the ``doFinal`` method is applied on the ``key`` argument rather than the ``value`` argument of ``putString`` . But the situation is so rare that we can neglect it.)
3539+
3540+
If the answer is **NO**, it indicates that the value may be stored in plaintext, which could lead to a CWE-256 vulnerability.
3541+
3542+
Quark Script CWE-256.py
3543+
========================
3544+
3545+
.. image:: https://i.postimg.cc/brxQ0JNR/image.png
3546+
3547+
.. code-block:: python
3548+
3549+
from quark.script import runQuarkAnalysis, Rule
3550+
3551+
SAMPLE_PATH = "ovaa.apk"
3552+
RULE_PATH = "putStrAndCommit.json"
3553+
3554+
encryptAPI = ["Ljavax/crypto/Cipher;", "doFinal", ""]
3555+
3556+
passwordPatterns = ["password", "pswd", "passwd"]
3557+
3558+
3559+
ruleInstance = Rule(RULE_PATH)
3560+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
3561+
3562+
for putStrAndCommit in quarkResult.behaviorOccurList:
3563+
paramValues = [
3564+
paramValue.lower() for paramValue in putStrAndCommit.getParamValues()
3565+
]
3566+
if not any(
3567+
passwordPattern in paramValues for passwordPattern in passwordPatterns
3568+
):
3569+
continue
3570+
3571+
if not putStrAndCommit.isArgFromMethod(encryptAPI):
3572+
print(
3573+
f"CWE-256 is detected in method",
3574+
putStrAndCommit.methodCaller.fullName
3575+
)
3576+
3577+
Quark Rule: putStrAndCommit.json
3578+
=================================
3579+
3580+
.. image:: https://i.postimg.cc/h4sFPGpg/image.png
3581+
3582+
.. code-block:: json
3583+
3584+
{
3585+
"crime": "Use editor to store information",
3586+
"permission": [],
3587+
"api": [
3588+
{
3589+
"class": "Landroid/content/SharedPreferences$Editor;",
3590+
"method": "putString",
3591+
"descriptor": "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;"
3592+
},
3593+
{
3594+
"class": "Landroid/content/SharedPreferences$Editor;",
3595+
"method": "commit",
3596+
"descriptor": "()Z"
3597+
}
3598+
],
3599+
"score": 1,
3600+
"label": []
3601+
}
3602+
3603+
Quark Script Result
3604+
====================
3605+
3606+
.. code-block:: TEXT
3607+
3608+
$ python3 CWE-256.py
3609+
CWE-256 is detected in method, Loversecured/ovaa/utils/LoginUtils; saveCredentials (Loversecured/ovaa/objects/LoginData;)V
3610+
3611+
3612+

0 commit comments

Comments
 (0)