How to check root access in android?
Asked Answered
J

6

7

I created a method for checking whether the android phone is rooted or not. This is done as follows

public int checkrootcommand(String string) {
    // TODO Auto-generated method stub
    Process exec;
    try {

        exec = Runtime.getRuntime().exec(new String[]{"su","-c"});

        final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
        out.write("exit");
        out.flush();

        Log.i(SUPER_USER_COMMAND, "su command executed successfully");
        return 0; // returns zero when the command is executed successfully
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
    return 1; //returns one when the command execution fails
}

But the problem is that the method checkrootcommand() is executed first it works perfectly, but when the same method called again the superuser session is still running. Is there any way to end the superuser session once the method is executed??

Janettjanetta answered 18/7, 2011 at 16:52 Comment(7)
Try putting a \n at the end of the exit. But this is not a reliable way to test if the phone is rooted.Dextroamphetamine
@Dextroamphetamine : Can you please suggest a suitable way to do the same?Janettjanetta
code.google.com/p/roottools maybe you are interested in that insteadPetro
@Badr Hari :Thanks for the suggestion.Janettjanetta
Not only is this unreliable, trying to use su for a check in an application that doesn't advertise itself as using root permission to do something beneficial to the user will likely lead users to report your app as nefarious. And even if they don't report it, they may use an su wrapper that will blacklist your app, while permitting the ones they want to have root access to do so - so you may falsely think its not rooted when it is not only rooted, but set up for rooted access in one of the most common ways.Duane
#1101880Ruffina
You may not be able to universally detect whether phone is rooted or not but you should be able to request and then confirm is your app can access root by running id as root e.g., su -c id validate if the command executed successfully and the output contains uid=0 i.e., the uid of the root user.Vicereine
D
5

There is no reliable means of detecting a rooted condition on a device where hardware protections have been overcome by exploiting software vulnerabilities.

At best you can detect the presence of particular toolsets or scan for things that aren't supposed to be there or changes in files that are - but that requires knowledge of what a given installation should look like, and assumes that the OS functionality you are using to make the checks hasn't been modified to hide the changes.

To reliably scan, you need to be sure trusted code runs at a lower level than untrusted code; a rooted device is one where this assurance has been fundamentally broken, or where the end user is trusted more than you the developer are.

Duane answered 18/7, 2011 at 17:46 Comment(0)
T
1

Method 1 : Application asks for ROOT access :

Add this in your app-level gradle build file :

dependencies {
    compile 'eu.chainfire:libsuperuser:201501111220'
}

Now,

System.out.println(Shell.Su.available());
//Outputs true if user-granted else false

Method 2 : Application doesn't asks for ROOT :

boolean deviceisRooted() {
            String[] filespaths = {"/system/app/Superuser.apk","/sbin/su", "/system/bin/su","/system/xbin/su"};
            for (String xyz : filespaths) {
                if (new File(xyz).exists()) return true;
            }
            return false;
        }

System.out.prinln(deviceisRooted());

//Outputs true if device is ROOTED else false
//Doesn't asks user
//Also returns true IF NOT PROPERLY ROOTED (But ROOTED somehow)
Talcahuano answered 30/5, 2017 at 14:47 Comment(0)
V
1

In your case, you should kill the process after executing it for the job which is done before returning. The following changes to your code should do the thing.

public int checkrootcommand(String string) {
    // TODO Auto-generated method stub
    Process exec = null;
    try {

        exec = Runtime.getRuntime().exec(new String[]{"su","-c"});

        final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
        out.write("exit");
        out.flush();

        Log.i(SUPER_USER_COMMAND, "su command executed successfully");
        return 0; // returns zero when the command is executed successfully
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (exec != null) {
            try {
                exec.destroy();
            } catch (Exception ignored) {
            }
        }
    }
    return 1; //returns one when the command execution fails
}

You may not be able to universally detect whether phone is rooted or not but you should be able to request and then confirm is your app can access root by running id as root e.g., su -c id validate if the command executed successfully and the output contains uid=0 i.e., the uid of the root user.

Vicereine answered 31/5, 2017 at 4:34 Comment(0)
M
0

Use this code:

Process executor = Runtime.getRuntime().exec("su -c ls /data/data");
executor.waitFor();
int iabd = executor.exitValue();
if(iabd != 0){ /*process exit value is not 0, so user is not root*/ }else{ /* user is root*/ }
Marsipobranch answered 18/7, 2016 at 15:21 Comment(0)
F
0

You can achieve this from a terminal command and you can run terminal commands within an app.

if [ ! -z "$(/system/bin/ps -A | grep -v grep | grep -c daemonsu)" ]; then echo "device is rooted"; else echo "device is not rooted"; fi

Your application also doesn't require root access this way.

Frodi answered 14/2, 2020 at 12:3 Comment(0)
C
0

After searching for a solution to test if SuperUser Access granted or not, only Lexip's solution with Scanner works fine.

To complete his response:

    boolean isRooted = false;
    boolean isRootGranted = false;
    try {
        Process process = Runtime.getRuntime().exec("su -c ls");     // Ask for su and list
        Scanner scanner = new Scanner(process.getInputStream()).useDelimiter("\\A");
        if (scanner.hasNext()) isRootGranted = true;
        scanner.close();
        process.waitFor();
        process.getInputStream().close();
        process.destroy();
        isRooted = true;
        if (isRootGranted) Toast.makeText(MainActivity.this, "ROOT granted", Toast.LENGTH_SHORT).show();
        else Toast.makeText(MainActivity.this, "ROOT not granted", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        isRooted = false;
        Toast.makeText(MainActivity.this, "ERROR #SU AUTORISATION\nPHONE NON ROOTED", Toast.LENGTH_SHORT).show();
    }

isRooted = true on a rooted phone, and false if not.

isRootGranted = true if superUser Access is granted, and false if not.

Working everytime you call it.

Regards.

Cyrstalcyrus answered 18/12, 2023 at 7:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.