Apache Web Server Hardening
Apache Web Server Hardening
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
configuration 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.
1. This assumes you have installed Apache on UNIX platform. If not, you can go throughInstallation
guide. You can also refer very free video about how to Install Apache, MySQL & PHP.
2. We will call Apache installation directory /opt/apache as $Web_Server throughout this course.
3. You are advised to take a backup of existing configuration file before any modification.
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 configuration 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 verification. Lets do this by install firebug add-on in Firefox.
Open Firefox
Access https://addons.mozilla.org/en-US/firefox/addon/firebug/
We will use this icon to open firebug 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://geekflare.com/tools/tool.php?id=check-headers
www.seositecheckup.com
Implementation:
Go to $Web_Server/conf folder
Restart apache
ServerSignature will remove the version information from the page generated like 403, 404, 502, etc.
by apache web server.
ServerTokens will change Header to production only, i.e. Apache
Verification:
Open Firefox
Go to $Web_Server/htdocs directory
# touch hi
# touch hello
As you could see it reveals what all file/folders you have which is certainly you dont want to expose.
Implementation:
Go to $Web_Server/conf 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.
Verification:
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 fix for PCI compliance.
Implementation:
Go to $Web_Server/conf directory
Restart apache
Verification:
Check HTTP response headers in firebug, you should not see Etag at all.
Authorization
Go to $Web_Server/conf
Modify httpd.conf using vi
Search for User & Group Directive and change as non-privileged account apache
User apache
Group apache
Restart Apache
Verification:
grep for running http process and ensure its running with apache user
# ps ef |grep http
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.
Go to $Web_Server directory
Verification:
In default installation, users can override apache configuration using .htaccess. if you want to stop
users changing your apache server settings, you can add AllowOverride to None as shown below. This
must be done at root level.
Implementation:
Go to $Web_Server/conf directory
Go to $Web_Server/conf directory
Apache web server misconfiguration or not hardened properly can exploit web application. Its critical
to harden your web server configuration.
4.1 Cookies
4.1.1
Disable Trace HTTP Request
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 configuration.
#telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
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
Restart apache
Verification:
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 '^]'.
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 flag in
cookie. Without having HttpOnly and Secure, it is possible to steal or manipulate web application
session and cookies and its dangerous.
Implementation:
Go to $Web_Server/conf directory
Restart apache
Verification:
Go to $Web_Server/conf directory
Restart apache
Verification:
Check HTTP response headers in firebug, you should see X-Frame-Options as shown below.
Go to $Web_Server/conf directory
</Directory>
Restart Apache
Note: if you have multiple Directory directives in your environment, you should consider doing the
same for all.
Go to $Web_Server/conf directory
Restart Apache
Verification:
Check HTTP response headers in firebug, you should see XSS Protection is enabled and mode is
blocked.
Enable RewriteEngine directive as following and add Rewrite condition to allow only HTTP 1.1
RewriteEngine On
RewriteCond %{THE_REQUEST} !HTTP/1\.1$
RewriteRule .* - [F]
Go to $Web_Server/conf directory
SSL
Having SSL is additional layer of security you are adding into Web Application. However, default SSL
configuration leads to certain vulnerabilities and you should consider tweaking those
configurations. 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/
Outlook.com
Microsoft.com
Live.com
Skype.com
Apple.com
Yahoo.com
Bing.com
Hotmail.com
Twitter.com
Implementation:
You can use openssl to generate CSR with 2048 bit as below.
openssl req -out localhost.csr -new -newkey rsa:2048 -nodes -keyout localhost.key
Add Personal Cert, Signer Cert and Key file in httpd-ssl.conf file under below directive
Verification:
Execute sslscan utility with following parameter. Change localhost to your actual domain name.
As you can see current ssl key is 2048 bit, which is stronger.
As you could see above, in current configuration DHE, AES, EDH, RC4 cipher is accepted. Now if you
are performing penetration test or PCI compliance test, your report will say RC4 Cipher detected.
Lately, it was found that RC4 is weak cipher and to pass certain security test, you must not accept RC4
or any weak cipher. You should also ensure not to accept any cipher, which is less than 128 bits.
Implementation:
Go to $Web_Server/conf/extra folder
So now we dont see RC4 anymore as accepted Cipher. Its good to reject any low, medium, null or
vulnerable cipher to keep yourself tension free from getting attacked. You can also scan your domain
against Qualys SSL Labs to check if you have weak or vulnerable cipher in your environment.
Go to $Web_Server/conf/extra folder
Modify SSLProtocol directive in httpd-ssl.conf as below to accept only SSL v3 and TLS v1
SSLProtocol ALL +SSLv3 +TLSv1
Verification:
Lets use sslscan utility to validate as below command. Change localhost to your actual domain name.
sslscan no-failed localhost
As you could see above, accepted is only SSLv3 and TLSv1, which is safe from SSLv2 vulnerabilities.
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 afford commercial web application
firewall, 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 defined usage policy
Real-time Blacklist Lookups utilizes 3rd Party IP Reputation
Web-based Malware Detection identifies 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 files uploaded through the web
application.
Tracking Sensitive Data Tracks Credit Card usage and blocks leakages.
Trojan Protection Detecting access to Trojans horses.
Identification of Application Defects alerts on application misconfigurations.
Error Detection and Hiding Disguising error messages sent by the server.
apache 2.x or higher
libpcre package
libxml2 package
liblua package
libcurl package
Extract modsecurity-apache_2.7.5.tar.gz
# gunzip c modsecurity-apache_2.7.5.tar.gz | tar xvf
Once installation is done, you would see mod_security2.so in modules folder under /opt/apache
as shown below
Now this concludes, you have installed Mod Security module in existing Apache web server.
6.2 Configuration
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 identifier 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 configuration file
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so
Unzip core rule file, 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.
6.3.1
Logging
Logging is one of the first things to configure 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 flexibility to configure Audit, Debug or both logging. By default configuration will write both
logs. However, you can change based on your requirement. Log is controlled in SecDefaultAction
directive. Lets look at default logging configuration 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:
After restart, you should see modsec_audit.log getting generated as shown below.
6.3.2
By default Engine Rule is Off 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:
SecRuleEngine On
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
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 file. Ex /etc/passwd, .htaccess, etc.
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] #
Verification:
Check HTTP response headers in firebug, you should see Server banner is changed now as
shown below.
General Configuration
Configure Listen directive in httpd.conf with absolute IP and port as shown example below
Listen 10.10.10.1:80
To capture time taken to serve the request and SESSION ID in access log
Info Module The mod_info module can leak sensitive information using .htaccess once this module is
loaded. To disable comment following in httpd.conf
#LoadModule info_module modules/mod_info.so
http://httpd.apache.org/docs/2.4/
http://www.modsecurity.org/documentation/
https://www.owasp.org/index.php/Category:OWASP_ModSecurity_Core_Rule_Set_Project
Conclusion I hope this practical guide has helped you in securing your Apache Web Server. Let me
know your feedback in comment and dont forget to Share!