您好,欢迎访问一九零五行业门户网

Linux系统编程:用PHP执行Root命令

在玩c以前玩过一段时间的php, 哪个时候需要用php 来运行root命令,一直未果,直到有一天搜索到了super这个插件。 随着玩c的日子多了,发现可以用c语言来包裹 要运行的外部命令。实验了一下,成功了。不需要任何外部工具就可以实现用php 执行root命令。我下面就
在玩c以前玩过一段时间的php, 哪个时候需要用php 来运行root命令,一直未果,直到有一天搜索到了super这个插件。
随着玩c的日子多了,发现可以用c语言来包裹 要运行的外部命令。实验了一下,成功了。不需要任何外部工具就可以实现用php 执行root命令。我下面就把方法发布给大家,有需求用php来运行root命令的朋友可以不用发愁了。
平台:linux
实验命令iptables,当前的目录是/var/www/html/http,写程序的时候用root用户,大家都知道iptables 非root用户不能运行。
首先写个c程序,命名为:ipt.c。
#include
#include
#include
#include
int main()
{
uid_t uid ,euid;
char cmd[1024];
uid = getuid() ;
euid = geteuid();
printf(my uid :%u/n,getuid()); //这里显示的是当前的uid 可以注释掉.
printf(my euid :%u/n,geteuid()); //这里显示的是当前的euid
if(setreuid(euid, uid)) //交换这两个id
perror(setreuid);
printf(after setreuid uid :%u/n,getuid());
printf(afer sertreuid euid :%u/n,geteuid());
system(/sbin/iptables -l); //执行iptables -l命令
return 0;
}
编译该文件:
gcc -o ipt -wall ipt.c
在该路径下生成ipt,这个可执行文件。如果现在用php网页调用 该ipt的话,即使setreuid了 也是不行的。
接下来要做的是:
chmod u+s ./ipt
ls
-rwsr-xr-x 1 root root 5382 jul 2 21:45 ipt
好了,已经设置上了,再写一个php页面调用它。
在浏览器中浏览。
[color=red]chain input (policy accept)
target prot opt source destination
chain forward (policy drop)
target prot opt source destination
accept all -- anywhere anywhere
state related,established
chain output (policy accept)
target prot opt source destination [/color]
[color=blue]my uid :48
my euid :0
after setreuid uid :0
afer sertreuid euid :48[/color]
---------------------------------------------------------
last line of the output: afer sertreuid euid :48
---------------------------------------------------------
return value: 0
该命令执行成功。
众 所周知: apache的uid 为48。调用setreuid后将有效用户id和实际用户id互换了。(必须在chmod u+s生效的情况下) 使apache当前的uid为0这样就能执行root命令了。大家只需要更改 c文件中的system所要执行的命令就可以实现自己的php执行root命令了。
linux联盟收集整理
其它类似信息

推荐信息