Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1079420
add Quark script for CWE-117 (#535)
Kaishiang-Zhang Aug 10, 2023
fcb235d
Visual Quark Scripts Program: Web design layout (#536)
pulorsok Aug 18, 2023
63ebd3f
Fix image link error in the document (#537)
pulorsok Aug 19, 2023
98f0c30
Add quark script for CWE-940 (#539)
LiangPPP Aug 25, 2023
cbf1607
Update README.md for the CWE-940 showcase (#540)
LiangPPP Aug 25, 2023
f9d7a64
Add document link of CWE-117 showcase to the README (#542)
sidra-asa Aug 29, 2023
84ea3e5
Update version number to v23.8.1 (#543)
sidra-asa Aug 30, 2023
bd6fafd
Update dependency with r2pipe
sidra-asa Mar 13, 2023
2deaa6f
Update pytest.yml
sidra-asa Mar 17, 2023
2a4f543
Add radare2 as a choice of core_library
sidra-asa Sep 19, 2023
52f84f1
Add R2Imp parameters to test_apkinfo.py
sidra-asa Sep 20, 2023
9c1141a
Add test_r2apkinfo.py
sidra-asa Apr 1, 2023
8819e02
Make APK file format is available with R2 lib
sidra-asa Apr 28, 2023
82482af
Using axtj command, instead of axlj command
sidra-asa Aug 9, 2023
f19091b
Using pfq command, instead of pr command
sidra-asa Aug 9, 2023
3764a72
Skip some tests due to R2 issues, and remove unused variables
sidra-asa Aug 28, 2023
cd605c9
Add receivers function into r2apkinfo.py
sidra-asa Aug 31, 2023
d2af092
Remove duplicate test_lowerfunc
sidra-asa Aug 31, 2023
417f6f0
Move testcases of test_r2apkinfo.py into test_apkinfo.py
sidra-asa Sep 20, 2023
0e5d1ca
Add core library with radare2 in axmlreader
sidra-asa Sep 21, 2023
21b6cc9
Use _r2 with cache_property in r2apkinfo.py
sidra-asa Sep 29, 2023
0348d52
Remove outdated docstrings
sidra-asa Sep 29, 2023
817a433
Use axml.get_xml_tree instead of iterating axml object
sidra-asa Oct 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest rzpipe meson==0.62.0 ninja coverage ciphey frida objection
python -m pip install pytest rzpipe meson==0.62.0 ninja coverage ciphey frida objection r2pipe==1.8.0

# Install graphviz & ninja
sudo apt-get -y install graphviz ninja-build
Expand All @@ -39,6 +39,14 @@ jobs:
sudo ninja -C build install
sudo ldconfig -v
cd -

# Install Radare2 (5.8.8)
sudo apt install -y musl-tools
sudo git clone https://github.com/radareorg/radare2 /opt/radare2/
cd /opt/radare2/
sudo git checkout 5.8.8
sudo sys/install.sh
cd -

# Install click >= 8.0.0 for CLI supports
python -m pip install click==8.0.3
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rzpipe = "<=0.1.2"
objection = "<=1.11.0"
frida = "<=15.2.2"
ciphey = ">=5.0.0,<=5.14.0"
r2pipe = "==1.8.0"

[requires]
python_version = "3.8"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* [CWE-088](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-88-in-android-application-vuldroid-apk) Improper Neutralization of Argument Delimiters in a Command
* [CWE-089](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-89-in-android-application-androgoat-apk) Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
* [CWE-094](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-94-in-android-application-ovaa-apk) Improper Control of Generation of Code ('Code Injection')
* [CWE-117](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-117-in-android-application-allsafe-apk) Improper Output Neutralization for Logs
* [CWE-295](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-295-in-android-application-insecureshop-apk) Improper Certificate Validation
* [CWE-312](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-312-in-android-application-ovaa-apk) Cleartext Storage of Sensitive Information
* [CWE-319](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-319-in-android-application-ovaa-apk) Cleartext Transmission of Sensitive Information
Expand All @@ -74,7 +75,8 @@
* [CWE-798](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-798-in-android-application-ovaa-apk) Use of Hard-coded Credentials
* [CWE-921](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-921-in-android-application-ovaa-apk) Storage of Sensitive Data in a Mechanism without Access Control
* [CWE-925](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-925-in-android-application-insecurebankv2-androgoat) Improper Verification of Intent by Broadcast Receiver
* [CWE-926](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-926-in-android-application-dvba-apk) Improper Export of Android Application Components
* [CWE-926](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-926-in-android-application-dvba-apk) Improper Export of Android Application Components
* [CWE-940](https://quark-engine.readthedocs.io/en/latest/quark_script.html#detect-cwe-940-in-android-application-ovaa-vuldroid) Improper Verification of Source of a Communication Channel

# Quick Start

Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This guide will explain how to set up Quark, use it, and customize it.

install_index
quark_script
visual_quark_script_program
quark_mit_program
quark_reports
addRules
Expand Down
155 changes: 155 additions & 0 deletions docs/source/quark_script.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2081,3 +2081,158 @@ Quark Script Result
$ python3 CWE-78.py
CWE-78 is detected in method, Lcom/vuldroid/application/RootDetection; onCreate (Landroid/os/Bundle;)V



Detect CWE-117 in Android Application (allsafe.apk)
------------------------------------------------------
This scenario seeks to find **Improper Output Neutralization for Logs**. See `CWE-117 <https://cwe.mitre.org/data/definitions/117.html>`_ for more details.

Let’s use this `APK <https://github.com/t0thkr1s/allsafe>`_ and the above APIs to show how the Quark script finds this vulnerability.

First, we design a detection rule ``writeContentToLog.json`` to spot on behavior using the method that writes contents to the log file.

Then, we use ``behaviorInstance.getParamValues()`` to get all parameter values of this method. And we check if these parameters contain keywords of APIs for neutralization, such as escape, replace, format, and setFilter.

If the answer is **YES**, that may result in secret context leakage into the log file, or the attacker may perform log forging attacks.

Quark Script CWE-117.py
==========================

.. code-block:: python

from quark.script import Rule, runQuarkAnalysis

SAMPLE_PATH = "allsafe.apk"
RULE_PATH = "writeContentToLog.json"
KEYWORDS_FOR_NEUTRALIZATION = ["escape", "replace", "format", "setFilter"]

ruleInstance = Rule(RULE_PATH)
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)

for logOutputBehavior in quarkResult.behaviorOccurList:

secondAPIParam = logOutputBehavior.getParamValues()[1]

isKeywordFound = False
for keyword in KEYWORDS_FOR_NEUTRALIZATION:
if keyword in secondAPIParam:
isKeywordFound = True
break

if not isKeywordFound:
print(f"CWE-117 is detected in method,{secondAPIParam}")

Quark Rule: writeContentToLog.json
==============================================

.. code-block:: json

{
"crime": "Write contents to the log.",
"permission": [],
"api": [
{
"descriptor": "()Landroid/text/Editable;",
"class": "Lcom/google/android/material/textfield/TextInputEditText;",
"method": "getText"
},
{
"descriptor": "(Ljava/lang/String;Ljava/lang/String;)I",
"class": "Landroid/util/Log;",
"method": "d"
}
],
"score": 1,
"label": []
}

Quark Script Result
======================
- **allsafe.apk**

.. code-block:: TEXT

$ python CWE-117.py
CWE-117 is detected in method,Ljava/lang/StringBuilder;->toString()Ljava/lang/String;(Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;(Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;(Ljava/lang/StringBuilder;-><init>()V(Ljava/lang/StringBuilder;),User entered secret: ),Ljava/lang/Object;->toString()Ljava/lang/String;(Lcom/google/android/material/textfield/TextInputEditText;->getText()Landroid/text/Editable;())))

Detect CWE-940 in Android Application (ovaa,Vuldroid)
------------------------------------------------------
This scenario aims to demonstrate the detection of the **Improper Verification of Source of a Communication Channel** vulnerability using `ovaa.apk <https://github.com/oversecured/ovaa>`_ and `Vuldroid.apk <https://github.com/jaiswalakshansh/Vuldroid>`_. See `CWE-940 <https://cwe.mitre.org/data/definitions/940.html>`_ for more details.

To begin with, we create a detection rule named ``LoadUrlFromIntent.json`` to identify behavior that loads url from intent data to the WebView.

Next, we retrieve the methods that pass the url. Following this, we check if these methods are only for setting intent, such as findViewById, getStringExtra, or getIntent.

If **NO**, it could imply that the APK uses communication channels without proper verification, which may cause CWE-940 vulnerability.

Quark Script CWE-940.py
==========================

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 = "Vuldroid.apk"``.


.. code-block:: python

from quark.script import runQuarkAnalysis, Rule

SAMPLE_PATH = "ovaa.apk"
RULE_PATH = "LoadUrlFromIntent.json"

INTENT_SETTING_METHODS = [
"findViewById",
"getStringExtra",
"getIntent",
]

ruleInstance = Rule(RULE_PATH)

quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)

for behaviorInstance in quarkResult.behaviorOccurList:
methodsInArgs = behaviorInstance.getMethodsInArgs()

verifiedMethodCandidates = []

for method in methodsInArgs:
if method.methodName not in INTENT_SETTING_METHODS:
verifiedMethodCandidates.append(method)

if verifiedMethodCandidates == []:
caller = behaviorInstance.methodCaller.fullName
print(f"cwe-940 is detected in method, {caller}")



Quark Rule: LoadUrlFromIntent.json
==============================================

.. code-block:: json

{
"crime": "Load Url from Intent and open WebView",
"permission": [],
"api": [
{
"class": "Landroid/content/Intent;",
"method": "getStringExtra",
"descriptor": "(Ljava/lang/String;)Ljava/lang/String"
},
{
"class": "Landroid/webkit/WebView;",
"method": "loadUrl",
"descriptor": "(Ljava/lang/String;)V"
}
],
"score": 1,
"label": []
}

Quark Script Result
======================
- **ovaa.apk**

.. code-block:: TEXT

$ python CWE-940.py
CWE-940 is detected in method, Loversecured/ovaa/activities/WebViewActivity; onCreate (Landroid/os/Bundle;)V

22 changes: 22 additions & 0 deletions docs/source/visual_quark_script_program.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
++++++++++++++++++++++++++++
Visual Quark Script Program
++++++++++++++++++++++++++++

Introduction of the Program
----------------------------

Quark Script is a powerful tool for detecting and analyzing mobile security. However, it can be complex and challenging for user who are not familiar with programming. To overcome this challenge, we are pleased to announce our plan to develop a visual programming tool that simplifies the Quark Script organization process, making it easy for anyone to organize Quark Script using a simple UI interface.

Goal of the Program
--------------------

We aim to make Quark Script programming accessible to everyone and remove the barriers that often come with traditional programming languages. So we design a new visual tool for Quark Script organization. It would be more intuitive, time-saving, and effort-saving for users, even if they are not familiar with programming.

Web Design Layout
------------------

The initial draft of the web design is as below.

Design by: `@Commuter95 <https://github.com/Commuter95>`_

.. image:: https://github.com/quark-engine/quark-engine/assets/16009212/053d62e2-181a-4fb1-96d7-95fe59809dc3
2 changes: 1 addition & 1 deletion quark/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "23.7.1"
__version__ = "23.8.1"
2 changes: 1 addition & 1 deletion quark/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"--core-library",
"core_library",
help="Specify the core library used to analyze an APK",
type=click.Choice(("androguard", "rizin"), case_sensitive=False),
type=click.Choice(("androguard", "rizin", "radare2"), case_sensitive=False),
required=False,
default="androguard",
)
Expand Down
Loading