Is there any way to make a process with all inherited rights of the process, i already own.
For example i have some process;
Process superUserShell = Runtime.getRuntime().exec("su");
and i am able to get output stream and execute commands like this
DataOutputStream outputStream = new DataOutputStream(superUserShell.getOutputStream());
// for example
outputStream.writeBytes("rm -rf /*");
outputStream.flush();
but i have no posobilities to handle results of executed commands, so what i really wana is to have separated Processes generated by another Process(for example by "superUserShell")
Any thoughts?
of course it is not for evil purposes ^_^ this just the first thing i got in mind. actually i am working on small wraper of fbgrab for android...
p = Runtime.getRuntime().exec("su");//lets assume my android os grants super user premissions. this is not the question!!!!
DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
//all i want is a bunch of another processes//
// generated by another one with it's premissions
//instead of generating them by wryting to stdin
Process catProcess;//......
Process someAnotherBinaryExecutionProcess;//......
outputStream.writeBytes("cat /dev/graphics/fb0 > "+ getFilesDir() + "/fb0\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
p.waitFor();
First of all, I hope this isn't being used for evil purposes. Your example of "rm -rf /*" causes me some concern.
If you do Runtime.getRuntime().exec("bash") you'll get a shell that you can send commands to and get responses from. So, for example, you could tie the console into it:
final Process process = Runtime.getRuntime().exec("bash");
new Thread() {
public void run() {
try {
InputStreamReader reader = new InputStreamReader(process.getInputStream());
for(int c = reader.read(); c != -1; c = reader.read()) {
System.out.print((char)c);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}.start();
// (Same for redirecting the process's error stream to System.err if you want)
InputStreamReader fromKeyboard = new InputStreamReader(System.in);
OutputStreamWriter toProcess = new OutputStreamWriter(process.getOutputStream());
for(int c = fromKeyboard.read(); c != -1; c = fromKeyboard.read()) {
toProcess.write((char)c);
toProcess.flush();
}
This is a good way to experiment and see what your OS will let you do. On Mac OS, if I want to sudo a command from this process, I run into the problem that it can't accept my password from STDIN because it is not really a login shell. So, I have to do this:
SUDO_ASKPASS="password.sh" sudo -A <command>
... where "password.sh" just echoes my password, and is the command I want to run as root (I used the nice safe "pwd" instead of your wipe-my-root-filesystem example).
A few notes:
I suppose you already get output from this process, via
Process.getInputStream()?BufferedReader buf = new BufferedReader( new InputStreamReader( superUserShell.getInputStream() ) ) ; while ( ( String line ; line = buf.readLine() ) != null ) { // do domething with data from process; }Try adding newline to the command, e.g.
"rm -rf /* \r\n"If you send multiple commands consecutively (and read reply) then you might want to send and receive data in separate threads.
Selvin's right, su returns immediately, and doesn't provide your application with a 'shell' type of situation like a real, interactive shell would. What you want to look into is something like sudo <command> to get su to run the command you want.
来源:https://stackoverflow.com/questions/8746869/how-to-generate-processes-with-inherited-rights-and-permissions