Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a shell command as root with only the su binary and no SuperSU or Superuser apk installed?

Tags:

android

root

su

I have an Android phone that has only su binary installed and it works, meaning I can adb shell into the phone and run an 'su' command and I will be root.

When I try to run a command via code, it doesn't seem to work no matter which way I try to run it. I've tried many different variants of the following command.

Runtime.getRuntime().exec("su -c ps");

When I run this command on another rooted phone with a Superuser.apk or SuperSU.apk app installed, I get a dialog asking if I want to allow it to run with root permissions. When the apks are not there, it never asks and the command never works.

I've tried installing the apks on the first phone but they don't seem to do anything. So, as the original question asks --> Is there any way to run the elevated command from within the app without the SU apps installed?

like image 371
brianestey Avatar asked Jan 14 '13 07:01

brianestey


2 Answers

It might be because you need to pass the commands to su as parameters like this:
su -c 'ls -l'
Or you might need to specify the full path to su, but I don't see why it wouldn't work the way you have it: Runtime.exec("/system/bin/su -c ps")
Or maybe
Runtime.exec("/system/bin/su -c \'ps\'")

Try checking the output of this command too: System.getenv("PATH")

Another variant could be Runtime.exec("su -c \'ls -s \'")
Make sure you don't forget to escape the single quotes as they are part of the actual String.

Thats the way that I've found works most consistently, and it has also worked on devices that don't have Superuser or SuperSU installed, as those apps only listen for the Broadcast that is sent out when an application tries to run a command as root. @Boardy SuperSU and Superuser intercept the broadcast and so act as a middle man between the app and root privileges, but its not necessary for a rooted device. It IS necessary if you want to have more control over the applications that are running commands as root, but even then it still only limits you to deciding which applications, not which commands, are given root privileges.

Also, you might wanna take a look at RootTools and more specifically, the RootTools.isAccessGiven() command, which requests root privileges for your app.

Source: Launch a script as root through ADB

like image 103
cnexus Avatar answered Oct 23 '22 11:10

cnexus


Not all versions of an 'su' for Android will accept a command to execute from the command line parameters.

Where they do not, you will need to let 'su' launch a privileged shell, obtain its input file descriptor, and pipe command(s) into that. This has been covered numerous times here on Stackoverflow.

like image 45
Chris Stratton Avatar answered Oct 23 '22 09:10

Chris Stratton