Skip to content

Commit 34a4c1e

Browse files
authored
Add CWE-502 Quark Script (#34)
* Create CWE-502.py * Create README.md * Create deserializeData.json * Update README.md
1 parent 37bcff5 commit 34a4c1e

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

CWE-502/CWE-502.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from quark.script import runQuarkAnalysis, Rule
2+
3+
SAMPLE_PATH = "pivaa.apk"
4+
RULE_PATH = "deserializeData.json"
5+
6+
ruleInstance = Rule(RULE_PATH)
7+
8+
result = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
9+
10+
verificationApis = [
11+
["Ljava/io/File;", "exists", "()Z"],
12+
["Landroid/content/Context;", "getFilesDir", "()Ljava/io/File;"],
13+
["Landroid/content/Context;", "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;"],
14+
["Landroid/os/Environment;", "getExternalStorageDirectory", "()Ljava/io/File;"],
15+
]
16+
17+
for dataDeserialization in result.behaviorOccurList:
18+
apis = dataDeserialization.getMethodsInArgs()
19+
caller = dataDeserialization.methodCaller
20+
if not any(api in apis for api in verificationApis):
21+
print(f"CWE-502 is detected in method, {caller.fullName}")

CWE-502/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Detect CWE-502 in Android Application (pivaa)
2+
This scenario aims to demonstrate the detection of the **Deserialization of Untrusted Data** vulnerability using [pivaa.apk](https://github.com/htbridge/pivaa). See [CWE-502](https://cwe.mitre.org/data/definitions/502.html) for more details.
3+
4+
To begin with, we create a detection rule named ``deserializeData.json`` to identify behaviors that deserialize data.
5+
6+
Next, we retrieve the methods that interact with the deserialization API. Following this, we check if there are any of the APIs in ``verificationApis`` are found.
7+
8+
If **NO**, it could imply that the APK deserializes the untrusted data, potentially leading to a CWE-502 vulnerability.
9+
10+
11+
12+
## Quark Script CWE-502.py
13+
The Quark Script below uses pivaa.apk to demonstrate.
14+
15+
```python
16+
from quark.script import runQuarkAnalysis, Rule
17+
18+
SAMPLE_PATH = "pivaa.apk"
19+
RULE_PATH = "deserializeData.json"
20+
21+
ruleInstance = Rule(RULE_PATH)
22+
23+
result = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
24+
25+
verificationApis = [
26+
["Ljava/io/File;", "exists", "()Z"],
27+
["Landroid/content/Context;", "getFilesDir", "()Ljava/io/File;"],
28+
["Landroid/content/Context;", "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;"],
29+
["Landroid/os/Environment;", "getExternalStorageDirectory", "()Ljava/io/File;"],
30+
]
31+
32+
for dataDeserialization in result.behaviorOccurList:
33+
apis = dataDeserialization.getMethodsInArgs()
34+
caller = dataDeserialization.methodCaller
35+
if not any(api in apis for api in verificationApis):
36+
print(f"CWE-502 is detected in method, {caller.fullName}")
37+
```
38+
39+
40+
41+
## Quark Rule: deserializeData.json
42+
```json
43+
44+
{
45+
"crime": "Deserialize Data",
46+
"permission": [],
47+
"api": [
48+
49+
{
50+
"class": "Ljava/io/ObjectInputStream;",
51+
"method": "<init>",
52+
"descriptor": "(Ljava/io/InputStream;)V"
53+
},
54+
{
55+
"class": "Ljava/io/ObjectInputStream;",
56+
"method": "readObject",
57+
"descriptor": "()Ljava/lang/Object;"
58+
}
59+
60+
],
61+
"score": 1,
62+
"label": []
63+
}
64+
```
65+
66+
## Quark Script Result
67+
- **pivaa.apk**
68+
69+
```
70+
$ python CWE-502.py
71+
CWE-502 is detected in method, Lcom/htbridge/pivaa/handlers/ObjectSerialization; loadObject ()V
72+
```

CWE-502/deserializeData.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"crime": "Deserialize Data",
3+
"permission": [],
4+
"api": [
5+
6+
{
7+
"class": "Ljava/io/ObjectInputStream;",
8+
"method": "<init>",
9+
"descriptor": "(Ljava/io/InputStream;)V"
10+
},
11+
{
12+
"class": "Ljava/io/ObjectInputStream;",
13+
"method": "readObject",
14+
"descriptor": "()Ljava/lang/Object;"
15+
}
16+
17+
],
18+
"score": 1,
19+
"label": []
20+
}

0 commit comments

Comments
 (0)