downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Sécurité des fichiers> <Cas 4 : Exécutable PHP à l'extérieur de l'arborescence du serveur
[edit] Last updated: Fri, 26 Apr 2013

view this page in

Installé en tant que module Apache

Lorsque PHP est utilisé en tant que module Apache, celui-ci hérite des permissions accordées à l'utilisateur faisant tourner Apache (par défaut, l'utilisateur "nobody"). Ceci a plusieurs impacts sur la sécurité et les autorisations. Par exemple, si vous utilisez PHP pour accéder à une base de données, à moins que la base n'ait un système de droits d'accès interne, vous devrez la rendre accessible à l'utilisateur "nobody". Cela signifie qu'un script mal intentionné peut accéder à la base et la modifier, sans identification. Il est même possible qu'un robot accède une page d'administration, et détruise toutes les bases de données. Vous pouvez vous protéger contre cela avec les autorisations Apache, ou définir votre propre modèle d'accès en utilisant LDAP, des fichiers .htaccess, etc. et inclure ce code dans vos scripts PHP.

Souvent, lorsqu'on a établi les droits de l'utilisateur PHP (ici, l'utilisateur Apache) pour minimiser les risques, on s'aperçoit que PHP ne peut plus écrire de fichiers dans les répertoires des utilisateurs. Ou encore, qu'il ne peut plus accéder à, ou modifier, une base de données privée. En somme, les sécurités mises en place empêchent à la fois l'écriture de bons et de mauvais fichiers, en même temps que les bonnes et mauvaises opérations en bases de données.

Arrivé là, une erreur de sécurité fréquente est de donner à l'utilisateur Apache les droits de superadministrateur ("root"), ou d'accroitre les possibilités d'Apache d'une quelconque autre façon.

Donner de telles permissions à l'utilisateur Apache est extrêmement dangereux, et pourrait compromettre tout le système ; en conséquence, l'utilisation de sudo, de chroot, ou de toute autre solution permettant de fonctionner en tant que superadministrateur ("root"), ne devrait pas être envisagée par toute personne qui ne soit pas experte en sécurité.

Il existe des solutions plus simples. En utilisant open_basedir, vous pouvez contrôler et restreindre les dossiers qui seront accessibles par PHP. Vous pouvez aussi créer des aires de restrictions Apache, pour limiter les activités en provenance du web à des fichiers qui ne soient en rapport ni avec des utilisateurs, ni avec le système.



add a note add a note User Contributed Notes Installé en tant que module Apache - [7 notes]
up
1
bk 2 at me dot com
1 year ago
doc_root already limits apache/php script folder locations.

open_basedir is better used to restrict script access to folders
which do NOT contain scripts. Can be a sub-folder of doc_root as in php doc example doc_root/tmp, but better yet in a separate folder tree, like ~user/open_basedir_root/. Harmful scripts could modify other scripts if doc_root (or include_path) and open_basedir overlap.
If apache/php can't browse scripts in open_basedir, even if malicious scripts uploaded more bad scripts there, they won't be browse-able (executable).

One should also note that the many shell execute functions are effectively a way to bypass open_basedir limits, and such functions should be disabled if security demands strict folder access control. Harmful scripts can do the unix/windows version of "delete */*/*/*" if allowed to execute native os shell commands via those functions. OS Shell commands could similarly bypass redirect restrictions and upload file restrictions by just brute force copying files into the doc_root tree. It would be nice if they could be disabled as a group or class of functions, but it is still possible to disable them one by one if needed for security.

PS. currently there is a bug whereby the documented setting of open_basedir to docroot/tmp will not work if any include or require statements are done. Right now include will fail if the included php file is not in BOTH the open_basedir tree and the doc_root+include_path trees. Which is the opposite of safe.
This means by any included php file must be in open_basedir, so is vulnerable to harmful scripts and php viruses like Injektor.
up
1
Vikanich
4 years ago
Big thanks to "daniel dot eckl at gmx dot de" but i have to change his config, because it doesn't work (may be wrong syntax).
I have add only this string to VirtualHost config and it works.
php_admin_value open_basedir  /www/site1/
Now all php scripts are locked in the directory.
up
1
leeps
10 years ago
@pollux: additionally, tell your users to set their file-permissions to
- r-- (group) for files
- --x (group) for directories.

this disables the webserver to browse user's directory. if you don't know the filename, you cannot open it, e.g. by running malicious php-code through one of the users scripts.
up
2
daniel dot eckl at gmx dot de
10 years ago
There is a better solution than starting every virtual host in a seperate instance, which is wasting ressources.

You can set open_basedir dynamically for every virtual host you have, so every PHP script on a virtual host is jailed to its document root.

Example:
<VirtualHost www.example.com>
  ServerName www.example.com
  DocumentRoot /www-home/example.com
[...]
  <Location />
    php_admin_value open_basedir     \ "/www-home/example.com/:/usr/lib/php/"
  </Location>
</VirtualHost>

If you set safe_mode on, then the script can only use binaries in given directories (make a special dir only with the binaries your customers may use).

Now no user of a virtual host can read/write/modify the data of another user on your machine.

Windseeker
up
0
Kibab
7 years ago
I'm running Windows version of Apache with php as module. System is Windows XP Service Pack 2 on NTFS filesystem. To avoid potential security problems, I've set Apache to run under NT AUTHORITY\Network Service account, and there is only one directory, named Content, with Full Access for this account. Other directories are either not accessible at all or with readonly permissions (like %systemroot%)... So, even if Apache will be broken, nothing would happen to entire system, because that account doesn't have admin privilegies :)
up
-1
tifkap
9 years ago
There is a safe way to support a lot of users in a secure way, without having to use CGI, in a way which is probebly faster
than mod_php.

Use FastCGI, with the SuExecWrapper set to your suid wrapper. It means every user wil get his own program-group, with processes
which are being reused. If the numer of processes that is being
started on startup is 0, then the processgroup for a user will be generated when needed.

This means: The first page is slow, after that the Zend Engine  caching kicks in. When the load on the virtualhost reduces, the
processes wil die off, and extra processes for a user-process-group
will only be started when (again) needed.

Your apache will be a LOT! lichter, because it won't have to drag all
the php-memory overhead with it. This means static content is
faster, and the whole system uses less memory.
The PHP itself also won't need to drag along the apache overhead.

If for one reason or the other php craches, your apache will simple
start some new php-processes. If you want to upgrade/patch php,
you can simple create the new fastcgi binary, and after testing, you can simple update the system by copying it, and maybe doing a
'apachectl gracefull'

In short :  Sepparating distinct functions in different processes
                communicating useing IPC methodes can be very good
                for performance and security. The best example of this
                principle at work is Postfix, where every process runs
                chroot() under its own uid.

http://wiki.openisis.org/i/view/Php/HowtoFastCgi
up
-1
Georgee at CWC
10 years ago
Additional CAUTION to anyone trying Pollux's solution:
It's kind a good. Probably works right. I think I'll give it a try myself. BUT...
its safe ONLY on the assumption that apache is 100% CLEAN. (codes and confs.) Any flaws on apache, almost ANYTHING could happen to ALL users -precisely, web users. (Because apache is a member of ALL -again, web user's- GID.) So, leeps's hint should be one of the important things.

There is nothing close to perfect. What I wrote is just one thing you'll have to keep in mind. So, consider carefully BEFORE you try this solution. (Well, this applies to any other solutions though...)

 
show source | credits | stats | sitemap | contact | advertising | mirror sites