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 the file "/etc/apache2/mods-enabled/security2.conf", the line "IncludeOptional /usr/share/modsecurity-crs/*.load" must now be commented out and replaced by the following two lines:
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
To do this, the file "/etc/apache2/sites-available/001-exchange-proxy.conf" is adapted. For MapiOverHTTP, for example, the following 3 rules return False/Positives:
- 920420
- 949110
- 980120
These 3 rules can be deactivated in the "MAPIoverHTTP" section as follows:
SecRuleRemoveById 920420 949110 980120

For EWS are a few must be switched off the following rules:
- 921130
- 941100
- 941130
- 941140
- 941160
- 941180
- 941190
- 941250
- 941260
- 949110
- 980130
The following block is therefore inserted in the "EWS" section:
SecRuleRemoveById 921130 941100 941130 941140 941160 941180 941190 941250 941260 949110 980130

As can be seen, "LocationMatch" is used to specify the URI and "SecRuleRemoveById" is used to specify the rules that are to be deactivated.
The log file "/var/log/apache2/exchange_443_error.log" provides information on which rules can cause problems. As ModSecurity is set to "DetectOnly" by default, i.e. it only logs but does not block anything, you can take your time to see which rules are problematic. The log can be tracked live with the following command, for example:
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.
If you look at the log with "tail", you will probably be quickly overwhelmed by the mass of entries, so the following command can only list the most necessary information about rules that have been posted in the log:
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.
Since every Exchange installation is different and the programs/tools used are different, everyone has to identify the right rules for themselves. As already mentioned, ModSecurity initially only works in "DetectOnly" mode, once all important functions have been tested (don't forget ActiveSync), ModSecurity can then be switched to "Blocking mode". To do this, edit the file "/etc/modsecurity/modsecurity.conf" and set the value for "SecRuleEngine" to "On":

Then restart Apache again:
systemctl restart apache2
Finally, if you want to block known malicious IP addresses, you can use the "Project Honeypot" can be used. All you need is a free account with Project Honeypot and a corresponding API key:

The API key can be entered and the configuration activated in the file "/etc/apache2/modsecurity-crs/coreruleset-3.3.2/crs-setup.conf":

Finally, restart Apache and test again extensively.
Hallo,
hat schon jemand auf die neue OWASP ModSecurity Core Rule Set Version 3.3.5 upgedatet?
Ich würde mich über eine kurze Anleitung freuen.
Gruß Marko
Schön wäre jetzt noch auf dem Apache Client Based Authentication zu machen – dann kommen Clients ohne Zertifikat (fremde Clients) gar nicht bis zum Exchange…
Hallo Volker,
CBA wird nur von ActiveSync und OWA unterstützt. Outlook unterstützt es nicht, hilft also nur bedingt.
Beste Grüße,
Frank
Hallo,
absolut Weltstark die Anleitung! Herzlichen Dank, konnte so fast 1:1 auch mit Exchange 2016 umsetzen.
Einzig unklar ist mir die Einbindung des HoneyPots. Kann jemand hier noch etwas ausführlicher beschreiben was zu tun ist?
Vielen Dank
Hallo,
über IPv6 verbindet Outlook von extern nicht mit dem Exchangeserver 2019, wenn ich aber in der Zyxel Firewall die Regel „HTTPS“ für den Exchangeserver hinzufüge, funktioniert die Verbindung. Allerdings ist dann der Exchange Admin Center auch wieder erreichbar was ja nicht sein soll. ActiveSync usw. über Handy funktioniert aber ohne Probleme.
HTTPS soll ja nur für den Proxy aktiviert sein.
Wie wäre für IPv6 die Lösung? Konnte in den Logs kein Fehler finden.
ich möchte mich der Frage von MaGo anschließen was die Verwendung für Exchange 2016 betrifft
Ich habe die mal testweise installiert, jedoch bekomme ich andere Ergebnisse bei den Abfragen der Logs als hier aufgezeigt
das Suchen der passenden ID’s nimmt etwas Zeit in Anspruch
funktioniert gut, nur habe ich die SecRuleRemoveById nicht bei /EWS sondern bei /owa/* eingetragen, ansonsten ging z.B. senden oder löschen nicht etc.
Hallo,
ich hätte da noch eine Frage/kleines Problem…. vielleicht hat jemand eine Antwort, ich habe leider noch nichts gefunden.
Wenn ich „SecRuleEngine“ auf „On“ stelle, werden Mails über ActiveSync nicht mehr versendet, sobald ein etwas größerer Anhang der Mail beigefügt ist. Sobald ich wieder auf „DetectionOnly“ zurück gehe, wird die Mail korrekt versendet.
Von daher vermute ich, dass es irgendwo noch einen Parameter gibt, welcher hier auf die Größe wirkt, nur leider finde ich den nicht.
Vielen Dank schon einmal für eine Rückmeldung, ob das Problem auch bei anderen Auftritt!
Viele Grüße
Alex
…okay, gefunden…
in der „/etc/modsecurity/modsecurity.conf“ müssen die Werte „SecRequestBodyLimit“, „SecRequestBodyNoFilesLimit“ und „SecRequestBodyInMemoryLimit“ entsprechend konfiguriert werden, um Mails größer als 128 KB versenden zu können.
Hallo und guten Abend,
bin wieder begeistert von deinen Anleitungen, nur ein kleiner Hinweis, was mir ein wenig Schweißperlen eingebracht hat..
hatte „SecRuleEngine“ auf „On“ gestellt und danach ging etwas leider nicht mehr. Also wollte ich wieder zurückstellen, und der korrekte Wert um wieder auf den „Beobachtungsmodus“ zu kommen lautet „SecRuleEngine DetectionOnly“ und nicht „DetectOnly“. Nimmt man „DetectOnly“ kommt der Apache nicht mehr zurück und verweigert den Start.
Viele Grüße und immer weiter so!
Alex
Kann man diese Anleitung auch für einen Exchange 2016 verwenden?
mfg
grandios!
ich bin mir sicher dass die Sicherheit des Exchange dadurch massiv erhöht werden kann!
Danke Dir