-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Update MASTG-KNOW-0036 and implement MASTG-TEST-0287 #3706
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
45830b4
Update MASTG-KNOW-0036 with EncryptedSharedPreferences links
cpholguera 08287f6
fix test ref
cpholguera 53098e2
Add Deterministic AEAD
cpholguera d49374d
Create content for MASTG-TEST-0287 SharedPreferences security test (#…
Copilot 7afd0af
draft best
cpholguera 8ecf00a
Merge branch 'master' into cpholguera-patch-2
cpholguera 73521a3
Merge branch 'master' into cpholguera-patch-2
cpholguera d5b2d64
Merge branch 'master' into cpholguera-patch-2
cpholguera 0d32804
Apply suggestions from code review
cpholguera 178fa89
Apply suggestion from @jacobocasado
cpholguera 55c0ffe
Apply suggestions from code review
cpholguera e39e500
Update MASTG-KNOW-0036.md to clarify SharedPreferences usage and depr…
cpholguera ab28e1b
fix md
cpholguera 1e4e9f0
Apply suggestions from code review
cpholguera 8256ce5
Update MASTG-TEST-0287.md to change test type and refine steps for va…
cpholguera 5f5680d
fix best ID
cpholguera 2e4a002
update title
cpholguera e36cec2
Merge branch 'master' into cpholguera-patch-2
cpholguera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| title: Store Data Encrypted in App Sandbox Directory | ||
| alias: store-data-encrypted-in-the-app-sandbox-directory | ||
| id: MASTG-BEST-0050 | ||
| platform: android | ||
| knowledge: [MASTG-KNOW-0036] | ||
| --- | ||
|
|
||
| Store sensitive data in `SharedPreferences` only after encrypting it. Standard `SharedPreferences` stores values in XML files inside the app's private data directory, so values such as credentials, authentication tokens, private keys, or personally identifiable information (PII) should not be stored in cleartext. | ||
|
|
||
| For apps that use `SharedPreferences`, use [`EncryptedSharedPreferences`](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences) or an equivalent mechanism that encrypts preference keys and values before they are written to disk. An equivalent mechanism should use authenticated encryption, protect encryption keys with the Android Keystore or another appropriate key management system, and avoid custom cryptography. | ||
|
|
||
| !!! note | ||
| `EncryptedSharedPreferences` is part of the Jetpack Security Crypto library. All APIs in that library were [deprecated](https://developer.android.com/privacy-and-security/cryptography#security-crypto-jetpack-deprecated) in version `1.1.0`, and Android states that there will be no subsequent releases. It may still be a practical mitigation for existing apps that must keep using `SharedPreferences`, but it should not be treated as a long term storage strategy. Monitor Android's cryptography and DataStore guidance, and plan a migration to a supported encryption approach when available. | ||
|
|
||
| Android recommends [`DataStore`](https://developer.android.com/topic/libraries/architecture/datastore) as a modern replacement for `SharedPreferences`, but `DataStore` does not encrypt data by default. If you migrate sensitive data to `DataStore`, use an appropriate encryption layer before writing the data. AndroidX introduced the `androidx.datastore:datastore-tink` artifact for DataStore encryption support in version `1.3.0-alpha07`. This provides encryption using the Tink library and [`AeadSerializer`](https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer), which wraps an existing DataStore serializer and performs authenticated encryption and decryption. See the [DataStore release notes](https://developer.android.com/jetpack/androidx/releases/datastore#1.3.0-alpha07) for more information and an example of use. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,49 @@ | ||
| --- | ||
| title: References to Sensitive Data Stored Unencrypted via the SharedPreferences API to the App Sandbox | ||
| title: Runtime Storage of Unencrypted Data via the SharedPreferences API | ||
| platform: android | ||
| id: MASTG-TEST-0287 | ||
| type: [static, code] | ||
| type: [dynamic, hooks, manual] | ||
| weakness: MASWE-0006 | ||
| best-practices: [] | ||
| best-practices: [MASTG-BEST-0050] | ||
| profiles: [L1, L2] | ||
| prerequisites: | ||
| - identify-sensitive-data | ||
| knowledge: [MASTG-KNOW-0036] | ||
| status: placeholder | ||
| note: This test checks if the app is using the SharedPreferences API to store sensitive data (e.g. user credentials, tokens) in an unencrypted format within the app's sandbox. This includes checking for the use of `SharedPreferences` without encryption as well as not using `EncryptedSharedPreferences` or similar secure storage mechanisms. | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| In Android, applications can use the [`SharedPreferences`](https://developer.android.com/reference/android/content/SharedPreferences) API to store sensitive data without encryption, typically under the app's private data directory, such as `/data/user/0/<package-name>/shared_prefs/` or `/data/data/<package-name>/shared_prefs/`. | ||
|
|
||
| While `MODE_PRIVATE` restricts file access to the app itself, it doesn't protect the data from being read by attackers who gain access to the device's file system (for example, through device compromise, backup extraction, or physical access to rooted/unlocked devices). | ||
|
|
||
| This test uses runtime instrumentation to detect when the app writes data via `SharedPreferences` and determines whether sensitive data is being stored unencrypted. | ||
|
cpholguera marked this conversation as resolved.
|
||
|
|
||
| Relevant API calls regarding `SharedPreferences` include `SharedPreferences.Editor.putString(...)` and `putStringSet(...)`, which write string values to the XML files in the app's sandbox. There's also `put*` methods for other data types, but strings are the most common way to store sensitive data such as API keys, tokens, passwords, or private keys. | ||
|
|
||
| For encryption, relevant API calls include `javax.crypto.Cipher`, `java.security.KeyStore`, or `javax.crypto.KeyGenerator`. | ||
|
|
||
| For more information about the `SharedPreferences` API, refer to @MASTG-KNOW-0036. | ||
|
|
||
| ## Steps | ||
|
|
||
| 1. Use @MASTG-TECH-0005 to install the app. | ||
| 2. Use @MASTG-TECH-0043 to hook the relevant API calls. | ||
| 3. Exercise the app extensively to trigger as many flows as possible and enter sensitive data wherever you can. | ||
| 4. Use @MASTG-TECH-0008 to retrieve the app's `SharedPreferences` XML files. | ||
|
|
||
| ## Observation | ||
|
|
||
| The output should contain a list of all calls to `SharedPreferences` write methods, including the keys, values, and stack traces showing where in the app's code these calls originate. The trace should also include related cryptographic operations that may indicate encryption is being used. | ||
|
|
||
| The output should also contain the contents of all `SharedPreferences` XML files. | ||
|
|
||
| ## Evaluation | ||
|
|
||
| The test case fails if sensitive data is written to `SharedPreferences` without being encrypted first. | ||
|
|
||
| **Further Validation Required:** | ||
|
|
||
| 1. **High-level trace inspection**: Review the sequence of calls from the hook output to identify if `SharedPreferences.Editor.putString` or `putStringSet` calls are preceded by `Cipher` operations. Values written without prior encryption are likely stored in cleartext. | ||
| 2. **Pattern matching**: Use a secrets detection tool (for example, @MASTG-TOOL-0144) to scan the output for known secret patterns such as API keys, tokens, passwords, or private keys. | ||
|
cpholguera marked this conversation as resolved.
|
||
| 3. **Manual verification**: Use the stack traces from the hook output to navigate to the relevant code locations in the reversed app (@MASTG-TECH-0023) and trace back the source of the values being written to confirm whether they contain sensitive data and whether encryption is applied. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.