-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Port MASTG-TEST-0035: Testing for Android overlay attack protections #3649
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
Draft
Copilot
wants to merge
10
commits into
master
Choose a base branch
from
copilot/add-overlay-attacks-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7a26775
Initial plan
Copilot 284a03e
Port MASTG-TEST-0035: Testing for Overlay Attacks - create v2 test, d…
Copilot 4226d8a
Apply suggestions from code review
cpholguera ac8e4b2
Use fake IDs
cpholguera 4a1481c
Merge branch 'master' into copilot/add-overlay-attacks-test
cpholguera 4005d31
Merge branch 'master' into copilot/add-overlay-attacks-test
cpholguera dbe8198
Add setHideOverlayWindows to knowledge and best practices, fix demo s…
Copilot 3e67a11
Reorganize overlay protection mechanisms and add HIDE_OVERLAY_WINDOWS…
Copilot 9ec69ee
add temp IDs
cpholguera 169fa74
Merge branch 'master' into copilot/add-overlay-attacks-test
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
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,60 @@ | ||
| --- | ||
| title: Preventing Overlay Attacks | ||
| alias: preventing-overlay-attacks | ||
| id: MASTG-BEST-0029 | ||
| platform: android | ||
| knowledge: [MASTG-KNOW-0022] | ||
| --- | ||
|
|
||
| Apps should protect sensitive user interactions from overlay attacks by implementing appropriate defensive mechanisms. Overlay attacks (including tapjacking) occur when malicious apps place deceptive UI elements over legitimate app interfaces to trick users into unintended actions. | ||
|
|
||
| ## Recommendation | ||
|
|
||
| Implement touch filtering to prevent touch events when the app's UI is obscured by another app. Use one or more of the following mechanisms: | ||
|
|
||
| 1. **Set the layout attribute `android:filterTouchesWhenObscured="true"`** for sensitive views such as login buttons, payment confirmations, or permission requests. This filters touch events when the view is obscured. | ||
|
|
||
| 2. **Call `setFilterTouchesWhenObscured(true)`** programmatically on sensitive views to enable touch filtering at runtime. | ||
|
|
||
| 3. **Call `setHideOverlayWindows(true)`** on the window (API level 31+) to hide all non-system overlay windows while the activity is in the foreground. This provides stronger protection by preventing overlays entirely rather than just filtering touch events. | ||
|
|
||
| 4. **Override `onFilterTouchEventForSecurity`** for more granular control and to implement custom security policies based on your app's specific requirements. | ||
|
|
||
| 5. **Check motion event flags** such as `FLAG_WINDOW_IS_OBSCURED` (API level 9+) or `FLAG_WINDOW_IS_PARTIALLY_OBSCURED` (API level 29+) in touch event handlers to detect obscured windows and respond appropriately. | ||
|
|
||
| Apply these protections selectively to security-sensitive UI elements where user confirmation is critical, such as: | ||
|
|
||
| - Login and authentication screens | ||
| - Permission request dialogs | ||
| - Payment confirmation buttons | ||
| - Sensitive data entry fields | ||
| - Security settings changes | ||
|
|
||
| ## Rationale | ||
|
|
||
| Without overlay protection, malicious apps can: | ||
|
|
||
| - Capture user credentials by overlaying fake login screens | ||
| - Trick users into granting dangerous permissions | ||
| - Intercept sensitive data entry | ||
| - Perform unauthorized actions by obscuring the true nature of UI elements | ||
|
|
||
| Touch filtering mechanisms help ensure that user interactions occur with the intended UI elements and not with overlays placed by malicious apps. | ||
|
|
||
| ## Caveats and Considerations | ||
|
|
||
| - Touch filtering is not a complete solution on older Android versions that have system-level vulnerabilities. Apps should target modern API levels when possible. | ||
| - Some attacks, particularly those exploiting system-level vulnerabilities (for example, Toast Overlay on Android versions before 8.0), cannot be fully mitigated at the app level. | ||
| - Applying touch filtering too broadly may impact legitimate use cases where overlays are expected (for example, system dialogs, accessibility features). | ||
| - Users can still be tricked through social engineering even with touch filtering enabled. Apps should combine these protections with user education and clear UI indicators. | ||
| - For maximum protection, apps targeting older API levels should consider upgrading their `targetSdkVersion` to benefit from platform-level protections introduced in newer Android versions. | ||
|
|
||
| ## References | ||
|
|
||
| - Android Developer Documentation: [Tapjacking](https://developer.android.com/privacy-and-security/risks/tapjacking) | ||
| - Android Developer Documentation: [View Security](https://developer.android.com/reference/android/view/View#security) | ||
| - Android Developer Documentation: [setFilterTouchesWhenObscured](https://developer.android.com/reference/android/view/View#setFilterTouchesWhenObscured(boolean)) | ||
| - Android Developer Documentation: [setHideOverlayWindows](https://developer.android.com/reference/android/view/Window#setHideOverlayWindows(boolean)) | ||
| - Android Developer Documentation: [onFilterTouchEventForSecurity](https://developer.android.com/reference/android/view/View#onFilterTouchEventForSecurity(android.view.MotionEvent)) | ||
| - Android Developer Documentation: [FLAG_WINDOW_IS_OBSCURED](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_OBSCURED) | ||
| - Android Developer Documentation: [FLAG_WINDOW_IS_PARTIALLY_OBSCURED](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_PARTIALLY_OBSCURED) | ||
52 changes: 52 additions & 0 deletions
52
demos/android/MASVS-PLATFORM/MASTG-DEMO-0x83/MASTG-DEMO-0x83.md
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,52 @@ | ||
| --- | ||
| platform: android | ||
| title: Overlay Attack Protection Implementation | ||
| id: MASTG-DEMO-0x83 | ||
| code: [kotlin, java] | ||
| test: MASTG-TEST-0x35 | ||
| tools: [semgrep] | ||
| --- | ||
|
|
||
| ### Sample | ||
|
|
||
| This sample demonstrates different approaches to protecting against overlay attacks in Android apps. It includes both a vulnerable implementation (without protection) and secure implementations using various overlay protection mechanisms. | ||
|
|
||
| {{ MastgTest.kt # MastgTest_reversed.java }} | ||
|
|
||
| The code shows three buttons: | ||
|
|
||
| 1. **Vulnerable button** - A Compose button without any overlay protection, making it susceptible to tapjacking attacks | ||
| 2. **Protected button** - A traditional Android View Button with `filterTouchesWhenObscured = true` to block touches when the window is obscured | ||
| 3. **Custom protected button** - A button with a custom implementation that overrides `onFilterTouchEventForSecurity` to manually check for the `FLAG_WINDOW_IS_OBSCURED` flag | ||
|
|
||
| ### Steps | ||
|
|
||
| Let's run our semgrep rule against the decompiled code to detect overlay protection mechanisms. | ||
|
|
||
| {{ ../../../../rules/mastg-android-overlay-protection.yml }} | ||
|
|
||
| {{ run.sh }} | ||
|
|
||
| ### Observation | ||
|
|
||
| The semgrep rule detected three instances of overlay protection mechanisms in the code: | ||
|
|
||
| {{ output.txt }} | ||
|
|
||
| The output shows: | ||
|
|
||
| 1. Line 59: `setFilterTouchesWhenObscured(true)` - enabling touch filtering on the protected button | ||
| 2. Lines 73-79: `onFilterTouchEventForSecurity` - custom override implementation | ||
| 3. Line 74: Check for `FLAG_WINDOW_IS_OBSCURED` flag - detecting when the window is obscured | ||
|
|
||
| ### Evaluation | ||
|
|
||
| The test partially passes and partially fails: | ||
|
|
||
| **FAIL:** The first button (lines 38-48 in the Kotlin code, not shown in the output) does not implement any overlay protection. This button performs a sensitive action (payment confirmation) and should be protected against overlay attacks. | ||
|
|
||
| **PASS:** The second button (line 59 in the decompiled output) properly implements overlay protection using `setFilterTouchesWhenObscured(true)`, which will filter touch events when the view is obscured by another window. | ||
|
|
||
| **PASS:** The third button (lines 73-79 in the decompiled output) implements custom overlay protection by overriding `onFilterTouchEventForSecurity` and manually checking the `FLAG_WINDOW_IS_OBSCURED` flag. This provides fine-grained control over how the app responds to overlay attempts. | ||
|
|
||
| In a real-world assessment, the vulnerable button should be flagged as a finding. Sensitive UI elements such as payment confirmations, permission grants, or authentication buttons should implement overlay protection using one of the demonstrated mechanisms. |
|
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. Demo is missing: class MastgTest (private val context: Context){
fun shouldRunInMainThread(): Boolean = true
fun mastgTest(): String { |
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,58 @@ | ||
| package org.owasp.mastestapp | ||
|
|
||
| import android.content.Context | ||
| import android.view.MotionEvent | ||
| import android.widget.Button | ||
| import android.widget.LinearLayout | ||
|
|
||
| // SUMMARY: This sample demonstrates different approaches to handling overlay attacks in Android apps, | ||
| // showing both vulnerable patterns and proper protections using filterTouchesWhenObscured. | ||
|
|
||
| class MastgTest (private val context: Context){ | ||
|
|
||
| fun mastgTest(): String { | ||
| val layout = LinearLayout(context) | ||
| layout.orientation = LinearLayout.VERTICAL | ||
|
|
||
| // FAIL: [MASTG-TEST-0x35] Sensitive button without overlay protection | ||
| val vulnerableButton = Button(context).apply { | ||
| text = "Vulnerable: Confirm Payment" | ||
| setOnClickListener { | ||
| // Sensitive action: confirming a payment | ||
| } | ||
| } | ||
| layout.addView(vulnerableButton) | ||
|
|
||
| // PASS: [MASTG-TEST-0x35] Button with overlay protection using filterTouchesWhenObscured | ||
| val protectedButton = Button(context).apply { | ||
| text = "Protected: Confirm Payment" | ||
| filterTouchesWhenObscured = true | ||
| setOnClickListener { | ||
| // Sensitive action protected from overlay attacks | ||
| } | ||
| } | ||
| layout.addView(protectedButton) | ||
|
|
||
| // PASS: [MASTG-TEST-0x35] Custom view with manual obscured check | ||
| val customProtectedButton = object : Button(context) { | ||
| override fun onFilterTouchEventForSecurity(event: MotionEvent): Boolean { | ||
| if ((event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { | ||
| // Window is obscured, filter the touch event | ||
| return false | ||
| } | ||
| return super.onFilterTouchEventForSecurity(event) | ||
| } | ||
| }.apply { | ||
| text = "Custom Protection: Grant Permission" | ||
| setOnClickListener { | ||
| // Sensitive permission grant protected by custom implementation | ||
| } | ||
| } | ||
| layout.addView(customProtectedButton) | ||
|
|
||
| return "Created buttons with various overlay protections:\n" + | ||
| "1. Vulnerable button (no protection)\n" + | ||
| "2. Protected button (filterTouchesWhenObscured)\n" + | ||
| "3. Custom protected button (onFilterTouchEventForSecurity)" | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
demos/android/MASVS-PLATFORM/MASTG-DEMO-0x83/MastgTest_reversed.java
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,59 @@ | ||
| package org.owasp.mastestapp; | ||
|
|
||
| import android.content.Context; | ||
| import android.view.MotionEvent; | ||
| import android.widget.Button; | ||
| import android.widget.LinearLayout; | ||
| import kotlin.jvm.internal.Intrinsics; | ||
|
|
||
| public final class MastgTest { | ||
| private final Context context; | ||
|
|
||
| public MastgTest(Context context) { | ||
| Intrinsics.checkNotNullParameter(context, "context"); | ||
| this.context = context; | ||
| } | ||
|
|
||
| public final String mastgTest() { | ||
| LinearLayout layout = new LinearLayout(this.context); | ||
| layout.setOrientation(1); | ||
|
|
||
| // FAIL: [MASTG-TEST-0x35] Sensitive button without overlay protection | ||
| Button vulnerableButton = new Button(this.context); | ||
| vulnerableButton.setText("Vulnerable: Confirm Payment"); | ||
| vulnerableButton.setOnClickListener(view -> { | ||
| // Sensitive action: confirming a payment | ||
| }); | ||
| layout.addView(vulnerableButton); | ||
|
|
||
| // PASS: [MASTG-TEST-0x35] Button with overlay protection using filterTouchesWhenObscured | ||
| Button protectedButton = new Button(this.context); | ||
| protectedButton.setText("Protected: Confirm Payment"); | ||
| protectedButton.setFilterTouchesWhenObscured(true); | ||
| protectedButton.setOnClickListener(view -> { | ||
| // Sensitive action protected from overlay attacks | ||
| }); | ||
| layout.addView(protectedButton); | ||
|
|
||
| // PASS: [MASTG-TEST-0x35] Custom view with manual obscured check | ||
| Button customProtectedButton = new Button(this.context) { | ||
| public boolean onFilterTouchEventForSecurity(MotionEvent event) { | ||
| if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { | ||
| // Window is obscured, filter the touch event | ||
| return false; | ||
| } | ||
| return super.onFilterTouchEventForSecurity(event); | ||
| } | ||
| }; | ||
| customProtectedButton.setText("Custom Protection: Grant Permission"); | ||
| customProtectedButton.setOnClickListener(view -> { | ||
| // Sensitive permission grant protected by custom implementation | ||
| }); | ||
| layout.addView(customProtectedButton); | ||
|
|
||
| return "Created buttons with various overlay protections:\n" + | ||
| "1. Vulnerable button (no protection)\n" + | ||
| "2. Protected button (filterTouchesWhenObscured)\n" + | ||
| "3. Custom protected button (onFilterTouchEventForSecurity)"; | ||
| } | ||
| } |
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,29 @@ | ||
|
|
||
|
|
||
| ┌─────────────────┐ | ||
| │ 4 Code Findings │ | ||
| └─────────────────┘ | ||
|
|
||
| MastgTest_reversed.java | ||
| ❱ [1mrules.mastg-android-overlay-protection-setfiltertoucheswhenobscured[0m | ||
| [MASVS-PLATFORM-3] setFilterTouchesWhenObscured is used to protect against overlay attacks | ||
|
|
||
| 59┆ button.setFilterTouchesWhenObscured(true); | ||
|
|
||
| ❱ [1mrules.mastg-android-overlay-protection-onfiltertoucheventforsecurity[0m | ||
| [MASVS-PLATFORM-3] onFilterTouchEventForSecurity is overridden for custom overlay protection | ||
|
|
||
| 73┆ public boolean onFilterTouchEventForSecurity(MotionEvent event) { | ||
| 74┆ if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { | ||
| 75┆ Toast.makeText(this.getContext(), "Touch blocked - window obscured", 0).show(); | ||
| 76┆ return false; | ||
| 77┆ } | ||
| 78┆ return super.onFilterTouchEventForSecurity(event); | ||
| 79┆ } | ||
|
|
||
| ❱ [1mrules.mastg-android-overlay-protection-flag-window-is-obscured[0m | ||
| [MASVS-PLATFORM-3] FLAG_WINDOW_IS_OBSCURED is checked to detect overlay attacks | ||
|
|
||
| 74┆ if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { | ||
| ⋮┆---------------------------------------- | ||
| 74┆ if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { |
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,4 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Run semgrep to detect overlay protection mechanisms | ||
| NO_COLOR=true semgrep -c ../../../../rules/mastg-android-overlay-protection.yml ./MastgTest_reversed.java --text -o output.txt |
cpholguera marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot In the 1st and 2nd bullet points above you described one mechanism but applied in two different ways. The 3rd bullet points(this one) also has another way to apply the same technique by setting HIDE_OVERLAY_WINDOWS. I would even say this might be the most reliable way to stop overlays on supported API versions. I would add it too to the list.
Also, how about we sort this list by the most robust solutions. I believe
HIDE_OVERLAY_WINDOWS/setHideOverlayWindowswould be first one.Then I would mention
android:filterTouchesWhenObscured/setFilterTouchesWhenObscured.Checking
FLAG_WINDOW_IS_OBSCUREDandFLAG_WINDOW_IS_PARTIALLY_OBSCUREDmight belong to a different category. They don't prevent touching UI if there is an overlay on the screen. Instead, they detect these overlays and allow to response accordingly. This might be worth mentioning.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot In the 1st and 2nd bullet points above you described one mechanism but applied in two different ways. The 3rd bullet points(this one) also has another way to apply the same technique by setting HIDE_OVERLAY_WINDOWS. I would even say this might be the most reliable way to stop overlays on supported API versions. I would add it too to the list.
Also, how about we sort this list by the most robust solutions. I believe
HIDE_OVERLAY_WINDOWS/setHideOverlayWindowswould be first one.Then I would mention
android:filterTouchesWhenObscured/setFilterTouchesWhenObscured.Checking
FLAG_WINDOW_IS_OBSCUREDandFLAG_WINDOW_IS_PARTIALLY_OBSCUREDmight belong to a different category. They don't prevent touching UI if there is an overlay on the screen. Instead, they detect these overlays and allow to response accordingly. This might be worth mentioning.Also, let's add Let's also add HIDE_OVERLAY_WINDOWS in knowledge/android/MASVS-PLATFORM/MASTG-KNOW-0022.md