Whitelisting in ModSecurity
Broken down into two parts our article's first section hits on “how to whitelist IPs or URIs," for people who are somewhat familiar with ModSecurity but want to know further about the process. Our second section examines why we configure ModSecurity and how to prevent the security of the server from getting in the way of our work. If you have a Fully Managed Liquid Web server reach out to our Heroic Support team for assistance with whitelisting!
How to Whitelist IPs or URIs
“ModSecurity is a toolkit for real-time web application monitoring, logging, and access control.” (modsecurity.org). In simple terms, this means that ModSec, also called mod_security or ModSecurity, is a web application firewall that can actively look for attacks to the system and stop malicious activity. However, sometimes these rules trigger when legitimate work is taking place, blocking your IP and stopping you or your developer’s until you can remove the IP block. The way around for being blocked is known as whitelisting, which essentially allows for a specific IP to access the server. There are a few ways to whitelist a request in ModSec, either by IP or by URI (URIs are specific pages on the website).
1. Find your IP or ask your developer for theirs. (You can find this by going to ip.liquidweb.com)If you or your developer have a static IP (one that will not change), one way you can whitelist the ModSec rules is by IP.
2. Find the ModSec error in the Apache error logs with the following command (Be sure to modify the command with your IP replacing “IP here.").
grep ModSec /usr/local/apache/logs/error_log | grep “IP here”.
3. The output of this command will give you a list of hits for ModSecurity from you or your developer’s IP, which you can see below. While these instructions may look intimidating, you will only want to pay attention to 3 bits of information highlighted. Please note, the output will not show these colors when you are viewing the files.
[Fri May 25 23:07:04.178701 2018] [:error] [pid 78007:tid 139708457686784] [client 18.104.22.168:30095] [client 22.214.171.124] ModSecurity: Access denied with code 406 (phase 2). Pattern match "Mozilla/(4|5)\\\\.0$" at REQUEST_HEADERS:User-Agent. [file "/etc/apache2/conf.d/modsec2.liquidweb.conf"] [line "109"] [id "20000221"] [hostname "126.96.36.199"] [uri "/db/index.php"] [unique_id "WwjPWChxvG1CO4kz-D55eQAAACU"]
1. Once you have the correct ModSec error, you will need to edit the ModSec configuration. If you are using Easy Apache 4 you will find the configuration file with this path.
2. Open the file with your favorite text editor, such as Vim, Nano, or File Manager. This tutorial uses Vim.
3. The blue text above will be the IP address that you are whitelisting from the original error. You must keep the backslashes (\) and up-carrot (^) in order for the IP to be read correctly. Thus, it will always look something like the following.
For the id noted in red, you must change the number after the colon to a new rule that doesn't exist rather than the ID of an existing one. For this example, the new rule is as follows.
Add the following code with the colored sections edited to match your intended IP.
SecRule REMOTE_ADDR "^64\.14\.210\.4" "phase:1,nolog,allow,ctl:ruleEngine=off,id:20000221"
If your IP is dynamic (changing) and you keep getting blocked in the firewall, it is best to whitelist it via URI, the yellow item in the ModSec error.
1. Begin by opening the Easy Apache 4 configuration file.
2. Add the following text to the configuration. Change /db/index.php to match your URI and the id to match the id of your error. Do not use the colon in this one.
<LocationMatch "/db/index.php"> SecRuleRemoveById 20000221 </LocationMatch>
3. The final step for whitelisting, before you finalize the process, is to ensure you have correctly set up the whitelist. For Easy Apache 4, you will run the following command.
As long as the command returns Syntax Ok you are safe to make the whitelist active by restarting Apache. Otherwise, review the whitelists to make sure the syntax matches up correctly with the above directions.
4. Lastly, restart Apache with the following command.
You have successfully whitelisted yourself in ModSec!
Cyber Security is a hydra; once one threat is cut off, two more grow back. While this is not a new analogy, it’s important to understand as we battle threats to our network, computers, and servers. With all the complexities that come with security, I want to talk about adequately configuring ModSec to deter threats while still allowing you to work on your websites. Often, when it comes to server security, too much protection can hinder effectiveness.
For example, say you have the following set up on your server:
- You do not allow root SSH login to the server
- utilize dual-factor authentication for any SSH logins
- use an SSH key for the sudo user and require other security safeguards
While this type of configuration is secure, it takes longer to log into your system to make a quick edit to your settings, a double-edged sword; how can you keep the server safe while not tying your hands? A great example of how this plays out is using ModSec.
ModSec can block your IP if it falsely flags your work. While this module improves system security, you'll need to be aware of properly implementing and “scoping” the technology. Scoping in this sense means to manage risks, the focus of what is important for security while still allowing work on the server with minimal interference. To tune out legitimate requests to your server, such as when you are editing your website's code via a plugin, ModSec has the options to whitelist rules or IPs and keep your work on track.
Whitelisting an IP from the rules that ModSec follows is a great option so long as the IP never changes (i.e. a static IP; See the article here to learn more) and is limited to only people you trust. This method prevents ModSec from viewing your requests as malicious and blocking your IP. This practice has the drawback that if someone (say an unhappy employee) has access to your network, they now have a way around ModSec to attack your server.
With non-static (dynamic) IPs the problems of whitelisting an IP are apparent. With the continual change of a dynamic IP, it creates the potential of exploiting your server, as someone could use an old IP to access the server. Whitelisting specific rules comes to save the day! When you whitelist by rules, you can edit with granularity and limit the rules to particular domains and URIs, protecting the rest of the server from attacks related to that same rule!
Example of ModSecurity
ModSec reads a series of rules and applies them to incoming requests being made to the web server. An example of what a block looks like is as follows.
[Sat Jun 30 02:21:56.013837 2018] [:error] [pid 79577:tid 139862413879040] [client 188.8.131.52:24397] [client 184.108.40.206] ModSecurity: Access denied with code 406 (phase 2). Pattern match "Mozilla/(4|5)\\\\.0$" at REQUEST_HEADERS:User-Agent. [file "/etc/apache2/conf.d/modsec2.liquidweb.conf"] [line "109"] [id "2000064"] [hostname "220.127.116.11"] [uri "/mysql/index.php"] [unique_id "WzchhAjuZ6wPAzo9AwW1WwAAAE8"]
This error shows Apache stopped a potential attack on a file at /mysql/index.php. This is an error similar to what appears when the code is being written or edited within programs like Drupal or WordPress.
If you are persistently being blocked in your firewall while working on your code, ModSec is the likely culprit. The ModSec errors can be found in the Apache error log (in cPanel the path is /usr/local/apache/logs/error_log). The phrase “ModSec” can be quickly isolated from the log (via the command ‘grep “ModSec” /usr/local/apache/logs/error_log’). By comparing you or your developer(s) IP to the log, you'll be able to identify stopped requests that are legitimate. Verify these are valid requests by double-checking that someone in your organization made them. Once you have done so, you can move forward in setting up a whitelist for the error, per the steps above.
Again, we want to scope to allow the least amount of wiggle room for an attack and ensure we can keep working. If you are unable to have a trusted static IP, you'll need to use the whitelist URI method, providing the specific page as an exemption. Once completed, remove both whitelisted items from the configuration file, in case of a genuine attack.
On a parting note, I encourage you to explore ModSec and learn more of the ins and outs of the software. Exploring different methods of whitelisting can be a lot of for to learn and most importantly helps to tighten server security. As always, our Fully Supported Customers can contact our Helpful Human Support team for assistance. Check out articles on security in our Knowledge Base, like this one on Maldet! It's another excellent way to learn about your server and develop an understanding of server security.
- 4 Methods for How to Install Yarn on Windows Server
- How to Install Bpytop Resource Monitoring Tool on AlmaLinux
- How to Fix “This Site Can’t Provide a Secure Connection” Error
- How to Install MongoDB on AlmaLinux
- How to Install PostgreSQL on AlmaLinux
- How to Use the WP Toolkit to Secure and Update WordPress
About the Author: Cameron Paxton
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
What Is WebP and What Makes it Different from Other Image Formats?Read Article
Top 10 Password Security StandardsRead Article
Top 10 Password Security StandardsRead Article
How to Install MongoDB on AlmaLinuxRead Article
How to Use the WP Toolkit to Secure and Update WordPressRead Article