Skip to content

Commit 0511c15

Browse files
authored
Add CWE-329 Quark Script (#54)
1 parent d8f79e1 commit 0511c15

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

CWE-329/CWE-329.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from quark.script import runQuarkAnalysis, Rule
2+
3+
SAMPLE_PATH = "InsecureBankv2.apk"
4+
RULE_PATH = "initializeCipherWithIV.json"
5+
6+
randomAPIs = [
7+
["Ljava/security/SecureRandom", "next", "(I)I"],
8+
["Ljava/security/SecureRandom", "nextBytes", "([B)V"],
9+
]
10+
11+
ruleInstance = Rule(RULE_PATH)
12+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
13+
14+
for initCipherWithIV in quarkResult.behaviorOccurList:
15+
methodcaller = initCipherWithIV.methodCaller
16+
cipherName = initCipherWithIV.getParamValues()[0]
17+
18+
if "CBC" not in cipherName:
19+
break
20+
21+
if not any(
22+
initCipherWithIV.isArgFromMethod(api) for api in randomAPIs
23+
):
24+
print(f"CWE-329 is detected in method, {methodcaller.fullName}")

CWE-329/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Detect CWE-329 in Android Application
2+
3+
4+
This scenario seeks to find **Generation of Predictable IV with CBC Mode** in the APK file.
5+
6+
## CWE-329: Generation of Predictable IV with CBC Mode
7+
8+
9+
We analyze the definition of CWE-329 and identify its characteristics.
10+
11+
See [CWE-329](https://cwe.mitre.org/data/definitions/329.html) for more details.
12+
13+
![](https://i.postimg.cc/ZY6WjB5z/Screenshot-2025-07-11-17-13-40.png)
14+
15+
## Code of CWE-329 in InsecureBankv2.apk
16+
17+
18+
We use the [InsecureBankv2.apk](https://github.com/dineshshetty/Android-InsecureBankv2) sample to explain the vulnerability code of CWE-329.
19+
20+
![](https://i.postimg.cc/LXgBX9SB/Screenshot-2025-07-11-17-46-25.png)
21+
22+
## CWE-329 Detection Process Using Quark Script API
23+
24+
25+
![](https://i.postimg.cc/50cscyh2/Screenshot-2025-07-12-10-02-34.png)
26+
27+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
28+
29+
To begin with, we created a detection rule named ``initializeCipherWithIV.json`` to identify behaviors that initialize a cipher object with IV. Then, we use API `behaviorInstance.getParamValues()` to check if the cipher object uses CBC mode.
30+
31+
Finally, we use API ``behaviorInstance.isArgFromMethod(targetMethod)`` to check if any random API is applied on the IV used in the cipher object. If **NO**, it could imply that the APK uses a predictable IV in CBC mode cipher, potentially leading to a CWE-329 vulnerability.
32+
33+
## Quark Script CWE-329.py
34+
35+
![](https://i.postimg.cc/prCCnZpm/Screenshot-2025-07-12-10-02-58.png)
36+
37+
```python
38+
from quark.script import runQuarkAnalysis, Rule
39+
40+
SAMPLE_PATH = "InsecureBankv2.apk"
41+
RULE_PATH = "initializeCipherWithIV.json"
42+
43+
randomAPIs = [
44+
["Ljava/security/SecureRandom", "next", "(I)I"],
45+
["Ljava/security/SecureRandom", "nextBytes", "([B)V"],
46+
]
47+
48+
ruleInstance = Rule(RULE_PATH)
49+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
50+
51+
for initCipherWithIV in quarkResult.behaviorOccurList:
52+
methodcaller = initCipherWithIV.methodCaller
53+
cipherName = initCipherWithIV.getParamValues()[0]
54+
55+
if "CBC" not in cipherName:
56+
break
57+
58+
if not any(
59+
initCipherWithIV.isArgFromMethod(api) for api in randomAPIs
60+
):
61+
print(f"CWE-329 is detected in method, {methodcaller.fullName}")
62+
```
63+
64+
## Quark Rule: initializeCipherWithIV.json
65+
66+
![](https://i.postimg.cc/Y9tM29YT/Screenshot-2025-07-11-17-49-41.png)
67+
68+
```json
69+
{
70+
"crime": "Initialize a cipher object with IV",
71+
"permission": [],
72+
"api": [
73+
{
74+
"class": "Ljavax/crypto/spec/IvParameterSpec;",
75+
"method": "<init>",
76+
"descriptor": "([B)V"
77+
},
78+
{
79+
"class": "Ljavax/crypto/Cipher;",
80+
"method": "init",
81+
"descriptor": "(ILjava/security/Key;Ljava/security/spec/AlgorithmParameterSpec;)V"
82+
}
83+
],
84+
"score": 1,
85+
"label": []
86+
}
87+
```
88+
89+
## Quark Script Result
90+
91+
```text
92+
$ python CWE-329.py
93+
CWE-329 is detected in method, Lcom/google/android/gms/internal/zzar; zzc ([B Ljava/lang/String;)[B
94+
CWE-329 is detected in method, Lcom/android/insecurebankv2/CryptoClass; aes256encrypt ([B [B [B)[B
95+
CWE-329 is detected in method, Lcom/android/insecurebankv2/CryptoClass; aes256decrypt ([B [B [B)[B
96+
```
97+
98+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"crime": "Initialize a cipher object with IV",
3+
"permission": [],
4+
"api": [
5+
{
6+
"class": "Ljavax/crypto/spec/IvParameterSpec;",
7+
"method": "<init>",
8+
"descriptor": "([B)V"
9+
},
10+
{
11+
"class": "Ljavax/crypto/Cipher;",
12+
"method": "init",
13+
"descriptor": "(ILjava/security/Key;Ljava/security/spec/AlgorithmParameterSpec;)V"
14+
}
15+
],
16+
"score": 1,
17+
"label": []
18+
}

0 commit comments

Comments
 (0)