How to Obtain ROOT Permission on Android Using Kotlin
Root permission on Android is a specialized authorization process that provides the owner or user (super user) of an Android device with full access and control over the device’s operating system. This process allows the user to intervene at deeper levels of the device and remove certain restrictions.
When root permission is obtained, the user can perform various operations, including:
• It can read, write, or delete and modify system files and folders.
• It can manage system applications and services.
• It can have direct access to hardware.
In Kotlin, the following code can be used to obtain ROOT permission:
fun grantRootAccess() {
var isRooted = try {
Runtime.getRuntime().exec("su")
true
} catch (e: IOException) {
false
} catch (e: TimeoutException) {
false
}
Thread.sleep(1000)
if (isRooted) {
Log.d("Superuser", "Root access is successful.")
} else {
Log.e("Superuser", "Root access could not be provided.")
}
}
Access the system file system using the following code:
import android.os.Environment
fun getRootDirectory(): File {
return Environment.getRootDirectory()
}
fun readRootFile(path: String): String {
return File(getRootDirectory(), path).readText()
}
fun writeRootFile(path: String, data: String) {
File(getRootDirectory(), path).writeText(data)
}
fun deleteRootFile(path: String) {
File(getRootDirectory(), path).delete()
}
These codes perform operations such as accessing the root directory of the Android device’s file system, reading text from a file, writing text to a file, and deleting a file.
Manage system applications and services using the following code:
import android.content.pm.PackageManager
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInstaller
fun getSystemApplications(packageManager: PackageManager): List<ApplicationInfo> {
return packageManager.getInstalledApplications(0)
}
fun getSystemApplicationInfo(packageManager: PackageManager, packageName: String): ApplicationInfo? {
return packageManager.getApplicationInfo(packageName, 0)
}
fun installSystemApp(packageInstaller: PackageInstaller, packageName: String, apkFile: File) {
val appPackage = PackageInstaller.SessionParams(
installerPackageName = packageName,
installLocation = PackageInstaller.SessionParams.Mode.INTERNAL
)
val session = packageInstaller.createSession(appPackage)
session.openSession(apkFile)
session.commit(PackageInstaller.SessionCallback())
}
fun uninstallSystemApp(packageManager: PackageManager, packageName: String) {
packageManager.deletePackage(packageName, null, null)
}
These codes perform operations such as listing all system applications installed on an Android device, retrieving information about a specific system application, installing a system application, and uninstalling a system application.
Directly access hardware using the following code:
import android.hardware.SensorManager
import android.hardware.Sensor
fun getSensorManager(): SensorManager {
return getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
fun getSensors(sensorManager: SensorManager): List<Sensor> {
return sensorManager.getSensorList(Sensor.TYPE_ALL)
}
fun getSensor(sensorManager: SensorManager, sensorType: Int): Sensor? {
return sensorManager.getDefaultSensor(sensorType)
}
fun registerSensorListener(sensorManager: SensorManager, sensor: Sensor, listener: SensorEventListener) {
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL)
}
fun unregisterSensorListener(sensorManager: SensorManager, listener: SensorEventListener) {
sensorManager.unregisterListener(listener)
}
These codes perform operations related to the Android device’s sensor manager, including accessing all the sensors available on the device, retrieving information about a specific sensor, registering a listener for a particular sensor, and removing a listener from a specific sensor.
Soliciting root permission from the user is a sensitive event. When granted, your application can potentially become a system application, operate at the kernel level, and additionally, applications requesting root permission may not be eligible for distribution on Google Play.
NOTE: This blog post does not aim to develop any hacking applications or malicious software targeting user sensitive information!