Secure Apache Web Server - Practical Guide: Chandan Kumar
Secure Apache Web Server - Practical Guide: Chandan Kumar
Chandan Kumar
http://geekflare.com
Contents
1
Introduction ............................................................................................................................... 3
1.1 Audience .................................................................................................................................... 3
Information Leakage.............................................................................................................. 4
2.1 Remove Server Version Banner ...................................................................................... 5
2.2 Disable directory browser listing ................................................................................... 6
2.3 Etag ............................................................................................................................................. 7
Authorization ............................................................................................................................ 8
3.1 Run Apache from non-privileged account .................................................................. 8
3.2 Protect binary and configuration directory permission ....................................... 9
3.3 System Settings Protection ............................................................................................... 9
3.4 HTTP Request Methods ...................................................................................................... 9
4.1.2
SSL ............................................................................................................................................... 15
5.1 SSL Key .................................................................................................................................... 15
5.2 SSL Cipher ............................................................................................................................. 16
5.3 Disable SSL v2 ...................................................................................................................... 18
Mod Security............................................................................................................................ 19
6.1 Download & Installation .................................................................................................. 20
6.2 Configuration ....................................................................................................................... 21
6.3 Getting Started ..................................................................................................................... 22
6.3.1
Logging ...................................................................................................................... 23
6.3.2
6.3.3
6.3.4
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.
2.
3.
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.
We will call Apache installation directory /opt/apache as $Web_Server throughout this course.
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/
Click on Add to Firefox
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.
1.
http://geekflare.com/tools/tool.php?id=check-headers
2.
3.
www.seositecheckup.com
www.apikitchen.com
Implementation:
Go to $Web_Server/conf folder
Modify httpd.conf by using vi editor
Add following directive and save the httpd.conf
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
Go to $Web_Server/htdocs directory
Create a folder and few files inside that
# mkdir test
# touch hi
# touch hello
Now, lets try to access Apache by http://localhost/test
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
Open httpd.conf using vi
Search for Directory and change Options directive to None or Indexes
<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
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
Add following directive and save the httpd.conf
FileETag None
Restart apache
Verification:
Authorization
#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 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
Change permission of bin and conf folder
Go to $Web_Server/conf directory
Open httpd.conf using vi
Search for Directory at root level
<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, HEAD, POST request methods in web application, which can be configured in
respective Directory directive. Default apache configuration support OPTIONS, GET, HEAD, POST, PUT, DELETE,
TRACE, CONNECT method in HTTP 1.1 protocol.
Implementation:
Go to $Web_Server/conf directory
Open httpd.conf using vi
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
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.
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
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 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 flag in cookie. Without
having HttpOnly and Secure, it is possible to steal or manipulate web application session and cookies and its dangerous.
Implementation:
Restart apache
Verification:
Restart apache
Verification:
Go to $Web_Server/conf directory
Open httpd.conf using vi
Search for Directory and add Includes in Options directive
<Directory /opt/apache/htdocs>
Options Indexes -Includes
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.
Go to $Web_Server/conf directory
Open httpd.conf using vi and add following Header directive
Restart Apache
Verification:
RewriteEngine On
RewriteCond %{THE_REQUEST} !HTTP/1\.1$
RewriteRule .* - [F]
Go to $Web_Server/conf directory
Open httpd.conf using vi
Add following in httpd.conf
Timeout 60
You may also go through Web Application Defender's Cookbook: Battling Hackers and Protecting Users--Free
Sample Chapter
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.
Generate self-signed certificate
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 localhost.key
Add Personal Cert, Signer Cert and Key file in httpd-ssl.conf file under below directive
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
Modify SSLCipherSuite directive in httpd-ssl.conf as below to reject RC4
SSLCipherSuite HIGH:!MEDIUM:!aNULL:!MD5:!RC4
Note: if you have many weak cipher in your SSL auditing report, you can easily reject them adding ! at beginning. For
ex to reject RC4: !RC4
Verification:
Again, we will use sslscan utility to validate as below command. Change localhost to your actual domain name.
sslscan no-failed localhost
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
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.
Now, lets download the latest stable version of Mod Security 2.7.5 from http://www.modsecurity.org/download/
Extract modsecurity-apache_2.7.5.tar.gz
# cd modsecurity-apache_2.7.5
# ./configure with-apxs=/opt/apache/bin/apxs
# make
#make install
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
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
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.
<IfModule security2_module>
Include conf/crs/modsecurity_crs_10_setup.conf
Include conf/crs/base_rules/*.conf
</IfModule>
In above configuration, we are loading Mod Security main configuration file modsecurity_crs_10_setup.conf and
base rules base_rules/*.conf provided by Mod Security Core Rules to protect web applications.
You have successfully configured Mod Security with Apache! Well done. Now, Apache Web server is protected with
Mod Security web application firewall.
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:
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 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
There are three values for SecRuleEngine:
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
http://localhost/?<script>xss attack</script>
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.
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 geekflare.com
[/opt/apache/conf/crs] #
Verification:
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
Add %T & %sessionID in httpd.conf under LogFormat directive
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.