-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRootChecker.java
166 lines (150 loc) · 5.38 KB
/
RootChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ahmed Moussa on 7/15/18.
* This is the main class that is responsible for checking if the device is rooted or not
*/
public class RootChecker {
/**
* application context to use to search the installed packages for a rooting application
*/
private Context context;
/**
* List of application that can be used to root the device
* TODO: Make this list populated from a remote DB Source
*/
private final String[] RootedAPKs = new String[] {
"com.noshufou.andriod.su" ,
"com.thirdparty.superuser",
"eu.chainfire.supersu",
"com.koushikdutta.superuser",
"com.devadvance.rootcloak2",
"com.zachspong.temprootremovejb",
"com.ramdroid.appquarantine"
};
/**
* RootChecker Constructor
* @param context access to package manager which will be used to check if rooted application are installed or not
*/
public RootChecker(Context context) {
this.context = context;
}
/**
* Main function to run to check if device is rooted or not
* @return true if device is rooted, false if not
*/
public boolean isDeviceRooted() {
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3() || checkRootMethod4(this.context);
}
/**
* Device is rooted if the build tags contains the provided tag
* @return true if build tag exist, false if not
*/
private boolean checkRootMethod1() {
String buildTags = android.os.Build.TAGS;
byte[] data = {116, 101, 115, 116, 45, 107, 101, 121, 115};
return buildTags != null && buildTags.contains(new String(data));
}
/**
* Device is rooted if the app have access to any of the provided directories
* @return true if app have access to any of the directories, false if not
*/
private boolean checkRootMethod2() {
try {
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",
"/sbin/su",
"/system/su",
"/system/bin/.ext/.su"
};
for (String path : paths) {
if (new File(path).exists()) {
return true;
}
}
return false;
} catch (Exception e) {
return false;
}
}
/**
* Run a shell command
* @return true if command was executed and returned result, false if returned null
*/
private boolean checkRootMethod3() {
ExecShell execShell = new ExecShell();
return execShell.executeCommand(ExecShell.SHELL_CMD.check_su_binary) != null;
}
/**
* Check if the provided packages names are installed on the device or not
* @param context current application context
* @return true if any of the provided packages exist, false if none is installed
*/
private boolean checkRootMethod4(Context context) {
final PackageManager packageManager = context.getPackageManager();
for (String s : this.RootedAPKs) {
Intent intent = packageManager.getLaunchIntentForPackage(s);
if (intent != null) {
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}
return false;
}
/**
* Created by Ahmed Moussa on 7/15/18.
* This class represent my interface with the shell commands
*/
private static class ExecShell {
/**
* list of shell commands that I will be suing
*/
public enum SHELL_CMD {
check_su_binary(new String[] { "/system/xbin/which", "su" });
String[] command;
SHELL_CMD(String[] command) {
this.command = command;
}
}
/**
* Run a shell command
* @param shellCmd Shell command that will be executed
* @return result of the given shell command
*/
public ArrayList<String> executeCommand(SHELL_CMD shellCmd) {
String line = null;
ArrayList<String> fullResponse = new ArrayList<String>();
Process localProcess = null;
try {
localProcess = Runtime.getRuntime().exec(shellCmd.command);
} catch (Exception e) {
return null;
}
BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
try {
while ((line = in.readLine()) != null) {
fullResponse.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return fullResponse;
}
}
}