The Root of Things: Detecting if an android device is rooted
For frequent readers of this blog theres no doubt you have seen some of my previous posts on detecting jailbroken phones on iOS I’m also working on developing an android version of PassVult my privacy focused password manager so now I’m looking at how to stop rooted or compromised phones getting access to a users app data. Without further ado here are some of the best methods I have found.
Rootbeer Library:
RootBeer rootBeer = new RootBeer(context); if (rootBeer.isRooted()) { //we found indication of root } else { //we didn't find indication of root }
Fabric/Crashlytics method:
// usage CommonUtils.isRooted(context) // Implementation public static boolean isRooted(Context context) { boolean isEmulator = isEmulator(context); String buildTags = Build.TAGS; if(!isEmulator && buildTags != null && buildTags.contains("test-keys")) { return true; } else { File file = new File("/system/app/Superuser.apk"); if(file.exists()) { return true; } else { file = new File("/system/xbin/su"); return !isEmulator && file.exists(); } } }
Custom function found on StackOverflow (I believe this comes from google)
// Function public static boolean findBinary(String binaryName) { boolean found = false; if (!found) { String[] places = { "/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/", "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/" }; for (String where : places) { if (new File(where + binaryName).exists()) { found = true; break; } } } return found; } // Looking for the "su" folder private static boolean isRooted() { return findBinary("su"); } // Example if (isRooted()) { textView.setText("Device Rooted"); } else { textView.setText("Device Unrooted"); }
Code from Kevin Kowalewski which provides a few different methods:
/** @author Kevin Kowalewski */ public class RootUtil { public static boolean isDeviceRooted() { return checkRootMethod1() || checkRootMethod2() || checkRootMethod3(); } private static boolean checkRootMethod1() { String buildTags = android.os.Build.TAGS; return buildTags != null && buildTags.contains("test-keys"); } private static boolean checkRootMethod2() { String[] paths = { "/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 (String path : paths) { if (new File(path).exists()) return true; } return false; } private static boolean checkRootMethod3() { Process process = null; try { process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" }); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); if (in.readLine() != null) return true; return false; } catch (Throwable t) { return false; } finally { if (process != null) process.destroy(); } } }
Hope you found this useful! Feel free to leave a comment with some other methods you have tried.
Until Next time!
The Security Sleuth




















