Ich habe einen Apache 2.4-Konfigurationsabschnitt, den ich verwende, um die seltene Situation zu handhaben, in der eine falsch konfigurierte Apache-Box PHP-Dateien als statischen Klartext bereitstellen und möglicherweise Anmeldeinformationen usw. preisgeben könnte.
<IfModule !mod_php5.c>
<IfModule !mod_php7.c>
<Files "*.php">
Require all denied
</Files>
</IfModule>
</IfModule>
Diese Anweisungen prüfen, ob das PHP-Modul vorhanden ist. Wenn keines von beiden gefunden wird, werden die Seiten nicht bereitgestellt.
Im aktuellen Build von httpd 2.4 für CentOS 8 ist php-fpm jedoch die Standardmethode zum Konfigurieren eines Handlers für PHP und lädt kein Modul für PHP:
<IfModule !mod_php5.c>
<IfModule !mod_php7.c>
# Enable http authorization headers
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
<FilesMatch \.(php|phar)$>
SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
</FilesMatch>
</IfModule>
</IfModule>
Leider wird das Modul proxy_fcgi automatisch von httpd geladen und ist daher kein geeigneter Hinweis darauf, ob php-fpm konfiguriert ist (oder nicht):
[root@web httpd]# rpm -q --whatprovides /usr/lib64/httpd/modules/mod_proxy_fcgi.so
httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64
Entsprechendder ArztEs gibt eine HANDLER
Variable:
HANDLER Der Name des Handlers, der die Antwort erstellt
... was einen der folgenden Werte zurückgeben sollte:in eingebauten Handlern
default-handler: Send the file using the default_handler(), which is the handler used by default to handle static content. (core) send-as-is: Send file with HTTP headers as is. (mod_asis) cgi-script: Treat the file as a CGI script. (mod_cgi) imap-file: Parse as an imagemap rule file. (mod_imagemap) server-info: Get the server's configuration information. (mod_info) server-status: Get the server's status report. (mod_status) type-map: Parse as a type map file for content negotiation. (mod_negotiation)
Meine Abgleichversuche sind jedoch nicht erfolgreich. Beispielsweise funktioniert Folgendes nicht:
<FilesMatch \.(php|phar)$>
<If "%{HANDLER} == 'default-handler'">
Require all denied
</If>
</FilesMatch>
Alle Vorschläge sind willkommen.
Bearbeiten
Ich habe den Wert der Variable %{HANDLER} mit installiertem php-fpm ausgegeben, und zwar nicht so:
<IfModule headers_module>
Header always set X-HANDLER "expr=%{HANDLER}"
</IfModule>
und es wurden die folgenden Header zurückgegeben:
X-HANDLER: text/plain # no php-fpm
X-HANDLER: proxy:unix:/run/php-fpm/www.sock|fcgi://localhost # with php-fpm
deshalb habe ich den Test wie folgt aktualisiert:
<IfModule !mod_php5.c>
<IfModule !mod_php7.c>
<If "%{HANDLER} == 'text/plain'">
<Files "*.php">
Require all denied
</Files>
</If>
</IfModule>
</IfModule>
Aber auch das funktioniert nicht