欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php以root权限执行shell命令 有大用 有大大用

学习笔记


日志里出现:not in sudoers,说明php执行存在权限问题。


查看Apache用户(root用户下)


lsof -i:80

1

执行命令


sudo visudo

1

在该界面做如下修改(添加红线及蓝色框内容,www-data是我的执行用户)


重启Apache


sudo service apache2 restart

————————————————

版权声明:本文为CSDN博主「冰枫随寒叶cium」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_45484297/article/details/108906601


学习笔记

日志里出现:not in sudoers,说明php执行存在权限问题。

查看Apache用户(root用户下)

lsof -i:80

sudo visudo

在该界面做如下修改(添加红线及蓝色框内容,www-data是我的执行用户)
在这里插入图片描述
重启Apache

sudo service apache2 restart

来自  https://blog.csdn.net/weixin_45484297/article/details/108906601



今天晚上:19:30分,这个困扰了我好久的问题终于解决,其中的原理也终于弄清楚了,总之是利用sudo来赋予Apache的用户root的执行权限,下面记录一下:

利用php利用root权限执行shell脚本必须进行以下几个步骤:(所有步骤都是我亲自实验,若有不妥可指出,谢谢!)

1. 确定一下你的Apache的执行用户是谁。注:不一定就是nobody,我自行安装的httpd,我的Apache的用户就是daemon

2. 利用visudo为你的Apache执行用户赋予root执行权限,当然还有设置无密码。注:为了安全起见,这里最好是新建一个用户,让他作为Apache的执行用户即可(修改httpd.conf文件,后面我会指出)

3. 这步就简单了,编写你的脚本,利用php的exec,system...函数来执行。

接下来就是详细的实现过程:

1. 查看一下你的Apache的执行用户是谁: lsof -i:80         运行之后的结果为:

   

从图中我们可以清楚的看到,httpd(也就是Apache)的执行用户为:exec_shell(注:这是我本机上改过之后的用户,只是用来说明一下,你的肯定不是这个!)  

lsof 就是 List of file 的缩写,就是列出当前系统打开文件的工具,关于他具体的使用方法可参考:http://club.topsage.com/thread-234763-1-1.html   说的比较不错

确定了你的Linux上Apache的执行者是谁,下面为了安全起见,新建一个用户将Apache的执行用户修改为我们新建的用户。

2. 新建Apache的执行用户

    useradd your_exec_user  我们知道创建用户的时候都会默认创建一个用用户名同样的用户组,也就是说现在我们也有一个your_exec_user的用户组

    下面我们修改一下Apache的配置文件,使它的执行用户改为我们刚才新建的这个用户your_exec_user :

     vi  /home/houqingdong/httpd-exe/config/httpd.conf(这个是你的Apache所在的目录位置)

    找到下面的地方,修改为你新建的用户:your_exec_user

    

   重新启动Apache:   /home/houqingdong/httpd-exe/bin/apachect1  restart              -------------> 重启完之后你可以利用:lsof -i:80 查看一下。

3. 执行visudo(或者是 vi /etc/sudoers) , 为your_exec_user赋予root权限,并且不需要密码,还有一步重要的修改(我被困扰的就是这个地方)

    visudo    找到这个地方,添加your_exec_user,并且设置无需密码

    

   我之前的时候,做完这里就去执行php脚本去了,结果一直创建不成功,而且很郁闷的是我切换到your_exec_user用户下直接执行是可以执行成功的。

   后来,查看了一下Apache的日志文件,发现:   

      这里明显看出,在执行sudo的时候说必须要有一个tty去运行sudo , 知道问题出在哪里问题就好解决了: vi /etc/sudoers   将下面的这句注释掉:      

      这是因为默认的情况下,执行sudo需要一个终端,这里注释掉就可以了。接下来,写你的shell脚本和php命令吧

4. 这里贴一下我写的很简单的一个脚本,就是利用在php端传来的$directory和$name,在该目录下创建一个$name的目录

   

[plain]  view plain copy

Flash Player has been removed from browsers in 2021.

This content is not yet supported by the Ruffle emulator and will likely not run as intended.

More info

Flash Player has been removed from browsers in 2021.

This content is not yet supported by the Ruffle emulator and will likely not run as intended.

More info
  1. #!/bin/bash  

  2. #Program  

  3. #     This program will execute mkdir: cd $directory ; mkdir $name  

  4. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin  

  5. export PATH  

  6. cd $1  

  7. if [ ! -d $2 ]; then  

  8.     mkdir $2  

  9. else  

  10.     echo "Already exist..."  

  11.     exit 1  

  12. fi  


功能很简单,就是进入到$directory   判断要创建的目录名是否存在,  然后创建该目录 。

构造的php执行函数:(部分)

[php]  view plain copy

Flash Player has been removed from browsers in 2021.

This content is not yet supported by the Ruffle emulator and will likely not run as intended.

More info

Flash Player has been removed from browsers in 2021.

This content is not yet supported by the Ruffle emulator and will likely not run as intended.

More info
  1. if($type=="dir"){  

  2.           $make_dir_command="/usr/bin/sudo /home/houqingdong/myshell/mkdir.sh /$directory/ $name" ;  

  3.           echo $make_dir_command;  

  4.           exec($make_dir_command,$output,$return);  

  5.   

  6.               if($return == 0){  

  7.                   echo "<script>alert('Build directory seccuss!');location.href='right.php?id=\"$directory\"';</script>";  

  8.               }else{  

  9.                   echo "<script>alert('Build directory err!');history.go(-1);</script>";  

  10.               }  

这里顺带提一句:构造的命令里面最好都使用绝对路径

5. 在网页端的执行结果:

    

提交之后,要过几秒中才会弹出执行结果的提示信息:

     执行成功,在我们的/home/目录下:

哈哈。。。大功告成!


来自  https://blog.csdn.net/lxwxiao/article/details/8513355


普通分类: