如何检查在Android的根访问?

我创建了一个方法来检查android手机是否已经生根。 这如下完成

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()方法首先被执行,它完美地工作,但是当再次调用同一个方法时,超级用户会话仍在运行。 一旦方法被执行,有没有办法结束超级用户会话?


在通过利用软件漏洞已经克服了硬件保护的设备上没有可靠的手段来检测根源状况。

充其量,您可以检测到特定工具集的存在或扫描不应该存在的内容或文件更改 - 但需要了解给定安装的外观,并假定您所使用的操作系统功能用于使检查未被修改以隐藏更改。

为了可靠地进行扫描,您需要确保可信任代码的运行级别低于不可信代码的级别; 根植设备就是这种保证已经被彻底破坏的设备,或者最终用户比开发商更信任的设备。


在你的情况下,你应该在返回之前完成的工作后终止进程。 以下对代码的更改应该做到这一点。

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用户身份运行id来访问root,例如, su -c id验证命令是否成功执行以及输出包含uid=0即, root用户的uid。


方法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)
链接地址: http://www.djcxy.com/p/53723.html

上一篇: How to check root access in android?

下一篇: (MapView and 1700 Overlays Items).equals(" Slow")