Home  >  Article  >  php教程  >  Linux系统编程:用PHP执行Root命令

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

WBOY
WBOYOriginal
2016-06-06 19:55:051018browse

在玩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。

<font><ccid_code>#include <stdio.h> <br>#include <stdlib.h> <br>#include <sys> <br>#include <unistd.h> <br><br>int main() <br>{ <br>    uid_t uid ,euid; <br>    char cmd[1024]; <br><br>    uid = getuid() ; <br>    euid = geteuid(); <br><br>    printf("my uid :%u/n",getuid());  //这里显示的是当前的uid 可以注释掉. <br>    printf("my euid :%u/n",geteuid()); //这里显示的是当前的euid <br>    if(setreuid(euid, uid))  //交换这两个id <br>        perror("setreuid"); <br>    printf("after setreuid uid :%u/n",getuid()); <br>    printf("afer sertreuid euid :%u/n",geteuid()); <br><br>    system("/sbin/iptables -L"); //执行iptables -L命令 <br>    return 0; <br>}</unistd.h></sys></stdlib.h></stdio.h></ccid_code></font>

编译该文件:

<font><ccid_code>gcc -o ipt -Wall ipt.c</ccid_code></font>

在该路径下生成ipt,这个可执行文件。如果现在用PHP网页调用 该ipt的话,即使setreuid了 也是不行的。

接下来要做的是:

<font><ccid_code>chmod u+s ./ipt <br><br>ls <br>-rwsr-xr-x  1 root root 5382 Jul  2 21:45 ipt</ccid_code></font>

好了,已经设置上了,再写一个php页面调用它。

<font><ccid_code><?php <br />echo '<pre class="brush:php;toolbar:false">'; <br><br>$last_line = system('/var/www/html/http/ipt', $retval); <br><br>echo ' <br>


Last line of the output: ' . $last_line . '

Return value: ' . $retval;
?>

在浏览器中浏览。

<font><ccid_code>[color=Red]Chain INPUT (policy ACCEPT) <br>target     prot opt source               destination          <br><br>Chain FORWARD (policy DROP) <br>target     prot opt source               destination          <br>ACCEPT     all  --  anywhere             anywhere            <br>state RELATED,ESTABLISHED  <br><br>Chain OUTPUT (policy ACCEPT) <br>target     prot opt source               destination         [/color] <br>[color=Blue]my uid :48 <br>my euid :0 <br>after setreuid uid :0 <br>afer sertreuid euid :48[/color] <br><br><br>---------------------------------------------------------<br>Last line of the output: afer sertreuid euid :48  <br>---------------------------------------------------------<br>Return value: 0</ccid_code></font>

该命令执行成功。

众 所周知: apache的uid 为48。调用setreuid后将有效用户id和实际用户id互换了。(必须在chmod u+s生效的情况下) 使apache当前的uid为0这样就能执行root命令了。大家只需要更改 C文件中的system所要执行的命令就可以实现自己的PHP执行root命令了。

Linux联盟收集整理 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn