0

I'm looking for a clean and secure way to run specific Linux system commands from a web interface.

Let's say I would like to sudo -u differentuser ping 0.0.0.0. The IP address is supplied by user in a HTTP request and I'd like to print the result on a web page. What would be the best way to do this?

I was thinking about using PHP's exec() command :

<?php
echo exec('sudo -u differentuser ping ' . $_POST['ip']);

But this seems very dirty and unsecure and I'd have to add the apache user to the sudoers file.

What language would you use and how would you achieve this?

Thanks for your help.

Edit : the sudo is important here, because I need to run those commands as a specific user different from the one used to host the web interface.

user181932
  • 9
  • 1
  • 3
  • 4
    I wouldn't. That is a security nightmare right there. If I *did* need to run a very specific set of commands, I would have the commands themselves hardcoded, and I certainly would not pass the parameters straight through without checking them first. (Also, I would never use PHP since that language itself contains enough security holes to sail a moderately large container ship through.) – Jenny D Jul 17 '13 at 09:14

2 Answers2

1

Usualy is not safe. It doesn't matter if you let users send commands or any other kind of interactivity. Even if your script runs alone, exploits can be invented to make use of it in one form or another and maybe alter it's actions.

But, this applies only if you want to have insane security rules on your server. In real world, the chance is minimal that you can compromise your server security.

I still have some suggestions for you :

make sure yo

  1. Make sure your script does not accept any input from outside, it does not read a database or a file. Everything must be enclosed inside the script.

  2. Try to put the script somewhere outside the documentRoot so it won't be accesible by users.

  3. Put some special permissions on the script so that it's actions are limited to the user it runs as. Even if someone breaks it somehow, the OS will not let him do something else than running just that particular command in a particular environment.

This of course may be completed with more rules, but this is just what comes in mind now.

-1

Put your commands in a shell script and do something like this:

chown root:wheel ping.sh
chmod +xs ping.sh

And then run that script in your php script by exec()

This should work :-)

Lingfeng Xiong
  • 208
  • 4
  • 10