Ap Ache Web Ser Ver Har Dening Ap Ache Web Ser Ver Har Dening & Secur Ity Guide & Secur Ity Guide
Ap Ache Web Ser Ver Har Dening Ap Ache Web Ser Ver Har Dening & Secur Ity Guide & Secur Ity Guide
1 . I n t rodu ct ion
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
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 are due to weak
programming code and failure to sanitize web application infrastructure.
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 don’t see any reason it won’t work with Apache 2.2.x.
1. This assumes you have installed Apache on UNIX platform. If not, you
can go through Installation guide. You can also refer very free video
any modification.
Contents
1. Introduction
1.1 Audience
2. Information Leakage
2.1 Remove Server Version Banner
2.3 Etag
3. Authorization
4.1 Cookies
5. SSL
5.1 SSL Key
5.2 SSL Cipher
1.1 Audience
2 . I n f orma t ion L ea k a ge
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.
I would say this is one of the first things to consider, as you don’t want to
expose what web server version you are using. Exposing version means you
Go to $Web_Server/conf folder
ServerTokens Prod
ServerSignature Off
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
Activate firebug by clicking firebug icon at top right side
Click on Net tab
Expand the GET request and you could see Server directive is just
showing Apache, which is much better than exposing version and OS
type.
Disable directory listing in a browser so the visitor doesn’t see what all file
and folders you have under root or subdirectory. Let’s test how does it look
like in default settings.
Go to $Web_Server/htdocs directory
Create a folder and few files inside that
# mkdir test
# touch hi
# touch hello
Implementation:
Go to $Web_Server/conf directory
<Directory /opt/apache/htdocs>
Options None
Order allow,deny
Allow from all
</Directory>
(or)
<Directory /opt/apache/htdocs>
Options -Indexes
Order allow,deny
Allow from all
</Directory>
Restart Apache
Verification:
2.3 Etag
PCI compliance.
Implementation:
Go to $Web_Server/conf directory
FileETag None
Restart apache
Verification:
Implementation:
#groupadd apache
# useradd –G apache apache
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
Verification:
grep for running http process and ensure it’s running with apache user
Note: You could see one process is running with root. That’s because Apache
is listening on port 80 and it has to be started with root. We will talk about
By default, permission for binary and configuration is 755 that mean any user
on a server can view the configuration. You can disallow another user to get
Implementation:
Go to $Web_Server directory
Verification:
.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
Implementation:
Go to $Web_Server/conf directory
<Directory />
Options -Indexes
AllowOverride None
</Directory>
HTTP 1.1 protocol support many request methods which may not be required
and some of them are having potential risk. Typically you may just need GET,
Implementation:
Go to $Web_Server/conf directory
4.1 Cookies
enabled can allow Cross Site Tracing attack and potentially giving an option
to a hacker to steal cookie information. Let’s see how it looks like in default
configuration.
Do a telnet web server IP with listening port
#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. Let’s
Implementation:
Go to $Web_Server/conf directory
TraceEnable off
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 '^]'.
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 PUBLI
C "-//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> </b
ody></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 doesn’t allow TRACE
You can mitigate most of the common Cross Site Scripting attack using
HttpOnly and Secure flag in a cookie. Without having HttpOnly and Secure, it
is possible to steal or manipulate web application session and cookies and
it’s dangerous.
Implementation:
Ensure mod_headers.so is enabled in your httpd.conf
Go to $Web_Server/conf directory
Restart apache
Verification:
Implementation:
Go to $Web_Server/conf directory
Add the following directive and save the httpd.conf
Verification:
Server Side Include (SSI) has a risk of increasing the load on the server. If you
have shared the environment and heavy traffic web applications you should
Implementation:
Go to $Web_Server/conf directory
Restart Apache
Cross Site Scripting (XSS) protection can be bypassed in many browsers. You
can apply this protection for a web application if it was disabled by the user.
Implementation:
Go to $Web_Server/conf directory
Open httpd.conf using vi and add following Header directive
Restart Apache
Verification:
Check HTTP response headers in firebug, you should see XSS Protection
is enabled and a mode is blocked.
4.5 Disable HT T P 1.0 Pr otocol
we use older HTTP version of the protocol, let’s disable them as well? HTTP
1.0 has security weakness related to session hijacking. We can disable this by
Implementation:
RewriteEngine On
RewriteCond %{THE_REQUEST} !HTTP/1.1$
RewriteRule .* - [F]
Slow Loris attack and DoS. To mitigate this you can lower the timeout value
to maybe 60 seconds.
Implementation:
Go to $Web_Server/conf directory
Timeout 60
5. SSL
Having SSL is an additional layer of security you are adding into Web
Application. However, default SSL configuration leads to certain
http://sourceforge.net/projects/sslscan/
Breaching SSL key is hard, but not impossible. It’s just matter of
the higher key length you have, the more complex it becomes to break SSL
key. The majority of giant Web Companies use 2048 bit key, as below so why
don’t we?
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 -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.
key -out localhost.crt
openssl req -out localhost.csr -new -newkey rsa:2048 -nodes -keyout loc
alhost.key
Add Personal Cert, Signer Cert and Key file in httpd-ssl.conf file under
below directive
Verification:
Execute sslscan utility with the 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.
plain text into secret ciphered codes. It’s based on your web server SSL
Cipher configuration the data encryption will take place. So it’s important to
configure SSL Cipher, which is stronger and not vulnerable. Let’s validate the
test, your report will say RC4 Cipher detected. Lately, it was found that RC4 is
a 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
Implementation:
Go to $Web_Server/conf/extra folder
Modify SSLCipherSuite directive in httpd-ssl.conf as below to reject RC4
SSLCipherSuite HIGH:!MEDIUM:!aNULL:!MD5:!RC4
Note: if you have many weak ciphers in your SSL auditing report, you can
So now we don’t
see RC4 anymore as accepted Cipher. It’s 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.
5.3 Disable SSL v 2
SSL v2 has many security flaws and if you are working towards penetration
test or PCI compliance then you are expected to close security finding to
disable SSL v2. Any SSL v2 communication may be vulnerable to a Man-in-
The-Middle attack that could allow data tampering or disclosure. Let’s
implement apache web server to accept only latest SSL v3 and reject SSL v2
connection request.
Implementation:
Go to $Web_Server/conf/extra folder
Modify SSLProtocol directive in httpd-ssl.conf as below to accept only
Verification:
above, accepted is only SSLv3 and TLSv1, which is safe from SSLv2
vulnerabilities.
6 . M od Secu rit y
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 can’t afford commercial web application firewall, this would be a good
choice to go for it. Mod Security says: In order to provide generic web
server.
Following prerequisites must be installed on the server where you wish to use
Mod Security with Apache. If any one of these doesn’t exist then Mod
Security compilation will fail. You may use yum install on Linux or Centos to
install these packages.
liblua package
libcurl package
libapr and libapr-util package
Extract modsecurity-apache_2.7.5.tar.gz
# cd modsecurity-apache_2.7.5
# ./configure –with-apxs=/opt/apache/bin/apxs
# make
#make install
Now this concludes, you have installed Mod Security module in existing
Apache web server.
In order to use Mod security feature with Apache, we have to load mod
Add following a line to load module for Mod Security in httpd.conf and
save the configuration file
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 a link, which is free.
https://github.com/SpiderLabs/owasp-modsecurity-crs/zipball/master
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.
Go to crs folder and rename
modsecurity_crs10_setup.conf.example to
modsecurity_crs10_setup.conf
Now, let’s enable these rules to get it working with Apache web server.
<IfModule security2_module>
Include conf/crs/modsecurity_crs_10_setup.conf
Include conf/crs/base_rules/*.conf
</IfModule>
You have successfully configured Mod Security with Apache! Well done .
/opt/apache/conf/crs/modsecurity_crs_10_setup.conf as setup.conf in
this section for example purpose. It’s important to understand what are the
OWASP rules are provided in free. There are two types of rules provided by
OWASP.
Base Rules – these rules are heavily tested and probably false alarm ratio is
less.
Experimental Rules – these rules are for an experimental purpose and you
may have the high false alarm. It’s important to configure, test and
Optional Rules – these optional rules may not be suitable for the entire
environment. Based on your requirement you may use them. If you are
looking for CSRF, User tracking, Session hijacking, etc. protection then you
may consider using optional rules. We have the base, optional and
experimental rules after extracting the downloaded crs zip file from OWASP
download page. These rules configuration file is available in crs/base_rules,
crs/optional_rules and crs/experimental_rules folder. Let’s get familiar with
some of the base rules.
header.
modsecurity_crs_23_request_limits.conf: This rule has the
dependency on application specific like request size, upload size, a
length of a parameter, etc.
signature.
modsecurity_crs_47_common_exceptions.conf: This is used as an
exception mechanism to remove common false positives that may be
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 the flexibility to configure Audit, Debug
or both logging. By default configuration will write both logs. However, you
SecDefaultAction “phase:1,deny,log”
To log Debug, Audit log – use “log” To log only audit log – use
Implementation:
SecAuditLog /opt/apache/logs/modsec_audit.log
By default Engine Rule is Off that means if you don’t 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
Once Rule Engine is on – Mod Security is ready to protect with some of the
common attack types.
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. Let’s test few of them.
XSS Attack:-
Open Firefox and access your application and put <script> tag at the
http://localhost/?../.../boot
As you can see Mod Security blocks request as it contains directory
traversal.
Earlier in this guide, you learned how to remove Apache and OS type, version
help of ServerTokens directive. Let’s go one step ahead, how about keeping
server name whatever you wish? It’s possible with SecServerSignature
header, you must set ServerTokesn to Full in httpd.conf of Apache web server.
Implementation:
Ex:
Verification:
When you have multiple interface and IP’s on a single server, it’s
recommended to have Listen directive configured with absolute IP and Port
number. When you leave apache configuration to Listen on all IP’s with some
port number, it may create the problem in forwarding HTTP request to some
other web server. This is quite common in the shared environment.
Implementation:
Listen 10.10.10.1:80
7 .2 Access Logging
It’s essential to configure access log properly in your web server. Some of the
important parameter to capture in the log would be the time taken to serve
Implementation:
Web Server.
If you have compiled and installed with all modules then there are high
chances you will have many modules loaded in Apache, which may not be
required. Best practice is to configure Apache with required modules in your
web applications. Following modules are having security concerns and you
might be interested in disabling in httpd.conf of Apache Web Server.
WebDAV (Web-based Distributed Authoring and Versioning) This module
allows remote clients to manipulate files on the server and subject to various
Info Module The mod_info module can leak sensitive information using
http://httpd.apache.org/docs/2.4/
http://www.modsecurity.org/documentation/
https://www.owasp.org/index.php/Category:OWASP_ModSecurity_Core_Rule_Set_Pr
So that was some of the best practices you can use to secure your Apache
Random thoughts!