Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "Root access rejected" in android application

Tags:

android

I am getting an error while running this code and the error is " Root access rejected [java.io.IOException] : Error running exec(). Command: [su] Working Directory: null Environment: null"

try 
{
    suProcess = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
    DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

    if (null != os && null != osRes) 
    {
        // Getting the id of the current user to check if this is root
        os.writeBytes("id\n");
        os.flush();
        String currUid = osRes.readLine();
        boolean exitSu = false;
        if (null == currUid) 
        {
            retval = false;
            exitSu = false;
            Log.d("ROOT", "Can't get root access or denied by user");
        }
        else if (true == currUid.contains("uid=0"))
        {
        retval = true;
        exitSu = true;
        Log.d("ROOT", "Root access granted");
        } 
        else 
        {
        retval = false;
        exitSu = true;
        Log.d("ROOT", "Root access rejected: " + currUid);
        }

        if (exitSu)
        {
           os.writeBytes("exit\n");
           os.flush();
        }
    }
} 
catch (Exception e)
{
    Log.d("ROOT", "Root access rejected [" + e.getClass().getName()
                + "] : " + e.getMessage());
}
like image 868
bhangu_23 Avatar asked Jun 18 '14 05:06

bhangu_23


People also ask

How do I authorize root permission on Android?

In most versions of Android, that goes like this: Head to Settings, tap Security, scroll down to Unknown Sources and toggle the switch to the on position. Now you can install KingoRoot. Then run the app, tap One Click Root, and cross your fingers. If all goes well, your device should be rooted within about 60 seconds.

How do I prevent apps from detecting root?

In Settings, tap on the Configure DenyList option. Now, select the app for which you want to hide root detection. In our case, we're hiding it for Google Pay. Finally, clear the data of the app that you've just selected.


2 Answers

This is because apps do not have a super user permission in the android system.

like image 53
Shivam Verma Avatar answered Oct 26 '22 17:10

Shivam Verma


Well, first of all ... you need to get your device rooted. A little Google Search will help with that.

If your application will be running on anonymous client devices, then you should better check first if the device is rooted or not. My library has a root availability check method which looks like below, but it doesn't work in a few cases.

    public static boolean hasRoot() {
        String tags = Build.TAGS;
        if ((tags != null) && tags.contains("test-keys"))
            return true;
        try {
            if (new File("/system/app/Superuser.apk").exists())
                return true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        String[] commands = {
            "/system/xbin/which su",
            "/system/bin/which su",
            "which su"
        };
        for (String command : commands) {
            try {
                Runtime.getRuntime().exec(command);
                return true;
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return false;
    }

Then you can proceed with your procedure to execute a superuser shell & check for uid.

Or if you want to simplify things, you can have a look at my open-source Android CLI library with which, this task would be simpler. You can find the github project here.

like image 43
VPZ Avatar answered Oct 26 '22 16:10

VPZ