PHP Run command as sudo without changing any Apache settings

Here is how to run a php script as sudo without changing apache settings.

Save the following snippet as receiver.sh. This will act as the receiving script to run commands with sudo permissions.

#!/bin/bash

while :; do
    command=$(nc -l 1234)

    # "[Sun Apr 21 14:46:34 PDT 2013] ":command""
    echo "["$(date)"] \"${command}\""

    # Run command received with sudo permissions
    sudo -i "${command}"

    echo -e "\nreturn code: ${?}"
done

Run this script with sudo and keep the script running:

$ sudo bash receiver.sh

Now send a command to be run. Here are two examples to issue commands:

Inside a php script

<?php
$data = 'whoami'; // Command to send
$fp = stream_socket_client('tcp://127.0.0.1:1234');
fwrite($fp, $data);
fclose($fp);

On the command line

$ echo "whoami" >/dev/tcp/localhost/1234

Example response:

$ sudo bash receiver.sh 
[Sun Apr 21 16:16:54 PDT 2013] "whoami"
root

return code: 0
View this page on GitHub.
Posted .

Comments

Leave a Reply