{"id":8686,"date":"2020-06-15T07:42:54","date_gmt":"2020-06-15T11:42:54","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8686"},"modified":"2021-12-04T09:10:41","modified_gmt":"2021-12-04T09:10:41","slug":"configure-mod-security","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/configure-mod-security\/","title":{"rendered":"How to Configure mod_security With Apache on Debian\/Ubuntu or CentOS"},"content":{"rendered":"\n
mod_security is a free Web Application Firewall (WAF) that comes with Apache<\/a>, Nginx<\/a>, and IIS<\/a> web servers. It is designed to monitor HTTP<\/a> traffic in real-time in order to detect and mitigate attacks.<\/p>\n\n\n\n ModSecurity which acts as an intrusion detection tool, allows server administrators to react to suspicious activity and is able to prevent SQL<\/a> Injection, Cross-site Scripting, Malware<\/a>, Trojans, Bad user agents, Session hijacking, and a lot of other exploits.<\/p>\n\n\n\n ModSecurity comes with a default configuration however there is an OWASP ModSecurity Core Rule Set (CRS)<\/a> that provides a set of generic attack detection rules that provide a base level of protection for any web application from a wide range of attacks with minimal false alerts.<\/p>\n\n\n\n This tutorial will assume you have already installed the LAMP stack<\/a> on your server.<\/p>\n\n\n\n Debian<\/strong><\/p>\n\n\n\n Restart Apache:<\/p>\n\n\n\n Verify that the version of ModSecurity is 2.8.0 or higher:<\/p>\n\n\n\n Ubuntu<\/strong><\/p>\n\n\n\n Restart Apache:<\/p>\n\n\n\n Verify that the version of ModSecurity is 2.8.0 or higher:<\/p>\n\n\n\n If you use apachectl -M<\/strong> to list all mods, ModSecurity will be listed under the name security2_module. or you could filter the result by using:<\/p>\n\n\n\n The output will be:<\/p>\n\n\n\n CentOS<\/strong><\/p>\n\n\n\n Restart Apache by entering the following command:<\/p>\n\n\n\n Verify that the version of ModSecurity is 2.8.0 or higher:<\/p>\n\n\n\n Start by renaming the default ModSecurity file (the path below is for Ubuntu<\/a>\/Debian based distros):<\/p>\n\n\n\n We then need to download the OWASP ModSecurity CRS from Github. In case you need to install git (if you haven’t installed it before) run the following:<\/p>\n\n\n\n Download the OWASP ModSecurity CRS:<\/p>\n\n\n\n From the downloaded location move the relevant files as shown below:<\/p>\n\n\n\n Open this configuration file:<\/p>\n\n\n\n Add the IncludeOptional<\/strong> directive and the Include directive as shown below:<\/p>\n\n\n\n Restart Apache for changes to take effect:<\/p>\n\n\n\n Open the default Apache configuration and add two additional directives, using the default configuration as an example:<\/p>\n\n\n\n Add the SecRuleEngine<\/strong> and SecRule<\/strong> directives as shown below:<\/p>\n\n\n\n Restart Apache for changes to take effect:<\/p>\n\n\n\n Curl the index page to intentionally trigger the alarms using the following:<\/p>\n\n\n\n You should expect to get a 403 response code and see the response from the logs.<\/p>\n\n\n\n The default configuration file is set to DetectionOnly<\/strong> which logs requests according to rule matches and doesn’t block anything besides what the OWASP CRS provides. This can be changed by editing the file below:<\/p>\n\n\n\n Find this line<\/p>\n\n\n\n and change it to:<\/p>\n\n\n\n In production, be sure to change this directive only after testing all your rules.<\/p>\n\n\n\n You might also want to change SecResponseBodyAccess<\/strong>. This configures whether response bodies are buffered. This is only necessary if data leakage detection and protection are required. Leaving it On<\/strong> will use up droplet resources and also increase the log file size.<\/p>\n\n\n\n Find this<\/p>\n\n\n\n and change it to:<\/p>\n\n\n\n limit the maximum data that can be posted to your web application using these two directives configure these:<\/p>\n\n\n\n The value mentioned in the configuration file is 12MB:<\/p>\n\n\n\n The SecRequestBodyNoFilesLimit<\/strong> directive limits the size of POST data minus file uploads. This value should be as low as practical.<\/p>\n\n\n\n The default value in the configuration file is 128KB.<\/p>\n\n\n\n The SecRequestBodyInMemoryLimit<\/strong> can also be reviewed as it affects server performance too. This directive specifies how much POSTed<\/strong> data should be kept in the memory (RAM). Additional data will be placed in the hard disk like a swap. if you have plenty of RAM then you can be generous with this value. The default value in the configuration file is 128KB.<\/p>\n\n\n\n ModSecurity can block genuine traffic for various reasons so you need to be able to exclude certain directories or entire hosts. Examples are phpMyAdmin where SQL queries will be blocked or CMS<\/a> application backends like WordPress admin.<\/p>\n\n\n\n To disable mod_security for a complete Virtual Host place the following<\/p>\n\n\n\n Inside the VirtualHost<\/strong> section.<\/p>\n\n\n\n For a particular directory:<\/p>\n\n\n\n You may not want to completely disable ModSecurity. Therefore use the SecRuleRemoveById<\/strong> directive to remove a particular rule or rule chain by specifying its ID.<\/p>\n\n\n\n Next, we will show you how to set up mod_evasive in addition to this mod_security configuration to help safeguard against DDOS attacks.<\/p>\n","protected":false},"excerpt":{"rendered":" mod_security is a free Web Application Firewall (WAF) that comes with Apache, Nginx, and IIS web servers. It is designed to monitor HTTP traffic in real-time in order to detect…<\/p>\n","protected":false},"author":1,"featured_media":8688,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,16,18],"tags":[58,121,136,184,304,308,313,320,364,400,433,449,526,543,591,598,635,636,638,643],"yoast_head":"\nStep 1: Install mod_security<\/h2>\n\n\n\n
$ sudo apt install libapache2-modsecurity<\/code><\/pre>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n
$ apt-cache show libapache2-modsecurity<\/code><\/pre>\n\n\n\n
$ sudo apt-get install libapache2-mod-security2<\/code><\/pre>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n
$ apt-cache show libapache2-mod-security2<\/code><\/pre>\n\n\n\n
$ apachectl -M | grep --color security<\/code><\/pre>\n\n\n\n
security2_module (shared)<\/code><\/pre>\n\n\n\n
$ yum install mod_security<\/code><\/pre>\n\n\n\n
$ \/etc\/init.d\/httpd restart<\/code><\/pre>\n\n\n\n
$ yum info mod_security<\/code><\/pre>\n\n\n\n
Step 2: Enable the OWASP ModSecurity Core Rule Set<\/h2>\n\n\n\n
$ mv \/etc\/modsecurity\/modsecurity.conf-recommended modsecurity.conf<\/code><\/pre>\n\n\n\n
$ sudo apt install git<\/code><\/pre>\n\n\n\n
$ git clone https:\/\/github.com\/SpiderLabs\/owasp-modsecurity-crs.git<\/code><\/pre>\n\n\n\n
$ owasp-modsecurity-crs\n$ mv crs-setup.conf.example \/etc\/modsecurity\/crs-setup.conf\n$ mv rules\/ \/etc\/modsecurity\/<\/code><\/pre>\n\n\n\n
$ sudo nano \/etc\/apache2\/mods-available\/security2.conf<\/code><\/pre>\n\n\n\n
<IfModule security2_module>\n # Default Debian dir for modsecurity's persistent data\n SecDataDir \/var\/cache\/modsecurity\n\n # Include all the *.conf files in \/etc\/modsecurity.\n # Keeping your local configuration in that directory\n # will allow for an easy upgrade of THIS file and\n # make your life easier\n IncludeOptional \/etc\/modsecurity\/*.conf\n Include \/etc\/modsecurity\/rules\/*.conf\n<\/IfModule><\/code><\/pre>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n
Step 3: Test ModSecurity<\/h2>\n\n\n\n
$ sudo nano \/etc\/apache2\/sites-available\/000-default.conf<\/code><\/pre>\n\n\n\n
<VirtualHost *:80>\n ServerAdmin webmaster@localhost\n DocumentRoot \/var\/www\/html\n\n ErrorLog ${APACHE_LOG_DIR}\/error.log\n CustomLog ${APACHE_LOG_DIR}\/access.log combined\n\n SecRuleEngine On\n SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'Our test rule has triggered'"\n<\/VirtualHost><\/code><\/pre>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\n
$ curl localhost\/index.html?testparam=test<\/code><\/pre>\n\n\n\n
$ sudo tail -f \/var\/log\/apache2\/error.log<\/code><\/pre>\n\n\n\n
Step 4: Review the ModSecurity Configuration Settings<\/h2>\n\n\n\n
$ sudo nano \/etc\/modsecurity\/modsecurity.conf<\/code><\/pre>\n\n\n\n
SecRuleEngine DetectionOnly<\/code><\/pre>\n\n\n\n
SecRuleEngine On<\/code><\/pre>\n\n\n\n
SecResponseBodyAccess On<\/code><\/pre>\n\n\n\n
SecResponseBodyAccess Off<\/code><\/pre>\n\n\n\n
SecRequestBodyLimit 13107200<\/code><\/pre>\n\n\n\n
SecRequestBodyNoFilesLimit 131072<\/code><\/pre>\n\n\n\n
SecRequestBodyInMemoryLimit 131072<\/code><\/pre>\n\n\n\n
Step 5: Exclude Hosts and Directories<\/h2>\n\n\n\n
<IfModule security2_module>\n SecRuleEngine Off\n<\/IfModule><\/code><\/pre>\n\n\n\n
<Directory "\/var\/www\/website\/document_root\/wp-admin">\n <IfModule security2_module>\n SecRuleEngine Off\n <\/IfModule>\n<\/Directory><\/code><\/pre>\n\n\n\n
<LocationMatch "\/wp-admin\/update.php">\n <IfModule security2_module>\n SecRuleRemoveById 981173\n <\/IfModule>\n<\/LocationMatch><\/code><\/pre>\n\n\n\n