繁体   English   中英

如何防止有 Root 权限的 Android 手机安装我的应用程序?

[英]How to prevent Rooted Android Phones from Installing my app?

在这种情况下的目的是防止在排行榜中报告虚假高分(我的应用程序是游戏)。 这发生在 Flappy Birds 上 - 请参阅此链接 - http://www.androidpit.com/forum/589832/flappy-bird-high-score-cheat-set-your-own-high-score

由于 root 用户可以用他的手机做任何他想做的事情,我想其他的工作都不会奏效,唯一的解决方案是阻止 root 用户安装应用程序。 我对吗? 有没有办法做到这一点?

PS:我的游戏并不总是需要互联网连接,因此当它发生在另一台服务器上时报告分数是不可行的。 只有当互联网连接可用时,高分才会报告给排行榜。

我也有类似的需求。 我无法实现该应用程序不应安装在有根设备上,但我为此使用了解决方法:

  • 检查您的设备是否植根于您活动的onResume
  • 如果它已植根,只需向他显示警告“此设备已植根。您无法使用此应用程序。”,然后退出应用程序。

例子:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    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(MainActivity.this).create();
    alertDialog.setTitle("Alert");
    alertDialog.setMessage(message);
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
            });

    alertDialog.show();
}

DeviceUtis.java是一个实用程序类,它返回设备是否有DeviceUtis.java

public class DeviceUtils {

    public Boolean isDeviceRooted(Context context){
        boolean isRooted = isrooted1() || isrooted2();
        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() {
        return canExecuteCommand("/system/xbin/which su")
                || canExecuteCommand("/system/bin/which su")
                || canExecuteCommand("which su");
    }
}

我们使用了 5 种方法进行测试,我在这里只展示了 2 种。 您可以使用任何您认为好的方法。

希望这可以帮助。

PS:我已经把这个调用放在所有活动的onResume因为用户(有黑客意图)可以安装应用程序,导航到其他一些活动,然后根设备。

private static boolean canExecuteCommand(String command) {
        boolean executedSuccesfully;
        try {
            Runtime.getRuntime().exec(command);
            executedSuccesfully = true;
        } catch (Exception e) {
            executedSuccesfully = false;
        }

        return executedSuccesfully;
    }

没有必要像正常的用户行为那样阻止使用 root 手机的用户。 那么,如果您的游戏中没有获得高分、应用内购买等的在线连接,您担心什么样的危险或损害?

玩家想通过欺骗达到最后一级或本地(!)排行榜的顶部? 伤害在哪里?

通过阻止您的游戏在有 root 权限的设备上运行,您只会排斥应用程序的合法用户。

编辑:

使用云存档服务保存玩家的高分。 如果离线,它将被加密并存储在设备上。 下次在线时,您阅读高分并将其发送到播放服务。 播放服务提供了您可能还需要的反盗版功能。

使用 Kotlin Extention,您可以轻松检查设备是否已植根。 下面是可以帮助你的代码

DeviceUtils.kt

object DeviceUtils {
    fun isDeviceRooted(context: Context?): Boolean {
        return isRooted1 || isRooted2
    }

    private val isRooted1: Boolean
        get() {
            val file = File("/system/app/Superuser.apk")
            return file.exists()
        }

    // try executing commands
    private val isRooted2: Boolean
        get() = (canExecuteCommand("/system/xbin/which su")
                || canExecuteCommand("/system/bin/which su")
                || canExecuteCommand("which su"))

    private fun canExecuteCommand(command: String): Boolean {
        return try {
            Runtime.getRuntime().exec(command)
            true
        } catch (e: Exception) {
            false
        }
    }
}

Extention.kt

fun Activity.checkDeviceRoot(): Boolean {
    return if (DeviceUtils.isDeviceRooted(this)) {
        AlertDialog.Builder(this)
                .setMessage("Your device is rooted. you can not use this app into rooted device.")
                .setCancelable(false)
                .setPositiveButton(R.string.alert_ok) { _, _ ->
                    exitProcess(0)
                }.show()
        true
    } else {
        false
    }
}

如何使用此扩展:

class MyActivity : Activity{
    ...
    override 
    fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        if(checkDeviceRoot())
            return
        ....
        ....
    }
}

希望这个答案能帮到你。

我们在安全网排除的Playstore 控制台中有一个选项,我们可以使用它来防止应用程序出现在 Playstore 上以在有根设备上下载。

Google Play 服务Safety Net Attestation API ,我们可以通过它来评估设备并确定它是否被植根/篡改。

谁想处理root设备,请看我的回答: https : //stackoverflow.com/a/58304556/3908895

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM