简体   繁体   中英

Android root detected? Uninstall app immediately

I am working on an app that contains sensitive data (encryption keys) that should not leak. The app should be able to work offline at any time so I can not store the keys in the cloud.

When a user has a rooted device, he can extract the apk and get the keys. I want to prevent this.

Is there any way to immediately force uninstalling my app without user confirmation, once root access has been detected?

(Or is there another approach to prevent the keys leaking??)

I have taken a look at Samsung Knox, which encrypts the data and uses a hardware bit to detect root access, and makes the apps and data inaccessible once the device has been tampered with. It works very well, but I am looking for a solution that works on a wider range of devices (not only Samsung devices).

Edit: Those things you are trying to achive (save storage for encryption keys, ...) are not very easy. You should read this , it'll probably help you the most.

To uninstall your app, you could try this (not tested!):

Uri packageURI = Uri.parse("package:"+MyMainActivity.class.getPackage().getName());
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);

When it comes to root detection, there is not just the "best" solution. There are various ways, here is something to help you getting started:

/** @author Kevin Kowalewski */
public class RootUtil {
    public static boolean isDeviceRooted() {
        return checkRootMethod1() || checkRootMethod2() || checkRootMethod3();
    }

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

    private 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;
    }

    private 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()));
            if (in.readLine() != null) return true;
            return false;
        } catch (Throwable t) {
            return false;
        } finally {
            if (process != null) process.destroy();
        }
    }
}

I think Uninstall app immediately is not solution, you must find out where is root directory, and beware about magisk manager app, can bypass root detection you can try root detection using Rootbeer, roottools, or create root class. using Rootbeer for magisk manager

//root example you can call this class.
public static boolean findBinary(String binaryName) {
    boolean found = false;
    if (!found) {
        String[] places = { "/sbin/", "/system/bin/", "/system/xbin/",
                "/data/local/xbin/", "/data/local/bin/",
                "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/",
                "/system/app/Superuser.apk", "/sbin/su", "/sbin/su/", "/system/bin/su","/system/bin/su/",
                "/system/xbin/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", "/su/",
                "/data/local/xbin/",
                "/system/bin/.ext/",
                "/system/bin/failsafe/",
                "/system/sd/xbin/",
                "/su/xbin/",
                "/su/bin/",
                "/magisk/.core/bin/",
                "/system/usr/we-need-root/",
                "/system/xbin/",
                "/system/su","/system/bin/.ext/.su","/system/usr/we-need-root/su-backup",
                "/system/xbin/mu",
                "/system/su/","/system/bin/.ext/.su/","/system/usr/we-need-root/su-backup/",
                "/system/xbin/mu/"};
        for (String where : places) {
            if (new File(where + binaryName).exists()) {
                found = true;
                break;
            }
        }
    }
    return found;
}
private static boolean isRooted() {
    return findBinary("su");
}

Using this for uninstall app (tested)

Intent intent=new Intent(Intent.ACTION_DELETE);
String packageName = "yourpackagename";
intent.setData(Uri.parse("package:"+packageName));
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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