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).
Related
my environment:
cpu:rk3288
os:android7.1
transfer method:sftp
I wrote android code to do these things below:
get the logcat with code "adb logcat -d -v time -f /mnt/sdcard/logcat.txt"
pull the file logcat.tx to the server with sftp
in 1st step I coding some java language with android studio like below, if anyone can help me, thanks!
Process p = Runtime.getRuntime().exec("adb logcat -d -v time -f /mnt/sdcard/logcat.txt");
error massage:
java.io.IOException: Cannot run program "adb": error=13, Permission denied
You can't use adb commands from inside the device, even if you somehow have it in the device you would need root permissions. The adb is the bridge between the PC and the device. Take a look here: https://developer.android.com/studio/command-line/adb
Although you probably can just remove the adb and use logcat directly, like:
Process p = Runtime.getRuntime().exec("logcat -d -v time -f /mnt/sdcard/logcat.txt");
Here you can see some more options about using the logcat command: Read logcat programmatically within application, including Reetpreet Brar's answer, that I think will be better for you:
Process pq=Runtime.getRuntime().exec("logcat v main"); //adapt the command for yourself
BufferedReader brq = new BufferedReader(new InputStreamReader(pq.getInputStream()));
String sq="";
while ((sq = brq.readLine()) != null)
{
//here you can do what you want with the log, like writing in a file to
//send to the server later.
}
Here you can choose methods to write your file: How to Read/Write String from a File in Android
Then, just send the file to the server.
I'm trying to use linux command in my app. But I failed to get root permission.
Could you please let me know how to get root permission?
String[] cmd = {"su"};
String result_process2 = runProcess2(cmd);
public String runProcess2(String[] cmd) {
try {
Process process = Runtime.getRuntime().exec(cmd);
It looks like doesn't work.
One thing weird is
I found su in /system/xbin, but when I check this directory through ls in app, su file didn't exist there.
Command is
String ls = "ls -al /system/xbin/";
String resultls = runProcess((String)ls);
public String runProcess(String arg) {
try {
Process process = Runtime.getRuntime().exec(arg);
Do you know why I couldn't see su file in /system/xbin/ even though it can be found through adb shell in my laptop?
A standard Android device does not have access to root privileges. In order to attain this you will have to root the device. See this link for more information.
As for why you can find the file through the ADB shell and not from within the app is probably that you don't have read access for the file outside the ADB shell.
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.
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:
I try to change file permission in application. Code is below:
Runtime.getRuntime().exec("chmod 777 /sdcard/test.txt");
This code NOT works in my application, but no error log.
I also checked the shell tools under /system/bin, find chmod is under /system/bin, but some other info shown that chmod > toolbox. I am not clear about this. My application has used android:sharedUserId="android.uid.system".
How to run this code or how to change permission of file? Thanks a lot.
Runtime rt = Runtime.getRuntime();
Process proc;
try {
proc = rt.exec(new String[] { "su", "-c", "chmod 777 " + Constants.filename });
proc.waitFor();
} catch (Exception e){ //DOSTUFFS }
This should do the trick
You've used the path /sdcard/ in your test -- is your SD Card formatted with a filesystem that supports standard Unix permissions? (FAT does not.)
You did not give an explicit path to chmod(1) in your string -- are you certain that chmod(1) is:
available on your device
available with your current PATH environment variable setting?
You can only change the permissions on files you own; are you certain that whatever your application's effective userid is owns the file on the SD card?
Lastly, Android may have additional security restrictions on changing file permissions. I don't know Android well, but perhaps changing file permission bits requires entries in the manifest declaring the operations, perhaps changing file permissions can only be done through provided APIs.