• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Set Up mod_security And fail2ban To Create Anti-Spam Filter

By Abhishek Ghosh November 25, 2018 6:38 pm Updated on November 25, 2018

Set Up mod_security And fail2ban To Create Anti-Spam Filter

Advertisement

It is common for the blogs, forums to face malicious web traffic on regular basis. This malicious traffic not only harmful by simply wasting server resources, slowing down the site but also can lead to ban by third party Advertisement networks, such as Google AdSense. Here is How To Set Up mod_security And fail2ban To Create Anti-Spam Filter For Web Software Auch as WordPress, Forum Software on Debian/Ubuntu Server. Method to setup on REHL/CentOS is slightly different. We are taking it granted that Apache is already installed and running in some method like our guide on installing Apache, in the same way fail2ban also installed and running in some method like our guide on installing fail2ban. We have a separate guide on WordPress Brute Force And Mod Security.

Set Up mod_security And fail2ban To Create Anti-Spam Filter

 

Steps To Set Up mod_security And fail2ban

 

In easy way, we have to do the following step :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
apt update
apt upgrade
apt install apache2 -y
sudo systemctl enable apache2.service
sudo systemctl start apache2.service
sudo a2enmod headers
apt install libapache2-modsecurity
# sudo a2enmod mod-security
sudo a2enmod security2
apachectl -M | grep security
# output
## security2_module (shared)

We need to enable the mod_security rules. We will copy the mod_security configuration file, edit it and set the SecRuleEngine option to On:

Advertisement

---

Vim
1
2
sudo cp /etc/modsecurity/modsecurity.conf{-recommended,}
sudo nano /etc/modsecurity/modsecurity.conf

Change these settings :

Vim
1
2
3
4
5
...
SecRuleEngine On
...
SecResponseBodyAccess Off
…

mod_security rules are available in following directories:

Vim
1
2
3
/usr/share/modsecurity-crs/base_rules
/usr/share/modsecurity-crs/optional_rules
/usr/share/modsecurity-crs/experimental_rules

To enable all CRS base rules, create symbolic links :

Vim
1
sudo ln -s /usr/share/modsecurity-crs/base_rules/*.conf /usr/share/modsecurity-crs/activated_rules/

CRS optional and experimental rules needs separate symbolic links (not shown here).

We can configure and enable the Open Web Application Security Project (OWASP) core rule set:

Vim
1
2
3
4
5
apt install git
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
mv /usr/share/modsecurity-crs /usr/share/modsecurity-crs.bak
mv owasp-modsecurity-crs /usr/share/modsecurity-crs
mv /usr/share/modsecurity-crs/crs-setup.conf.example /usr/share/modsecurity-crs/crs-setup.conf

Edit the /etc/apache2/mods-enabled/security2.conf file:

Vim
1
/etc/apache2/mods-enabled/security2.conf

Add the lines at the end:

Vim
1
2
IncludeOptional "/usr/share/modsecurity-crs/*.conf
IncludeOptional "/usr/share/modsecurity-crs/rules/*.conf

Restart Apache:

Vim
1
systemctl restart apache2

You need to read more information on how to configure and use mod_security from official documentation. Install the mod_evasive module using the following command:

Vim
1
2
apt-get install libapache2-mod-evasive
sudo a2enmod evasive

Open mod-evasive.conf file, configure mod_evasive module:

Vim
1
nano /etc/apache2/mods-available/mod-evasive.conf

Change the values like this :

Vim
1
2
3
4
5
6
7
8
9
...
DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 30
DOSPageInterval 1
DOSSiteInterval 3
DOSBlockingPeriod 3600
DOSLogDir /var/log/apache2/mod_evasive.log
...

Save that file. Create a log file for mod_evasive, give proper permission and restart Apache :

Vim
1
2
3
touch /var/log/apache2/mod_evasive.log
sudo chown www-data:www-data /var/log/apache2/mod_evasive.log
systemctl restart apache2

Read README file in the mod_evasive module for details on the various configuration parameters. We can configure fail2ban with mod security reading official guide :

Vim
1
http://www.fail2ban.org/wiki/index.php/HOWTO_fail2ban_with_ModSecurity2.5

When configuring fail2ban, you can test a failregex and ignoreregex patterns, against the mod_security logfile before activating the fail2ban mod_security filter:

Vim
1
2
fail2ban-regex /var/log/apache2/modsec_audit.log "FAIL_REGEX" "IGNORE_REGEX"
fail2ban-regex /var/log/apache2/modsec_audit.log "\[.*?\]\s[\w-]*\s<HOST>\s" "\[.*?\]\s[\w-]*\s<HOST>\s"

Sometimes, we need to un-ban an IP address, we can list of all current rules and check blocked IPs:

Vim
1
iptables -L -n

You can extract the IP address from this list :

Vim
1
iptables -L -n | grep DROP | sed 's/.*[^-]--\s\+\([0-9\.]\+\)\s\+.*$/\1/g'

We can pass it to perform the unbanning :

Vim
1
iptables -L -n | grep DROP | sed 's/.*[^-]--\s\+\([0-9\.]\+\)\s\+.*$/\1/g' | xargs -i{} iptables -D fail2ban-ModSec -s {} -j DROP

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Set Up mod_security And fail2ban To Create Anti-Spam Filter

  • What is ModSecurity? Why Should We Use ModSecurity

    ModSecurity is an open-source web application firewall. It is available as a module for the Apache Server, and also Microsoft IIS and Nginx web server. It provides a rule configuration language (SecRules) for real-time monitoring, logging, and filtering of HTTP communications. ModSecurity is probably most commonly used to add protection against general vulnerabilities using the […]

  • Configure Apache With Fail2Ban on Ubuntu 18.04

    Here is How To Configure Apache With Fail2Ban on Ubuntu 18.04 to block more types of malicious attempts towards server to create a practical firewall.

  • Configure Fail2Ban With Mod Security And Other Filters

    Here is How To Configure Fail2Ban With Mod Security & Others On Apache Server To Protect From PHP And Other Exploits. Config Files Included.

  • How to Install and Configure ModSecurity for WordPress

    In an earlier article, we published an introduction on mod security. While there are many good reasons to use mod security, this module adds complexity to any WordPress installation. This guide is based on the official directions for WordPress to create a window so that a WordPress user can edit any post, and upload media. […]

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy