让PHP以ROOT权限执行系统命令的方法


Posted in PHP onFebruary 10, 2011

用来作为解决php以root权限执行一些普通用户不能执行的命令或应用的参考。
其实php里的popen()函数是可以解决这个问题的,但是由于某些版本的linux(如我使用的Centos 5)对系统安全的考虑,
使得这个问题解决起来麻烦了好多。先来看一个网友使用popen()函数的例子。

/* PHP中如何增加一个系统用户 
下面是一段例程,增加一个名字为james的用户, 
root密码是 louis。仅供参考 
*/ 
$sucommand = "su root --command"; 
$useradd = "/scripts/demo/runscripts.php"; 
$rootpasswd = "louis"; 
$user = "james"; 
$user_add = sprintf("%s %s",$sucommand,$useradd); 
$fp = @popen($user_add,"w"); 
@fputs($fp,$rootpasswd); 
@pclose($fp);

经过自己的测试,证实此段代码是不能实现(至少在我的系统里是这样的)作者想要获得的结果的。经过自己很长时间的google之后,
问题的关键是su root这个命令需要的密码必须以终端的方式输入,不能通过其它的方式(我也不知道还有没有其它的方式)获得。
又由于项目要求不能使用类似于sudo这种应用,无奈之下,我选择了网友提出的用编写C程序的方法来解决此问题。
首先写个C程序,命名为:run.c 放在目录/scripts/demo/下
#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("/scripts/demo/runscripts.php"); //执行脚本 
return 0; 
}

编译该文件:
gcc -o run -Wall run.c
在该路径下生成run文件,这个可执行文件。如果现在用PHP脚本调用 该run的话,即使setreuid了 也是不行的。
接下来要做的是:给run赋予suid权限
# chmod u+s run
# ls
# -rwsr-xr-x 1 root root 5382 Jul 2 21:45 run
好了,已经设置上了,再写一个php页面调用它。
<?php 
echo '<pre>'; 
$last_line = system('/scripts/demo/run', $retval); 
echo ' 
</pre> 
<hr />Last line of the output: ' . $last_line . ' 
<hr />Return value: ' . $retval; 
?>

在浏览器中浏览。
my uid :48
my euid :0
after setreuid uid :0
afer sertreuid euid :48

--------------------------------------------------------------------------------
Last line of the output: afer sertreuid euid :48
--------------------------------------------------------------------------------
Return value: 0
该命令执行成功。
从显示结果可以看出: apache(daemon)的uid 为48(事实上很多linux系统下daemon的uid为2)。
调用setreuid后将有效用户id和实际用户id互换了。(必须在chmod u+s生效的情况下) 使apache当前的uid为0这样就能执行root命令了。
只需要更改 C文件中的system所要执行的命令就可以实现自己的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 <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 
int main() 
{ 
uid_t uid ,euid; 
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
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
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命令了.

PHP 相关文章推荐
在任意字符集下正常显示网页的方法一
Apr 01 PHP
PHP 模拟$_PUT实现代码
Mar 15 PHP
深入理解php的MySQL连接类
Jun 07 PHP
Codeigniter注册登录代码示例
Jun 12 PHP
ThinkPHP3.1新特性之内容解析输出详解
Jun 19 PHP
Nginx下配置codeigniter框架方法
Apr 07 PHP
PHP使用Mysqli类库实现完美分页效果的方法
Apr 07 PHP
php is_executable判断给定文件名是否可执行实例
Sep 26 PHP
PHP实现webshell扫描文件木马的方法
Jul 31 PHP
PDO::errorInfo讲解
Jan 28 PHP
PHP配置ZendOpcache插件加速
Feb 14 PHP
php5对象复制、clone、浅复制与深复制实例详解
Aug 14 PHP
PHP开发中常用的字符串操作函数
Feb 08 #PHP
php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
Feb 08 #PHP
php 数组的指针操作实现代码
Feb 08 #PHP
PHP游戏编程25个脚本代码
Feb 08 #PHP
PHP通用检测函数集合
Feb 08 #PHP
.htaccess文件保护实例讲解
Feb 06 #PHP
延长phpmyadmin登录时间的方法
Feb 06 #PHP
You might like
PHP 输出缓存详解
2009/06/20 PHP
浅析ThinkPHP中的pathinfo模式和URL重写
2014/01/06 PHP
php实现httpRequest的方法
2015/03/13 PHP
JS类库Bindows1.3中的内存释放方式分析
2007/03/08 Javascript
几个有趣的Javascript Hack
2010/07/24 Javascript
ASP.NET jQuery 实例13 原创jQuery文本框字符限制插件-TextArea Counter
2012/02/03 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
动态加载dtree.js树treeview(示例代码)
2013/12/17 Javascript
js、jquery图片动画、动态切换示例代码
2014/06/03 Javascript
jquery中ready()函数执行的时机和window的load事件比较
2015/06/22 Javascript
javaScript数组迭代方法详解
2016/04/14 Javascript
js计算时间差代码【包括计算,天,时,分,秒】
2016/04/26 Javascript
Jquery实现select multiple左右添加和删除功能的简单实例
2016/05/26 Javascript
第四篇Bootstrap网格系统偏移列和嵌套列
2016/06/21 Javascript
nodeJs实现基于连接池连接mysql的方法示例
2018/02/10 NodeJs
微信小程序商品详情页的底部弹出框效果
2020/11/16 Javascript
vue动态绑定class选中当前列表变色的方法示例
2018/12/19 Javascript
vue根据条件不同显示不同按钮的操作
2020/08/04 Javascript
VUE 实现element upload上传图片到阿里云
2020/08/12 Javascript
[04:44]显微镜下的DOTA2第二期——你所没有注意到的细节
2014/06/20 DOTA
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
2014/06/18 Python
python中threading超线程用法实例分析
2015/05/16 Python
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
深入讲解Java编程中类的生命周期
2016/02/05 Python
numpy.ndarray 交换多维数组(矩阵)的行/列方法
2018/08/02 Python
python 切换root 执行命令的方法
2019/01/19 Python
Keras预训练的ImageNet模型实现分类操作
2020/07/07 Python
使用phonegap进行提示操作的具体方法
2017/03/30 HTML / CSS
Opodo意大利:欧洲市场上领先的在线旅行社
2019/10/24 全球购物
工程造价与财务管理专业应届生求职信
2013/10/06 职场文书
后勤采购员岗位职责
2013/12/19 职场文书
大学生会计职业生涯规划范文
2014/02/28 职场文书
遗体告别仪式主持词
2014/03/20 职场文书
工伤事故赔偿协议书
2014/04/15 职场文书
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
2021/06/26 MySQL
python unittest单元测试的步骤分析
2021/08/02 Python