Android检查手机是否被root

目前来说Android平台并没有提供能够root检查的工具。但是可以通过两种方式来判断

  • 系统是否包含su文件
  • su文件是否能够执行

但是这两种检查方式都存在缺点:

(1)第一种存在误测和漏测的情况,比如su没有放到常规路径下,就容易漏掉,但是这种情况是有办法尽量规避(或者说减小误差)的,比喻运行which检查,或者遍历shell中所有的环境变量的PATH;还有一种情况是手机没有root但是存在su文件,这种情况一般出现在手机曾经被root过,但是又进行了系统还原操作。
(2)而第二种,有可能检查不准确,或者会有弹窗提示用户是否要授予root权限。

public class RootUtil {
    public static boolean isDeviceRooted() {
        return checkRootBuildTags() || checkRootFile() || checkSuFile();
    }

    private static boolean checkRootBuildTags() {
        String buildTags = android.os.Build.TAGS;
        return buildTags != null && buildTags.contains("test-keys");
    }

    private static boolean checkRootFile() {
        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 static boolean checkSuFile() {
        Process process = null;
        try {
            //  /system/xbin/which 或者 /system/bin/which
            process = Runtime.getRuntime().exec(new String[]{"which", "su"});
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            if (in.readLine() != null) return true;
            return false;
        } catch (Throwable t) {
            return false;
        } finally {
            if (process != null) process.destroy();
        }
    }

}

参考:Android检查手机是否被root
参考:determine-if-running-on-a-rooted-device

上一篇 Android开发安全设置
下一篇 Android实现仿银行APP回退至后台,并在通知栏里显示
目录
文章列表
1 AWS WAF 配置
AWS WAF 配置
2
Redis 字符串(String)
Redis 字符串(String)
3
Android HTTPS请求 CertPathValidatorException
Android HTTPS请求 CertPathValidatorException
4
Android Studio 3.x上使用Lombok
Android Studio 3.x上使用Lombok
5
Linux下为用户创建SSH连接
Linux下为用户创建SSH连接
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。