android 如何通过编程找到根设备?

xuo3flqw  于 2023-03-16  发布在  Android
关注(0)|答案(4)|浏览(187)

在安装我的应用程序之前,我想让它检查一下设备是否是根设备。

  1. private static boolean isRooted()
  2. return findBinary("su");
  3. }
  4. public static boolean findBinary(String binaryName) {
  5. boolean found = false;
  6. if (!found) {
  7. String[] places = {"/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
  8. "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"};
  9. for (String where : places) {
  10. if ( new File( where + binaryName ).exists() ) {
  11. found = true;
  12. break;
  13. }
  14. }
  15. }
  16. return found;
  17. }

它工作正常。但我听说文件名“su”可以更改,也可以在非根设备中创建一个名为“su”的文件。在这种情况下,此源不是dependable.so。我想知道除了搜索“su”之外的其他方法来查找根设备。我使用了以下代码

  1. Public static boolean checkRootMethod1()
  2. {
  3. String buildTags = android.os.Build.TAGS;
  4. if (buildTags != null && buildTags.contains("test-keys")) {
  5. return true;
  6. }
  7. return false;
  8. }

它不是正常工作。对于根设备,它按预期工作。但对于一些非根设备,它也显示为根。由于输出是不同的设备,我找不到解决方案。。任何帮助将不胜感激

cyvaqqii

cyvaqqii1#

如果你想限制你的应用在根设备中的使用,那么你必须考虑两点:
1)限制下载根设备中的应用程序。
2)限制根设备中应用的旁载

按照以下步骤限制从Play商店下载:

1)转到播放商店控制台。
2)在左侧菜单中,转到发布管理。
3)在该对话框中,转到设备目录。
4)然后,您将获得3个选项卡选项,转到“Excluded devices”(排除的设备)。
5)您将获得一个指定排除规则的选项。单击“管理排除规则”。
6)您可以看到SafetyNet排除的选择器。选择选项:排除未通过基本完整性测试的设备以及未经谷歌认证的设备。

按照以下步骤限制应用程序的旁载:

1)使用以下方法获取API密钥:https://developer.android.com/training/safetynet/attestation.html#obtain-api-key
2)在Gradle文件中添加safetynet依赖项。
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
3)我已经把下面的代码放在我的BaseActivity中,我的其他活动扩展,所以如果一个黑客与根设备试图侧载,并试图进入任何活动,然后下面的代码执行。

  1. private void ifGooglePlayServicesValid() {
  2. if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext())
  3. == ConnectionResult.SUCCESS) {
  4. // The SafetyNet Attestation API is available.
  5. callSafetyNetAttentationApi();
  6. } else {
  7. // Prompt user to update Google Play services.
  8. }
  9. }
  10. private void callSafetyNetAttentationApi() {
  11. SafetyNet.getClient(this).attest(generateNonce(), SAFETY_NET_CHECK_API_KEY)
  12. .addOnSuccessListener(this,
  13. response -> {
  14. // Use response.getJwsResult() to get the result data.
  15. String jwsResponse = decodeJws(response.getJwsResult());
  16. try {
  17. JSONObject attestationResponse = new JSONObject(jwsResponse);
  18. boolean ctsProfileMatch = attestationResponse.getBoolean("ctsProfileMatch");
  19. boolean basicIntegrity = attestationResponse.getBoolean("basicIntegrity");
  20. if (!ctsProfileMatch || !basicIntegrity) {
  21. // this indicates it's rooted/tampered device
  22. }
  23. } catch (JSONException e) {
  24. // json exception
  25. }
  26. })
  27. .addOnFailureListener(this, e -> {
  28. // An error occurred while communicating with the service.
  29. });
  30. }
  31. public String decodeJws(String jwsResult) {
  32. if (jwsResult == null) {
  33. return null;
  34. }
  35. final String[] jwtParts = jwsResult.split("\\.");
  36. if (jwtParts.length == 3) {
  37. return new String(Base64.decode(jwtParts[1], Base64.DEFAULT));
  38. } else {
  39. return null;
  40. }
  41. }
  42. private byte[] generateNonce() {
  43. byte[] nonce = new byte[16];
  44. new SecureRandom().nextBytes(nonce);
  45. return nonce;
  46. }

SAFETY_NET_CHECK_API_KEY是在第1步中获得的密钥。
证明API返回一个JWS响应,如下所示:

  1. {
  2. "timestampMs": 9860437986543,
  3. "nonce": "R2Rra24fVm5xa2Mg",
  4. "apkPackageName": "com.package.name.of.requesting.app",
  5. "apkCertificateDigestSha256": ["base64 encoded, SHA-256 hash of the
  6. certificate used to sign requesting app"],
  7. "ctsProfileMatch": true,
  8. "basicIntegrity": true,
  9. }

JWS响应包含指示设备状态的ctsProfileMatchbasicIntegrity

参考:https://developer.android.com/training/safetynet/attestation.html

展开查看全部
rxztt3cl

rxztt3cl2#

  1. public static boolean checkRooted()
  2. {
  3. try
  4. {
  5. Process p = Runtime.getRuntime().exec("su", null, new File("/"));
  6. DataOutputStream os = new DataOutputStream( p.getOutputStream());
  7. os.writeBytes("pwd\n");
  8. os.writeBytes("exit\n");
  9. os.flush();
  10. p.waitFor();
  11. p.destroy();
  12. }
  13. catch (Exception e)
  14. {
  15. return false;
  16. }
  17. return true;
  18. }
展开查看全部
pbpqsu0x

pbpqsu0x3#

验证码:

  1. /**
  2. * Checks if the device is rooted.
  3. *
  4. * @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
  5. */
  6. public static boolean isRooted() {
  7. // get from build info
  8. String buildTags = android.os.Build.TAGS;
  9. if (buildTags != null && buildTags.contains("test-keys")) {
  10. return true;
  11. }
  12. // check if /system/app/Superuser.apk is present
  13. try {
  14. File file = new File("/system/app/Superuser.apk");
  15. if (file.exists()) {
  16. return true;
  17. }
  18. } catch (Exception e1) {
  19. // ignore
  20. }
  21. // try executing commands
  22. return canExecuteCommand("/system/xbin/which su")
  23. || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
  24. }
  25. // executes a command on the system
  26. private static boolean canExecuteCommand(String command) {
  27. boolean executedSuccesfully;
  28. try {
  29. Runtime.getRuntime().exec(command);
  30. executedSuccesfully = true;
  31. } catch (Exception e) {
  32. executedSuccesfully = false;
  33. }
  34. return executedSuccesfully;
  35. }

见以下链接:-
Determine if running on a rooted device
Determining if an Android device is rooted programatically?

展开查看全部
ergxz8rk

ergxz8rk4#

以前,我们使用SafetyNet Attestation API检测root/越狱设备。但是,此API现在已弃用,因此我们必须切换到Play Integrity API。
播放完整性API将提供以下信息
1.应用是否已从您的Google Play商店列表中安装?这有助于您确保您的用户未运行您的应用的篡改版本。
1.该应用程序是否获得许可,并且是用户在支付后从Play商店购买的,还是盗版?
1.设备本身是否为可信设备,或者是否为根设备、篡改设备或其他被视为不安全的设备。
安全网认证-https://developer.android.com/training/safetynet/attestation
播放完整性API -https://developer.android.com/google/play/integrity/overview

相关问题