kiri
kiri

Reputation: 2007

How to test If Android apk runs in rooted device

An Infosec team wanted to know if our app was executable on rooted Android devices. We developed a solution for it and now want to test our app. Are we able to replicate rooted devices online? If not, can anyone provide other ways to test our app?

Thanks in advance!

Upvotes: 5

Views: 2450

Answers (2)

Paul P Joby
Paul P Joby

Reputation: 773

I think the best idea is to use the rooted/ rootable android emulator. Here are few android emulators that you can download for windows pc.

Links for rootable emulators

If you want to use the emulator provided by the android studio then i think you would have to create a new android emulator with API 22 (LOLLIPOP) or above using android studio and try rooting it with help of Kingroot app. This app helps you to root your device by directly installing on your device like an normal application. This solution must work as it works on normal real devices. I have myself tried on the real devices but not on android emulator.

Hope this solves your query

Upvotes: 3

seyed Jafari
seyed Jafari

Reputation: 1265

I have written a few util functions to help me with determining if my app is running in root environment:

private fun isDeviceRooted(): Boolean {
    return checkRootMethod1() || checkRootMethod2() || checkRootMethod3()
}

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

private fun checkRootMethod2(): 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 checkRootMethod3(): 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()
    }
}

Upvotes: 2

Related Questions