Executing root commands with PHP ... Is there a safe way?

I was thinking of creating a small Linux web control panel (just for fun).

The first potential problem that occurred to me was that I would need to grant users access privileges at the Apache user level to execute these commands, which would compromise the security of the entire system.

Installing a dedicated web server for the system is not really an option.

Perhaps I can start the second instance of Apache (keeping the 1st number for regular users), but not even sure if this is possible.

So what do you guys think? What are my best options?

Thanks in advance for any input.

EDIT: Ok guys, thanks for all the suggestions, I will remember them.

+8
php command root
source share
3 answers

Write specific scripts that can be run as root, and use the setuid bit to do this so that Apache can run only these scripts with root privileges. IE

#! /usr/bin/php (or wherever your php binary is) <?php // Some PHP code that takes in very limited user input, // validates it carefully, and does a sequence of actions // that you need to run as root. // This could easily be a shell script (or whatever else you want) // instead of PHP. That might be preferable in many situations. ?> 

Then make sure that this script belongs to your root and group'd user, which Apache works like:

 chown root:www-data myscript.php 

Then run it as the owner:

 chmod u+s myscript.php 

And make sure Apache can execute it:

 chmod g+x myscript.php 
+5
source share

Running root commands through a web server seems like a crazy idea to me, but anyway.

You can use sudo to not run any unwanted commands.

A small example taken from here is sudo config:

 peter, %operator ALL= /sbin/, /usr/sbin, /usr/local/apps/check.pl 

And in php:

 exec( 'sudo /usr/local/apps/check.pl ...'); 

Be sure to avoid all arguments correctly, etc.

Or you could build the db table as follows:

 commands ( action, serialized_parameters. result, return_code ) 

Use php to insert commands into this table, and another script that will be run in cron by different users. You will not have real-time results (but you can have them for 30 seconds), but the apache user will not be able to use any command directly (of course, you can easily limit actions when reading records).

+3
source share

I recently published a project that allows PHP to get and interact with a real Bash shell (by root request), it solves the limitations of exec () and shell_exec (). Get it here: https://github.com/merlinthemagic/MTS

After downloading, you simply use the following code:

 $shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true); $return1 = $shell->exeCmd('yourFirstCommand'); //the return will be a string containing the return of the command echo $return1; 

From a security point of view, this is much better than running apache as root. But letting PHP somewhere near the root is always difficult.

The project I created reaches the Bash root shell in one of two ways:

1) You allow apache sudo python right.

OR

2) You pass the root credentials to the object every time you need a shell with a root installation.

Choose your poison. :) Read the documentation.

+1
source share

All Articles