Skip to content

Commit 465a7cf

Browse files
authored
Optimize the document of Quark Script CWE-22, 23, and 78 (#56)
1 parent 0511c15 commit 465a7cf

File tree

8 files changed

+99
-110
lines changed

8 files changed

+99
-110
lines changed

CWE-22/CWE-22.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
1717

1818
for accessExternalDir in quarkResult.behaviorOccurList:
19+
1920
filePath = accessExternalDir.secondAPI.getArguments()[2]
2021

2122
if quarkResult.isHardcoded(filePath):
2223
continue
2324

2425
caller = accessExternalDir.methodCaller
2526
strMatchingAPIs = [
26-
api
27-
for api in STRING_MATCHING_API
28-
if quarkResult.findMethodInCaller(caller, api)
27+
api for api in STRING_MATCHING_API if quarkResult.findMethodInCaller(
28+
caller, api)
2929
]
3030

3131
if not strMatchingAPIs:
32-
print(f"CWE-22 is detected in method, {caller.fullName}")
32+
print(f"CWE-22 is detected in method, {caller.fullName}")

CWE-22/README.md

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
# Detect CWE-22 in Android Application
22

3-
This scenario seeks to find **the improper limitation of a pathname to a
4-
restricted directory ('Path Traversal')**.
3+
This scenario seeks to find **the improper limitation of a pathname to a restricted directory ('Path Traversal')**.
54

6-
## CWE-22: Improper Limitation of a Pathname to a Restricted Directory (\'Path Traversal\')
5+
## CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
76

87
We analyze the definition of CWE-22 and identify its characteristics.
98

10-
See [CWE-22](https://cwe.mitre.org/data/definitions/22.html) for more
11-
details.
9+
See [CWE-22](https://cwe.mitre.org/data/definitions/22.html) for more details.
1210

13-
![image](https://imgur.com/agRPwp8.png)
11+
![image](https://imgur.com/XnOUZsV.png)
1412

1513
## Code of CWE-22 in ovaa.apk
1614

17-
We use the [ovaa.apk](https://github.com/oversecured/ovaa) sample to
18-
explain the vulnerability code of CWE-22.
15+
We use the [ovaa.apk](https://github.com/oversecured/ovaa) sample to explain the vulnerability code of CWE-22.
1916

20-
![image](https://imgur.com/WFpfzFk.png)
17+
![image](https://imgur.com/bgWgeT7.png)
2118

22-
## Quark Script: CWE-22.py
19+
## CWE-22 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/D852ZLV.png)
22+
23+
Let's use the above APIs to show how the Quark script finds this vulnerability.
2324

24-
Let's use the above APIs to show how the Quark script finds this
25-
vulnerability.
25+
First, we design a detection rule `accessFileInExternalDir.json` to spot behavior accessing a file in an external directory.
2626

27-
First, we design a detection rule `accessFileInExternalDir.json` to spot
28-
behavior accessing a file in an external directory.
27+
Next, we use API `methodInstance.getArguments()` to get the argument for the file path and use `quarkResultInstance.isHardcoded(argument)` to check if the argument is hardcoded into the APK. If **No**, the argument is from external input.
2928

30-
Next, we use API `methodInstance.getArguments()` to get the argument for
31-
the file path and use `quarkResultInstance.isHardcoded(argument)` to
32-
check if the argument is hardcoded into the APK. If No, the argument is
33-
from external input.
29+
Finally, we use Quark API `quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)` to check if there are any APIs in the caller method for string matching. If **NO**, the APK does not neutralize special elements within the argument, which may cause CWE-22 vulnerability.
30+
31+
## Quark Script: CWE-22.py
3432

35-
Finally, we use Quark API
36-
`quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)` to
37-
check if there are any APIs in the caller method for string matching. If
38-
NO, the APK does not neutralize special elements within the argument,
39-
which may cause CWE-22 vulnerability.
33+
![image](https://imgur.com/4b2e4tN.png)
4034

41-
``` python
35+
```python
4236
from quark.script import runQuarkAnalysis, Rule
4337

4438
SAMPLE_PATH = "ovaa.apk"
@@ -57,25 +51,26 @@ ruleInstance = Rule(RULE_PATH)
5751
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
5852

5953
for accessExternalDir in quarkResult.behaviorOccurList:
54+
6055
filePath = accessExternalDir.secondAPI.getArguments()[2]
6156

6257
if quarkResult.isHardcoded(filePath):
6358
continue
6459

6560
caller = accessExternalDir.methodCaller
6661
strMatchingAPIs = [
67-
api
68-
for api in STRING_MATCHING_API
69-
if quarkResult.findMethodInCaller(caller, api)
62+
api for api in STRING_MATCHING_API if quarkResult.findMethodInCaller(
63+
caller, api)
7064
]
7165

7266
if not strMatchingAPIs:
7367
print(f"CWE-22 is detected in method, {caller.fullName}")
7468
```
75-
7669
## Quark Rule: accessFileInExternalDir.json
7770

78-
``` json
71+
![image](https://imgur.com/N2uKsZj.png)
72+
73+
```json
7974
{
8075
"crime": "Access a file in an external directory",
8176
"permission": [],
@@ -98,7 +93,7 @@ for accessExternalDir in quarkResult.behaviorOccurList:
9893

9994
## Quark Script Result
10095

101-
``` TEXT
96+
```
10297
$ python3 CWE-22.py
10398
CWE-22 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
104-
```
99+
```

CWE-22/accessFileInExternalDir.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@
1616
"score": 1,
1717
"label": []
1818
}
19-

CWE-23/CWE-23.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,3 @@
3535

3636
if not strMatchingAPIs:
3737
print(f"CWE-23 is detected in method, {caller.fullName}")
38-
elif strMatchingAPIs.find("..") == -1:
39-
print(f"CWE-23 is detected in method, {caller.fullName}")

CWE-23/README.md

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
# Detect CWE-23 in Android Application
22

3-
This scenario aims to demonstrate the detection of the **Relative Path
4-
Traversal** vulnerability.
3+
This scenario aims to demonstrate the detection of the **Relative Path Traversal** vulnerability.
54

65
## CWE-23: Relative Path Traversal
76

87
We analyze the definition of CWE-23 and identify its characteristics.
98

10-
See [CWE-23](https://cwe.mitre.org/data/definitions/23.html) for more
11-
details.
9+
See [CWE-23](https://cwe.mitre.org/data/definitions/23.html) for more details.
1210

13-
![image](https://imgur.com/YS9umQp.png)
11+
![image](https://imgur.com/k4UPsKO.png)
1412

1513
## Code of CWE-23 in ovaa.apk
1614

17-
We use the [ovaa.apk](https://github.com/oversecured/ovaa) sample to
18-
explain the vulnerability code of CWE-23.
15+
We use the [ovaa.apk](https://github.com/oversecured/ovaa) sample to explain the vulnerability code of CWE-23.
1916

20-
![image](https://imgur.com/GosANyj.png)
17+
![image](https://imgur.com/KT277GG.png)
2118

22-
## Quark Script: CWE-23.py
19+
## CWE-23 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/D852ZLV.png)
22+
23+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
2324

24-
Let's use the above APIs to show how the Quark script finds this
25-
vulnerability.
25+
To begin with, we create a detection rule named ``accessFileInExternalDir.json`` to identify behavior that accesses a file in an external directory.
2626

27-
To begin with, we will create a detection rule named
28-
`accessFileInExternalDir.json` to identify behavior that accesses a file
29-
in an external directory.
27+
Next, we use ``methodInstance.getArguments()`` to retrieve the file path argument and check whether it belongs to the APK. If it does not belong to the APK, the argument is likely from external input.
3028

31-
Next, we will use `methodInstance.getArguments()` to retrieve the file
32-
path argument and check whether it belongs to the APK or not. If it does
33-
not belong to the APK, the argument is likely from external input.
29+
Then, we use the Quark Script API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to search for any APIs in the caller method that are used to match strings. If no API is found, that implies the APK does not neutralize special elements within the argument, possibly resulting in CWE-23 vulnerability.
30+
31+
## Quark Script: CWE-23.py
3432

35-
Finally, we will use the Quark API
36-
`quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)` to
37-
search for any APIs in the caller method that match the string. If no
38-
matching API is found, the APK does not neutralize special elements
39-
within the argument, which may result in the CWE-23 vulnerability. If a
40-
matching API is found, we will verify whether it neutralizes the
41-
Relative Path string or not. If it does not neutralize it, the APK may
42-
still be vulnerable to CWE-23.
33+
![image](https://imgur.com/lk1C4CX.jpg)
4334

44-
``` python
35+
```python
4536
from quark.script import runQuarkAnalysis, Rule
4637

4738
SAMPLE_PATH = "ovaa.apk"
@@ -79,27 +70,27 @@ for accessExternalDir in quarkResult.behaviorOccurList:
7970

8071
if not strMatchingAPIs:
8172
print(f"CWE-23 is detected in method, {caller.fullName}")
82-
elif strMatchingAPIs.find("..") == -1:
83-
print(f"CWE-23 is detected in method, {caller.fullName}")
8473
```
8574

8675
## Quark Rule: accessFileInExternalDir.json
8776

88-
``` json
77+
![image](https://imgur.com/N2uKsZj.png)
78+
79+
```json
8980
{
9081
"crime": "Access a file in an external directory",
9182
"permission": [],
9283
"api": [
93-
{
94-
"class": "Landroid/os/Environment;",
95-
"method": "getExternalStorageDirectory",
96-
"descriptor": "()Ljava/io/File;"
97-
},
98-
{
99-
"class": "Ljava/io/File;",
100-
"method": "<init>",
101-
"descriptor": "(Ljava/io/File;Ljava/lang/String;)V"
102-
}
84+
{
85+
"class": "Landroid/os/Environment;",
86+
"method": "getExternalStorageDirectory",
87+
"descriptor": "()Ljava/io/File;"
88+
},
89+
{
90+
"class": "Ljava/io/File;",
91+
"method": "<init>",
92+
"descriptor": "(Ljava/io/File;Ljava/lang/String;)V"
93+
}
10394
],
10495
"score": 1,
10596
"label": []
@@ -108,7 +99,7 @@ for accessExternalDir in quarkResult.behaviorOccurList:
10899

109100
## Quark Script Result
110101

111-
``` TEXT
102+
```
112103
$ python3 CWE-23.py
113104
CWE-23 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
114-
```
105+
```

CWE-23/accessFileInExternalDir.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"class": "Landroid/os/Environment;",
77
"method": "getExternalStorageDirectory",
8-
"descriptor": "()Ljava/io/File"
8+
"descriptor": "()Ljava/io/File;"
99
},
1010
{
1111
"class": "Ljava/io/File;",

CWE-78/CWE-78.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
("Ljava/lang/String;", "indexOf", "(I)I"),
1010
("Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"),
1111
("Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"),
12-
("Ljava/lang/String;", "replaceAll", "(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;")
12+
(
13+
"Ljava/lang/String;",
14+
"replaceAll",
15+
"(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;",
16+
),
1317
])
1418

1519
specialElementsPattern = r"[ ;|,>`]+"
@@ -28,4 +32,4 @@
2832
if methodCalled.intersection(STRING_MATCHING_API) and not ExternalStringCommand.hasString(specialElementsPattern):
2933
continue
3034
else:
31-
print(f"CWE-78 is detected in method, {caller.fullName}")
35+
print(f"CWE-78 is detected in method, {caller.fullName}")

CWE-78/README.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
# Detect CWE-78 in Android Application
22

3-
This scenario seeks to find **Improper Neutralization of Special
4-
Elements used in an OS Command** in the APK file.
3+
This scenario seeks to find **Improper Neutralization of Special Elements used in an OS Command** in the APK file.
54

6-
## CWE-78 Improper Neutralization of Special Elements used in an OS Command (\'OS Command Injection\')
5+
## CWE-78 Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
76

87
We analyze the definition of CWE-78 and identify its characteristics.
98

10-
See [CWE-78](https://cwe.mitre.org/data/definitions/78.html) for more
11-
details.
9+
See [CWE-78](https://cwe.mitre.org/data/definitions/78.html) for more details.
1210

13-
![image](https://imgur.com/aUB195P.png)
11+
![image](https://imgur.com/HpMGGsO.png)
1412

1513
## Code of CWE-78 in Vuldroid.apk
1614

17-
We use the [Vuldroid.apk](https://github.com/jaiswalakshansh/Vuldroid)
18-
sample to explain the vulnerability code of CWE-78.
15+
We use the [Vuldroid.apk](https://github.com/jaiswalakshansh/Vuldroid) sample to explain the vulnerability code of CWE-78.
1916

20-
![image](https://imgur.com/hO6m3Bz.png)
17+
![image](https://imgur.com/7Tu0Y3H.png)
2118

22-
## Quark Script: CWE-78.py
19+
## CWE-78 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/Hi7qGjw.png)
22+
23+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
2324

24-
Let's use the above APIs to show how the Quark script finds this
25-
vulnerability.
25+
First, we design a detection rule `ExternalStringsCommands.json` to spot on behavior using external strings as commands.
2626

27-
First, we design a detection rule `ExternalStringsCommands.json` to spot
28-
on behavior using external strings as commands.
27+
Next, we use Quark API `behaviorInstance.getMethodsInArgs()` to get the methods that passed the external command.
2928

30-
Next, we use Quark API `behaviorInstance.getMethodsInArgs()` to get the
31-
methods that passed the external command.
29+
Then we check if the method neutralizes any special elements in the argument.
3230

33-
Then we check if the method neutralizes any special elements found in
34-
the argument.
31+
If the neutralization is not complete, then it may cause CWE-78 vulnerability.
32+
33+
## Quark Script: CWE-78.py
3534

36-
If the neutralization is not complete, then it may cause CWE-78
37-
vulnerability.
35+
![image](https://imgur.com/UpRWgGe.png)
3836

39-
``` python
37+
```python
4038
from quark.script import runQuarkAnalysis, Rule, findMethodInAPK
4139

4240
SAMPLE_PATH = "Vuldroid.apk"
@@ -48,7 +46,11 @@ STRING_MATCHING_API = set([
4846
("Ljava/lang/String;", "indexOf", "(I)I"),
4947
("Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"),
5048
("Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"),
51-
("Ljava/lang/String;", "replaceAll", "(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;")
49+
(
50+
"Ljava/lang/String;",
51+
"replaceAll",
52+
"(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;",
53+
),
5254
])
5355

5456
specialElementsPattern = r"[ ;|,>`]+"
@@ -69,10 +71,12 @@ for ExternalStringCommand in quarkResult.behaviorOccurList:
6971
else:
7072
print(f"CWE-78 is detected in method, {caller.fullName}")
7173
```
72-
74+
7375
## Quark Rule: ExternalStringCommand.json
7476

75-
``` json
77+
![image](https://imgur.com/eoV8hnZ.png)
78+
79+
```json
7680
{
7781
"crime": "Using external strings as commands",
7882
"permission": [],
@@ -95,9 +99,7 @@ for ExternalStringCommand in quarkResult.behaviorOccurList:
9599

96100
## Quark Script Result
97101

98-
- **Vuldroid.apk**
99-
100-
``` TEXT
102+
```
101103
$ python3 CWE-78.py
102104
CWE-78 is detected in method, Lcom/vuldroid/application/RootDetection; onCreate (Landroid/os/Bundle;)V
103-
```
105+
```

0 commit comments

Comments
 (0)