判斷手機是否root或越獄


給客戶開發的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];
    }
    

記錄一下,下次再用到可以直接從博客里找


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM