2

When I use the following php script:

<?php
    echo 'I am '.exec('whoami').' user';
?>

on my localhost, the result is I am nobody user and is what I expect.

But when I use:

<?php
    echo 'I am '.exec('echo <PASSWD> | sudo -S -u <USER> whoami').' user';
?>

where <PASSWD> is my password and <USER> is my user name, the result is I am user and I expect to be I am <USER> user. How can I make it to work as I wish?


As a note, when I use echo 'I am '.exec('echo <PASSWD> | sudo -S -u <USER> whoami').' user'; inside php interpreter (php -a), everything is fine.

2 Answers 2

1

The user that is being used is the one running php/httpd process on the server. Depending on your server setup this could be Apache, root, or in FastCGI setups it can be any user or no user. This is a fallacy since there must always be a user. Typically shared hosting servers will try to circumvent allowing users to execute functions as a sudo user. This may be what you are experiencing.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, this I experiecing. But my question is how can I make it to work (if there is a way)... When I use php interpreter is working.
Unfortunately, in this context, whoami will always return the user that is running the service, regardless of the username you are logged in with. The reason is because you are not running the script from a shell like you typically would, you are running the script from the server. The server is always the same user, so it will always be that. You could try using something like PHP's FTP functions or PHPSECLIB to connect from your script and print the user/pass.
0

A straight answer is you may need to go via shell to use the pipes etc

  • shell_exec..

For large scale applications, creating all those small processes is quite expensive.

IMO, a better solution is using these ::

  • pcntl_fork
  • posix_seteuid
  • posix_setegid
  • exec
  • Optional proc_nice

The calls to sudo are technically running with root permission. So this is the same, in a more visible fashion..

I'm sure you don't need me to tell you that putting those passwords in your source is a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.