如何检查 Android 中的 root 访问权限?

发布于 2024-11-24 13:57:48 字数 798 浏览 3 评论 0原文

我创建了一个方法来检查Android手机是否已root。这样做的方法如下,

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
}

但问题是,首先执行方法 checkrootcommand() 它工作正常,但是当再次调用相同的方法时,超级用户会话仍在运行。有没有办法在方法执行后结束超级用户会话?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦行僧 2024-12-01 13:57:48

对于通过利用软件漏洞克服硬件保护的设备,没有可靠的方法来检测设备上的 root 情况。

最好的情况是,您可以检测特定工具集的存在或扫描不应该存在的东西或文件中的更改 - 但这需要了解给定的安装应该是什么样子,并假设您正在使用的操作系统功能用于进行检查的方法尚未被修改以隐藏更改。

为了可靠地扫描,您需要确保受信任的代码运行在比不受信任的代码更低的级别上;获得 root 权限的设备是这种保证从根本上被破坏的设备,或者是最终用户比开发人员更受信任的设备。

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.

猫烠⑼条掵仅有一顆心 2024-12-01 13:57:48

在您的情况下,您应该在执行返回之前完成的作业后终止该进程。对代码进行以下更改应该可以完成此操作。

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
}

您可能无法普遍检测手机是否已 root,但您应该能够请求并确认您的应用程序是否可以通过以 root 身份运行 id 来访问 root,例如 su -c id 验证是否命令执行成功,输出包含uid=0,即root用户的uid。

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.

匿名的好友 2024-12-01 13:57:48

方法 1:应用程序要求 ROOT 访问权限:

将其添加到应用程序级 gradle 构建文件中:

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

现在,

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

方法 2:应用程序不要求 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)

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)
够钟 2024-12-01 13:57:48

使用此代码:

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*/ }

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*/ }
書生途 2024-12-01 13:57:48

您可以通过终端命令实现此目的,并且可以在应用程序中运行终端命令。

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

您的应用程序也不需要这种方式的 root 访问权限。

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.

苏佲洛 2024-12-01 13:57:48

在搜索解决方案来测试是否授予超级用户访问权限后,只有Lexip 的解决方案与扫描仪工作正常。

要完成他的回复:

    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();
    }

在已 root 的手机上使用 isRooted = true,如果没有,则返回 false。

如果授予超级用户访问权限,则 isRootGranted = true;如果没有,则为 false。

每次你打电话时都在工作。

问候。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文