PHP Doku:: Verwendung als Apache-Modul - security.apache.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSicherheitVerwendung als Apache-Modul

Ein Service von Reinhard Neidl - Webprogrammierung.

Sicherheit

<<Fall 4: PHP-Parser außerhalb des Webverzeichnisbaums

Dateisystem - Sicherheit>>

Verwendung als Apache-Modul

Wenn PHP als Apache-Modul eingesetzt wird, übernimmt es die Benutzerrechte des Apache (üblicherweise die des Users "nobody"). Das hat verschiedene Auswirkungen auf Sicherheit und Authentifizierung. Wenn Sie beispielsweise via PHP auf eine Datenbank zugreifen, müssen Sie dem Benutzer "nobody" Zugriffsrechte auf die Datenbank erteilen, es sei denn, diese Datenbank hat eine integrierte Zugriffskontrolle. Das heißt, dass ein böswilliges Skript auch ohne Benutzerkennung und Passwort auf die Datenbank zugreifen und sie verändern könnte. Es ist durchaus möglich, dass ein Web-Spider über die Webseite eines Datenbankadministrators stolpert und alle Ihre Datenbanken löscht. Sie können sich dagegen mit Apache-Authentifizierung schützen, oder ein eigenes Zugangsmodell unter Verwendung von LDAP, .htaccess Dateien etc. entwerfen, und diesen Code als Teil Ihrer PHP-Skripte einbinden.

Ist erst einmal eine Sicherheitsstruktur bis zu dem Punkt eingerichtet, an dem der PHP-User (in diesem Falle der Apache-User) nur noch ein geringes Risiko darstellt, hat man meist die Situation, dass PHP gehindert wird, beliebige Dateien in das Benutzerverzeichnis zu schreiben. Oder vielleicht ist PHP dann nicht mehr in der Lage, auf Datenbanken lesend oder verändernd zuzugreifen. In gleicher Weise wird auch verhindert, "gute" oder "bösartige" Dateien zu schreiben, oder "gute" bzw. "bösartige" Datenbanktransaktionen durchzuführen.

Ein an diesem Punkt oft geöffnetes Sicherheitsloch ist die Zuweisung von Root-Rechten an den Apache-Prozess oder die Ausweitung der Rechte des Apache auf anderem Wege.

Die Ausweitung der Benutzerrechte für Apache auf root ist extrem gefährlich und kann das gesamte System kompromittieren, daher sollten Prozesse, die wie sudo oder chroot mit Rootrechten arbeiten, niemandem zugänglich sein, der kein Sicherheitsprofi ist.

Es gibt deutlich einfachere Lösungen. Mit open_basedir können Sie kontrollieren, welche Verzeichnisse PHP benutzen darf oder nicht. Sie können auch einen Bereich nur für Apache einrichten, um alle webbasierten Aktivitäten auf dem System oder nicht dem Benutzer gehörende Dateien zu verhindern.


6 BenutzerBeiträge:
- Beiträge aktualisieren...
Vikanich
14.08.2008 13:41
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.
Kibab
30.09.2005 15:56
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 :)
tifkap
2.03.2004 0:21
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
Georgee at CWC
30.04.2003 15:16
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...)
leeps
10.03.2003 14:59
@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.
daniel dot eckl at gmx dot de
8.08.2002 13:16
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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)