Skip to content

Instantly share code, notes, and snippets.

@Ashutoshwahane
Last active September 13, 2023 12:54
Show Gist options
  • Save Ashutoshwahane/b0b2eca58d21857e006401d9f58c051a to your computer and use it in GitHub Desktop.
Save Ashutoshwahane/b0b2eca58d21857e006401d9f58c051a to your computer and use it in GitHub Desktop.
Util class to check for the root detection
/*
The RootDetection class has a method called isPhoneJailbreakOrRooted(),
which returns a boolean value indicating whether the phone is jailbroken or rooted.
*/
class RootDetection {
fun isPhoneJailbreakOrRooted(): Boolean {
val testKey = detectTestKeys()
val suExist = checkSuExists()
val suBinary = checkForSuBinary()
val busyBoxyBinary = checkForBusyBoxBinary()
return testKey || suExist || suBinary || busyBoxyBinary
}
/*The checkSuExists() method checks for the existence of the su binary by trying to execute the which su command*/
private fun checkSuExists(): Boolean {
var process: Process? = null
return try {
process = Runtime.getRuntime().exec(arrayOf("/system /xbin/which", "su"))
val `in` = BufferedReader(
InputStreamReader(process.inputStream)
)
val line = `in`.readLine()
process.destroy()
line != null
} catch (e: Exception) {
process?.destroy()
false
}
}
/*The detectTestKeys() method checks for the presence of test keys by getting the value of the Build.TAGS property.
The Build.TAGS property is a comma-separated list of tags that are used to describe the build configuration.
If the test-keys tag is present, then the device is using test keys.*/
private fun detectTestKeys(): Boolean {
val buildTags = Build.TAGS
return buildTags != null && buildTags.contains("test-keys")
}
private fun checkForSuBinary(): Boolean {
return checkForBinary("su")
}
private fun checkForBusyBoxBinary(): Boolean {
return checkForBinary("busybox")
}
private fun checkForBinary(filename: String): Boolean {
for (path in binaryPaths) {
val f = File(path, filename)
val fileExists = f.exists()
if (fileExists) {
return true
}
}
return false
}
private val binaryPaths = arrayOf(
"/data/local/",
"/data/local/bin/",
"/data/local/xbin/",
"/sbin/",
"/su/bin/",
"/system/bin/",
"/system/bin/.ext/",
"/system/bin/failsafe/",
"/system/sd/xbin/",
"/system/usr/we-need-root/",
"/system/xbin/",
"/system/app/Superuser.apk",
"/cache",
"/data",
"/dev"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment