网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > PHP > PHP技术进阶 > linux编程:PHP执行root命令  

linux编程:PHP执行root命令

2006-07-19  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

    在玩C 以前 玩过一段时间的PHP, 哪个时候需要用PHP 来运行root命令,一直未果,直到有一天搜索到了super这个插件. 

   随着玩C的日子多了.发现可以用C语言来包裹 要运行的外部命令. 实验了一下.成功了.

   不需要任何外部工具就可以实现用PHP 执行root命令.

   我下面就把方法发布给大家,有需求用php来运行root命令的朋友可以不用发愁了.

   平台: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]; 中国网管论坛bbs.bitsCN.com

    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;
}
[/CODE]


编译该文件 gcc -o ipt -Wall ipt.c

在该路径下生成ipt 这个可执行文件.

如果现在用PHP网页调用 该ipt的话,即使setreuid了 也是不行的. 网管联盟bitsCN@com

接下来要做的是chmod u+s ./ipt

ls 一下
-rwsr-xr-x  1 root root 5382 Jul  2 21:45 ipt

s位已经设置上了.

再写一个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          网管网www_bitscn_com
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 

中国网管联盟bitsCN.com
该命令执行成功..

众所周知: apache的uid 为48. 调用setreuid后 将有效用户id 和实际用户id互换了.(必须在chmod u+s生效的情况下) 使apache当前的 uid为0 这样就能执行root命令了。


大家只需要更改 C文件中的 system所要执行的命令就可以实现自己的PHP执行root命令了.

TAGs   命令   执行   编程   root   uid   PHP   可以   printf   用户   当前      
 上一篇:菜鸟学PHP之Smarty入门(组图)   下一篇:五个常见 PHP 数据库问题
linux编程:PHP执行root命令 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: