首页 > 解决方案 > 检测设备是否无系统植根

问题描述

由于无系统 root 可以使用几年,因此检测设备是否已 root 变得“更难”。我开始寻找一种可能的解决方案来检测设备是否无系统地植根,但我仍然无法找出解决方案。检查文件是否存在于给定路径(在 /system 中)是没有意义的,因为如果设备无系统地植根,它就不会存在。同样寻找管理 root 访问的应用程序可能没有意义,因为用户可能没有安装它们/或者只是使用其他的,不如 Magisk、SuperSu 等流行。

所以,我的想法是执行“su”命令,然后检查 shell 是否在 root 模式下运行(“#”)或不(“$”)。但是我如何检查它以哪种模式运行?据我所知,在简单地执行命令后:

       Runtime.getRuntime().exec("su");

由于不再使用“SU 用户”(?),进程被终止。

简单查看我对 adb 用法的含义:

user@user:~$ adb shell
device:/ $ su

如果执行此命令后变为:

device:/ #

将知道设备是否已植根,如果返回“权限被拒绝”将知道设备未植根。我可以在 Android 应用程序中实现类似的功能吗?

标签: androidrootandroid-shell

解决方案


最好只使用 SafetyNet api。虽然它不能具体告诉 root,但它会让你知道该设备是不可信的。代码也很简单。

val nonce = ByteArray(20)
SecureRandom().nextBytes(nonce)
SafetyNet.getClient(context).attest(nonce, API_KEY)
        .addOnSuccessListener(this) { response ->
            // Indicates communication with the service was successful.
            // Use response.jwsResult to get the result data.
            val resultParts = result.jwsResult.split(".")
            if (resultParts.size == 3) {
                val bytes = Base64Utils.decode(resultParts[1])
                val newString = String(bytes)
                val json = JSONObject(newString)
                    // Check json fields to see if device is secure
                    // NOTE best practice is to send this to server instead of handling locally.
                }
        }
        .addOnFailureListener(this) { e: Exception ->
            // An error occurred while communicating with the service.
            if (e is ApiException) {
                // An error with the Google Play services API contains some
                // additional details.
                // You can retrieve the status code using the
                // e.statusCode property.
            } else {
                // A different, unknown type of error occurred.
                Log.d(TAG, "Error: ${e.message}")
            }

        }

以下是不同的检查场景。请查看安卓文档 场景


推荐阅读