WebHostGear.com - the hosting resource for professionalshosting tutorials 
hosting howto webhost guide server managementAugust 29, 2008
server management, apache tutorials, hosting tutorials, cpanel, server security
Home / Hosting Tutorials / Server Security / How to install mod_security for Apache

How to install mod_security for Apache



Printer Friendly Printer Friendly Send to a friend Send to a friend
By : ramprage Rating : Average Rating : 6.65 From 34 Voter(s)

What is mod_security or modsecurity?
ModSecurity is an open source intrusion detection and prevention engine for web applications. It operates embedded into the web server, acting as a powerful umbrella - shielding applications from attacks. ModSecurity supports both branches of the Apache web server.

Rfx Networks Recommended:
"mod_security is great and I encourage it be used by everyone; it does have the potential to break some web applications but so far iv seen very few issues to say the least. Likewise it is easy to fix any applications that may break with the granular filter rules that can be setup to either deny or allow certain content. Overall mod_security is a needed addition to apache, providing a layer of security yet unseen for apache. I highly encourage you read the reference document on the modsecurity.org site (under documentation) to better understand each directive and the role it plays in protecting your server and sites."

Requirements:
Apache Web Server 1.3x or 2.x

Note: We have confirmed this security addon works with Cpanel based servers.

UPDATE: Sept. 15, 2004:
Changed # Prevent path traversal (..) attacks rules to fix a typo in tutorial.


Article provided by WebHostGear.com
How to install?
1.
Login to your server through SSH and su to the root user.

Article provided by WebHostGear.com

2. First your going to start out by grabbing the latest version of mod_security
wget http://www.modsecurity.org/download/mod_security-1.7.4.tar.gz

3. Next we untar the archive and cd into the directory:
tar zxvf mod_security-1.7.4.tar.gz
cd mod_security-1.7.4/

4. Now you need to determine which version of apache you use:
APACHE 1.3.x users
cd apache1/
APACHE 2.x users
cd apache2/

5. Lets Compile the module now:
/usr/local/apache/bin/apxs -cia mod_security.c

6. Ok, now its time to edit the httpd conf file. First we will make a backup just incase something goes wrong:
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.backup

7. Now that we have backed it all up, we can edit the httpd.conf. Replace pico with nano depending on what you have
pico /usr/local/apache/conf/httpd.conf

8. Lets look for something in the config, do this by holding control and pressing W and you are going to search for

<IfModule mod_dir.c> (altho any of the IfModules would work fine)

9. Now add this

<IfModule mod_security.c>
    # Turn the filtering engine On or Off
    SecFilterEngine On

    # Change Server: string
    SecServerSignature " "

    # Make sure that URL encoding is valid
    SecFilterCheckURLEncoding On

    # This setting should be set to On only if the Web site is
    # using the Unicode encoding. Otherwise it may interfere with
    # the normal Web site operation.
    SecFilterCheckUnicodeEncoding Off

    # Only allow bytes from this range
    SecFilterForceByteRange 1 255

    # The audit engine works independently and
    # can be turned On of Off on the per-server or
    # on the per-directory basis. "On" will log everything,
    # "DynamicOrRelevant" will log dynamic requests or violations,
    # and "RelevantOnly" will only log policy violations
    SecAuditEngine RelevantOnly

    # The name of the audit log file
    SecAuditLog /var/log/httpd/audit_log

    # Should mod_security inspect POST payloads
    SecFilterScanPOST On

    # Action to take by default
    SecFilterDefaultAction "deny,log,status:500"

    # Require HTTP_USER_AGENT and HTTP_HOST in all requests
    SecFilterSelective "HTTP_USER_AGENT|HTTP_HOST" "^$"

    # Prevent path traversal (..) attacks
    SecFilter "../"

    # Weaker XSS protection but allows common HTML tags
    SecFilter "<[[:space:]]*script"

    # Prevent XSS atacks (HTML/Javascript injection)
    SecFilter "<(.|n)+>"

    # Very crude filters to prevent SQL injection attacks
    SecFilter "delete[[:space:]]+from"
    SecFilter "insert[[:space:]]+into"
    SecFilter "select.+from"

    # Protecting from XSS attacks through the PHP session cookie
    SecFilterSelective ARG_PHPSESSID "!^[0-9a-z]*$"
    SecFilterSelective COOKIE_PHPSESSID "!^[0-9a-z]*$"
</IfModule>

10. Save the file Ctrl + X then Y

11. Restart Apache

/etc/rc.d/init.d/httpd stop
/etc/rc.d/init.d/httpd start

You've successfully installed mod_security!

Have some questions or input on this article? Discuss it forums thread specific to this article!

New! - Need server help? Hire an Expert

Get professional help with your configuration, script installation or server issue.
Learn how we can help you with any server problem and make your server run like new. Professional staff will contact you, after submitting a quote request, by phone or email.

Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent
Sources
Thanks for part of this article from: thelinuxguy on WHT

Related Articles


» Force (SSL) secure logins in WHM and Cpanel
» Disable Direct Root Logins
» Guide to Chkrootkit - checking for intruders
» Creating a Welcome message for SSH logins
» Securing Your /tmp Partition with Cpanel/WHM
» Common SSH Commands - Linux Shell Commands
» Mask Your Web Server for Enhanced Security
» Firewalls Explained - Part 1
» How to install BFD (Brute Force Detection)
» How to install APF (Advanced Policy Firewall)


Discuss this article with others in our new hosting forums

Comments / Feedback

max crandale
it will be the most popular thing on linux server i guess.... a ensim how to do is welcome for that
Sebas
Its works fine, by.... in the cpanel system the option "apache status" in the WHM dont work before this install
Thomas
Hi, I have followed the steps but when starting apache i am getting this error: mod_security: Failed to open the audit log file. PLease advise. T
Enigmatic
Unfortunetelly, mod_security breaks some applications as well as functionality of Front Page...
delara
Should we replace: DirectoryIndex index.html index.wml index.cgi index.shtml index.jsp index.js index.jp index.php4 ind$ with: BLAH BLAH
jane doe
confirmed
some application are breaks

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Steve
delara - no do not replace that line.
Diep
thank you so much .I also find this problem
mike
Im getting a problem:

mod_security: Failed to open the audit log file: /var/log/httpd/audit_log

What actions should I take?
Brandon
Mike,

try:
mkdir /var/log/httpd
touch /var/log/httpd/audit_log

-bb
http://www.nexgenis.com
vikykhung
Error 500 Interanl . .. . . sau khi restart Httpd.

How I fix this problems.
Mike
I noticed that the version is wrong above. Here is the new download link:

http://www.modsecurity.org/download/modsecurity-apache-1.9.2.tar.gz
Mike
http://www.modsecurity.org/download/modsecurity-apache-1.9.4.tar.gz

now
M.AC
thanks,

I have followed the steps with the new version but
how I know it is working or not ?
Ahmet AKBULUT

# Turn the filtering engine On or Off
SecFilterEngine On

# Change Server: string
SecServerSignature " "

# Make sure that URL encoding is valid
SecFilterCheckURLEncoding On

# This setting should be set to On only if the Web site is
# using the Unicode encoding. Otherwise it may interfere with
# the normal Web site operation.
SecFilterCheckUnicodeEncoding Off

# Only allow bytes from this range
SecFilterForceByteRange 1 255

# The audit engine works independently and
# can be turned On of Off on the per-server or
# on the per-directory basis. "On" will log everything,
# "DynamicOrRelevant" will log dynamic requests or violations,
# and "RelevantOnly" will only log policy violations
SecAuditEngine RelevantOnly

# The name of the audit log file
SecAuditLog /var/log/httpd/audit_log

# Should mod_security inspect POST payloads
SecFilterScanPOST On

# Action to take by default
SecFilterDefaultAction "deny,log,status:500"

# Require HTTP_USER_AGENT and HTTP_HOST in all requests
SecFilterSelective "HTTP_USER_AGENT|HTTP_HOST" "^$"

# Prevent path traversal (..) attacks
SecFilter "../"

# Weaker XSS protection but allows common HTML tags
SecFilter "<[[:space:]]*script"

# Prevent XSS atacks (HTML/Javascript injection)
SecFilter "<(.|n)+>"

# Very crude filters to prevent SQL injection attacks
SecFilter "delete[[:space:]]+from"
SecFilter "insert[[:space:]]+into"
SecFilter "select.+from"

# Protecting from XSS attacks through the PHP session cookie
SecFilterSelective ARG_PHPSESSID "!^[0-9a-z]*$"
SecFilterSelective COOKIE_PHPSESSID "!^[0-9a-z]*$"



added my apache.conf
im using debian 3.1
but all web pages don't show images why?
Saviola
I using mod_security with version 1.9.4 for apache 1.3.37 .
After I configed for mod_security , all website on my server go to down with error code 500 :

User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)
mod_security-message: Access denied with code 500. Pattern match "../" at REQUEST_URI [severity "EMERGENCY"]
mod_security-action: 500

HTTP/1.1 500 Internal Server Error
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
--8e5db40a--

I installed mod_security with default config .
OS RHEL 4 , Apache 1.3.37 , php 4.3.3 and mysql 4.1
red root
hey
there were aproplrm with me
there were .htaccess file make my mod_security on automatic when it upload in my serever
this file include this command

# Turn off mod_security filtering.
SecFilterEngine off

# The below probably isn't needed...
SecFilterScanPOST off

and we add in mod_security rulles

# Turn the filtering engine On or Off
SecFilterEngine On

# Change Server: string
SecServerSignature " "

# Make sure that URL encoding is valid
SecFilterCheckURLEncoding On

and when we add this no thing happened

we wait your response for that proplem
thanx
Luke
If you images do not display propperly, its becuase you have somthing leading back directorys.

ie: img src="../filename.jpg"

Just comment out the line

SecFilter "../"
to
#SecFilter "../"
bman
but the client can disable it by adding a .htaccess file and inside it he puts

SecFilterEngine Off
SecFilterScanPOST Off


is there a way to stop this with out disabling .htaccess ?
derak
This faq doesn't work for mod_security version 2.

 Add Comment
Name
Email
Image Code
Refresh Image

Comments / Feedback



Web Hosting News RSS ?


WebHostGear Hire an Expert - NEW!
Let us improve your servers performance, find that spammer and take care of that kernel upgrade. Hire us to help with any tutorials listed on the site or any other services needed. Get your free, NO obligation quote now

Our site offers free hosting tutorials, cpanel tutorial, web hosting news, shell commands, running a web hosting business, dedicated guides, linux tutorial, apache install, home web server, web server guide, ssh commands, dedicated servers, DNS nameservers, chkrootkit, apf firewall, exim configuration, server compromised, cron backup solution, ftp backup script

Server Tutorials


WebHostGear Reviewed by Ping Zine - Click here

Special Offer:


Links:
cPanel server administration

MidPhase Coupons

Reseller Hosting

Reseller Hosting FAQ

Icon

Web Hosting

Datacenter Discussion Forum

Lunarpages Coupon

Hosting Coupon



WebhostGear Sponsors
Going Up Advertise Hosting Free Uptime Check Web Hosting Chat Icons Banners Mall