0

I have downloaded a library from GitHub to run shell commands from within a PHP script. It works like this:

    $process = new Process("su - {$login}"); 
    $process->put($password);
    $process->put("\n");
    $process->put($command);
    $process->put("\n");
    $data = $process->get();
    $process->put("exit"); 
    $process->put("\n");

It is based on proc_open(). I use it on my localhost with my Linux login and password and everything works fine. I want to use this library on a web server. I wanna try 'www-data' as login but have no idea what to pass as www-data's password. What to use as login to allow users use this library on real web server, not localhost?

(I posted this to stackoverflow but they downvoted it adding that it is not about programming and more about server fault)

  • 1
    You may want to start by looking at your /etc/passwd and /etc/shadow for your www-data. Your /etc/passwd will tell you what the default shell for the user is and /etc/shadow will give you the encrypted version of it's password (provided it has one).. – ryekayo Sep 11 '17 at 12:49
  • Is it a normal practice to use www-data with password? – Bruno Valejo Sep 11 '17 at 12:51
  • Not that i know of, the www-data is usually used for running Apache.. What are you trying to do with www-data exactly? – ryekayo Sep 11 '17 at 12:51
  • I want to provide users access to a 'calc' program on Linux server thru website. The library that I use to run calc interactively requires to be passed some login – Bruno Valejo Sep 11 '17 at 12:53
  • Hmmmm ok.. this is a suggestion, you might be able to use caching to help ease the pain of constantly relogging in rather than using www-data – ryekayo Sep 11 '17 at 12:55
2

First of all it is actually a bad idea to run system commands from web-server PHP script and even store your password in a php script with a plaintext. Imagine you are getting some user input to pass it to $command, now if you miss some checks there you will allow a malicious user to craft user parameters to run any arbitrary command on your system.

Next why you can't actually do this is most likely because out of 2 things:

  1. Your www-data user most likely doesn't has any password set. This disallows logging in with su and that user.

  2. Your www-data user shell are most likely set to something like /sbin/nologin which makes sense as this user aren't supposed to log into the system as real user.

But you actually don't need to change your user to run a system program. Besides most likely your distro has that www-data user to run web server under it (using system means) and every PHP script you run on your webserver are already running under that user privileges. Just run your command without doing su and that should work. Something like this:

$process = new Process($command); 
$data = $process->get();

If that doesn't works, refer to that library manual on how to spawn a shell without su, probably $process = new Process('/bin/bash') will work too.

| improve this answer | |
  • Dear NStorm, you are a genius!!!! The last paragraph about spawning a shell is sth I will never forget!!! I have just tested it!! – Bruno Valejo Sep 11 '17 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.