How to run system commands with ROOT permission in PHP

Source: Internet
Author: User
I think everyone may have encountered this problem, and there are many solutions provided by netizens. I just summarized the requirements of my system and the solutions provided by netizens as a reference for php to execute commands or applications that common users cannot execute with root permissions.
In fact, the popen () function in php can solve this problem, but because some versions of linux (such as Centos 5 I use) have considerations for system security,
This makes it a lot of trouble to solve the problem. Let's take a look at an example of using the popen () function.

The code is as follows:


/* Add a system user in PHP
The following is a routine. add a user named james,
The root password is louis. For reference only
*/
$ Sucommand = "su root -- command ";
$ Useradd = "/scripts/demo/runscripts. php ";
$ Rootpasswd = "louis ";
$ User = "james ";
$ User_add = sprintf ("% s", $ sucommand, $ useradd );
$ Fp = @ popen ($ user_add, "w ");
@ Fputs ($ fp, $ rootpasswd );
@ Pclose ($ fp );


After self-testing, it is confirmed that this code segment cannot be implemented (at least in my system) and the result the author wants to obtain. After a long time of google,
The key to the problem is that the password required for the su root command must be entered as a terminal and cannot be obtained in other ways (I don't know if there are other methods.
Because the project requirements cannot use applications similar to sudo, I chose the method proposed by netizens to write C programs to solve this problem.
First, write a C program named run. c in the directory/scripts/demo /.

The code is as follows:


# Include
# Include
# Include
# Include
Int main ()
{
Uid_t uid, euid;
// Char cmd [1024]; // The variable is not used currently
Uid = getuid ();
Euid = geteuid ();
Printf ("my uid: % u \ n", getuid (); // The current uid can be commented out.
Printf ("my euid: % u \ n", geteuid (); // The current euid is displayed here
If (setreuid (euid, uid) // exchange the two IDs
Perror ("setreuid ");
Printf ("after setreuid uid: % u \ n", getuid ());
Printf ("afer sertreuid euid: % u \ n", geteuid ());
System ("/scripts/demo/runscripts. php"); // execute the script
Return 0;
}


Compile the file:
Gcc-o run-Wall run. c
Generate the run file in this path, which is an executable file. If you use a PHP script to call this run, it will not work even if setreuid is used.
Next, grant the suid permission to run.
# Chmod u + s run
# Ls
#-Rwsr-xr-x 1 root 5382 Jul 2 21:45 run
Okay, you have already set it up. write another php page to call it.

The code is as follows:


Echo'

'; 
$last_line = system('/scripts/demo/run', $retval);
echo '

Last line of the output: '. $ last_line .'
Return value: '. $ retval;
?>


Browse in a browser.
My uid: 48
My euid: 0
After setreuid uid: 0
Afer sertreuid euid: 48

--------------------------------------------------------------------------------
Last line of the output: afer sertreuid euid: 48
--------------------------------------------------------------------------------
Return value: 0
The command is successfully executed.
The result shows that the uid of apache (daemon) is 48 (in fact, the uid of daemon is 2 in many linux systems ).
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.
You only need to change the command to be executed by the system in the C file to implement your own PHP to execute the command as root.

When PHP was used to run the root command before playing C for a period of time, it never worked until the super plug-in was found one day.
With the increasing days of playing C, 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.
I will release the method to you below. if you need to use php to run the root command, you don't have to worry about it.
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

The code is as follows:


# Include
# Include
# Include
# Include
Int main ()
{
Uid_t uid, euid;
Uid = getuid ();
Euid = geteuid ();
Printf ("my uid: % u \ n", getuid (); // The current uid can be commented out.
Printf ("my euid: % u \ n", geteuid (); // The current euid is displayed here
If (setreuid (euid, uid) // exchange the two IDs
Perror ("setreuid ");
Printf ("after setreuid uid: % u \ n", getuid ());
Printf ("afer sertreuid euid: % u \ n", geteuid ());
System ("/sbin/iptables-L"); // execute the iptables-L command
Return 0;
}



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.

The code is as follows:


Echo'

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

Last line of the output: '. $ last_line .'
Return value: '. $ retval;
?>


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.

You only need to change the system command in the C file to execute the root command in PHP.

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.