1

I was been trying to find whether a device is rooted or not and if the device is found rooted i do not want my application to get installed.I have tried two of the below methods

private boolean isRooted() {
  return findBinary("su");
    }

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;
        }
    }
    try {

        File file = new File("/system/app/Superuser.apk");
        if (file.exists()) {
            Log.e("ERROR", "Unable to find icon for package '"
                   + "apk found");
       found = true;
        }
      } catch (Exception e1) {
        // ignore
      }
}
return found;
}

But i don't think that these methods are enough to find a rooted device,since there are tools to hide an apk and the su file can be renamed or deleted.Is there any other way or any suggestions which is 100 percent reliable to find a rooted device? I was trying to edit the su but couldn't do anything.Is it just a word of mouth or really possible to do so? Thanks in advance

***EDITED***: I have used "HIDE MY ROOT" application to hide the SU binary aswell as superuser.apk.I can make my rooted device, look like unrooted using hide my root application.Therefore i can say that this source is falseproof and not completely reliable to find rooted device. Kindly let me know if there is any alternative way to find the rooted device..

Arun Inbasekaran
  • 297
  • 1
  • 3
  • 14
  • 1
    possible duplicate of [Determine if running on a rooted device](http://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device) – duggu Jul 08 '14 at 04:21
  • @golu:Please try to understand my question,i want to know some reliable method other than this,like buildtags.But buildtags doesnot works on all devices. – Arun Inbasekaran Jul 08 '14 at 04:34
  • If you think that your question is different from the flagged duplicate then you should also mention the reason in your post. – SMR Jul 08 '14 at 05:54
  • @Golu:hope u understood my problem now – Arun Inbasekaran Jul 08 '14 at 08:33

3 Answers3

3

I did this in the following way :

 /*
 * Run su command on  device 
 * @throws IOException, InterruptedException
 */

private static boolean suRun() throws IOException, InterruptedException
{
    try {
     Process su = null;
     su = Runtime.getRuntime().exec(new String[] {"su","-c","exit"});
     su.waitFor();

     InputStream in = su.getInputStream();
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

     String suOutput = bufferedReader.readLine();

     if (suOutput == null)
     {
         return true;
     }
     else
     {
         return false;
     }
    } catch (Exception ex)
    {
        return false;
    }
 }


public static boolean isPhoneRooted() {
 // check if /system/app/Superuser.apk is present and can run su
 try {
     File file = new File("/system/app/Superuser.apk");
     if (file.exists() && suRun()) {
         Log.d("Blocking Service", "ROOTED PHONE DETECTED");
        return true;
     }
  } 
  catch (Throwable e1) {
      // ignore
  }
     return false;
}
mdavid
  • 563
  • 6
  • 20
2

you can use SafetyNet API from google play service. this is what being used by android pay not only for the root detection but also to check compatibility with android CTS.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0

Call isRooted() from ShellInterface

isRooted() depend upon majority of three factor

    public static boolean isRooted() {

    return isRooted1() ? (isRooted2() || isRooted3()) : (isRooted2() && isRooted3());
   }
private static boolean isRooted1() {
    Process mProcess = null;
    boolean mRoot;
    try {
        // This is executing on terminal
        mProcess = Runtime.getRuntime().exec("su");
        mRoot = true;

        // If the execute successfully then it return to true

    } catch (Exception e) {
        // if is not successfully then it return to false
        mRoot = false;

    } finally {
        if (mProcess != null) {
            try {
                mProcess.destroy();
            } catch (Exception ignored) {
            }
        }
    }
    return  mRoot;
}
private static boolean isRooted2() {
    String buildTags = Build.TAGS;
    return  buildTags !=null && buildTags.contains("test-keys");
}

private static boolean isRooted3() {
    boolean mRoot = false;
    boolean found = false;
    if (!found) {
        String[] places = {"/sbin/", "/system/bin/","/system/xbin",
                "/data/local/xbin","/system/sd/xbin","/data/local"
        };
        for (String path : places){
            if (new File(path+"su").exists()) {
                mRoot = true;
                found = true;
            }
        }
    }
    return mRoot;
}