Linux系统编程:用PHP执行Root命令
在玩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> Copy after login |
编译该文件:
<font><ccid_code>gcc -o ipt -Wall ipt.c</ccid_code></font> Copy after login |
在该路径下生成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> Copy after login |
好了,已经设置上了,再写一个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> Copy after login 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> Copy after login |
该命令执行成功。
众 所周知: apache的uid 为48。调用setreuid后将有效用户id和实际用户id互换了。(必须在chmod u+s生效的情况下) 使apache当前的uid为0这样就能执行root命令了。大家只需要更改 C文件中的system所要执行的命令就可以实现自己的PHP执行root命令了。
Linux联盟收集整理
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Using python in Linux terminal...

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

To open a web.xml file, you can use the following methods: Use a text editor (such as Notepad or TextEdit) to edit commands using an integrated development environment (such as Eclipse or NetBeans) (Windows: notepad web.xml; Mac/Linux: open -a TextEdit web.xml)
