|
| 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 | +``` |
0 commit comments