Android 应用安全 - 检测设备是否Root

版权归作者所有,转发请注明出处:https://www.jianshu.com/p/a8dc37b0ed85

Android 应用安全 - 应用安全概览
Android 应用安全 - 移动应用安全
Android 应用安全 - Mobile安全漏洞Top10(OWASP)
Android 应用安全 - 案例
Android 应用安全 - 检测设备是否Root
Android 应用安全 - 加密算法

前言

当用户设备已经被Root之后,在此设备上使用的APP将会有巨大安全隐患,因为Root设备可以获取到最高权限从而有安全风险,特别是会议,金融,银行类APP需要额外注意

1.检测用户是否可以执行以下命令

 su
 busybox

2.检测用户是否已经安装下列已知的Root安装包

com.noshufou.android.su
com.noshufou.android.su.elite
eu.chainfire.supersu
com.koushikdutta.superuser
com.thirdparty.superuser
com.yellowes.su

3.检测用户是否已经安装下列已知的Root权限控制安装

com.devadvance.rootcloak
com.devadvance.rootcloakplus
de.robv.android.xposed.installer
com.saurik.substrate
com.zachspong.temprootremovejb
com.amphoras.hidemyroot
com.amphoras.hidemyrootadfree
com.formyhm.hiderootPremium
com.formyhm.hideroot

4.检测系统是否为测试版本

Build.TAGS

4.示例代码

      private val knownRootPackages = listOf(
        "com.noshufou.android.su",
        "com.noshufou.android.su.elite",
        "eu.chainfire.supersu",
        "com.koushikdutta.superuser",
        "com.thirdparty.superuser",
        "com.yellowes.su"
    )
    private val knownRootCloakers = listOf(
        "com.devadvance.rootcloak",
        "com.devadvance.rootcloakplus",
        "de.robv.android.xposed.installer",
        "com.saurik.substrate",
        "com.zachspong.temprootremovejb",
        "com.amphoras.hidemyroot",
        "com.amphoras.hidemyrootadfree",
        "com.formyhm.hiderootPremium",
        "com.formyhm.hideroot"
    )

    fun isRooted(context: Context): Boolean {
        return when {
            checkTags() || checkRootSu() || checkRootSu1() || canExecuteCommand("su") ||
                    canExecuteCommand("busybox") || isPackageInstalled(
                knownRootPackages,
                context
            ) || isPackageInstalled(knownRootCloakers, context) -> true
            else -> false
        }
    }

    private fun checkTags(): Boolean {
        val buildTags = Build.TAGS
        return buildTags != null && buildTags.contains("test-keys")
    }

    private fun checkRootSu(): Boolean {
        val paths = arrayOf(
            "/system/app/Superuser.apk",
            "/sbin/su",
            "/system/bin/su",
            "/system/xbin/su",
            "/data/local/xbin/su",
            "/data/local/bin/su",
            "/system/sd/xbin/su",
            "/system/bin/failsafe/su",
            "/data/local/su",
            "/su/bin/su"
        )
        for (path in paths) {
            if (File(path).exists()) return true
        }
        return false
    }

    private fun checkRootSu1(): Boolean {
        var process: Process? = null
        return try {
            process = Runtime.getRuntime()
                .exec(arrayOf("/system/xbin/which", "su"))
            val `in` = BufferedReader(InputStreamReader(process.inputStream))
            `in`.readLine() != null
        } catch (t: Throwable) {
            false
        } finally {
            process?.destroy()
        }
    }

    private fun canExecuteCommand(command: String): Boolean {
        try {
            Runtime.getRuntime().exec(command)
            return true
        } catch (e: Exception) {
            return false
        }
    }

    private fun isPackageInstalled(pkgList: List, context: Context): Boolean {
        return pkgList.any {
            try {
                context.packageManager.getPackageInfo(it, 0)
                true
            } catch (e: Exception) {
                false
            }
        }
    }

5.注意

Root检查判断尽量靠前,避免在检查之前就已经被侵入,配合应用加壳技术使用更能达到理想效果

欢迎关注Mike的

Android 知识整理

你可能感兴趣的:(Android 应用安全 - 检测设备是否Root)