|
| 1 | +# Detect CWE-24 in Android Application |
| 2 | + |
| 3 | +This scenario aims to demonstrate the detection of the **Relative Path Traversal** vulnerability. |
| 4 | + |
| 5 | +## CWE-24: Path Traversal: '../filedir' |
| 6 | + |
| 7 | +We analyze the definition of CWE-24 and identify its characteristics. |
| 8 | + |
| 9 | +See [CWE-24](https://cwe.mitre.org/data/definitions/24.html) for more details. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Code of CWE-24 in ovaa.apk |
| 14 | + |
| 15 | +We use the [ovaa.apk](https://github.com/oversecured/ovaa) sample to explain the vulnerability code of CWE-24. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## CWE-24 Detection Process Using Quark Script API |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Let’s use the above APIs to show how the Quark script finds this vulnerability. |
| 24 | + |
| 25 | +To begin with, we create a detection rule named ``accessFileInExternalDir.json`` to identify behavior that accesses a file in an external directory. |
| 26 | + |
| 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. |
| 28 | + |
| 29 | +Finally, 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, and `getParamValues(none)` to retrieve the parameters. |
| 30 | + |
| 31 | +If no API is found or `"../"` is not in parameters, that implies the APK does not neutralize the special element `../` within the argument, possibly resulting in CWE-24 vulnerability. |
| 32 | + |
| 33 | +## Quark Script: CWE-24.py |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +```python |
| 38 | +from quark.script import runQuarkAnalysis, Rule |
| 39 | + |
| 40 | +SAMPLE_PATH = "ovaa.apk" |
| 41 | +RULE_PATH = "accessFileInExternalDir.json" |
| 42 | + |
| 43 | + |
| 44 | +STRING_MATCHING_API = [ |
| 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 | + [ |
| 50 | + "Ljava/lang/String;", |
| 51 | + "replaceAll", |
| 52 | + "(Ljava/lang/String; Ljava/lang/String;)Ljava/lang/String;", |
| 53 | + ], |
| 54 | +] |
| 55 | + |
| 56 | +ruleInstance = Rule(RULE_PATH) |
| 57 | +quarkResult = runQuarkAnalysis(SAMPLE_PATH, ruleInstance) |
| 58 | + |
| 59 | +for accessExternalDir in quarkResult.behaviorOccurList: |
| 60 | + |
| 61 | + filePath = accessExternalDir.secondAPI.getArguments()[2] |
| 62 | + |
| 63 | + if quarkResult.isHardcoded(filePath): |
| 64 | + continue |
| 65 | + |
| 66 | + caller = accessExternalDir.methodCaller |
| 67 | + strMatchingAPIs = [ |
| 68 | + api |
| 69 | + for api in STRING_MATCHING_API |
| 70 | + if quarkResult.findMethodInCaller(caller, api) |
| 71 | + ] |
| 72 | + |
| 73 | + if not strMatchingAPIs or "../" not in accessExternalDir.getParamValues(): |
| 74 | + print(f"CWE-24 is detected in method, {caller.fullName}") |
| 75 | +``` |
| 76 | + |
| 77 | +## Quark Rule: accessFileInExternalDir.json |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +```json |
| 82 | +{ |
| 83 | + "crime": "Access a file in an external directory", |
| 84 | + "permission": [], |
| 85 | + "api": [ |
| 86 | + { |
| 87 | + "class": "Landroid/os/Environment;", |
| 88 | + "method": "getExternalStorageDirectory", |
| 89 | + "descriptor": "()Ljava/io/File;" |
| 90 | + }, |
| 91 | + { |
| 92 | + "class": "Ljava/io/File;", |
| 93 | + "method": "<init>", |
| 94 | + "descriptor": "(Ljava/io/File;Ljava/lang/String;)V" |
| 95 | + } |
| 96 | + ], |
| 97 | + "score": 1, |
| 98 | + "label": [] |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Quark Script Result |
| 103 | + |
| 104 | +``` |
| 105 | +$ python3 CWE-24.py |
| 106 | +CWE-24 is detected in method, Loversecured/ovaa/providers/TheftOverwriteProvider; openFile (Landroid/net/Uri; Ljava/lang/String;)Landroid/os/ParcelFileDescriptor; |
| 107 | +``` |
0 commit comments