Skip to content

Commit 586a6cd

Browse files
authored
Optimize the document of Quark Script CWE-73, 79, and 88 (#57)
* Optimize the document of Quark Script CWE-73, 79, and 88 * Optimize the document of Quark Script CWE-73, 79, and 88
1 parent 465a7cf commit 586a6cd

File tree

7 files changed

+103
-107
lines changed

7 files changed

+103
-107
lines changed

CWE-73/CWE-73.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from quark.script import runQuarkAnalysis, Rule
22

33
SAMPLE_PATH = "ovaa.apk"
4-
RULE_PATH = "accessFileInExternalDir.json"
4+
RULE_PATH = "useLastPathSegmentAsFileName.json"
55

66
OPEN_FILE_API = [
77
"Landroid/os/ParcelFileDescriptor;", # Class name
@@ -22,4 +22,4 @@
2222
result = quarkResult.findMethodInCaller(caller, OPEN_FILE_API)
2323

2424
if result:
25-
print("CWE-73 is detected in method, ", caller.fullName)
25+
print("CWE-73 is detected in method, ", caller.fullName)

CWE-73/README.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
1-
# Detect CWE-73 in Android Application
1+
# Detect CWE-73 in Android Application
22

3-
This scenario seeks to find **External Control of File Name or Path** in
4-
the APK file.
3+
This scenario seeks to find **External Control of File Name or Path** in the APK file.
54

65
## CWE-73 External Control of File Name or Path
76

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

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

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

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

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

20-
![image](https://imgur.com/9oa1HIC.png)
17+
![image](https://imgur.com/gLJ6zWr.png)
2118

22-
## Quark Script: CWE-73.py
19+
## CWE-73 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/zGjZHA1.png)
2322

24-
Let's use the above APIs to show how Quark script find this
25-
vulnerability.
23+
Let’s use the above APIs to show how Quark script finds this vulnerability.
2624

27-
First, we design a detection rule `accessFileInExternalDir.json` to spot
28-
behavior accessing a file in an external directory.
25+
First, we design a detection rule ``useLastPathSegmentAsFileName.json`` to spot behavior that uses the last path segment as the file name.
2926

30-
Second, we use API `methodInstance.getArguments()` to get the argument
31-
for the file path and use `quarkResultInstance.isHardcoded(argument)` to
32-
check if the argument is hardcoded into the APK. If **No**, the argument
33-
is from external input.
27+
Second, we use the 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.
3428

35-
Finally, we use Quark API
36-
`quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)` to
37-
check if any APIs in the caller method for opening files. If **YES**,
38-
the APK performs file operations using external input as a path, which
39-
may cause CWE-73 vulnerability.
29+
Finally, we use Quark API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` to check if there are any APIs in the caller method for opening files. If **YES**, the APK performs file operations using external input as a path, which may cause CWE-73 vulnerability.
30+
31+
## Quark Script: CWE-73.py
4032

41-
``` python
33+
![image](https://imgur.com/EHrcCPg.png)
34+
35+
```python
4236
from quark.script import runQuarkAnalysis, Rule
4337

4438
SAMPLE_PATH = "ovaa.apk"
45-
RULE_PATH = "accessFileInExternalDir.json"
39+
RULE_PATH = "useLastPathSegmentAsFileName.json"
4640

4741
OPEN_FILE_API = [
4842
"Landroid/os/ParcelFileDescriptor;", # Class name
@@ -66,17 +60,19 @@ for accessExternalDir in quarkResult.behaviorOccurList:
6660
print("CWE-73 is detected in method, ", caller.fullName)
6761
```
6862

69-
## Quark Rule: accessFileInExternalDir.json
63+
## Quark Rule: useLastPathSegmentAsFileName.json
64+
65+
![image](https://imgur.com/JxBdde0.png)
7066

71-
``` json
67+
```json
7268
{
73-
"crime": "Access a file in an external directory",
69+
"crime": "Use the last path segment as the file name",
7470
"permission": [],
7571
"api": [
7672
{
77-
"class": "Landroid/os/Environment;",
78-
"method": "getExternalStorageDirectory",
79-
"descriptor": "()Ljava/io/File;"
73+
"class": "Landroid/net/Uri;",
74+
"method": "getLastPathSegment",
75+
"descriptor": "()Ljava/lang/String;"
8076
},
8177
{
8278
"class": "Ljava/io/File;",
@@ -91,7 +87,7 @@ for accessExternalDir in quarkResult.behaviorOccurList:
9187

9288
## Quark Script Result
9389

94-
``` TEXT
90+
```
9591
$ python CWE-73.py
9692
CWE-73 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;
9793
```
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"crime": "Access a file in an external directory",
2+
"crime": "Use the last path segment as the file name",
33
"permission": [],
44
"api": [
55
{
6-
"class": "Landroid/os/Environment;",
7-
"method": "getExternalStorageDirectory",
8-
"descriptor": "()Ljava/io/File;"
6+
"class": "Landroid/net/Uri;",
7+
"method": "getLastPathSegment",
8+
"descriptor": "()Ljava/lang/String;"
99
},
1010
{
1111
"class": "Ljava/io/File;",
@@ -16,4 +16,3 @@
1616
"score": 1,
1717
"label": []
1818
}
19-

CWE-79/CWE-79.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
]
4848

4949
if not XSSFiltersInCaller:
50-
print(f"CWE-79 is detected in method, {caller.fullName}")
50+
print(f"CWE-79 is detected in method, {caller.fullName}")

CWE-79/README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
# Detect CWE-79 in Android Application
22

3-
This scenario seeks to find **Improper Neutralization of Input During
4-
Web Page Generation ('Cross-site Scripting')** in the APK file.
3+
This scenario seeks to find **Improper Neutralization of Input During Web Page Generation (‘Cross-site Scripting’)** in the APK file.
54

6-
## CWE-79 Improper Neutralization of Input During Web Page Generation (\'Cross-site Scripting\')
5+
## CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
76

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

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

13-
![image](https://imgur.com/jAwgD0x.png)
11+
![image](https://imgur.com/3W1QpU1.png)
1412

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

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

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

22-
## Quark Script CWE-79.py
19+
## CWE-79 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/MpUjFP0.png)
2322

24-
Let's use the above APIs to show how the Quark script finds this
25-
vulnerability.
23+
Let’s use the above APIs to show how the Quark script finds this vulnerability.
2624

27-
First, we design a detection rule `loadUrlFromIntent.json` to spot the
28-
behavior loading URL from intent data to the WebView instance.
25+
First, we design a detection rule ``loadUrlFromIntent.json`` to spot the behavior loading URL from intent data to the WebView instance.
2926

30-
Next, we use API
31-
`quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)` and
32-
`methodInstance.getArguments()` to check if the Javascript execution is
33-
enabled in the WebView. Finally, we check if there are any famous XSS
34-
filters. If NO, that may cause CWE-79 vulnerability.
27+
Next, we use API ``quarkResultInstance.findMethodInCaller(callerMethod, targetMethod)`` and ``methodInstance.getArguments()`` to check if the Javascript execution is enabled in the WebView. Finally, we check if there are any famous XSS filters. If **NO**, that may cause CWE-79 vulnerability.
28+
29+
## Quark Script CWE-79.py
3530

36-
``` python
31+
![image](https://imgur.com/NyMpLZW.png)
32+
33+
```python
3734
from quark.script import runQuarkAnalysis, Rule
3835

3936
SAMPLE_PATH = "Vuldroid.apk"
@@ -88,7 +85,9 @@ for loadUrl in quarkResult.behaviorOccurList:
8885

8986
## Quark Rule: loadUrlFromIntent.json
9087

91-
``` json
88+
![image](https://imgur.com/m4aa4Jk.png)
89+
90+
```json
9291
{
9392
"crime": "Load URL from intent to WebView",
9493
"permission": [],
@@ -111,7 +110,7 @@ for loadUrl in quarkResult.behaviorOccurList:
111110

112111
## Quark Script Result
113112

114-
``` TEXT
113+
```TEXT
115114
$ python CWE-79.py
116115
CWE-79 is detected in method, Lcom/vuldroid/application/ForgetPassword; onCreate (Landroid/os/Bundle;)V
117-
```
116+
```

CWE-88/CWE-88.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
if methodCalled.intersection(STRING_MATCHING_API) and not ExternalStringCommand.hasString(delimeter):
2929
continue
3030
else:
31-
print(f"CWE-88 is detected in method, {caller.fullName}")
31+
print(f"CWE-88 is detected in method, {caller.fullName}")
32+

CWE-88/README.md

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,79 @@
1-
# Detect CWE-88 in Android Application
2-
1+
# Detect CWE-88 in Android Application
32

43
This scenario seeks to find **Argument Injection** in the APK file.
54

65
## CWE-88 Improper Neutralization of Argument Delimiters in a Command
76

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

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

13-
![image](https://imgur.com/7EBPGUT.png)
11+
![image](https://imgur.com/5vfXkIE.png)
1412

1513
## Code of CWE-88 in vuldroid.apk
1614

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

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

22-
## Quark Script: CWE-88.py
19+
## CWE-88 Detection Process Using Quark Script API
20+
21+
![image](https://imgur.com/s7Ajr6M.png)
2322

24-
Let's use the above APIs to show how the Quark script finds this
25-
vulnerability.
23+
Let‘s use the above APIs to show how the Quark script finds this vulnerability.
2624

27-
First, we design a detection rule `ExternalStringsCommands.json` to spot
28-
on behavior using external strings as commands.
25+
First, we design a detection rule ``ExternalStringsCommands.json`` to spot on behavior using external strings as commands.
2926

30-
Next, we use Quark API `behaviorInstance.getMethodsInArgs()` to get the
31-
methods that passed the external command.
27+
Next, we use Quark API ``behaviorInstance.getMethodsInArgs()`` to get the methods that passed the external command.
3228

33-
Then we check if the method neutralizes any special elements in the
34-
argument.
29+
Then we check if the method neutralizes any special elements in the argument.
3530

36-
If the neutralization is not complete, then it may cause CWE-88
37-
vulnerability.
31+
If the neutralization is not complete, then it may cause CWE-88 vulnerability.
32+
33+
## Quark Script: CWE-88.py
3834

39-
``` python
35+
![image](https://imgur.com/f8Yee3P.png)
36+
37+
```python
4038
from quark.script import runQuarkAnalysis, Rule, findMethodInAPK
4139

42-
SAMPLE_PATH = "Vuldroid.apk"
43-
RULE_PATH = "ExternalStringCommand.json"
40+
SAMPLE_PATH = "Vuldroid.apk"
41+
RULE_PATH = "ExternalStringCommand.json"
42+
4443

44+
STRING_MATCHING_API = set([
45+
("Ljava/lang/String;", "contains", "(Ljava/lang/CharSequence)Z"),
46+
("Ljava/lang/String;", "indexOf", "(I)I"),
47+
("Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"),
48+
("Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"),
49+
("Ljava/lang/String;", "replaceAll", "(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;")
50+
])
4551

46-
STRING_MATCHING_API = set([
47-
("Ljava/lang/String;", "contains", "(Ljava/lang/CharSequence)Z"),
48-
("Ljava/lang/String;", "indexOf", "(I)I"),
49-
("Ljava/lang/String;", "indexOf", "(Ljava/lang/String;)I"),
50-
("Ljava/lang/String;", "matches", "(Ljava/lang/String;)Z"),
51-
("Ljava/lang/String;", "replaceAll", "(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;")
52-
])
52+
delimeter = "-"
5353

54-
delimeter = "-"
54+
ruleInstance = Rule(RULE_PATH)
55+
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
5556

56-
ruleInstance = Rule(RULE_PATH)
57-
quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance)
57+
for ExternalStringCommand in quarkResult.behaviorOccurList:
5858

59-
for ExternalStringCommand in quarkResult.behaviorOccurList:
59+
methodCalled = set()
60+
caller = ExternalStringCommand.methodCaller
6061

61-
methodCalled = set()
62-
caller = ExternalStringCommand.methodCaller
62+
for method in ExternalStringCommand.getMethodsInArgs():
63+
methodCalled.add(method.fullName)
6364

64-
for method in ExternalStringCommand.getMethodsInArgs():
65-
methodCalled.add(method.fullName)
65+
if methodCalled.intersection(STRING_MATCHING_API) and not ExternalStringCommand.hasString(delimeter):
66+
continue
67+
else:
68+
print(f"CWE-88 is detected in method, {caller.fullName}")
6669

67-
if methodCalled.intersection(STRING_MATCHING_API) and not ExternalStringCommand.hasString(delimeter):
68-
continue
69-
else:
70-
print(f"CWE-88 is detected in method, {caller.fullName}")
7170
```
7271

7372
## Quark Rule: ExternalStringCommand.json
7473

75-
``` json
74+
![image](https://imgur.com/s9QNF19.png)
75+
76+
```json
7677
{
7778
"crime": "Using external strings as commands",
7879
"permission": [],
@@ -95,7 +96,7 @@ for ExternalStringCommand in quarkResult.behaviorOccurList:
9596

9697
## Quark Script Result
9798

98-
``` TEXT
99+
```
99100
$ python3 CWE-88.py
100101
CWE-88 is detected in method, Lcom/vuldroid/application/RootDetection; onCreate (Landroid/os/Bundle;)V
101102
```

0 commit comments

Comments
 (0)