I’m investigating my hacked website under xampp environment. I put my website files in c:\xampp\htdocs\domainhostseotool\, and visit the site as localhost/domainhostseotool. I’m little worried about a possible security risk. Suppose the compromised website has an infected php file which contains the following code:
1 2 |
$pwd=file_get_contents("C:\\password.txt"); transfer the password to hacker's website |
It would be terrible if php can access files outside the htdocs directory. In fact, this is the situation under the default configuration of xampp.
How to disable php from accessing the files outside specific folders? You can use the open_basedir option in php.ini. Open php.ini, search for the open_basedir line, un-comment the line and set the value of open_basedir to the folders you allow php to access such as:
1 |
open_basedir =c:\xampp\htdocs |
restart httpd, now if you run the php script that contains file_get_contents as above, you will get the error:
Warning: file_get_contents(): open_basedir restriction in effect. File(c:\xampp\password.txt) is not within the allowed path(s): (c:\xampp\htdocs)
And file_get_contents cannot read the file any more.
You can specify multiple directories(separated by for open_basedir, and php scripts are restricted to access files from only those folders.
1 |
open_basedir = "/path/to/first/folder:/path/to/second/folder" |
You can use the php_value instruction in .htaccess like one of the following:
1 |
php_value open_basedir "c:\\xampp\\htdocs" |
1 |
php_value open_basedir c:\\xampp\\htdocs |
1 |
php_value open_basedir c:\xampp\htdocs |
1 |
php_value open_basedir c:/xampp/htdocs |
1 |
php_value open_basedir "c:/xampp/htdocs" |
1 |
php_value open_basedir "c:\\xampp\\htdocs\\" |
1 |
php_value open_basedir "c:\xampp\htdocs\" |
You can also use the php_admin_value instruction in httpd.conf like:
1 2 3 4 5 |
<VirtualHost *:80> ServerName domainhostseotool.com DocumentRoot "c:\xampp\htdocs\domainhostseotool" php_admin_value open_basedir c:\xampp\htdocs\domainhostseotool </VirtualHost> |
All three methods can prohibit php scripts from accessing files outside allowed directories. Note that the allowed folders include specific directories and their sub-directories.