【问题标题】:How to detect whether the device is rooted or not?如何检测设备是否已root?
【发布时间】:2019-08-21 09:34:45
【问题描述】:

我的应用程序必须不能在有根设备上运行。我想知道如何在运行时在应用程序中检测到运行应用程序的设备是否已植根。

有很多链接,我已经应用了所有可能的方法,但仍然在某些 root 设备上我无法检测到设备已 root 并且我的应用程序正在运行我想阻止 root 用户使用我的应用程序。

请大家帮忙

以下是我编写的部分代码。

@Override
protected void onResume() {
super.onResume();

if(new 
DeviceUtils().isDeviceRooted(getApplicationContext())){
showAlertDialogAndExitApp("This device is 
rooted. You can't use this app.");
}
}


public void showAlertDialogAndExitApp(String message) {

        AlertDialog alertDialog = new 
        AlertDialog.Builder(corporatefedmobile.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage(message);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(false);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
public void 
onClick(DialogInterface dialog, int which) {
finish();

}
}); 
alertDialog.show();
}

    public class DeviceUtils {

        public Boolean isDeviceRooted(Context context){
            boolean isRooted = isrooted1() || isrooted2() || 
                        isrooted4() || isrooted3() || isroot5() || 
                        isroot6();
            return isRooted;
        }

        private boolean isrooted1() {

            File file = new 
                            File("/system/app/Superuser.apk");
            if (file.exists()) {
                return true;
            }
            return false;
        }

        // try executing commands
        private boolean isrooted2() {
            String[] paths = { "/system/app/Superuser.apk", 
                                            "/sbin/su", 
                                            "/system/bin/su", 
                                            "/system/xbin/su", 
                                            "/data/local/xbin/su", 
                                            "/data/local/bin/su", 
                                            "/system/sd/xbin/su",
                                "/system/bin/failsafe/su", 
                                            "/data/local/su", 
                                       "/su/bin/su"};
for (String path : paths) {
if (new File(path).exists())
return true;
}
return false;
}

private boolean isrooted3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new 
String[] { "/system/xbin/which", "su" });
BufferedReader in = new 
BufferedReader(newInputStreamReader(process.getInputStream()));
if (in.readLine() != null) return true;
return false;
} catch (Throwable t) {
return false;
} finally {
if (process != null) process.destroy();
}
}

private boolean isrooted4() {
String buildTags = android.os.Build.TAGS;
return buildTags != null && 
 buildTags.contains("test-keys");
}
public boolean isRootAvailable(){
for(String pathDir : 
System.getenv("PATH").split(":")){
if(new File(pathDir, "su").exists()) {
return true;
}
}
return false;
}

public boolean isroot5(){
if (isRootAvailable()) {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[]{"su", "-c", "id"});
BufferedReader in = new 
BufferedReader(new 
InputStreamReader(process.getInputStream()));
String output = in.readLine();
if (output != null && 
output.toLowerCase().contains("uid=0"))
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null)
process.destroy();
}
}

return false;
}

public  boolean isroot6()

{
String buildTags = android.os.Build.TAGS;

if (buildTags != null && buildTags.contains("test-keys")) {

return true;
}

return false;
}
}

【问题讨论】:

  • 试试这个库:github.com/scottyab/rootbeer
  • 您将永远无法检测到设备是否已被植根。最后,root 用户总是比你的应用拥有更多的权限,因此它总是能够对你的应用隐藏自己。

标签: android root


【解决方案1】:

尝试使用以下方法,如果您的设备是 root 访问,它将返回 true,否则将返回 false。

public static boolean isRooted() {

    // get from build info
    String buildTags = android.os.Build.TAGS;
    if (buildTags != null && buildTags.contains("test-keys")) {
        return true;
    }

    // check if /system/app/Superuser.apk is present
    try {
        File file = new File("/system/app/Superuser.apk");
        if (file.exists()) {
            return true;
        }
    } catch (Exception e1) {
        // ignore
    }

    // try executing commands
    //return canExecuteCommand("/system/xbin/which su")|| canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
    if(!canExecuteCommand("su"))
        if(findBinary("su"))
            return true;
   return false;
}

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;
            }
        }
    }
    return found;
}

// executes a command on the system
private static boolean canExecuteCommand(String command) {
    boolean executedSuccesfully;
    try {
        Runtime.getRuntime().exec(command);
        executedSuccesfully = true;
    } catch (Exception e) {
        executedSuccesfully = false;
    }

    return executedSuccesfully;
}

【讨论】:

    【解决方案2】:

    如果你能丰富系统文件,请尝试查找系统文件,然后系统已root。

        boolean rooted = false;
        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()) {
                   rooted = true;
                   break;
             }
         }
       }
    return rooted;
    

    【讨论】:

    • 在 binaryName 变量中写什么? binaryName="su" 吗?
    • 您将尝试查找的二进制文件是“su”
    • 我已经实现了这个方法,但是在不同的登录中它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多