首页 > 技术文章 > 判断手机是否root或越狱

shenyuiOS 2022-02-16 23:09 原文

给客户开发的app通过第三方检测机构检测出来一些漏洞,其中一项判断手机是否root或者越狱,如果是的话需要加提示,从网上找了一些方法,修改一下用到了项目中

  1. 判断Android手机是否root

    //在app的入口activity的onCreate方法中添加如下一段代码
    if (CommonUtil.checkRoot()) {
        new AlertDialog.Builder(this)
                .setTitle("当前设备已ROOT,存在安全风险,请更换其他设备")
                .setMessage("")
                .setPositiveButton("退出应用", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);//退出app
                    }
                })
                .setCancelable(false)
                .show();
    }
    //判断是否root方法
    public static boolean checkRoot() {
        boolean isRoot = false;
        String buildTags = Build.TAGS;
        if (buildTags!=null && buildTags.contains("test-keys")) {
            isRoot = true;
        }
    
        String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su" };
        for (String path : paths) {
            if (new File(path).exists()) isRoot = true;
        }
        return isRoot;
    }
    
  2. 判断iOS手机是否越狱

    //在AppDelegate的didFinishLaunchingWithOptions中添加下面代码
    BOOL isBroken = [self checkJailbreaking];
    if (isBroken) [self showJailbreakingAlert];
    //检测是否越狱
    - (BOOL)checkJailbreaking {
        BOOL isBroken = NO;
        
        struct stat stat_info;
        if (0 == stat("/Applications/Cydia.app", &stat_info)) {
            isBroken = YES;
        }
        
        int ret ;
        Dl_info dylib_info;
        int (*func_stat)(const char *, struct stat *) = stat;
        if ((ret = dladdr(func_stat, &dylib_info))) {
            if (strcmp(dylib_info.dli_fname,"/usr/lib/system/libsystem_kernel.dylib") != 0) {
                isBroken = YES;
          }
        }
        
        char *env = getenv("DYLD_INSERT_LIBRARIES");
        if (env) isBroken = YES;
        
        return isBroken;
    }
    //弹出提示框
    - (void)showJailbreakingAlert {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前设备已ROOT,存在安全风险,请更换其他设备" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"退出应用" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            exit(0);
        }];
        [alert addAction:cancel];
        [self.window.rootViewController presentViewController:alert animated:YES completion:NULL];
    }
    

记录一下,下次再用到可以直接从博客里找

推荐阅读