Using adb.exe that comes with the Android SDK, I can get root access to an Android device.
For testing purposes, I would like to give an Android app root permissions as well.
I know that app is running under a particular account called app_68.
Is there an adb shell command to add app_68 to the "root group"?
Thanks in advance for your comments/solutions.
You need the superuser (su) binary to run your app as root user.
For running as root implement this:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("yourCommand\n");
os.writeBytes("exit\n");
os.flush();
os.close();
try { p.waitFor(); } catch (InterruptedException e) {}
If you get something like su: uid xxxx not allowed, then you need to update your su-binary using SuperSU.
Also see https://stackoverflow.com/a/26654728/4479004 if you want a fully detailed and working source.Just look at the code below:
Update: To get the result (the output to stdout), use:
Related
I have gone over all the possible answers to this questions, and none of them has worked for me. Seems like Android has very tightly blocked the running of the shell commands from within an app, even when the device is rooted.
I can do su and run commands perfectly fine from adb, but I cannot run them from the app itself. It fails with error:
Error running exec(). Command: [su] Working Directory: null Environment: null
I have searched for these errors and the solutions work for older versions of Android, not Marshmallow.
I have also tried libsuperuser (SuperSu), but it also fails on Marshmallow.
Other things I tried were like, e.g. for the device reboot:
Process p = Runtime.getRuntime().exec("su");
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
or
PowerManager pm = (PowerManager)...
Same with other operations. The commands fail right at su. I can't issue an adb command either from the app. But I can do both perfectly fine otherwise from the command line via adb shell.
The example which Android has on its website doesn't work either.
I read about disabling SELinux security and setting it to permissive mode. But that also require to run su first, where the app fails.
Now I am totally lost and don't know what to do next. I am really looking for a working solution since for this particular project, which requires the app to interact with the underlying hardware.
You need to first ensure you have busybox installed as that would install the list of most commonly used shell commands and then use the following code to run the command.
Runtime.getRuntime().exec("ls");
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Try this
Process command = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("-c reboot\n");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();
It can take time to execute so u might wanna run this on worker thread
There is nothing that blocks shell commands in "Marshmallow" . I am using it inside my app and all commands work flawlessly. There is something else you are missing. First confirm that in your "/system/bin/" directory all files are present. Alternatively check in other device.
Try to add "sh -c " as the beginning of your shell command. Otherwise you will get the error message about null directory and environment.
Example: sh -c su
I have an Android application utilizing RootTools v4.2 (the latest I know of) and I have followed their documentation on how to execute shell commands as root. Sometimes the commands execute just fine, other times the app crashes with the following exception.
java.lang.IllegalStateException: Unable to add commands to a closed shell
Here is the actual code the exception is being throw on:
RootTools.getShell(true).add(cmd);
So I'm wondering since the docs make no mention of this sort of problem if there is something else I'm doing wrong? Looking through the docs I see nothing on how to ensure I get an open shell before I start adding commands.
This code is working with me . Try to install the Library again may be its not vaild .
if(RootTools.isAccessGiven()){
try {
Shell shell = RootTools.getShell(true);
JavaCommand cmd = new JavaCommand(0,this,"input keyevent 26");
shell.add(cmd);
}
catch (Exception e){
Log.d("ERRORS : ",e.getMessage());
}
}
Process p1;
p1=Runtime.getRuntime().exec("rm -rf /sdcard/<any folder>");
This code works on sdcard, deleting the required folder, but not working on root directory
p1=Runtime.getRuntime().exec("rm -rf /data/data/<any folder>");
This code is not working any suggestions?
i rooted my phone and got super user access.
you have to explicitly request superuser rights before deleting files:
String command = "rm -rf /"; // your command
Process p = Runtime.getRuntime().exec( "su" );
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
also it's a good idea to wrap this in exception handler to handle various errors (no SU installed, wrong command, IOException, InterruptedException etc.)
Access to /sdcard is not restricted. Any process can read or write to it. Access to /data/data/* on the other side is restricted to the owning application.
A rooted phone doesn't mean, that all your applications have root access. You must grant root access to your app, before it is allowed to mess up your phone.
I need to run an .sh file that starts a process in background as root from an APK, but couldn't do it. Even when I use su it gives the APP level permissions. Here is my .sh fule contents
#!/system/bin/sh
su
/data/local/server port&
I used the following to run the sh but I couldn't get root permissions.
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("su");
proc = rt.exec("sh /sdcard/server.sh");
}catch(Exception e) {
e.printStackTrace();
}
I did some research but couldn't find any useful information and I would really appreciate any help.
Thanks.
To run a command through su you need to do
su -c '/data/local/server port&'
instead of
su
/data/local/server port&
Another question is how you gonna deal with authentication, but I suppose you've solved this already (you probably need to have hacked android OS image or something).
On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Or the command cp, it returns:
sh: tar: not found
Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:
int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
+ packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())), 64);
String inLine;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append(" ");
sbCommand.append("\n");
os.writeBytes(command.toString());
if (is != null) {
for (int i = 0; i < timeout; i++) {
if (is.ready())
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (is.ready()) {
inLine = is.readLine();
} else {
}
}
} catch (IOException e) {
e.printStackTrace();
}
It always stops in is.ready(), and when I changed it to process.waitfor() it also stopped. Why?
As far as i know, the only way to run shell commands is:
Process proc = Runtime.getRuntime().exec("your command");
You can run Linux commands on Android. But there are usually just very few pre-installed.
If you want to add more commands you might want to root your device and install busybox on it.
This is not for productive use within an application but can help you to work with your device.
If you have the binaries for your system, you can run anything on your system.
Saying that you have to understand that you have to find the binaries for tar.
Look here http://forum.xda-developers.com/showthread.php?t=872438
And possibly other places..
You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.
You can install Termux app on your android device and run Linux command by using that app
Install busybox, then type the command in the following format:
busybox [linux command]
You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.
FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:
ls -a /system/bin/sh
Hope this helps.
In reply to Igor Ganapolsky, You would have to have a database set up for locate.
Probably find would be adequate for your needs.
example:
find -name *.apk