Home » Categories » Multiple Categories |
How To Set Up mod_security with Apache on Debian/Ubuntu |
Article Number: 188 | Rating: Unrated | Last Updated: Sun, Jan 5, 2014 at 8:21 PM
|
PreludeMod security is a free Web Application Firewall (WAF) that works with Apache, Nginx and IIS. It supports a flexible rule engine to perform simple and complex operations and comes with a Core Rule Set (CRS) which has rules for SQL injection, cross site scripting, Trojans, bad user agents, session hijacking and a lot of other exploits. For Apache, it is an additional module which makes it easy to install and configure.In order to complete this tutorial, you will need LAMP installed on your server. Installing mod_securityModsecurity is available in the Debian/Ubuntu repository:
Verify if the mod_security module was loaded.
You should see a module named Modsecurity's installation includes a recommended configuration file which has to be renamed:
Reload Apache
You'll find a new log file for mod_security in the Apache log directory:
Configuring mod_securityOut of the box, modsecurity doesn't do anything as it needs rules to work. The default configuration file is set to DetectionOnly which logs requests according to rule matches and doesn't block anything. This can be changed by editing themodsecurity.conf file:
Find this line
and change it to:
If you're trying this out on a production server, change this directive only after testing all your rules. Another directive to modify is Find this
and change it to:
Now we'll limit the maximum data that can be posted to your web application. Two directives configure these:
The The value mentioned in the configuration file is
which is 12.5MB. Similar to this is the The value in the configuration file is
which is 128KB. Along the lines of these directives is another one which affects server performance:
This is the value (128KB) specified in the configuration file. Testing SQL InjectionBefore going ahead with configuring rules, we will create a PHP script which is vulnerable to SQL injection and try it out. Please note that this is just a basic PHP login script with no session handling. Be sure to change the MySQL password in the script below so that it will connect to the database:
This script will display a login form. Entering the right credentials will display a message "A Secret for you." We need credentials in the database. Create a MySQL database and a table, then insert usernames and passwords.
This will take you to the
Open your browser, navigate to
You'll see a message that indicates successful login. Now come back and enter a wrong pair of credentials-- you'll see the message Invalid username or password. We can confirm that the script works right. The next job is to try our hand with SQL injection to bypass the login page. Enter the following for the username field:
Note that there should be a space after Voila! The script shows the message meant for authenticated users. Setting Up RulesTo make your life easier, there are a lot of rules which are already installed along with mod_security. These are called CRS (Core Rule Set) and are located in
The documentation is available at
To load these rules, we need to tell Apache to look into these directories. Edit the
Add the following directives inside
The
Symlinks must be created inside the
Apache has to be reloaded for the rules to take effect.
Now open the login page we created earlier and try using the SQL injection query on the username field. If you had changed the Writing Your Own mod_security RulesIn this section, we'll create a rule chain which blocks the request if certain "spammy" words are entered in a HTML form. First, we'll create a PHP script which gets the input from a textbox and displays it back to the user.
Custom rules can be added to any of the configuration files or placed in modsecurity directories. We'll place our rules in a separate new file.
Add the following to this file:
Save the file and reload Apache. Open You'll either see a 403 page and a log entry or only a log entry based on
Here we used the chain action to match variables REQUEST_FILENAME with form.php, REQUEST_METHOD with POST and REQUEST_BODY with the regular expression (@rx) string (pills|insurance|rolex). The ?i: does a case insensitive match. On a successful match of all these three rules, the ACTION is to deny and log with the msg "Spam detected." The chain action simulates the logical AND to match all the three rules. Excluding Hosts and DirectoriesSometimes it makes sense to exclude a particular directory or a domain name if it is running an application like phpMyAdmin as modsecurity and will block SQL queries. It is also better to exclude admin backends of CMS applications like WordPress.To disable modsecurity for a complete VirtualHost place the following
inside the For a particular directory:
If you don't want to completely disable modsecurity, use the
|
Attachments
There are no attachments for this article.
|
How To Launch Your Site on a New Ubuntu 12.04 Server with LAMP, SFTP, and DNS
Viewed 2561 times since Thu, Dec 26, 2013
How To Create and Manage Databases in MySQL and MariaDB on a Cloud Server
Viewed 2814 times since Thu, Dec 26, 2013
How To Setup Ruby on Rails with Postgres
Viewed 11564 times since Mon, Dec 30, 2013
How to Add a Swap File on an Arch Linux Cloud Server
Viewed 2631 times since Fri, Dec 27, 2013
How To Protect SSH with fail2ban on Debian 7
Viewed 2666 times since Fri, Dec 27, 2013
Installing the Cacti Server Monitor on Ubuntu 12.04 Cloud Server
Viewed 3143 times since Sat, Jan 4, 2014
Initial Server Setup with Debian 7
Viewed 3676 times since Fri, Dec 27, 2013
Intermediate Sed: Manipulating Streams of Text in a Linux Environment
Viewed 9994 times since Fri, Dec 27, 2013
How To Set Up Master Slave Replication in MySQL
Viewed 3330 times since Thu, Dec 26, 2013
A Comparison of (Rack) Web Servers for Ruby Web Applications
Viewed 8248 times since Mon, Dec 30, 2013
|