Skip to content

Optimize the CWE-328 document#659

Merged
zinwang merged 2 commits into
ev-flow:masterfrom
JerryTasi:master
Jun 26, 2024
Merged

Optimize the CWE-328 document#659
zinwang merged 2 commits into
ev-flow:masterfrom
JerryTasi:master

Conversation

@JerryTasi

@JerryTasi JerryTasi commented Jun 25, 2024

Copy link
Copy Markdown
Contributor

Detect CWE-328 in Android Application

This scenario seeks to find the use of weak Hash.

CWE-328 Use of Weak Hash

We analyze the definition of CWE-328 and identify its characteristics.

See CWE-328 for more details.

image

Code of CWE-328 in allsafe.apk

We use the allsafe.apk sample to explain the vulnerability code of CWE-328.

image

Quark Scipt: CWE-328.py

Let’s use the above APIs to show how the Quark script finds this vulnerability.

First, we use API findMethodInAPK(samplePath, targetMethod) to find the method MessageDigest.getInstance() or SecretKeyFactory.getInstance(). Next, we use API methodInstance.getArguments() with a list to check if the method uses weak hashing algorithms. If YES, that causes CWE-328 vulnerability.

from quark.script import findMethodInAPK

SAMPLE_PATH = "./allsafe.apk"

TARGET_METHODS = [
    [
        "Ljava/security/MessageDigest;",
        "getInstance",
        "(Ljava/lang/String;)Ljava/security/MessageDigest;",
    ],
    [
        "Ljavax/crypto/SecretKeyFactory;",
        "getInstance",
        "(Ljava/lang/String;)Ljavax/crypto/SecretKeyFactory;",
    ],
]

HASH_KEYWORDS = [
    "MD2",
    "MD4",
    "MD5",
    "PANAMA",
    "SHA0",
    "SHA1",
    "HAVAL128",
    "RIPEMD128",
]

methodsFound = []
for target in TARGET_METHODS:
    methodsFound += findMethodInAPK(SAMPLE_PATH, target)

for setHashAlgo in methodsFound:
    algoName = setHashAlgo.getArguments()[0].replace("-", "")

    if any(keyword in algoName for keyword in HASH_KEYWORDS):
        print(
            f"CWE-328 is detected in {SAMPLE_PATH},\n\t"
            f"and it occurs in method, {setHashAlgo.fullName}"
        )

Quark Script Result

$ python3 CWE-328.py
CWE-328 is detected in ./allsafe.apk,
        and it occurs in method, Linfosecadventures/allsafe/challenges/SQLInjection; md5 (Ljava/lang/String;)Ljava/lang/String;
CWE-328 is detected in ./allsafe.apk,
        and it occurs in method, Linfosecadventures/allsafe/challenges/WeakCryptography; md5Hash (Ljava/lang/String;)Ljava/lang/String;
CWE-328 is detected in ./allsafe.apk,
        and it occurs in method, Lcom/google/firebase/database/core/utilities/Utilities; sha1HexDigest (Ljava/lang/String;)Ljava/lang/String;

@codecov

codecov Bot commented Jun 25, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.73%. Comparing base (264341b) to head (7f166f3).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #659   +/-   ##
=======================================
  Coverage   78.73%   78.73%           
=======================================
  Files          64       64           
  Lines        5324     5324           
=======================================
  Hits         4192     4192           
  Misses       1132     1132           
Flag Coverage Δ
unittests 78.73% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants