Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MASWE-0007] Sensitive Data Stored Unencrypted in Shared Storage Requiring No User Interaction #2594

Merged
merged 39 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0bc19a7
Add Risk and Tests for: Sensitive Data Stored Unencrypted in External…
serek8 Mar 5, 2024
af50568
Update risks/MASVS-STORAGE/1-store-sensitive-data-securely/data-unenc…
serek8 May 6, 2024
dc5a465
Fix spellings
serek8 Mar 5, 2024
e0ba4e1
Update tests and examples
serek8 May 7, 2024
7d23021
Update the title of a static test
serek8 May 7, 2024
d2df0de
Update examples and fix spellings
serek8 May 7, 2024
cc126f0
Added rules from Olivier
serek8 May 7, 2024
3cb96d2
Apply suggestions from code review
serek8 Jun 3, 2024
dfb01ff
Rename Sample to Demo
serek8 Jun 3, 2024
badad99
Update demo-2 with a reversed manifest file
serek8 Jun 3, 2024
5e5c8dc
Mention iOS in Risks
serek8 Jun 4, 2024
ac00a36
Update Demos with the MASTestApp
serek8 Jun 4, 2024
44b625b
Update demo-1
serek8 Jun 4, 2024
bdcea87
Add a new demo and refactor existing demos
serek8 Jun 5, 2024
1d0dbb6
Add a demo with listing all files
serek8 Jun 5, 2024
9ad1169
Fix the spelling errors
serek8 Jun 5, 2024
e8fab33
fix md lint issues
cpholguera Jun 7, 2024
e8093a9
fix md lint issues
cpholguera Jun 7, 2024
d79b2d8
update rules to remove false positive separating manifest from apis. …
cpholguera Jun 15, 2024
e17a638
minor corrections in android-data-unencrypted-shared-storage-no-user-…
cpholguera Jun 15, 2024
e7c2902
merge demo-4 into demo-1
cpholguera Jun 21, 2024
769257c
updated kotlin samples to include a password-like and API key-like st…
cpholguera Jun 21, 2024
709bfd4
Minor update to the risk mitigations paragraph.
cpholguera Jun 21, 2024
77ddf31
Updated tests titles and consolidated content. Additional content reg…
cpholguera Jun 21, 2024
247a928
Consolidated tests sections and linked to relevant techniques.
cpholguera Jun 21, 2024
858b4bd
Consolidated demos sections and titles. Added more details to the obs…
cpholguera Jun 21, 2024
81610ae
Remove SARIF support for now
cpholguera Jun 21, 2024
665171e
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 22, 2024
252ac64
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 23, 2024
7f4809c
fix paths to snippets
cpholguera Jun 23, 2024
dff1834
added one CWE and android risk maaping, some additional clarification…
cpholguera Jun 24, 2024
f68d567
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 24, 2024
d61e867
fix links to tools and tech
cpholguera Jun 24, 2024
99d0776
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 24, 2024
e373dcc
rename risk to weakness
cpholguera Jun 24, 2024
d44da95
move all to the weaknesses folder
cpholguera Jun 24, 2024
b8ad87d
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 24, 2024
b582669
Merge branch 'master' of https://github.com/OWASP/owasp-mastg into pr…
cpholguera Jun 24, 2024
edeee04
include link to frida and remove ref to run.sh from test
cpholguera Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update tests and examples
  • Loading branch information
serek8 committed May 7, 2024
commit e0ba4e1655874936578d6e85fef6b9017a70d1fd
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
---
platform: android
title: File Tracing
tools: [frida-trace]
tools: [frida]
code: [kotlin]
---

### Sample

The snippet below shows sample code that creates a file in external storage. You can put this code into your app and follow the steps to see a sample usage of `frida-trace` to identify a potential data leak.
The snippet below shows sample code that creates a file in external storage. You can put this code into your app and follow the steps below to identify a potential data leak.
serek8 marked this conversation as resolved.
Show resolved Hide resolved

{{ snippet.kt }}

### Steps

Execute `frida-trace` against the sample app to trace all usage of file IO.
1. Update the Package ID of your app inside `run.sh`
2. Execute `run.sh` against the sample app to trace all usage of file IO.
3. Close the app once you finish testing

{{ run.sh }}

### Observation

`frida-trace` has identified one file path in the external storage that the app opened.
The script will output the findings into `output.txt`

{{ output.txt }}

### Evaluation

Review each of the reported instances by manually opening a file and inspecting its content.

NOTE: If you want to test more file locations than only the file locations inside the external storage, remove `grep` from the script.
Review each warning in the output file and make sure you intended to store this file in the external storage.
serek8 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
638 ms open(path="/storage/emulated/0/Android/data/com.example/files/secret.json", oflag=0x241)
[WARNING] Opening a file from external storage at: /storage/emulated/0/Android/data/com.gs.owasp_storage_app1/files/secret.json
Invoked from: java.lang.Exception
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7581)
at libcore.io.IoBridge.open(IoBridge.java:482)
at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
at com.gs.owasp_storage_app1.MainActivity.runOwasp(MainActivity.kt:155)
at com.gs.owasp_storage_app1.MainActivity.onCreate$lambda$1(MainActivity.kt:36)
at com.gs.owasp_storage_app1.MainActivity.$r8$lambda$He9NTWO6dCDqltooAd5-9ovuSZ8(Unknown Source:0)
at com.gs.owasp_storage_app1.MainActivity$$ExternalSyntheticLambda1.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7201)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

# SUMMARY: This script uses frida-trace to trace files that an app has opened since it spawned
# SUMMARY: This script uses frida to trace files that an app has opened since it spawned
# The script filters the output of frida-trace to print only the paths belonging to external
# storage but the the predefined list of external storage paths might not be complete.
# A sample output is shown in "output.txt". If the output is empty, it indicates that no external
# storage is used.

frida-trace \
frida \
-U \
-f com.example \
-i "open" \
| grep -E "(/sdcard|/storage)"
-f com.gs.owasp_storage_app2 \
-l script.js \
-o output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Intercept libc`open to make sure we cover all Java's APIs
Interceptor.attach(Module.getExportByName(null, 'open'), {
onEnter(args) {
const external_paths = ['/sdcard', '/storage/emulated']
const path = Memory.readCString(ptr(args[0]));
external_paths.forEach(external_path => {
if(path.indexOf(external_path) == 0){
console.log('[WARNING] Opening a file from external storage at:', path)
Java.performNow(function() {
console.log("Invoked from: "+Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()))
});
}
});
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
platform: android
title: Storing Data in External Locations
title: Data Stored to External Locations on Runtime
apis: [Environment#getExternalStorageDirectory, Environment#getExternalStorageDirectory, Environment#getExternalFilesDir, Environment#getExternalCacheDir, SharedPreferences, FileOutPutStream]
type: [dynamic]
---
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could replace this test with another one that is solely focused on the files themselves.

  • Benefits: quick way to get file changes and search sensitive data on them
  • Limitations: we'll only see files for the current testing session and we won't know what part of the code is responsible

Steps:

  1. Setup the initial time
start_time=$(date +%s)
  1. Exercise the app

  2. Diff and pull all new/changed files

adb shell "find /sdcard/ -type f -newermt @${start_time}" > new_files.txt

mkdir -p new_files
while read -r line; do
  adb pull "$line" ./new_files/
done < new_files.txt

Evaluation: inspect the files and if they contain sensitive data this test fails.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will add it as another test

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
platform: android
title: Find common APIs that return paths to External Storage locations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example seems to be about Scoped Storage, maybe we can adapt the title and text to it.

I created a new example that you can use for the non-scoped storage case: output-demo-4.zip

tools: [semgrep]
code: [java]
code: [kotlin]
---

### Sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"results": [
{
"fingerprints": {
"matchBasedId/v1": "7fff0cca477ae1b0a93a5b8e265d8a7471c4144464dcbbd55d7256be707b55568882a55bbac36d334816deb905d562cb7f9c4cee168fb02f8ca6f31936c62fb7_0"
"matchBasedId/v1": "86181ae035702aa50a67f6c5fcae0e9cd18f97887aa9e1ffcb70225f86d21062067e3925de964b722f4593b60fd959f1b9b0778f189a7183e0609be44faea0c5_0"
},
"locations": [
{
Expand All @@ -33,10 +33,10 @@
}
],
"message": {
"text": "[MASVS-STORAGE] Make sure you encrypt files at these locations if necessary"
"text": "[MASVS-STORAGE] Make sure to encrypt files at these locations if necessary"
},
"properties": {},
"ruleId": "rules.mastg-android-data-unencrypted-external"
"ruleId": "rules.mastg-android-data-unencrypted-external-api"
}
],
"tool": {
Expand All @@ -48,20 +48,62 @@
"level": "warning"
},
"fullDescription": {
"text": "[MASVS-STORAGE] Make sure you encrypt files at these locations if necessary"
"text": "[MASVS-STORAGE] Make sure to encrypt files in external storage if necessary"
},
"help": {
"markdown": "[MASVS-STORAGE] Make sure you encrypt files at these locations if necessary",
"text": "[MASVS-STORAGE] Make sure you encrypt files at these locations if necessary"
"markdown": "[MASVS-STORAGE] Make sure to encrypt files in external storage if necessary",
"text": "[MASVS-STORAGE] Make sure to encrypt files in external storage if necessary"
},
"id": "rules.mastg-android-data-unencrypted-external",
"name": "rules.mastg-android-data-unencrypted-external",
"id": "rules.mastg-android-data-unencrypted-external-manifest",
"name": "rules.mastg-android-data-unencrypted-external-manifest",
"properties": {
"precision": "very-high",
"tags": []
},
"shortDescription": {
"text": "Semgrep Finding: rules.mastg-android-data-unencrypted-external"
"text": "Semgrep Finding: rules.mastg-android-data-unencrypted-external-manifest"
}
},
{
"defaultConfiguration": {
"level": "warning"
},
"fullDescription": {
"text": "[MASVS-STORAGE] Make sure to encrypt files at these locations if necessary"
},
"help": {
"markdown": "[MASVS-STORAGE] Make sure to encrypt files at these locations if necessary",
"text": "[MASVS-STORAGE] Make sure to encrypt files at these locations if necessary"
},
"id": "rules.mastg-android-data-unencrypted-external-api",
"name": "rules.mastg-android-data-unencrypted-external-api",
"properties": {
"precision": "very-high",
"tags": []
},
"shortDescription": {
"text": "Semgrep Finding: rules.mastg-android-data-unencrypted-external-api"
}
},
{
"defaultConfiguration": {
"level": "warning"
},
"fullDescription": {
"text": "[MASVS-STORAGE] Make sure to want this data to be shared with other apps"
},
"help": {
"markdown": "[MASVS-STORAGE] Make sure to want this data to be shared with other apps",
"text": "[MASVS-STORAGE] Make sure to want this data to be shared with other apps"
},
"id": "rules.mastg-android-data-unencrypted-external-mediastore",
"name": "rules.mastg-android-data-unencrypted-external-mediastore",
"properties": {
"precision": "very-high",
"tags": []
},
"shortDescription": {
"text": "Semgrep Finding: rules.mastg-android-data-unencrypted-external-mediastore"
}
}
],
Expand All @@ -71,4 +113,4 @@
}
],
"version": "2.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@


┌────────────────┐
│ 1 Code Finding │
└────────────────┘

use-of-external-store.kt
rules.mastg-android-data-unencrypted-external 0m
[MASVS-STORAGE] Make sure you encrypt files at these locations if necessary
❯❱ rules.mastg-android-data-unencrypted-external-api
[MASVS-STORAGE] Make sure to encrypt files at these locations if necessary

26┆ val externalDirPath = getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
platform: android
title: Common APIs to return paths to External Storage
title: Find permissions that allows an app to write to locations shared with other apps
tools: [semgrep]
code: [java]
code: [kotlin]
---

### Sample
Expand Down
Loading