Executing the root command in PHP _php tutorial

Source: Internet
Author: User
Learn c before learning a period of PHP, which need to use PHP to run the root command, has not been the result, until one day to search for super this plugin.

As the days of learning C are much more. Find that you can use the C language to wrap external commands to run. Experiment a bit. Success.

You do not need any external tools to implement the root command in PHP.

Platform: Linux. Experimental command iptables The current directory is/var/www/html/http
Use the root user when writing programs

Everyone knows that iptables non-root users cannot run.

First, write a C program.

Named as: ipt.c

[CODE]

#include
#include
#include
#include

int main ()
{
uid_t uid, euid;

UID = Getuid ();
Euid = Geteuid ();

printf ("My UID:%u", getuid ()); This shows that the current UID can be commented out.
printf ("My euid:%u", geteuid ()); This shows the current euid.
if (Setreuid (Euid, UID))//Exchange these two IDs
Perror ("Setreuid");
printf ("After Setreuid uid:%u", getuid ());
printf ("Afer sertreuid Euid:%u", geteuid ());

System ("/sbin/iptables-l"); Execute iptables-l command
return 0;
}
[/code]


Compile the file Gcc-o ipt-wall ipt.c

Generate the IPT executable file under this path.

If the IPT is now invoked on a PHP page, it will not work even if it is setreuid.

The next thing to do is chmod u+s./ipt

LS a bit
-rwsr-xr-x 1 root root 5382 Jul 2 21:45 IPT

The s bit is already set up.

Then write a PHP page to invoke it.

[CODE]
Echo




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 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 executed successfully:

It is well known that Apache has a UID of 48. After calling Setreuid, the valid user ID is swapped with the actual user ID. (Must be in the case of chmod u+s) The current UID of Apache is 0 so that the root command can be executed.

http://www.bkjia.com/PHPjc/486042.html www.bkjia.com true http://www.bkjia.com/PHPjc/486042.html techarticle before learning C for a period of time in PHP, which need to use PHP to run the root command, has not been the result, until one day to search for super this plug-in. As the days of learning C are much more. Discover ...

  • 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.