简体   繁体   English

如何以root身份运行PHP exec()?

[英]How to run PHP exec() as root?

I'm trying to build a firewall manager in PHP, but when I execute, <?php exec('iptables -L'); ?> 我正在尝试用PHP构建防火墙管理器,但是当我执行时, <?php exec('iptables -L'); ?> <?php exec('iptables -L'); ?> , the result array is empty. <?php exec('iptables -L'); ?> ,结果数组为空。

I have tried, <?php echo exec('whoami'); ?> 我试过了, <?php echo exec('whoami'); ?> <?php echo exec('whoami'); ?> , and the response is www-data (the user that Apache is using). <?php echo exec('whoami'); ?> ,响应为www-data (Apache使用的用户)。 What can I do to execute the exec function as root? 我该怎么做才能以根用户身份执行exec函数? (Preferably without changing the Apache user.) (最好不更改Apache用户。)

Don't do it! 别做! You will leave yourself wide open to all sorts of malicious hackery. 您将对各种恶意黑客敞开大门。

Have a look at the "sudo" documentation. 看看“ sudo”文档。

You should be able to set up all the commands you need as "sudo"able scripts. 您应该能够将所需的所有命令设置为“ sudo”脚本。 It is much better to write specific scripts with limited functions than to expose the underlying priviledged command. 编写功能有限的特定脚本要比公开基础特权命令好得多。

As in: 如:

exec ('sudo getIpTables.ksh')

You can run sudo through phpseclib, a pure PHP SSH implementation : 您可以通过phpseclib(纯PHP SSH实现)运行sudo:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("sudo command\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>

I know this is an old question 我知道这是一个老问题

add the user php runs on to the sudo group if it is not already assigned 将用户php运行到sudo组(如果尚未分配)

use sudo -S, so you can pass the password via echo 使用sudo -S,因此您可以通过echo传递密码

$exec = "echo your_passwd | /usr/bin/sudo -S your command";
exec($exec,$out,$rcode);

if you have trouble with the paths - use 如果您在使用路径时遇到麻烦-请使用

"bash -lc 'echo your_passwd | /usr/bin/sudo -S your command'"

so you get a new bash that acts like a login shell and has the paths set 这样您将获得一个新的bash,其作用类似于登录外壳并设置了路径

check the man pages of sudo 检查sudo的手册页

Unless you use suphp and configure it to run as root you wont be able to run any PHP script on behalf of any other system user besides who is running PHP. 除非您使用suphp并将其配置为以root身份运行,否则您将无法代表其他系统用户(运行PHP的人)运行任何PHP脚本。

Edit: 编辑:

Just an small idea. 只是一个小主意。 Add a queue process in some way and run a cron process in the root's crontab. 以某种方式添加队列进程,然后在根目录的crontab中运行cron进程。

Please please be really careful about this. 请注意这一点。 Any injection can literally destroy the system. 任何注入实际上都会破坏系统。

This is very unsafe and a bad idea. 这是非常不安全的,也是一个坏主意。 Rethink your design. 重新考虑您的设计。 If you really want to do this use sudo as advised. 如果您确实要这样做,请按照建议使用sudo。 An alternative solution might be to go ahead and run as root but do so inside a chroot or a vm image (both of which can be broken out of but still). 另一种解决方案可能是继续以root身份运行,但可以在chroot或vm映像(可以同时拆分但仍然可以使用)中运行。

Or best of all run as sudo inside a chroot! 或者最好是在chroot中以sudo的身份运行!

You can put the required commands in a separate script/executable file (sh, PHP, a real executable, doesn't matter), change its owner to root, and apply "setuid" to it. 您可以将所需的命令放在单独的脚本/可执行文件中(sh,PHP,实际可执行文件,无关紧要),将其所有者更改为root,然后对其应用“ setuid”。

This will allow anything and anyone to run this script as root, so you need to make sure that it has it's own security rules for seeing if this is allowed, and is very restricted in what it does. 这将允许任何人和任何人以root身份运行此脚本,因此您需要确保它具有自己的安全规则,以查看是否允许这样做,并且该脚本的工作受到很大限制。

I recently published a project that allows PHP to obtain and interact with a real Bash shell. 我最近发布了一个项目,该项目允许PHP获取真正的Bash shell并与之交互。 Get it here: https://github.com/merlinthemagic/MTS 在这里获取它: https : //github.com/merlinthemagic/MTS

After downloading you would simply use the following code: 下载后,您只需使用以下代码:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('iptables -L');
//the return will be a string containing the return of the command
echo $return1;

Just an assumption - Is this a PHP web app that will do this? 只是一个假设-这将是一个PHP Web应用程序吗? This doesn't sound too safe. 听起来不太安全。 The app that needs root - could you build that separately and then invoke it from PHP? 需要root的应用程序-您可以分别构建它,然后从PHP调用它吗? If not, maybe you can sudo the process so it can run as root. 如果没有,也许您可​​以对进程进行sudo操作,以便它可以以root身份运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM