通过PHP执行root命令

我有一个CentOS 5.7 linux服务器,并使用php5.3.x。

在pfSense系统上,您可以使用php网页重新启动需要root权限的服务。

我正在尝试做类似的事情,我已经写了一些php代码来执行shell命令。例如,重新启动sshd服务:

<?php

exec('/sbin/service sshd restart');

?>

我试图通过exec函数执行该命令,但是它需要root权限,但是我们拥有apache用户权限。

我遇到了一些解决方案:

  1. “以root用户运行apache”确实不安全。我不想那样做。
  2. 我尝试了“ apache ALL = NOPASSWD:/ sbin / service到/ etc / sudoers”,但是仍然有问题。

还有其他解决方案吗?感谢您的回答。


现在..这很有趣。我尝试了@refp post,它在我的本地ubuntu服务器上起作用。但是,当我在cenOS

vps服务器上尝试相同的操作时。它不起作用。这是apache的错误日志“ rm:无法删除`/ var / lock / subsys /

vsftpd’:权限被拒绝”

回答:

在尝试之前,请阅读整个文章,然后进行选择。


回答:

创建一个脚本(最好是.sh),其中包含要作为root用户运行的脚本。

# cat > php_shell.sh <<CONTENT

#!/bin/sh

/sbin/service sshd restart

CONTENT

该文件应归root用户所有,因为以后它将以root用户权限运行,因此请确保只有root用户具有写入文件的权限。

# chown root php_shell.sh

# chmod u=rwx,go=xr php_shell.sh

要以root用户身份运行脚本,无论执行该脚本的用户是什么,我们都需要一个二进制包装器。创建一个将执行我们的php_shell.sh

# cat > wrapper.c <<CONTENT

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

int

main (int argc, char *argv[])

{

setuid (0);

/* WARNING: Only use an absolute path to the script to execute,

* a malicious user might fool the binary and execute

* arbitary commands if not.

* */

system ("/bin/sh /path/to/php_shell.sh");

return 0;

}

CONTENT

编译并设置适当的权限,包括suid位(假设它应以root特权运行):

# gcc wrapper.c -o php_root

# chown root php_root

# chmod u=rwx,go=xr,+s php_root

php_root现在将以root权限运行,并执行中指定的命令php_shell.sh


如果您不需要轻松更改将要执行的命令的选项,建议您直接在wrapper.c步骤

下编写这些命令。然后,您无需让二进制文件执行外部脚本来执行所讨论的命令。

在中wrapper.c,用于system ("your shell command here");指定您要执行的命令。

以上是 通过PHP执行root命令 的全部内容, 来源链接: utcz.com/qa/399199.html

回到顶部