This article from chueyise.com provides an innovative approach for monitoring your Apache web access logs for suspicious activity and dynamically blocking nefarious source IPs using the iptables host-based firewall. The implementation involves the following:
Prerequisites:
Modify the Apache LogFormat setting to allow the HTTP Status Code to be easily recognized by a log monitoring tool (see Step 2). The HTTP Status Code is reflected in the LogFormat stanza as ‘%>s’. Below are steps to identify and modify the HTTP Status Code format:
The objective of this step is to monitor the web server access logs, identify HTTP Status Codes of 4XX, and copy the logs to a dedicated log file of 4XX log messages. Using syslog-ng optional, but this author believes it is far superior to rsyslog. The rsyslog service is likely installed and implemented by default on your Linux system. If you desire to use syslog-ng instead of rsyslog, perform the following to disable rsyslog, and install and enable syslog-ng. If you choose to use rsyslog, read the information below and translate the configurations into an equivalent rsyslog implementation.
Configure syslog-ng to monitor the Apache access logs for HTTP Status Codes of 400-499. These codes represent invalid (suspicious) activities against the web server site. Add the following stanzas to file /etc/syslog-ng/syslog-ng.conf. NOTE: ensure you provide a source stanza for each access log used by your apache configuration.
Ensure the 400.log file exists and restart the syslog-ng service to enable the new log collection and processing rules.
Some Linux distributions enable firewalld by default. In this case, firewalld needs to be disabled as follows:
Install and enable iptables:
If you are not familiar with developing custom firewall rules, use this resource as a tutorial. Once iptables is baselined and fully functional, install the following rules within the rules file (/etc/sysconfig/iptables). The BLACKLIST_REJECT chain is used by the ipset framework (described below).
Restart the iptables firewall with the new rules:
iptables-restore < /etc/sysconfig/iptables
Install and initialize the ipset framework. The ipset framework allows dynamic filter rules to be defined within iptables.
Use the following hyperlinked example files and customize as needed. This step enables all the services needed to continuously monitor for HTTP Status Codes 400-499 and dynamically block IP addresses performing suspicious activity.
If there were no errors from this step, your web server is actively monitoring the Apache access logs and blocking ip addresses performing web enumeration activities.
Monitor your apache logs to identify accesses resulting in 400 HTTP Status Codes that should not be blocked. As an example, if someone uses an iPhone to access your web site, the Apple browser will automatically attempt to access non-existent png files (apple-touch-icon.png, apple-touch-icon-precomposed.png) which will result in a 404 code. This is a legitimate access attempt that should not result in blocking the source IP address from further accesses to your site. This issue can be resolved by creating a nocares filter in your syslog-ng configuration file to ignore these types of accesses. Below are example settings to apply within the /etc/syslog-ng/syslog-ng.conf file.
filter f_nocares { not match (“systemd”) and
not match (“^.+\.png”) and
not match (“^.+Googlebot”) and
not match (“^.+bot/”) and
not match (“^.+\:\:408”);};
log { source(s_accesslog); filter(f_400); filter(f_nocares); destination(d_400); };
log { source(s_accessssllog); filter(f_400); filter(f_nocares); destination(d_400); };
Whenever you make changes to your /etc/syslog-ng/syslog-ng.config file, execute systemctl restart syslog-ng