This sample checks if the device has a secure lock screen via KeyguardManager.isDeviceSecure and if the device supports strong biometric authentication using BiometricManager.canAuthenticate.
packageorg.owasp.mastestappimportandroid.app.KeyguardManagerimportandroid.content.Contextimportandroid.hardware.biometrics.BiometricManagerimportandroid.os.BuildclassMastgTest(privatevalcontext:Context){funmastgTest():String{valisLocked=isDeviceSecure(context)valbiometricStatus=checkStrongBiometricStatus()return"Device has a passcode: $isLocked\n\n"+"Biometric status: $biometricStatus"}/** * Checks if the device has a secure lock screen (e.g., PIN, pattern, password). * * @return `true` if the device has a secure lock screen, `false` otherwise. */funisDeviceSecure(context:Context):Boolean{valkeyguardManager=context.getSystemService(Context.KEYGUARD_SERVICE)asKeyguardManagerreturnkeyguardManager.isDeviceSecure}/** * Checks if the device supports strong biometric authentication (e.g., fingerprint, face, iris) * and if the user has enrolled biometric credentials. * * **Note:** This API is available on API level 30 (Android R) and above. * * @return A human-readable string describing the biometric status. */funcheckStrongBiometricStatus():String{if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.R){valbiometricManager=context.getSystemService(BiometricManager::class.java)valresult=biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)returnwhen(result){BiometricManager.BIOMETRIC_SUCCESS->"BIOMETRIC_SUCCESS - Strong biometric authentication is available."BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE->"BIOMETRIC_ERROR_NO_HARDWARE - No biometric hardware available."BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE->"BIOMETRIC_ERROR_HW_UNAVAILABLE - Biometric hardware is currently unavailable."BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED->"BIOMETRIC_ERROR_NONE_ENROLLED - No biometrics enrolled."else->"Unknown biometric status: $result"}}else{return"Strong biometric authentication check is not supported on this API level."}}}
packageorg.owasp.mastestapp;importandroid.app.KeyguardManager;importandroid.content.Context;importandroid.hardware.biometrics.BiometricManager;importandroid.os.Build;importkotlin.Metadata;importkotlin.jvm.internal.Intrinsics;/* compiled from: MastgTest.kt */@Metadata(d1={"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u0006\u0010\u0005\u001a\u00020\u0006J\u000e\u0010\u0007\u001a\u00020\b2\u0006\u0010\u0002\u001a\u00020\u0003J\u0006\u0010\t\u001a\u00020\u0006R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\n"},d2={"Lorg/owasp/mastestapp/MastgTest;","","context","Landroid/content/Context;","(Landroid/content/Context;)V","checkStrongBiometricStatus","","isDeviceSecure","","mastgTest","app_debug"},k=1,mv={1,9,0},xi=48)/* loaded from: classes4.dex */publicfinalclassMastgTest{publicstaticfinalint$stable=8;privatefinalContextcontext;publicMastgTest(Contextcontext){Intrinsics.checkNotNullParameter(context,"context");this.context=context;}publicfinalStringmastgTest(){booleanisLocked=isDeviceSecure(this.context);StringbiometricStatus=checkStrongBiometricStatus();return"Device has a passcode: "+isLocked+"\n\nBiometric status: "+biometricStatus;}publicfinalbooleanisDeviceSecure(Contextcontext){Intrinsics.checkNotNullParameter(context,"context");ObjectsystemService=context.getSystemService("keyguard");Intrinsics.checkNotNull(systemService,"null cannot be cast to non-null type android.app.KeyguardManager");KeyguardManagerkeyguardManager=(KeyguardManager)systemService;returnkeyguardManager.isDeviceSecure();}publicfinalStringcheckStrongBiometricStatus(){if(Build.VERSION.SDK_INT>=30){BiometricManagerbiometricManager=(BiometricManager)this.context.getSystemService(BiometricManager.class);intresult=biometricManager.canAuthenticate(15);switch(result){case0:return"BIOMETRIC_SUCCESS - Strong biometric authentication is available.";case1:return"BIOMETRIC_ERROR_HW_UNAVAILABLE - Biometric hardware is currently unavailable.";case11:return"BIOMETRIC_ERROR_NONE_ENROLLED - No biometrics enrolled.";case12:return"BIOMETRIC_ERROR_NO_HARDWARE - No biometric hardware available.";default:return"Unknown biometric status: "+result;}}return"Strong biometric authentication check is not supported on this API level.";}}
rules:-id:mastg-android-device-passcode-presentlanguages:-javaseverity:INFOmetadata:summary:ThisrulesearchesforAPIthatcheckswhetherthedevicepasscodeisset.message:"[MASVS-STORAGE] Make sure to verify that your app runs on a device with a passcode set"pattern-either:-pattern:|$X.getSystemService("keyguard");...$Y.isDeviceSecure();-pattern:|BiometricManager$BM=(BiometricManager)$X.getSystemService(BiometricManager.class);...$BM.canAuthenticate($VAL);
The output shows all usages of APIs related to secure screen lock detection.
output.txt
1 2 3 4 5 6 7 8 91011121314151617
┌─────────────────┐│2CodeFindings│└─────────────────┘MastgTest_reversed.java❱[1mrules.mastg-android-device-passcode-present[0m[MASVS-STORAGE]Makesuretoverifythatyourapprunsonadevicewithapasscodeset30┆ObjectsystemService=context.getSystemService("keyguard");31┆Intrinsics.checkNotNull(systemService,"null cannot be cast to non-null typeandroid.app.KeyguardManager"); 32┆KeyguardManagerkeyguardManager=(KeyguardManager)systemService;33┆returnkeyguardManager.isDeviceSecure();⋮┆----------------------------------------38┆BiometricManagerbiometricManager=(BiometricManager)this.context.getSystemService(BiometricManager.class);39┆intresult=biometricManager.canAuthenticate(15);