Apache als Reverse Proxy mit WAF

Aus Xinux Wiki
Version vom 28. Mai 2025, 11:41 Uhr von Thomas.will (Diskussion | Beiträge) (→‎Test SQL Injection)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen

Ziel

Apache2 fungiert als Reverse Proxy für die öffentliche Seite blau.it113.int

ModSecurity wird als Web Application Firewall aktiviert

Zwei eigene Regeln werden erstellt:

– Gegen Command Injection – Gegen SQL Injection

Die Weiterleitung zum internen Webserver erfolgt über HTTPS ohne Zertifikatsprüfung

Die SSL-Zertifikate liegen unter /etc/apache2/ssl

Pakete installieren

Installiere Apache und ModSecurity
  • apt update
  • apt install apache2 libapache2-mod-security2

ModSecurity aktivieren

Kopiere die empfohlene Konfigurationsdatei und aktiviere den WAF-Modus.

  • cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
  • sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf

Verzeichnis für eigene Regeln erstellen

Erstelle ein Verzeichnis für benutzerdefinierte Regeln
  • mkdir /etc/modsecurity/custom-rules

Regel gegen Command Injection

Erstelle eine Datei mit einer einfachen Command-Injection-Erkennung.

  • nano /etc/modsecurity/custom-rules/10-command-injection.conf
 SecRule ARGS "(;|\||&|`)" \
   "id:10001,phase:2,deny,log,status:403,msg:'Command Injection erkannt'"

Regel gegen SQL Injection

Erstelle eine Datei mit einer einfachen SQL-Injection-Erkennung.

  • nano /etc/modsecurity/custom-rules/11-sql-injection.conf
 SecRule ARGS "(?i)(union select|select.+from|insert into|drop table|--|#|;)" \
   "id:10002,phase:2,deny,log,status:403,msg:'SQL Injection erkannt'"

Regeln einbinden

Binde die benutzerdefinierten Regeln in die aktive Konfiguration ein.

  • nano /etc/apache2/mods-enabled/security2.conf
 IncludeOptional /etc/modsecurity/custom-rules/*.conf

Apache als Reverse Proxy mit HTTPS konfigurieren

Aktiviere die benötigten Apache-Module.

  • a2enmod proxy
  • a2enmod proxy_http
  • a2enmod proxy_https
  • a2enmod ssl

Erstelle ein Verzeichnis für die SSL-Zertifikate.

  • mkdir -p /etc/apache2/ssl

Lege die Zertifikatsdateien dort ab: – blau.it113.int.crt – blau.it113.int.key

Erstelle die Virtual-Host-Konfiguration.

  • nano /etc/apache2/sites-available/blau.it113.int.conf
 <VirtualHost *:443>
   ServerName blau.it113.int
   SSLEngine on
   SSLCertificateFile /etc/apache2/ssl/blau.it113.int.crt
   SSLCertificateKeyFile /etc/apache2/ssl/blau.it113.int.key
   ProxyPreserveHost On
   SSLProxyEngine On
   SSLProxyVerify none
   SSLProxyCheckPeerCN off
   SSLProxyCheckPeerName off
   ProxyPass / https://10.113.0.200/
   ProxyPassReverse / https://10.113.0.200/
   ErrorLog /var/log/apache2/waf-error.log
   CustomLog /var/log/apache2/waf-access.log combined
 </VirtualHost>

Aktiviere die neue Site und lade Apache neu.

  • a2ensite blau.it113.int
  • systemctl reload apache2

Test Command Injection

Test eines typischen Kommandoinjektionsversuchs

Erwartung: 403 Forbidden Log prüfen: /var/log/apache2/waf-error.log

Test SQL Injection

Test einer einfachen SQL-Injection

Erwartung: 403 Forbidden Log prüfen: /var/log/apache2/waf-error.log e liegen unter /etc/apache2/ssl

Pakete installieren

  • apt update
  • apt install apache2 libapache2-mod-security2

ModSecurity aktivieren

  • cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
  • sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf

Verzeichnis für eigene Regeln erstellen

  • mkdir /etc/modsecurity/custom-rules

Regel gegen Command Injection erstellen

Erstelle die Datei mit folgendem Inhalt:

  • nano /etc/modsecurity/custom-rules/10-command-injection.conf

Inhalt:

SecRule ARGS "(;|\||&|`)" \

 "id:10001,phase:2,deny,log,status:403,msg:'Command Injection erkannt'"

Regel gegen SQL Injection erstellen

Erstelle die Datei mit folgendem Inhalt:

  • nano /etc/modsecurity/custom-rules/11-sql-injection.conf

Inhalt:

SecRule ARGS "(?i)(union select|select.+from|insert into|drop table|--|#|;)" \

 "id:10002,phase:2,deny,log,status:403,msg:'SQL Injection erkannt'"

Regeln einbinden

Bearbeite die Datei security2.conf:

  • nano /etc/apache2/mods-enabled/security2.conf

Füge am Ende ein:

IncludeOptional /etc/modsecurity/custom-rules/*.conf

Apache als Reverse Proxy mit HTTPS konfigurieren

Benötigte Module aktivieren:

  • a2enmod proxy
  • a2enmod proxy_http
  • a2enmod proxy_https
  • a2enmod ssl

Verzeichnis für Zertifikate erstellen:

  • mkdir -p /etc/apache2/ssl

Zertifikate dort ablegen: – blau.it113.int.crt – blau.it113.int.key

Virtuelle Host-Konfiguration erstellen:

  • nano /etc/apache2/sites-available/blau.it113.int.conf

Inhalt:

<VirtualHost *:443>

 ServerName blau.it113.int
 SSLEngine on
 SSLCertificateFile /etc/apache2/ssl/blau.it113.int.crt
 SSLCertificateKeyFile /etc/apache2/ssl/blau.it113.int.key
 ProxyPreserveHost On
 SSLProxyEngine On
 SSLProxyVerify none
 SSLProxyCheckPeerCN off
 SSLProxyCheckPeerName off
 ProxyPass / https://10.113.0.200/
 ProxyPassReverse / https://10.113.0.200/
 ErrorLog /var/log/apache2/waf-error.log
 CustomLog /var/log/apache2/waf-access.log combined

</VirtualHost>

Site aktivieren und Apache neu laden:

  • a2ensite blau.it113.int
  • systemctl reload apache2

Test Command Injection

Ziel: Test eines typischen Kommandoinjektionsversuchs per GET-Parameter

Erwartung: 403 Forbidden Log prüfen: /var/log/apache2/waf-error.log

Test SQL Injection

Ziel: SQL-Injection-Test mit einfachem manipuliertem Parameter

Erwartung: 403 Forbidden Log prüfen: /var/log/apache2/waf-error.log