packageorg.owasp.mastestappimportandroid.app.Activityimportandroid.content.Contextimportandroid.content.Intentimportandroid.graphics.Colorimportandroid.graphics.PixelFormatimportandroid.net.Uriimportandroid.os.Buildimportandroid.provider.Settingsimportandroid.view.Gravityimportandroid.view.Viewimportandroid.view.WindowManagerimportandroid.widget.Toast// SUMMARY: This sample demonstrates an attacker app that uses the SYSTEM_ALERT_WINDOW permission// to draw a visible overlay over other apps. It is used to demonstrate how overlay attacks work// against apps that lack overlay protections (see MASTG-DEMO-0x01).classMastgTest(privatevalcontext:Context){valshouldRunInMainThread=truecompanionobject{privatevaroverlayView:View? =null}funmastgTest():String{if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M&&!Settings.canDrawOverlays(context)){valintent=Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:${context.packageName}")).apply{addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)}context.startActivity(intent)Toast.makeText(context,"Grant overlay permission, then press Start again",Toast.LENGTH_LONG).show()return"Overlay permission required"}returnif(overlayView==null){showOverlay()"Overlay shown"}else{hideOverlay()"Overlay hidden"}}privatefunshowOverlay(){valwindowManager=context.getSystemService(Context.WINDOW_SERVICE)asWindowManagervaloverlay=View(context).apply{setBackgroundColor(Color.argb(140,255,0,0))}valparams=WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,800,if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY}else{@Suppress("DEPRECATION")WindowManager.LayoutParams.TYPE_PHONE},WindowManager.LayoutParams.FLAG_NOT_FOCUSABLEorWindowManager.LayoutParams.FLAG_NOT_TOUCHABLEorWindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,PixelFormat.TRANSLUCENT).apply{gravity=Gravity.CENTER}windowManager.addView(overlay,params)overlayView=overlayToast.makeText(context,"Overlay shown",Toast.LENGTH_SHORT).show()}privatefunhideOverlay(){valwindowManager=context.getSystemService(Context.WINDOW_SERVICE)asWindowManageroverlayView?.let{windowManager.removeView(it)overlayView=null}Toast.makeText(context,"Overlay hidden",Toast.LENGTH_SHORT).show()}}
You can use this attacker app to demonstrate the vulnerability shown in Missing Overlay Protection on a Sensitive View: install and run it while the victim app is in the foreground, then activate the overlay. This lets you verify whether the unprotected button in the victim app accepts touch events through the overlay.
Note that the SYSTEM_ALERT_WINDOW permission itself isn't a vulnerability in the app that declares it. It's a legitimate Android feature used by apps such as screen overlay tools, chat heads, or accessibility services. However, its presence means the app can display overlays over other apps, which can be used to conduct overlay attacks against victim apps that don't implement proper protections.
rules:-id:mastg-android-system-alert-window-permissionseverity:INFOlanguages:-xmlmetadata:summary:DetectsSYSTEM_ALERT_WINDOWpermissioninthemanifest,enablingtheapptodrawoverlaysoverotherappsmessage:"[MASVS-PLATFORM-3] SYSTEM_ALERT_WINDOW permission is declared, enabling the app to draw overlays over other apps"pattern:android:name="android.permission.SYSTEM_ALERT_WINDOW"
run.sh
1234
#!/bin/bash# Run semgrep to detect the SYSTEM_ALERT_WINDOW permission in the reversed manifestNO_COLOR=truesemgrep-c../../../../rules/mastg-android-system-alert-window.yml./AndroidManifest_reversed.xml--text>output.txt
The finding confirms that this app declares the SYSTEM_ALERT_WINDOW permission. This isn't a vulnerability in this app itself; it shows that the app has the capability to draw overlays over other apps. The actual vulnerability lies in victim apps (like the one in Missing Overlay Protection on a Sensitive View) that don't implement overlay protections on sensitive UI elements.
When assessing an app for overlay attack vulnerabilities, the threat model should include apps that can request SYSTEM_ALERT_WINDOW to create overlays.