In the first part of the article the installation and configuration of Apache as a reverse proxy for Exchange Server was carried out, this article deals with the installation and configuration of ModSecurity as a web application firewall for Exchange 2019. ModSecurity is an open source web application firewall that can protect Exchange from common attacks with an appropriate set of rules.
Installation of ModSecurity and OWASP ModSecurity Core Rule Set
First, the Apache module modSecurity is installed and activated:
apt-get install libapache2-mod-security2
a2enmod security2
mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
systemctl restart apache2
Now the rules can be OWASP ModSecurity Core Rule Set and integrate it into the configuration:
cd /tmp
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.tar.gz
tar xvf v3.3.2.tar.gz
mkdir /etc/apache2/modsecurity-crs/
mv coreruleset-3.3.2/ /etc/apache2/modsecurity-crs/
cd /etc/apache2/modsecurity-crs/coreruleset-3.3.2/
mv crs-setup.conf.example crs-setup.conf
In der Datei „/etc/apache2/mods-enabled/security2.conf“ muss nun die Zeile „IncludeOptional /usr/share/modsecurity-crs/*.load“ auskommentiert und durch die folgenden beiden Zeilen hinzugefügt werden:
IncludeOptional /etc/apache2/modsecurity-crs/coreruleset-3.3.2/crs-setup.conf
IncludeOptional /etc/apache2/modsecurity-crs/coreruleset-3.3.2/rules/*.conf
To activate ModSecurity, the Apache configuration can now be checked once and then restarted:
apache2ctl -t
systemctl restart apache2
Adaptation of the OWASP rules for Exchange 2019
Dazu wird die Datei „/etc/apache2/sites-available/001-exchange-proxy.conf“ angepasst. Für MapiOverHTTP liefern beispielsweise die folgenden 3 Regeln False/Positives:
- 920420
- 949110
- 980120
Diese 3 Regeln können im Abschnitt „MAPIoverHTTP“ wie folgt deaktiviert werden:
<LocationMatch "/mapi/*">
SecRuleRemoveById 920420 949110 980120
</LocationMatch>
For EWS are a few must be switched off the following rules:
- 921130
- 941100
- 941130
- 941140
- 941160
- 941180
- 941190
- 941250
- 941260
- 949110
- 980130
Im Abschnitt „EWS“ wird daher der folgende Block eingefügt:
<LocationMatch "/EWS/Exchange.asmx">
SecRuleRemoveById 921130 941100 941130 941140 941160 941180 941190 941250 941260 949110 980130
</LocationMatch>
Wie zu erkennen ist, wird mit „LocationMatch“ die URI angegeben und mit „SecRuleRemoveById“die Regeln, welche abgeschaltet werden sollen.
Das Logfile „/var/log/apache2/exchange_443_error.log“ liefert Hinweise, welche Regeln Probleme verursachen können. Da ModSecurity in der Standardeinstellung auf „DetectOnly“ steht, also nur protokolliert, aber nichts blockiert, kann man in aller Ruhe schauen, welche Regeln problematisch sind. Das Log lässt sich beispielsweise mit dem folgenden Befehl live verfolgen:
tail -f /var/log/apache2/exchange_443_error.log
Before ModSecurity can be set to blocking mode, as many functions as possible must now be tested. As many functions as possible should therefore be tried out in Outlook and as many clicks as possible should be made in OWA. Programs and tools that access Exchange interfaces such as EWS should be tested.
Wenn man sich das Log mit „tail“ anschaut, wird man wahrscheinlich schnell von der Masse der Einträge erschlagen, daher kann der folgende Befehl nur die nötigsten Informationen zu Regeln auflisten, welche im Log angeschlagen haben:
grep ModSecurity /var/log/apache2/exchange_443_error.log | grep "\[id" | sed -E -e 's#^.*\[id "([0-9]*).*hostname "([a-z0-9\-\_\.]*)"].*uri "(.*?)".*"#\1 \2 \3#' | cut -d\" -f1 | sort -n | uniq -c | sort -n
The command can be used to identify which rules have been applied to which URIs. The screenshot shows the rules for OWA, for example. I always tested one protocol/program after the other, first Outlook, then Autodiscover, then EWS, and finally OWA.
Da jede Exchange Installation anders ist und sich die verwendeten Programme/Tools unterscheiden, muss jeder für sich die richtigen Regeln identifizieren. Wie bereits erwähnt arbeitet ModSecurity erst einmal nur Modus „DetectOnly“, wenn alle wichtigen Funktionen getestet wurden (ActiveSync nicht vergessen), dann kann ModSecurity in den „Blocking Modus“ geschaltet werden. Dazu wird die Datei „/etc/modsecurity/modsecurity.conf“ bearbeitet und der Wert für „SecRuleEngine“ auf „On“ gesetzt:
Then restart Apache again:
systemctl restart apache2
Wer möchte kann zum Schluss noch bekannte schädliche IP Adressen sperren, dazu kann das „Project Honeypot“ verwendet werden. Alles was dazu nötig ist, ist ein kostenloser Account bei Project Honeypot und einen entsprechenden API Key:
In der Datei „/etc/apache2/modsecurity-crs/coreruleset-3.3.2/crs-setup.conf“ kann der API Schlüssel eingetragen und die Konfiguration aktiviert werden:
Finally, restart Apache and test again extensively.