Apache Webserver Hardening Security Guide
Apache Webserver Hardening Security Guide
Guide
Secure Apache Web Server Practical Guide
1
Introduction
The Web Server is a crucial part of web-based applications. Apache Web Server is often placed at the edge
of the network hence it becomes one of the most vulnerable services to attack. Having default
conguration supply many sensitive information which may help hacker to prepare for an attack the web
server.
The majority of web application attacks are through XSS, Info Leakage, Session Management and PHP
Injection attacks which is due to weak programming code and failure to sanitize web application
infrastructure. According to the security vendor Cenzic, 96% of tested applications have vulnerabilities.
Below chart from Cenzic shows the vulnerability trend report of 2013.
This practical guide provides you the necessary skill set to secure Apache Web Server. In this course, we
will talk about how to Harden & Secure Apache Web Server on Unix platform. Following are tested on
Apache 2.4.x and I dont see any reason it wont work with Apache 2.2.x.
2. This assumes you have installed Apache on UNIX platform. If not, you can go through Installation guide.
You can also refer very free video about how to Install Apache, MySQL & PHP.
4. We will call Apache installation directory /opt/apache as $Web_Server throughout this course.
6. You are advised to take a backup of existing conguration le before any modication.
1.1 Audience
This is designed for Middleware Administrator, Application Support, System Analyst, or anyone working or
eager to learn Hardening & Security guidelines. Fair knowledge of Apache Web Server & UNIX command is
mandatory. This is seven page guide, click on Next to proceed. You may navigate through table of
contents at right hand side.
Information Leakage
In default Apache conguration you would have many sensitive information disclosure, which can be used
to prepare for an attack. Its one of the most critical tasks for administrator to understand and secure
them. As per report by Cenzic, 16% of vulnerability is found in Info leakage. We require some tool to
examine HTTP Headers for verication. Lets do this by install rebug add-on in Firefox.
Open Firefox
Access https://addons.mozilla.org/en-US/refox/addon/rebug/
Click on Add to Firefox
We will use this icon to open rebug console to view HTTP Headers information. There are many online
tools also available which helps to check in HTTP header information. Below are some of them you can try
out. http://tools.geekare.com/seo/tool.php?id=check-headers
Order allow,deny
Allow from all
</Directory>
(or)
<Directory /opt/apache/htdocs>
Options -Indexes
Order allow,deny
Allow from all
</Directory>
Restart Apache
Note: if you have multiple Directory directives in your environment, you should consider doing the same
for all.
Verication:
Now, lets try to access Apache by http://localhost/test
As you could
see, it displays forbidden error instead showing test folder listing.
2.3 Etag
It allows remote attackers to obtain sensitive information like inode number, multipart MIME boundary and
child process through Etag header. To prevent this vulnerability, lets implement it as below. This is
required to x for PCI compliance.
Implementation:
Go to $Web_Server/conf directory
Add following directive and save the httpd.conf
FileETag None
Restart apache
Verication:
Open Firefox and access your application
Check HTTP response headers in rebug, you should not see Etag at all.
Authorization
Note: You could see one process is running with root. Thats because Apache is listening on port 80 and it
has to be started with root. We will talk about how to change port number later in this course.
Verication:
<Directory />
Options -Indexes
AllowOverride None
</Directory>
Save the httpd.conf
Restart Apache
Apache web server misconguration or not hardened properly can exploit web application. Its critical to
harden your web server conguration.
4.1 Cookies
4.1.1
By default Trace method is enabled in Apache web server. Having this enabled can allow Cross Site
Tracing attack and potentially giving an option to hacker to steal cookie information. Lets see how it looks
like in default conguration.
Do a telnet web server IP with listen port
Make a TRACE request as shown below
#telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
TRACE / HTTP/1.1 Host: test
HTTP/1.1 200 OK
Date: Sat, 31 Aug 2013 02:13:24 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: message/http 20
TRACE / HTTP/1.1
Host: test 0
Connection closed by foreign host.
#
As you could see in above TRACE request it has responded my query. Lets disable it and test it.
Implementation:
Go to $Web_Server/conf directory
Add following directive and save the httpd.conf
TraceEnable off
Restart apache
Verication:
Do a telnet web server IP with listen port and make a TRACE request as shown below
#telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
TRACE / HTTP/1.1 Host: test
HTTP/1.1 405 Method Not Allowed
Date: Sat, 31 Aug 2013 02:18:27 GMT
Server: Apache Allow:
Content-Length: 223
Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "//IETF//DTD HTML 2.0//EN"> <html><head> <title>405 Method Not Allowed</title>
</head><body> <h1>Method Not Allowed</h1>
<p>The requested method TRACE is not allowed for the URL /.</p> </body></html>
Connection closed by foreign host.
#
As you could see in above TRACE request it has blocked my request with HTTP 405 Method Not Allowed.
Now, this web server doesnt allow TRACE request and help in blocking Cross Site Tracing attack.
4.1.2
You can mitigate most of the common Cross Site Scripting attack using HttpOnly and Secure ag in cookie.
Without having HttpOnly and Secure, it is possible to steal or manipulate web application session and
cookies and its dangerous.
Implementation:
Ensure mod_headers.so is enabled in your httpd.conf
Go to $Web_Server/conf directory
Add following directive and save the httpd.conf
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
Restart apache
Verication:
Open Firefox and access your application
Check HTTP response headers in rebug, you should see Set-Cookie is agged with HttpOnly and
Secure as shown below.
Go to $Web_Server/conf directory
Open httpd.conf using vi and add following Header directive
Header set X-XSS-Protection 1; mode=block
Restart Apache
Verication:
Open Firefox and access your application
Check HTTP response headers in rebug, you should see XSS Protection is enabled and mode is
blocked.
SSL
Having SSL is additional layer of security you are adding into Web Application. However, default SSL
conguration leads to certain vulnerabilities and you should consider tweaking those congurations. We
require some tool to verify SSL settings. There are many available however, I would use SSL-Scan free tool.
You can download from http://sourceforge.net/projects/sslscan/
localhost.crt
Generate new CSR and private key
openssl req -out localhost.csr -new -newkey rsa:2048 -nodes -keyout
localhost.key
Add Personal Cert, Signer Cert and Key le in httpd-ssl.conf le under below directive
SSLCertificateFile # Personal Certificate
SSLCertificateKeyFile # Key File
SSLCACertificateFile # Signer Cert file
Verication:
Execute sslscan utility with following parameter. Change localhost to your actual domain name.
sslscan localhost | grep i key
As you can see current ssl key is 2048 bit, which is stronger.
Mod Security
Mod Security is an open-source Web Application Firewall, which you can use with Apache. It comes as a
module which you have to compile and install. If you cant aord commercial web application rewall, this
would be good choice to go for it. Mod Security says: In order to provide generic web applications
protection, the Core Rules use the following techniques:
HTTP Protection detecting violations of the HTTP protocol and a locally dened usage policy
Real-time Blacklist Lookups utilizes 3rd Party IP Reputation
Web-based Malware Detection identies malicious web content by check against the Google Safe
Browsing API.
HTTP Denial of Service Protections defense against HTTP Flooding and Slow HTTP DoS Attacks.
Common Web Attacks Protection detecting common web application security attack
Automation Detection Detecting bots, crawlers, scanners and other surface malicious activity
Integration with AV Scanning for File Uploads detects malicious les uploaded through the web
application.
Tracking Sensitive Data Tracks Credit Card usage and blocks leakages.
Trojan Protection Detecting access to Trojans horses.
Identication of Application Defects alerts on application miscongurations.
Error Detection and Hiding Disguising error messages sent by the server.
Extract modsecurity-apache_2.7.5.tar.gz
# gunzip c modsecurity-apache_2.7.5.tar.gz | tar xvf
Go to extracted folder modsecurity-apache_2.7.5
# cd modsecurity-apache_2.7.5
6.2 Conguration
In order to use Mod security feature with Apache, we have to load mod security module in httpd.conf.
mod_unique_id module is pre-requisite for Mod Security. This module provides an environment variable
with a unique identier for each request, which is tracked and used by Mod Security.
Add following line to load module for Mod Security in httpd.conf and save the conguration le
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so
Restart apache web server
Mod Security is now installed! Next thing you have to do is to install Mod Security core rule to take a full
advantage of its feature. Latest Core Rule can be downloaded from following link, which is free.
https://github.com/SpiderLabs/owasp-modsecurity-crs/zipball/master
Copy downloaded core rule zip to /opt/apache/conf folder
Unzip core rule le, you should see the extracted folder as shown below
You may wish to rename the folder to something short and easy to remember. In this example, I will
rename to crs.
Now, lets enable these rules to get it working with Apache web server.
Add following in httpd.conf
<IfModule security2_module>
Include conf/crs/modsecurity_crs_10_setup.conf
Include conf/crs/base_rules/*.conf
</IfModule>
In above conguration, we are loading Mod Security main conguration le
modsecurity_crs_10_setup.conf and base rules base_rules/*.conf provided by Mod Security Core
Rules to protect web applications.
Restart apache web server
You have successfully congured Mod Security with Apache! Well done. Now, Apache Web server is
protected with Mod Security web application rewall.
6.3.1
Logging
Logging is one of the rst things to congure so you can have logs created for what Mod Security is doing.
There are two types of logging available; Debug & Audit log.
Debug Log: this is to duplicate the Apache error, warning and notice messages from the error log.
Audit Log: this is to write the transaction logs that are marked by Mod Security rule Mod Security gives
you exibility to congure Audit, Debug or both logging. By default conguration will write both logs.
However, you can change based on your requirement. Log is controlled in SecDefaultAction directive. Lets
look at default logging conguration in setup.conf
SecDefaultAction phase:1,deny,log
To log Debug, Audit log use log To log only audit log use nolog,auditlog To log only debug log use
log,noauditlog You can specify the Audit Log location to be stored which is controlled by SecAuditLog
directive. Lets write audit log into /opt/apache/logs/modsec_audit.log by adding as shown below.
Implementation:
Add SecAuditLog directive in setup.conf and restart Apache Web Server
SecAuditLog /opt/apache/logs/modsec_audit.log
After restart, you should see modsec_audit.log getting generated as shown below.
6.3.2
By default Engine Rule is O that means if you dont enable Rule Engine you are not utilizing all the
advantages of Mod Security. Rule Engine enabling or disabling is controlled by SecRuleEngine directive.
Implementation:
Add SecRuleEngine directive in setup.conf and restart Apache Web Server
SecRuleEngine On
There are three values for SecRuleEngine:
On to enable Rule Engine
O to disable Rule Engine
DetectionOnly enable Rule Engine but never executes any actions like block, deny, drop, allow,
proxy or redirect
Once Rule Engine is on Mod Security is ready to protect with some of the common attack types.
6.3.3
Now web server is ready to protect with common attack types like XSS, SQL Injection, Protocol Violation,
etc. as we have installed Core Rule and turned on Rule Engine. Lets test few of them.
XSS Attack:Open Firefox and access your application and put <script> tag at the end or URL as shown below
Monitor the modsec_audit.log in apache/logs folder
As you can see Mod Security blocks request as it contains <script> tag which is the root of XSS attack.
Directory Traversal Attack:- Directory traversal attacks can create lot of damage by taking advantage
of this vulnerabilities and access system related le. Ex /etc/passwd, .htaccess, etc.
Open Firefox and access your application with directory traversal
Monitor the modsec_audit.log in apache/logs folder
http://localhost/?../.../boot
As you can see Mod Security blocks request as it contains directory traversal.
6.3.4
Earlier in this guide, you learnt how to remove Apache and OS type, version help of ServerTokens
directive. Lets go one step ahead, how about keeping server name whatever you wish? Its possible with
SecServerSignature directive in Mod Security. You see its an interesting.
Note: in order to use Mod Security to manipulate Server Banner from header, you must set ServerTokesn
to Full in httpd.conf of Apache web server.
Implementation:
Add SecServerSignature directive with your desired server name in setup.conf and restart Apache Web
Server
SecServerSignature YourServerName
Ex:
[/opt/apache/conf/crs] #grep SecServer modsecurity_crs_10_setup.conf
SecServerSignature chandank.com
[/opt/apache/conf/crs] #
Verication:
General Conguration