养浩然之气,活着不是为技术,请关心身边的人.

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

上一篇 / 下一篇  2007-08-23 14:55:32 / 个人分类:linux-C

平台:Linux

实验命令iptables,当前的目录是/var/www/html/http,写程序的时候用root用户,大家都知道iptables 非root用户不能运行。

首先写个C程序,命名为:ipt.c。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

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页面调用它。

<?php
echo '<pre>';

$last_line = system('/var/www/html/http/ipt', $retval);

echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

在浏览器中浏览。

[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命令了。

转载:http://www.xxlinux.com/linux/article/development/web/20070124/7188.html

TAG: linux-C

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

数据统计

  • 访问量: 21792
  • 日志数: 146
  • 建立时间: 2007-08-01
  • 更新时间: 2008-02-20

RSS订阅

Open Toolbar