-
-
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 10 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,18 @@ | ||
| --- | ||
| title: Store Data Encrypted in App Sandbox Directory | ||
| alias: store-data-encrypted-in-the-app-sandbox-directory | ||
| id: MASTG-BEST-0x31 | ||
| 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, API keys, 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. | ||
|
|
||
| `EncryptedSharedPreferences` wraps `SharedPreferences`, uses key material protected by the Android Keystore, and encrypts preference keys and values before storing them. | ||
|
|
||
| !!! note | ||
| `EncryptedSharedPreferences` is part of the Jetpack Security Crypto library, which has been [deprecated](https://developer.android.com/privacy-and-security/cryptography#jetpack_security_crypto_library). Until Android provides an official replacement, it remains a practical mitigation for apps that need to keep using `SharedPreferences` for sensitive data. Monitor Android's cryptography guidance and plan migrations when a supported replacement becomes available. | ||
|
|
||
| As Android recommends [`DataStore`](https://developer.android.com/topic/libraries/architecture/datastore) as a modern replacement for `SharedPreferences`, but `DataStore` doesn't encrypt data by default. If you migrate sensitive data to `DataStore`, apply an appropriate encryption layer before writing the data. | ||
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 |
|---|---|---|
|
|
@@ -4,9 +4,47 @@ platform: android | |
| id: MASTG-TEST-0287 | ||
| type: [static, code] | ||
| weakness: MASWE-0006 | ||
| best-practices: [] | ||
| best-practices: [MASTG-BEST-0x31] | ||
| 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 | ||
|
|
||
| When apps use the `SharedPreferences` API to store sensitive data without encryption, the data is written to plain-text XML files in the app's sandbox at `/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). | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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.
|
||
|
|
||
| ## Steps | ||
|
|
||
| 1. Install the app on a device (@MASTG-TECH-0005). | ||
| 2. Make sure you have @MASTG-TOOL-0145 installed on your machine and the Frida server running on the device. | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
| 3. Execute a method trace (@MASTG-TECH-0033) targeting the `SharedPreferences.Editor` methods that write data: | ||
|
cpholguera marked this conversation as resolved.
Outdated
cpholguera marked this conversation as resolved.
Outdated
|
||
| - `SharedPreferences.Editor.putString` | ||
| - `SharedPreferences.Editor.putStringSet` | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
| 4. Include tracing of cryptographic APIs to help determine if values are encrypted: | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
| - `javax.crypto.Cipher.*` | ||
| - `java.security.KeyStore.*` | ||
| - `javax.crypto.KeyGenerator.*` | ||
| - `android.util.Base64.*` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Base64 is not encryption and should not be used as a reference. Only encryption methods should be traced. |
||
| 5. Exercise the app, navigating through various workflows and inputting data that may be considered sensitive. | ||
|
cpholguera marked this conversation as resolved.
Outdated
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
|
cpholguera marked this conversation as resolved.
|
||
| ## 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 any related cryptographic operations that may indicate encryption is being used. | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Evaluation | ||
|
|
||
| The test case fails if sensitive data is written to `SharedPreferences` without being encrypted first. | ||
|
|
||
| To evaluate the output: | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
| 1. **High-level trace inspection**: Review the sequence of calls to identify if `SharedPreferences.Editor.putString` or `putStringSet` calls are preceded by `Cipher` operations. Values written without prior encryption are likely stored in cleartext. | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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.
|
||
|
|
||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
| 3. **Manual verification**: Use the stack traces to navigate to the relevant code locations in the reversed app (@MASTG-TECH-0117) and trace back the source of the values being written to confirm whether they contain sensitive data and whether encryption is applied. | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
|
|
||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
| 4. **File system inspection**: Retrieve the `SharedPreferences` XML files from the app's sandbox using @MASTG-TOOL-0004 and inspect their contents to verify whether sensitive data is stored in cleartext. | ||
|
cpholguera marked this conversation as resolved.
Outdated
|
||
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.