博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下php调用root权限实现方案
阅读量:6907 次
发布时间:2019-06-27

本文共 1039 字,大约阅读时间需要 3 分钟。

最近小二哥要在PHP上执行shell命令,例如:

<?php

  exec('/sbin/service sshd restart');

?>

然后发现没有执行权限。

通过二进制包装器(binary wrapper)来实现

1)新建一个希望以root权限运行的sh脚本

# cat > php_shell.sh <

2)更改此文件的权限,确保root用户可以读写

# chown root php_shell.sh# chmod u=rwx,go=xr php_shell.sh

3) 构建二进制包装器来运行我们的脚本

# cat > wrapper.c <
#include
#include
int main (int argc, char *argv[]) { setuid (0); /* WARNING: Only use an absolute path to the script to execute, * a malicious user might fool the binary and execute * arbitary commands if not. * */ system ("/bin/sh /path/to/php_shell.sh"); return 0; }CONTENT

4) 编译并设置合适的权限(使其可以root权限运行)

# gcc wrapper.c -o php_root# chown root php_root# chmod u=rwx,go=xr,+s php_root

php_root现在可以root权限运行在php_root.sh中定义的命令了


假如这些命令不需要被经常修改,为了安全,我推荐直接在wrapper.c中写这些命令,直接使用system ("your shell command here");来定义你希望执行的命令

 

参考文章:

https://stackoverflow.com/questions/8532304/execute-root-commands-via-php

转载于:https://www.cnblogs.com/boats/p/7777110.html

你可能感兴趣的文章
从贝叶斯定理到概率分布:综述概率论基本定义
查看>>
Satoshis Vision大会:‘乱局’之中的Bitcoin Cash
查看>>
前端中的 IoC 理念
查看>>
Android开源框架源码鉴赏:VirtualAPK
查看>>
在 V8 引擎中设置原型(prototypes)
查看>>
源码|并发一枝花之ReentrantLock与AQS(2):lockInterruptibly
查看>>
Lumen 使用 throttle 限制接口访问频率
查看>>
怎样给文件命名才能显得更加专业
查看>>
python多线程
查看>>
原来云数据库也是有思想的...
查看>>
GitHub 项目徽章的添加和设置
查看>>
写给前端新人:前端开发必会的知识点
查看>>
欢乐的票圈重构之旅——RecyclerView的头尾布局增加
查看>>
makefile-4--变量的定义与使用
查看>>
浅析Vue源码(七)——render到VNode的生成
查看>>
谈谈Shiro的原理及在SSM和SpringBoot两种环境下的使用姿势(下篇)
查看>>
Xcode 创建自定义模板
查看>>
webpack入门学习手记(四)
查看>>
多迪技术总监告诉你:程序员怎么跟非程序员解释编程呢?
查看>>
java语言适合用来做什么?
查看>>