PHP executes the root command-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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
# Include
# Include
# Include
# Include
Int main ()
{
Uid_t uid, euid;
Char cmd [1024];
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;
}
[/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.
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.
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
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 ..
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.