Execute the root command in PHP _ PHP Tutorial

Source: Internet
Author: User
Run the root command in PHP. I learned PHP for a period of time before C. When should I use PHP to run the root command? it never worked until I found the super plug-in one day. there are more days to learn C. it is found that PHP that has been learned for a period of time before learning C should be used to run the root command. it has never been unsuccessful until the super plug-in is found one day.

As I learned C more days, I found that the external commands to be run can be wrapped in C. I tried it and succeeded.

You can use PHP to execute the root command without any external tools.

Platform: Linux. the current directory of the experiment command iptables is/var/www/html/http
Use the root user to write the program.

We all know that iptables cannot be run by non-root users.

First write a C program

The name is

[CODE]

# Include
# Include
# Include
# Include

Int main ()
{
Uid_t uid, euid;

Uid = getuid ();
Euid = geteuid ();

Printf ("my uid: % u", getuid (); // The current uid can be commented out.
Printf ("my euid: % u", geteuid (); // The current euid is displayed here
If (setreuid (euid, uid) // exchange the two IDs
Perror ("setreuid ");
Printf ("after setreuid uid: % u", getuid ());
Printf ("afer sertreuid euid: % u", geteuid ());

System ("/sbin/iptables-L"); // execute the iptables-L command
Return 0;
}
[/CODE]


Compile the gcc-o ipt-wall EPT. c file.

Generate the file that runs the file.

If you use a PHP web page to call this, it will not work even if setreuid is used.

Next, we need to do chmod u + s./EPT.

Ls
-Rwsr-xr-x 1 root 5382 Jul 2 EPT

S bit is set.

Write another php page to call it.

[CODE]
Echo
; 

$last_line = system(/var/www/html/http/ipt, $retval);

echo

Last line of the output:. $ last_line.
Return value:. $ retval;
?>
[/CODE]

Browse in the browser.


[Color = Red] Chain INPUT (policy ACCEPT)
Target prot opt source destination

Chain FORWARD (policy DROP)
Target prot opt source destination
ACCEPT all -- 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


The command is successfully executed ..

As we all know, the uid of apache is 48. after setreuid is called, the valid user ID and the actual user ID are exchanged. (the chmod u + s must take effect) make apache's current uid 0 so that the root command can be executed.

Sorry. as I learned C more days, I found it...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.