Android Question [SOLVED] A simple and very oblique way to check if device is rooted

JackKirk

Well-Known Member
Licensed User
Longtime User
From:
https://www.b4x.com/android/forum/threads/check-if-device-rooted.127150/

I have this bit of code:
B4X:
Private Sub Activity_Create(FirstTime As Boolean)
   
    'Check if device is rooted
    Private jo As JavaObject
    jo.InitializeContext
    Private rooted As Boolean = jo.RunMethod("isRooted", Null)
    If rooted Then ExitApplication
   
    ...

#If Java
import java.io.*;
public static boolean isRooted() {
    return findBinary("su");
}
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/"};
        for (String where : places) {
            if (new File(where + binaryName).exists()) {
                found = true;
                break;
            }
        }
    }
    return found;
}
#End If

To make it harder for a miscreant to decompile and work out what I have done I would like to pass:

String[] places = {"/sbin/",
"/system/bin/",
"/system/xbin/",
"/data/local/xbin/",
"/data/local/bin/",
"/system/sd/xbin/",
"/system/bin/failsafe/",
"/data/local/"};

into the inline java from the B4A code, something like:
B4X:
Private Sub Activity_Create(FirstTime As Boolean)
   
    'Check if device is rooted
    Private jo As JavaObject
    jo.InitializeContext
    Private rooted As Boolean = jo.RunMethod("isRooted", Array( & _
        "/sbin/", & _
        "/system/bin/", & _
        "/system/xbin/", & _
        "/data/local/xbin/", & _
        "/data/local/bin/", & _
        "/system/sd/xbin/", & _
        "/system/bin/failsafe/", & _
        "/data/local/"))

    If rooted Then ExitApplication

...

#If Java
import java.io.*;
public static boolean isRooted(String[] places) {
    return findBinary("su");
}
public static boolean findBinary(String binaryName) {
    boolean found = false;
    if (!found) {places};
        for (String where : places) {
            if (new File(where + binaryName).exists()) {
                found = true;
                break;
            }
        }
    }
    return found;
}
#End If

but I'm getting totally lost - even with lots of googling.

Can anyone steer me to the correct syntax?

Thanks in anticipation...

PS I would also like to pass in the "su" string.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why do you need this inline Java?

B4X:
Private rawplaces As String = "/sbin/,/system/bin/,..." 'process globals - it will be obfuscated

Dim places() As String = Regex.Split(",", rawplaces)
For Each place As String In places
 If File.Exists(place, "su") Then Return True
Next
Return False
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erels solution hides the intent beautifully from decompiles via dex2jar and jd.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I've done a bit more googling on this subject - it suggests that the above may detect some rooting but not all - there appears to be no definitive detection method - given the nature of the problem I guess this is logical.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I'm just curious:

As I remember (10 years ago): If a phone is rooted, this doesn't mean every app has root privileges automatically. The app needs to aquire root access, doesn't it? So it's not just a folder check (if not root access is aquired the folders are not accessable anyway so it could seem as if the phone isn't rooted).

Maybe I'm wrong and there are newer root methods. 10 years ago you needed to grant access via a superuser or similar.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Upvote 0

amidgeha

Active Member
Licensed User
Longtime User
Many years back I used this code to check if phone is rooted:
Java:
 public boolean isPhoneRooted(){
        Process process = null;           
        try{
            process = Runtime.getRuntime().exec("su");
            return true;
        } catch (Exception e) {
            return false;
        } finally{
            if(process != null){
                try{
                    process.destroy();
                }catch (Exception e) {
                }
            }
        }
    }
 
Upvote 0
Top