在PHP中 执行root 命令_PHP教程
Jul 13, 2016 pm 05:42 PM
在学习C 以前 学过一段时间的PHP, 哪个时候需要用PHP 来运行root命令,一直未果,直到有一天搜索到了super这个插件.
随着学习C的日子多了.发现可以用C语言来包裹 要运行的外部命令. 实验了一下.成功了.
不需要任何外部工具就可以实现用PHP 执行root命令.
平台:Linux. 实验命令iptables 当前的目录是/var/www/html/http
写程序的时候 用root用户
大家都知道iptables 非root用户不能运行.
首先写个C程序
命名为:ipt.c
[CODE]
#include
#include
#include
#include
int main()
{
uid_t uid ,euid;
uid = getuid() ;
euid = geteuid();
printf("my uid :%u
",getuid()); //这里显示的是当前的uid 可以注释掉.
printf("my euid :%u
",geteuid()); //这里显示的是当前的euid
if(setreuid(euid, uid)) //交换这两个id
perror("setreuid");
printf("after setreuid uid :%u
",getuid());
printf("afer sertreuid euid :%u
",geteuid());
system("/sbin/iptables -L"); //执行iptables -L命令
return 0;
}
[/CODE]
编译该文件 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页面调用它.
[CODE]
echo
1 2 3 4 5 |
|
Last line of the output: . $last_line .
Return value: . $retval;
?>
[/CODE]
在浏览器中浏览.
[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命令了。

인기 기사

인기 기사

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법
