Skip to content

Commit 2bb149e

Browse files
authored
Optimize the showcase of Quark Script CWE-295 (#47)
* Optimize the showcase of Quark Script CWE-295
1 parent 63f57a0 commit 2bb149e

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

CWE-295/CWE-295.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
"proceed", # method name
77
"()V" # descriptor
88
]
9-
OVERRIDE_METHOD = [
9+
OVERRIDDEN_METHOD = [
1010
"Landroid/webkit/WebViewClient;", # class name
1111
"onReceivedSslError", # method name
12-
"(Landroid/webkit/WebView;"+" Landroid/webkit/SslErrorHandler;" + \
12+
"(Landroid/webkit/WebView;" + " Landroid/webkit/SslErrorHandler;" + \
1313
" Landroid/net/http/SslError;)V" # descriptor
1414
]
1515

1616
for sslProceedCaller in findMethodInAPK(SAMPLE_PATH, TARGET_METHOD):
17-
if (sslProceedCaller.name == OVERRIDE_METHOD[1] and
18-
sslProceedCaller.descriptor == OVERRIDE_METHOD[2] and
19-
OVERRIDE_METHOD[0] in sslProceedCaller.findSuperclassHierarchy()):
20-
print(f"CWE-295 is detected in method, {sslProceedCaller.fullName}")
17+
if (
18+
sslProceedCaller.name == OVERRIDDEN_METHOD[1]
19+
and sslProceedCaller.descriptor == OVERRIDDEN_METHOD[2]
20+
and OVERRIDDEN_METHOD[0] in sslProceedCaller.findSuperclassHierarchy()
21+
):
22+
print(f"CWE-295 is detected in method, {sslProceedCaller.fullName}")

CWE-295/README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
# Detect CWE-295 in Android Application (InsecureShop.apk)
1+
# Detect CWE-295 in Android Application
22

3-
This scenario seeks to find **Improper Certificate Validation**. See
4-
[CWE-295](https://cwe.mitre.org/data/definitions/295.html) for more
5-
details.
3+
This scenario seeks to find **Improper Certificate Validation**.
64

7-
Let's use this [APK](https://github.com/hax0rgb/InsecureShop) and the
8-
above APIs to show how the Quark script finds this vulnerability.
5+
## CWE-295: Improper Certificate Validation
96

10-
We use the API `findMethodInAPK(samplePath, targetMethod)` to locate all
11-
`SslErrorHandler.proceed` methods. Then we need to identify whether if
12-
the method `WebViewClient.onReceivedSslError` is overrode by its
13-
subclass.
7+
We analyze the definition of CWE-295 and identify its characteristics.
148

15-
First, we check and make sure that the `methodInstance.name` is
16-
`onReceivedSslError`, and the `methodInstance.descriptor` is
17-
`(Landroid/webkit/WebView; Landroid/webkit/SslErrorHandler; Landroid/net/http/SslError;)V`.
9+
See [CWE-295](https://cwe.mitre.org/data/definitions/295.html) for more details.
1810

19-
Then we use the API `methodInstance.findSuperclassHierarchy()` to get
20-
the superclass list of the method's caller class.
11+
![image](https://imgur.com/cuZ5qPp.jpg)
2112

22-
Finally, we check the `Landroid/webkit/WebViewClient;` is on the
23-
superclass list. If **YES**, that may cause CWE-295 vulnerability.
13+
## Code of CWE-295 in InsecureShop.apk
14+
15+
We use the [InsecureShop.apk](https://github.com/hax0rgb/InsecureShop) sample to explain the vulnerability code of CWE-295.
16+
17+
![image](https://imgur.com/t7Y5clb.jpg)
2418

2519
## Quark Script CWE-295.py
2620

27-
``` python
21+
To begin with, we use the API ``findMethodInAPK(samplePath, targetMethod)`` to locate all callers of method ``SslErrorHandler.proceed``.
22+
23+
Next, we must verify whether the caller overrides the method ``WebViewClient.onReceivedSslErroris``.
24+
25+
Therefore, we check if the method name and descriptor of the caller match those of ``WebViewClient.onReceivedSslErroris``. After that, we use the API ``methodInstance.findSuperclassHierarchy()`` to check if the superclasses of the caller include ``Landroid/webkit/WebViewClient``.
26+
27+
If both are **YES**, the APK will call ``SslErrorHandler.procees`` without certificate validation when an SSL error occurs, which may cause CWE-295 vulnerability.
28+
29+
```python
2830
from quark.script import findMethodInAPK
2931

3032
SAMPLE_PATH = "insecureShop.apk"
@@ -33,24 +35,25 @@ TARGET_METHOD = [
3335
"proceed", # method name
3436
"()V" # descriptor
3537
]
36-
OVERRIDE_METHOD = [
38+
OVERRIDDEN_METHOD = [
3739
"Landroid/webkit/WebViewClient;", # class name
3840
"onReceivedSslError", # method name
39-
"(Landroid/webkit/WebView;"+" Landroid/webkit/SslErrorHandler;" + \
41+
"(Landroid/webkit/WebView;" + " Landroid/webkit/SslErrorHandler;" + \
4042
" Landroid/net/http/SslError;)V" # descriptor
4143
]
4244

4345
for sslProceedCaller in findMethodInAPK(SAMPLE_PATH, TARGET_METHOD):
44-
if (sslProceedCaller.name == OVERRIDE_METHOD[1] and
45-
sslProceedCaller.descriptor == OVERRIDE_METHOD[2] and
46-
OVERRIDE_METHOD[0] in sslProceedCaller.findSuperclassHierarchy()):
46+
if (
47+
sslProceedCaller.name == OVERRIDDEN_METHOD[1]
48+
and sslProceedCaller.descriptor == OVERRIDDEN_METHOD[2]
49+
and OVERRIDDEN_METHOD[0] in sslProceedCaller.findSuperclassHierarchy()
50+
):
4751
print(f"CWE-295 is detected in method, {sslProceedCaller.fullName}")
4852
```
4953

5054
## Quark Script Result
5155

52-
``` TEXT
56+
```TEXT
5357
$ python3 CWE-295.py
54-
Requested API level 29 is larger than maximum we have, returning API level 28 instead.
5558
CWE-295 is detected in method, Lcom/insecureshop/util/CustomWebViewClient; onReceivedSslError (Landroid/webkit/WebView; Landroid/webkit/SslErrorHandler; Landroid/net/http/SslError;)V
5659
```

0 commit comments

Comments
 (0)