I would execute a command on my rooted Android 2.1 device
String path = "/data/data/com.android.providers.settings/databases/settings.db";
Runtime.getRuntime().exec("/system/bin/chmod -f 777 " + path);
But this command does nothing on the targeted file. Any idea?
The RootTools library offers simple methods to check for root and issue root commands:
RootTools.isRootAvailable()
List<String> output = RootTools.sendShell(command);
http://code.google.com/p/roottools/
You need to get the runtime as the root user first. There is a nice ShellInterface class that you can use from the MarketEnabler source available on Google Code. Though keep in mind this source code is released under the GPL.
Essentially what you need to do is determine where your su command is and create a kind of shell using an input stream and output stream for STDIN and STDOUT. With these you can then push your commands to your "terminal". When you are done all your commands, flush your buffer and then wait for the Runtime to complete. Once it is completed, you can then close your runtime interface.
Then take a look at the file you have tried creating/modifying/etc to see if everything worked properly.
Bao Le I believe you are trying to drop shell commands in an Android App, here (Running Android Native Code in your Android app)are few of many ways to run a command from an app.
Related
I've noticed that it is possible on Android to change the permissions on a file with chmod, which means we can easily execute anything from an application:
var runtime = Runtime.GetRuntime();
runtime.Exec("chmod 0755 /my/file").WaitFor();
// Then ProcessBuilder to execute it.
Would Google Play Store accept an application that takes advantage of this flaw? I can't find any documentation about it, but I confirm that it works.
Actually, I want to include ffmpeg for tasks that are too slow to be executed using MediaCodec.
(I've also noticed that the Android framework sometimes directly access to a native version of ffmpeg, so maybe I could access it directly from the phone?)
I don't know for sure if it is ok for Google Play.
However i don't think it is security issue. You will exec process with the autorisation of your app.
I hope the following example will help you.
You can try the following command line with your device connect.
adb shell
To have a shell on your devices
Then you can try to look what is inside the files for an app (replace com.your.package by the name of a debbugable apk)
ls /data/data/com.your.package
This command will failde because you have not the good permission.
Now run the following command.
run-as com.your.package
You will now exec your command line with the same permission as your app.
You can now retry the previous ls command. It will work. However it will not work for another package.
So, i think the command you will exec with your code, will be exec with the privilege of your app. So i don't think you can elevate the privilege of you app on a file with this method.
What is the default built in shell in Android phones and can I run runtime.exec Java method without installing a shell ?
You can call runtime.exec(String) in Android but it won't get you very far, because the runtime doesn't really offer commands to consume. However, if you've got a rooted phone with busybox and su installed, it is possible to call those embedded commands. It's even possible to create a superuser session by executing the su binary and consuming the streams of it.
I want to run script file on android Shell using Native C program.
I tried using system function but it's not working.
result = system("sh ./test.sh");
LOGD("result is %d", result);
system command returns 0 means its not executed script file successfully.
test.sh contains
echo "test...."
Android NDK application could not print test.... when this system call runs.
Even any script can not be started using system call. i checked more than 10 different scripts.
test.sh have 777 permissions
Any help would be appreciated.
where is 'sh'? and what is your '.' current directory when the application runs? try:
result = system("/system/bin/sh /full/path/to/test.sh");
Here i got the answer of this Question on Different post.
Run Shell Script file On Android Embedded Device using System function in Android NDK
thanks to all for Help
I've got a problem which is probably very easy to solve but I can't seem to find the answer.
I'm trying to execute strace on different processes running on an Android device. I've succeded in doing this by copying the strace binary to /system/xbin (just like the emulator) and works just fine. My problem is I copied the binary through the bootloader and I don't want users to have to go through that.
So I found this great tutorial to run binaries from within an app:
http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App
I follow the tutorial and do this:
Copy the binary of strace to /data/data/com.mypackage
Change execution permissions to the binary (I've tryed with 777 too)
Try to execute it with
Process process = Runtime.getRuntime().exec("/data/data/com.mypackage/strace -p PID -o /sdcard/folder_I_know_it_works");
I know the command is right because when I execute it from /xbin/system it works perfectly. When I run the one I copied from within my app I get empty outputs. I only get the proper output for my own process, so I understand it's a permission problem.
I didn't mention yet that I have root permission on my app of course. I also tryed changing the owner to the strace binary to root:root and system:system and nothing.
To sum up: I need to reach the same permission on a binary I copy from my app to the phone memory than the one I have on a binary found on /xbin/strace
What am I doing wrong?
Another good solution would be finding a way to copy strace to /system/xbin from within an app with root permission. Any ideas?
Thanks a lot in advance!
Found the answer to this in case it's useful for anyone else.
Process process = Runtime.getRuntime().exec("su");
OutputStream os;
os = process.getOutputStream();
os.write("your linux command here".getBytes());
os.close();
You are basically simply running su as a process and sending commands through its output stream.
Later you may want to call process.wait()
I want to write a small tool to move apps to SDcard.
I found the movePackage()-method in Android Open Source and reflect the method. I failed because this method need com.android.PERMISSION.MOVE_PACKAGE which I cannot get. So I want to using shell script to do this for rooted devices.
But I don't actually know what happened in the movePackage()-method. So I can't write the correct script.
Could you please tell what happened inside the Android when a app is moved to SDcard? Can I do this with program?
I'm not sure If I understand you, but on rooted device you can use adb.
For example:
adb push /home/username/Desktop/app.apk /sdcard/app.apk
Also you can do this (for removing):
adb shell rm /sdcard/app.apk
If you want to install:
adb install /home/username/Desktop/app.apk