1

Hey, I'm trying to use PHP to execute a shell command which will run remotely run a server on my box. Here is my PHP Code:

if ($key == "test") { echo "<font color='green'>Key is valid. Server satrted.</font>";
   $start = system('cd /root/st/; ls;');
}

The problem is, the ls command runs from the same directory as the web server, which returns all of the files from /var/www/html instead of /root/st/. I have also tried the chdir command to no avail. Anyone know how you would get the directory to change so that the command could be run from a specified directory? Thanks.

3
  • 1
    What are the permissions on that directory, /root/sl ? Commented Mar 11, 2011 at 19:35
  • why not system('ls /root/st/'); ? Commented Mar 11, 2011 at 19:36
  • ls was just an example, I'm trying to execute an actual file (./zserv -IWAD DOOM2.WAD -port 13051) Commented Mar 11, 2011 at 19:42

5 Answers 5

2

Does the user that PHP is running as (eg, the user invoking the CLI script) have permission to read the directory? If you're going into /root/ but aren't root, you'd need to either add cd to sudoers for the current user, or choose another directory.

Edit: note that adding cd to sudoers is not even remotely okay for anything other than a local, you-only script. :)

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

4 Comments

Do you know any workarounds in that case? Why would using cd not be okay?
@Ivan, I assume you've already been lectured on how utterly awful of an idea a PHP script running as root is?
Yes I know, haha. This is just a test and nobody else will see it for the moment.
No, using the cd command itself is fine - adding it to sudoers so that other users could execute it as root would not be. :) Also, since this is running inside a web server, are you having basedir restrictions?
1

There are two ways I would approach this.

1: use proper unix commands, and see if they work. IE:

if ($key == "test") { echo "<font color='green'>Key is valid. Server satrted.</font>";
    $start = system('ls /root/st/');
}

2: Make it run a script on the system, that can go outside the webserver's chroot.

if ($key == "test") { echo "<font color='green'>Key is valid. Server satrted.</font>";
    $start = system('server.sh');
}

and server.sh is

#!/bin/bash
cd /root/st
ls

2 Comments

Couldn't users access the bash script in that case?
Yes, they could, if you don't properly secure the system. But, if they are able to modify that script, your box is probably owned anyways.
0

PHP has a chdir() function. Not sure if it applies to exec/system calls, but worth a try.

Comments

0

Store the location in a variable, say $loc = '/root/st' and then do ls $loc in your code. Hope this helps.

Comments

0

Your problem is not the directory the script is running in (or more precisely the current working directory of the user running the script), but that cd /root/st/ will fail on any reasonable configured UNIX/Linux system. /root is usually owned by root and can't be accessed by any other user.

Using you snippet this will silently fail because you unconditionally chained the cd and the ls commands with a semicolon instead of &&.

2 Comments

What if apache is run off of the root user?
It usually isn't. You have to explicitly set the User directive in your httpd.conf to use that user and some distributions prevent running Apache httpd with root privileges at all.

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.