如何生成与继承的权利和权限的进程 [英] How to generate processes with inherited rights and permissions

查看:117
本文介绍了如何生成与继承的权利和权限的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让一个进程,这一进程的所有继承的权​​限,我已经拥有。

例如,我有一些工艺;

 过程superUserShell =调用Runtime.getRuntime()EXEC(苏);

和我能够得到的输出流,并执行这样的命令

  DataOutputStream类的OutputStream =新的DataOutputStream类(superUserShell.getOutputStream());
// 例如
outputStream.writeBytes(RM -rf / *);
outputStream.flush();

但我不posobilities处理执行的命令的结果,所以我真的瓦纳是被另一个进程已经产生分离过程(例如,通过 superUserShell

有什么想法?


当然它不是邪恶目的^ _ ^这仅仅是第一件事,我记住了。
其实我的工作为Android fbgrab小wraper ...

  P =调用Runtime.getRuntime()EXEC(苏); //让我们假设我的Andr​​oid操作系统授予超级用户premissions。这不是问题!!!!
DataOutputStream类的OutputStream =新的DataOutputStream类(p.getOutputStream());
//我要的是一堆另一个流程//
//由另一人用它产生的premissions
//而不是由wryting到标准输入生成它们
流程catProcess; // ......
流程someAnotherBinaryExecutionProcess; // ......
outputStream.writeBytes(猫的/ dev /图形/ FB0>中+ getFilesDir()+/ FB0 \\ n);outputStream.writeBytes(退出\\ n);outputStream.flush();p.waitFor();


解决方案

首先,我希望这不是被用于邪恶目的。您的例子RM -rf / *使我有些担心。

如果您调用Runtime.getRuntime()。EXEC(庆典)你会得到,你可以发送命令和从得到响应的外壳。因此,举例来说,你可以配合控制台进去:

 最后工序流程=调用Runtime.getRuntime()EXEC(喂)。新的Thread(){
    公共无效的run(){
        尝试{
            InputStreamReader的读者=新的InputStreamReader(process.getInputStream());
            对于(INT C = reader.read(!); C = -1; C = reader.read()){
                System.out.print((char)的C);
            }
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}。开始();//(同为进程的错误流重定向到System.err的,如果你想)InputStreamReader的fromKeyboard =新的InputStreamReader(System.in);
OutputStreamWriter麦克罗公司一直供应=新OutputStreamWriter(process.getOutputStream());对于(INT C = fromKeyboard.read(!); C = -1; C = fromKeyboard.read()){
    toProcess.write((char)的C);
    toProcess.flush();
}

这是试验,看看你的操作系统会让你做一个好办法。在Mac OS,如果我想从须藤此过程中的命令,我碰上它不能从STDIN接受我的密码的问题,因为它不是一个真正的登录shell。所以,我必须这样做:

  SUDO_ASKPASS =password.sh须藤-A<指挥GT;

...其中password.sh只是呼应我的密码,就是我要以root身份运行命令(我用漂亮的安全PWD,而不是你擦拭我的根文件系统的例子)。

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).

这篇关于如何生成与继承的权利和权限的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆