Home » Php » security – How to run PHP exec() as root?

security – How to run PHP exec() as root?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to build a firewall manager in PHP, but when I execute, <?php exec('iptables -L'); ?>, the result array is empty.

I have tried, <?php echo exec('whoami'); ?>, and the response is www-data (the user that Apache is using). What can I do to execute the exec function as root? (Preferably without changing the Apache user.)

How to&Answers:

Don’t do it! You will leave yourself wide open to all sorts of malicious hackery.

Have a look at the “sudo” documentation.

You should be able to set up all the commands you need as “sudo”able scripts.
It is much better to write specific scripts with limited functions than to expose
the underlying priviledged command.

As in:

exec ('sudo getIpTables.ksh')

Answer:

You can run sudo through phpseclib, a pure PHP SSH implementation:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("sudo command\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>

Answer:

I know this is an old question

add the user php runs on to the sudo group if it is not already assigned

use sudo -S, so you can pass the password via echo

$exec = "echo your_passwd | /usr/bin/sudo -S your command";
exec($exec,$out,$rcode);

if you have trouble with the paths – use

"bash -lc 'echo your_passwd | /usr/bin/sudo -S your command'"

so you get a new bash that acts like a login shell and has the paths set

check the man pages of sudo

Answer:

This is very unsafe and a bad idea. Rethink your design. If you really want to do this use sudo as advised. An alternative solution might be to go ahead and run as root but do so inside a chroot or a vm image (both of which can be broken out of but still).

Or best of all run as sudo inside a chroot!

Answer:

Unless you use suphp and configure it to run as root you wont be able to run any PHP script on behalf of any other system user besides who is running PHP.

Edit:

Just an small idea. Add a queue process in some way and run a cron process in the root’s crontab.

Please please be really careful about this. Any injection can literally destroy the system.

Answer:

You can put the required commands in a separate script/executable file (sh, PHP, a real executable, doesn’t matter), change its owner to root, and apply “setuid” to it.

This will allow anything and anyone to run this script as root, so you need to make sure that it has it’s own security rules for seeing if this is allowed, and is very restricted in what it does.

Answer:

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

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

Answer:

Just an assumption – Is this a PHP web app that will do this? This doesn’t sound too safe. The app that needs root – could you build that separately and then invoke it from PHP? If not, maybe you can sudo the process so it can run as root.