2017-05-29 69 views
-1

這是一個古老的問題,並被許多人回答,但在這裏我再次去..我想檢查設備是否根源。我不希望我的應用程序安裝在根目錄的設備上。現在我經歷了很多答案,得出的結論是,沒有確定的根源識別方法,如果我錯了,請糾正我。 現在我的問題,我們可以檢查設備是否真實,而不是檢查生根或不是? 是否有任何應用程序可以在其中測試簽名的APK以用於代碼簽名,ssl固定,代碼曝光和許多事情?Android設備根檢查

+0

根植設備大部分時間都有一個開放的引導加載程序。也會有busybox安裝90%的時間。但所有這一切。 Google Play Store剛剛發佈了一個選項,讓開發者可以隱藏他們的應用程序以查看根源設備。它肯定不會禁止它,但只會讓它變得有點難。 可能的重複:https://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device 有關在Play商店隱藏應用的文章:http://www.androidguys.com/2017/05/19/google-makes-it-easy-devs-to-hide-apps-root-phone-owners/ – Nico

+0

檢查此答案:https://stackoverflow.com/questions/3424195/determining- if-an-android-device-root-programatically –

+1

號碼什麼定義了「真正」的設備?對此沒有定義。 –

回答

0

檢查此鏈接:Determine if running on a rooted device

找到了一些方法來檢查設備是根與否。它工作正常。

DemoActivity.class

if (isDeviceRooted()) { 
      DialogFactory.getInstance().showAlertDialog(this, null, 0, "This application can't run on Rooted android phone", "Exit", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finishAffinity(); 
       } 
      }, false); 
     } 
     else { 
/* Do your code */ 
} 

public static boolean isDeviceRooted() { 
     return Utils.checkRootMethod1() || Utils.checkRootMethod2() || Utils.checkRootMethod3(); 
    } 

Utils.class

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

public static boolean checkRootMethod2() { 
    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; 
} 

public static boolean checkRootMethod3() { 
    Process process = null; 
    try { 
     process = Runtime.getRuntime().exec(new String[]{"/system/xbin/which", "su"}); 
     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     return in.readLine() != null; 
    } catch (Throwable t) { 
     return false; 
    } finally { 
     if (process != null) process.destroy(); 
    } 
} 
+0

這些方法似乎與2011年的這個回答相同https://stackoverflow.com/a/8097801/279320。 –

1

下面是我的根Checking.here實用程序類,我已經使用,這是爲我工作字符串常量。

public static final String[] knownRootAppsPackages = { 
      "com.noshufou.android.su", 
      "com.noshufou.android.su.elite", 
      "eu.chainfire.supersu", 
      "com.koushikdutta.superuser", 
      "com.thirdparty.superuser", 
      "com.yellowes.su", 
      "com.zachspong.temprootremovejb", 
      "com.ramdroid.appquarantine", 
      "eu.chainfire.supersu" 

    }; 

    public static final String[] knownDangerousAppsPackages = { 
      "com.koushikdutta.rommanager", 
      "com.koushikdutta.rommanager.license", 
      "com.dimonvideo.luckypatcher", 
      "com.chelpus.lackypatch", 
      "com.ramdroid.appquarantine", 
      "com.ramdroid.appquarantinepro" 
    }; 

    public static final String[] knownRootCloakingPackages = { 
      "com.devadvance.rootcloak", 
      "com.devadvance.rootcloakplus", 
      "de.robv.android.xposed.installer", 
      "com.saurik.substrate", 
      "com.zachspong.temprootremovejb", 
      "com.amphoras.hidemyroot", 
      "com.amphoras.hidemyrootadfree", 
      "com.formyhm.hiderootPremium", 
      "com.formyhm.hideroot" 
    }; 

    public static final String[] suPaths ={ 
      "/data/local/", 
      "/data/local/bin/", 
      "/data/local/xbin/", 
      "/sbin/", 
      "/su/bin/", 
      "/system/bin/", 
      "/system/bin/.ext/", 
      "/system/bin/failsafe/", 
      "/system/sd/xbin/", 
      "/system/usr/we-need-root/", 
      "/system/xbin/" 
    }; 


    public static final String[] pathsThatShouldNotBeWrtiable = { 
      "/system", 
      "/system/bin", 
      "/system/sbin", 
      "/system/xbin", 
      "/vendor/bin", 
      //"/sys", 
      "/sbin", 
      "/etc", 
      //"/proc", 
      //"/dev" 
    } 

;

public class RootUtil { 
    public static boolean isDeviceRooted() { 
     return detectRootManagementApps() || detectPotentiallyDangerousApps() || checkForBinary("su") 
       || checkForBinary("busybox") || checkForDangerousProps() || checkForRWPaths() 
       || detectTestKeys() || checkSuExists(); 
    } 

    public static boolean detectTestKeys() { 
     String buildTags = android.os.Build.TAGS; 
     String buildFinger= Build.FINGERPRINT; 
     String product=Build.PRODUCT; 
     String hardware=Build.HARDWARE; 
     String display=Build.DISPLAY; 
     return (buildTags != null) && (buildTags.contains("test-keys")|| buildFinger.contains("genric.*test-keys")||product.contains("generic")||product.contains("sdk")||hardware.contains("goldfish")||display.contains(".*test-keys")); 
    } 


    public static boolean detectRootManagementApps() { 
     return detectRootManagementApps(null); 
    } 


    public static boolean detectRootManagementApps(String[] additionalRootManagementApps) { 


     ArrayList<String> packages = new ArrayList<>(); 
     packages.addAll(Arrays.asList(Constants.knownRootAppsPackages)); 
     if (additionalRootManagementApps!=null && additionalRootManagementApps.length>0){ 
      packages.addAll(Arrays.asList(additionalRootManagementApps)); 
     } 

     return isAnyPackageFromListInstalled(packages); 
    } 


    public static boolean detectPotentiallyDangerousApps() { 
     return detectPotentiallyDangerousApps(null); 
    } 

    public static boolean detectPotentiallyDangerousApps(String[] additionalDangerousApps) { 


     ArrayList<String> packages = new ArrayList<>(); 
     packages.addAll(Arrays.asList(Constants.knownDangerousAppsPackages)); 
     if (additionalDangerousApps!=null && additionalDangerousApps.length>0){ 
      packages.addAll(Arrays.asList(additionalDangerousApps)); 
     } 

     return isAnyPackageFromListInstalled(packages); 
    } 


    public boolean detectRootCloakingApps() { 
     return detectRootCloakingApps(null); 
    } 


    public boolean detectRootCloakingApps(String[] additionalRootCloakingApps) { 


     ArrayList<String> packages = new ArrayList<>(); 
     packages.addAll(Arrays.asList(Constants.knownRootCloakingPackages)); 
     if (additionalRootCloakingApps!=null && additionalRootCloakingApps.length>0){ 
      packages.addAll(Arrays.asList(additionalRootCloakingApps)); 
     } 

     return isAnyPackageFromListInstalled(packages); 
    } 



    public boolean checkForSuBinary(){ 
     return checkForBinary("su"); 
    } 


    public boolean checkForBusyBoxBinary(){ 
     return checkForBinary("busybox"); 
    } 

    public static boolean checkForBinary(String filename) { 

     String[] pathsArray = Constants.suPaths; 

     boolean result = false; 

     for (String path : pathsArray) { 
      String completePath = path + filename; 
      File f = new File(completePath); 
      boolean fileExists = f.exists(); 
      if (fileExists) { 
       result = true; 
      } 
     } 

     return result; 
    } 

    private static String[] propsReader() { 
     InputStream inputstream = null; 
     try { 
      inputstream = Runtime.getRuntime().exec("getprop").getInputStream(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String propval = ""; 
     try { 
      propval = new Scanner(inputstream).useDelimiter("\\A").next(); 

     } catch (NoSuchElementException e) { 

     } 

     return propval.split("\n"); 
    } 

    private static String[] mountReader() { 
     InputStream inputstream = null; 
     try { 
      inputstream = Runtime.getRuntime().exec("mount").getInputStream(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     if (inputstream == null) return null; 

     String propval = ""; 
     try { 
      propval = new Scanner(inputstream).useDelimiter("\\A").next(); 
     } catch (NoSuchElementException e) { 
      e.printStackTrace(); 
     } 

     return propval.split("\n"); 
    } 

    private static boolean isAnyPackageFromListInstalled(List<String> packages){ 
     boolean result = false; 

     PackageManager pm = MobileTechnicianApp.getAppContext().getPackageManager(); 

     for (String packageName : packages) { 
      try { 
       pm.getPackageInfo(packageName, 0); 
       result = true; 
      } catch (PackageManager.NameNotFoundException e) { 

      } 
     } 

     return result; 
    } 

    public static boolean checkForDangerousProps() { 

     final Map<String, String> dangerousProps = new HashMap<>(); 
     dangerousProps.put("ro.debuggable", "1"); 
     dangerousProps.put("ro.secure", "0"); 

     boolean result = false; 

     String[] lines = propsReader(); 
     for (String line : lines) { 
      for (String key : dangerousProps.keySet()) { 
       if (line.contains(key)) { 
        String badValue = dangerousProps.get(key); 
        badValue = "[" + badValue + "]"; 
        if (line.contains(badValue)) { 
         result = true; 
        } 
       } 
      } 
     } 
     return result; 
    } 

    public static boolean checkForRWPaths() { 

     boolean result = false; 

     String[] lines = mountReader(); 
     for (String line : lines) { 
      String[] args = line.split(" "); 

      if (args.length < 4){ 
       continue; 
      } 

      String mountPoint = args[1]; 
      String mountOptions = args[3]; 

      for(String pathToCheck: Constants.pathsThatShouldNotBeWrtiable) { 
       if (mountPoint.equalsIgnoreCase(pathToCheck)) { 
        for (String option : mountOptions.split(",")){ 

         if (option.equalsIgnoreCase("rw")){ 
          result = true; 
          break; 
         } 
        } 
       } 
      } 
     } 

     return result; 
    } 

    public static boolean checkSuExists() { 
     Process process = null; 
     try { 
      process = Runtime.getRuntime().exec(new String[] { "which", "su" }); 
      BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      return in.readLine() != null; 
     } catch (Throwable t) { 
      return false; 
     } finally { 
      if (process != null) process.destroy(); 
     } 
    } 



}