【发布时间】:2019-08-21 09:34:45
【问题描述】:
我的应用程序必须不能在有根设备上运行。我想知道如何在运行时在应用程序中检测到运行应用程序的设备是否已植根。
有很多链接,我已经应用了所有可能的方法,但仍然在某些 root 设备上我无法检测到设备已 root 并且我的应用程序正在运行我想阻止 root 用户使用我的应用程序。
请大家帮忙
以下是我编写的部分代码。
@Override
protected void onResume() {
super.onResume();
if(new
DeviceUtils().isDeviceRooted(getApplicationContext())){
showAlertDialogAndExitApp("This device is
rooted. You can't use this app.");
}
}
public void showAlertDialogAndExitApp(String message) {
AlertDialog alertDialog = new
AlertDialog.Builder(corporatefedmobile.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(message);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
public class DeviceUtils {
public Boolean isDeviceRooted(Context context){
boolean isRooted = isrooted1() || isrooted2() ||
isrooted4() || isrooted3() || isroot5() ||
isroot6();
return isRooted;
}
private boolean isrooted1() {
File file = new
File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
return false;
}
// try executing commands
private boolean isrooted2() {
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 boolean isrooted3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new
String[] { "/system/xbin/which", "su" });
BufferedReader in = new
BufferedReader(newInputStreamReader(process.getInputStream()));
if (in.readLine() != null) return true;
return false;
} catch (Throwable t) {
return false;
} finally {
if (process != null) process.destroy();
}
}
private boolean isrooted4() {
String buildTags = android.os.Build.TAGS;
return buildTags != null &&
buildTags.contains("test-keys");
}
public boolean isRootAvailable(){
for(String pathDir :
System.getenv("PATH").split(":")){
if(new File(pathDir, "su").exists()) {
return true;
}
}
return false;
}
public boolean isroot5(){
if (isRootAvailable()) {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[]{"su", "-c", "id"});
BufferedReader in = new
BufferedReader(new
InputStreamReader(process.getInputStream()));
String output = in.readLine();
if (output != null &&
output.toLowerCase().contains("uid=0"))
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null)
process.destroy();
}
}
return false;
}
public boolean isroot6()
{
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
return false;
}
}
【问题讨论】:
-
您将永远无法检测到设备是否已被植根。最后,root 用户总是比你的应用拥有更多的权限,因此它总是能够对你的应用隐藏自己。