在尝试之前,请阅读整个文章,然后进行选择。
使用二进制包装器(带有suid位)的解决方案
1) 创建一个脚本(最好是
.sh),其中包含要作为root用户运行的脚本。
# cat > php_shell.sh <<ConTENT #!/bin/sh /sbin/service sshd restartCONTENT
2) 该文件应归root用户所有,因为以后它将以root用户权限运行,因此请确保只有root用户具有写入文件的权限。
# chown root php_shell.sh# chmod u=rwx,go=xr php_shell.sh
3) 要以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); system ("/bin/sh /path/to/php_shell.sh"); return 0; }CONTENT4) 编译并设置适当的权限,包括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步骤 4
下编写这些命令。然后,您无需让二进制文件执行外部脚本来执行所讨论的命令。
在中
wrapper.c,用于
system ("your shell command here");指定您要执行的命令。


