Skip to content
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

Port MASTG-TEST-0012 (by @guardsquare) #3113

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

serek8
Copy link
Collaborator

@serek8 serek8 commented Jan 14, 2025

This PR closes #2937

  • I removed USB Debugging and Root detection from this test. It should be considered as a part or MASVS-RESILIENCE

@serek8 serek8 marked this pull request as ready for review February 6, 2025 08:49
@serek8 serek8 requested a review from cpholguera February 6, 2025 08:49

### Evaluation

The test succeeds because the output files show references to Device-Access-Security APIs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The test succeeds because the output files show references to Device-Access-Security APIs
The test passes because the output show references to Device-Access-Security APIs.

- a recent OS version
- a OS build intended for the end users

{{ MastgTest.kt }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{ MastgTest.kt }}
{{ MastgTest.kt # MastgTest_reversed.java }}

Comment on lines +30 to +31
The third argument of `objc_msgSend(...)` is `LAPolicyDeviceOwnerAuthentication` because `w2` register at the time of the function invocation is set to `2` with a `mov` instruction at Line 9. `2` is an
[enum](https://developer.apple.com/documentation/localauthentication/lapolicy?language=objc) representation of `LAPolicyDeviceOwnerAuthentication`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice!

Suggested change
The third argument of `objc_msgSend(...)` is `LAPolicyDeviceOwnerAuthentication` because `w2` register at the time of the function invocation is set to `2` with a `mov` instruction at Line 9. `2` is an
[enum](https://developer.apple.com/documentation/localauthentication/lapolicy?language=objc) representation of `LAPolicyDeviceOwnerAuthentication`.
The third argument of `objc_msgSend(...)` is `LAPolicyDeviceOwnerAuthentication` because `w2` register at the time of the function invocation is set to `2` with a `mov` instruction at Line 9. `2` is an [enum](https://developer.apple.com/documentation/localauthentication/lapolicy?language=objc) representation of `LAPolicyDeviceOwnerAuthentication`.


### Evaluation

The test succeeds because the output files show references to Device-Access-Security API, which indicates that a developer checks whether a passcode is set.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The test succeeds because the output files show references to Device-Access-Security API, which indicates that a developer checks whether a passcode is set.
The test passes because the output show references to Device-Access-Security APIs, which indicates that a developer checks whether a passcode is set.

---
platform: ios
title: References to Device-Access-Security Policy APIs
id: MASTG-TEST-0243
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix file name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain in the PR description why this was removed:

You can implement checks on the Android device by querying Settings.Secure for system preferences. Device Administration API offers techniques for creating applications that can enforce password policies and device encryption.

My understanding is that:


This test checks whether an application is running on a device with secure policies such as

- device passcode is set
Copy link
Collaborator

@cpholguera cpholguera Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about including...?

https://developer.android.com/reference/android/hardware/biometrics/BiometricManager#canAuthenticate(int)

biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG) checks if the device supports strong biometric authentication and if the user has enrolled biometric credentials. The method returns one of the following constants:

  • BIOMETRIC_SUCCESS: The device supports biometrics, and the user has enrolled at least one biometric credential.
  • BIOMETRIC_ERROR_NO_HARDWARE: The device does not have biometric hardware.
  • BIOMETRIC_ERROR_HW_UNAVAILABLE: The biometric hardware is currently unavailable.
  • BIOMETRIC_ERROR_NONE_ENROLLED: The user has not enrolled any biometric credentials.

Requires Manifest.permission.USE_BIOMETRIC

Comment on lines +1 to +29
package org.owasp.mastestapp

import android.app.KeyguardManager
import android.content.Context
import android.os.Build


class MastgTest (private val context: Context){

fun mastgTest(): String {
val isLocked = isDeviceSecure(context)
val androidSdkVersion = getSystemSdkVersion()
val isSystemDebuggable = isSystemDebuggable()
return "Device has a passcode: $isLocked\nandroidSdkVersion:$androidSdkVersion\nisSystemDebuggable:$isSystemDebuggable"
}

fun isDeviceSecure(context: Context): Boolean {
val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
return keyguardManager.isDeviceSecure
}

fun getSystemSdkVersion(): Int {
return android.os.Build.VERSION.SDK_INT
}

fun isSystemDebuggable(): Boolean {
return Build.TYPE == "eng" || Build.TYPE == "userdebug"
}
}
Copy link
Collaborator

@cpholguera cpholguera Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package org.owasp.mastestapp
import android.app.KeyguardManager
import android.content.Context
import android.os.Build
class MastgTest (private val context: Context){
fun mastgTest(): String {
val isLocked = isDeviceSecure(context)
val androidSdkVersion = getSystemSdkVersion()
val isSystemDebuggable = isSystemDebuggable()
return "Device has a passcode: $isLocked\nandroidSdkVersion:$androidSdkVersion\nisSystemDebuggable:$isSystemDebuggable"
}
fun isDeviceSecure(context: Context): Boolean {
val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
return keyguardManager.isDeviceSecure
}
fun getSystemSdkVersion(): Int {
return android.os.Build.VERSION.SDK_INT
}
fun isSystemDebuggable(): Boolean {
return Build.TYPE == "eng" || Build.TYPE == "userdebug"
}
}
package org.owasp.mastestapp
import android.app.KeyguardManager
import android.content.Context
import android.hardware.biometrics.BiometricManager
import android.os.Build
class MastgTest(private val context: Context) {
fun mastgTest(): String {
val isLocked = isDeviceSecure(context)
val biometricStatus = checkStrongBiometricStatus()
return "Device has a passcode: $isLocked\n\n" +
"Biometric status: $biometricStatus"
}
fun isDeviceSecure(context: Context): Boolean {
val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
return keyguardManager.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.
*/
fun checkStrongBiometricStatus(): String {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val biometricManager = context.getSystemService(BiometricManager::class.java)
val result = biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)
return when (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."
}
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs <uses-permission android:name="android.permission.USE_BIOMETRIC" /> in the AndroidManifest

@cpholguera
Copy link
Collaborator

Using the new .kt file:

Emulator

image

Physical Pixel

image

Comment on lines +1 to +15
e asm.bytes=false
e scr.color=false
e asm.var=false

aao
e asm.emu=true

?e Print xrefs to \'canEvaluatePolicy\"
f~str.canEvaluatePolicy:error:

?e Print xrefs to 0x100008360
axt 0x100008360

?e Print disassembly around \"canEvaluatePolicy\" in the function
pdf @ 0x100004f10 | grep -B 5 "canEvaluatePolicy:error:"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works with the latest r2 from git

Suggested change
e asm.bytes=false
e scr.color=false
e asm.var=false
aao
e asm.emu=true
?e Print xrefs to \'canEvaluatePolicy\"
f~str.canEvaluatePolicy:error:
?e Print xrefs to 0x100008360
axt 0x100008360
?e Print disassembly around \"canEvaluatePolicy\" in the function
pdf @ 0x100004f10 | grep -B 5 "canEvaluatePolicy:error:"
e asm.bytes=false
e scr.color=false
e asm.var=false
?e Print xrefs to \'canEvaluatePolicy\"
f~canEvaluatePolicy
?e
?e Print xrefs to 0x100008360
axt @ 0x100008360
?e
?e Print xrefs to 0x1000100a0
axt @ 0x1000100a0
?e
?e Print disassembly around \"canEvaluatePolicy\" in the function
pdf @ 0x100004f10 | grep -C 5 "canEvaluatePolicy:error:"

@@ -0,0 +1,2 @@
#!/bin/bash
r2 -q -i isDevicePasscodeSet.r2 -A MASTestApp > output.asm
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r2 -q -i isDevicePasscodeSet.r2 -A MASTestApp > output.asm
r2 -q -i isDevicePasscodeSet.r2 -e emu.str=true -A MASTestApp > output.asm


## Overview

This test verifies that an application is running on a device with a set passcode. A set passcode ensures that data on the device is encrypted and access to the device is restricted.
Copy link
Collaborator

@cpholguera cpholguera Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also include this?

From the weakness draft:

to make sure that biometrics can be used, verify that the kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly or the kSecAttrAccessibleWhenPasscodeSet protection class is set when the SecAccessControlCreateWithFlags method is called

To use Optic ID, Face ID, or Touch ID, the user must set up their device so that a passcode or password is required to unlock it.

https://support.apple.com/en-ph/guide/security/sec9479035f1/web

@@ -0,0 +1,35 @@
---
platform: ios
title: Uses of Device-Access-Security APIs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: Uses of Device-Access-Security APIs
title: Uses of LAContext.canEvaluatePolicy with r2

@@ -0,0 +1,39 @@
---
platform: android
title: Uses of Device-Access-Security APIs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: Uses of Device-Access-Security APIs
title: Uses of KeyguardManager.isDeviceSecure with semgrep

Comment on lines +16 to +17
- running on a recent version of Android OS
- running on a secure system build intended for the end users
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in Slack, these two can be removed from here after the changes done in MASWE-0008. If anything, they'd belong in MASWE-0077 or other MASWEs related to MASVS-RESILIENCE-1

Suggested change
- running on a recent version of Android OS
- running on a secure system build intended for the end users

@@ -1,29 +1,28 @@
---
title: Device Access Security Policy Not Enforced
title: Secured Device Detection Not Implemented
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or "Missing Device Secure Lock Verification Implementation". Any other ideas?

@@ -0,0 +1,25 @@
---
platform: ios
title: References to Device-Access-Security Policy APIs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe?

Suggested change
title: References to Device-Access-Security Policy APIs
title: References to Secure Lock Verification APIs

@@ -0,0 +1,31 @@
---
platform: android
title: References to Device-Access-Security Policy APIs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe?

Suggested change
title: References to Device-Access-Security Policy APIs
title: References to Secure Lock Verification APIs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Comment on lines +2 to +3
NO_COLOR=true semgrep -c ../../../../rules/mastg-android-device-access-security-sdk-version.yml ./MastgTest_reversed.java --text -o output_version.txt
NO_COLOR=true semgrep -c ../../../../rules/mastg-android-device-access-security-debuggable-system.yml ./MastgTest_reversed.java --text -o output_debuggable_system.txt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NO_COLOR=true semgrep -c ../../../../rules/mastg-android-device-access-security-sdk-version.yml ./MastgTest_reversed.java --text -o output_version.txt
NO_COLOR=true semgrep -c ../../../../rules/mastg-android-device-access-security-debuggable-system.yml ./MastgTest_reversed.java --text -o output_debuggable_system.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MASTG v1->v2 MASTG-TEST-0012: Testing the Device-Access-Security Policy (android)
2 participants