-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRootChecker.kt
137 lines (127 loc) · 4.44 KB
/
RootChecker.kt
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
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
class RootChecker(private val context: Context) {
/**
* List of application that can be used to root the device
*/
private val RootedAPKs = arrayOf(
"com.noshufou.andriod.su",
"com.thirdparty.superuser",
"eu.chainfire.supersu",
"com.koushikdutta.superuser",
"com.devadvance.rootcloak2",
"com.zachspong.temprootremovejb",
"com.ramdroid.appquarantine"
)
/**
* Main function to run to check if device is rooted or not
* @return true if device is rooted, false if not
*/
val isDeviceRooted: Boolean
get() = checkRootMethod1() || checkRootMethod2() || checkRootMethod3() || checkRootMethod4(context)
/**
* Device is rooted if the build tags contains the provided tag
* @return true if build tag exist, false if not
*/
private fun checkRootMethod1(): Boolean {
val buildTags = Build.TAGS
val data = byteArrayOf(116, 101, 115, 116, 45, 107, 101, 121, 115)
return buildTags != null && buildTags.contains(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 fun checkRootMethod2(): Boolean {
return try {
val paths = arrayOf(
"/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 (path in paths) {
if (File(path).exists()) {
return true
}
}
false
} catch (e: Exception) {
false
}
}
/**
* Run a shell command
* @return true if command was executed and returned result, false if returned null
*/
private fun checkRootMethod3(): Boolean {
val execShell = 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 fun checkRootMethod4(context: Context): Boolean {
val packageManager: PackageManager = context.getPackageManager()
for (s in RootedAPKs) {
val intent = packageManager.getLaunchIntentForPackage(s)
if (intent != null) {
val 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 class ExecShell {
/**
* list of shell commands that I will be suing
*/
enum class SHELL_CMD(var command: Array<String>) {
check_su_binary(arrayOf<String>("/system/xbin/which", "su"));
}
/**
* Run a shell command
* @param shellCmd Shell command that will be executed
* @return result of the given shell command
*/
fun executeCommand(shellCmd: SHELL_CMD): ArrayList<String?>? {
var line: String? = null
val fullResponse = ArrayList<String?>()
var localProcess: Process? = null
localProcess = try {
Runtime.getRuntime().exec(shellCmd.command)
} catch (e: Exception) {
return null
}
val IN = BufferedReader(InputStreamReader(localProcess.inputStream))
try {
while (IN.readLine().also { line = it } != null) {
fullResponse.add(line)
}
} catch (e: Exception) {
e.printStackTrace()
}
return fullResponse
}
}
}