Apache Security Guide
Apache Security Guide
1.1 Notes
We require some tool to examine HTTP Headers for some of the implementation verification. There
are two ways to do this.
1. Use browser inbuilt developer tools to inspect the HTTP headers. Usually, it’s under Network
tab
2. Use online HTTP response header checker tool
2. Information Leakage
In default Apache configuration you would have much sensitive information disclosures, which can be
used to prepare for an attack.
It’s one of the most critical tasks for an administrator to understand and secure them.
The default configuration will expose Apache Version and OS type as shown below.
Implementation
Go to $Web_Server/conf folder
Modify httpd.conf by using the vi editor
Add the following directive and save the httpd.conf
ServerTokens Prod
ServerSignature Off
Restart apache
ServerSignature will remove the version information from the page generated by apache web
server.
Verification
Go to $Web_Server/htdocs directory
Create a folder and few files inside that
# mkdir test
# touch hi
# touch hello
Now, let’s try to access Apache by http://localhost/test
As you could see it reveals what all file/folders you have which are probably you don’t 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 -Indexes
</Directory>
(or)
<Directory /opt/apache/htdocs>
Options None
</Directory>
Restart Apache
Note: if you have multiple Directory directives in your environment, you should consider doing the
same for all.
Verification
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, let’s implement it as below. This is required to fix for PCI compliance.
Implementation
Go to $Web_Server/conf directory
Add the following directive and save the httpd.conf
FileETag None
Restart apache
3. Authorization
The idea here is to protect other services running in case of any security hole.
Implementation
Implementation
Implementation
Go to $Web_Server/conf directory
Open httpd.conf using vi
Search for Directory at the root level
<Directory /> Options -Indexes AllowOverride None
</Directory>
Save the httpd.conf
Restart Apache
Typically you may just need GET, HEAD, POST request methods in a web application, which can be
configured in the respective Directory directive.
Default apache configuration support OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
method in HTTP 1.1 protocol.
Implementation
4.1 Cookies
Having this 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.
Implementation
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: 223Content-Type: text/html; charset=
<p>The requested method TRACE is not allowed for the URL /.</p> </body></
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 request and help in blocking Cross Site Tracing attack.
Implementation
Implementation:
SSI attack allows the exploitation of a web application by injecting scripts in HTML pages or executing
codes remotely.
Implementation
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,denyAllow from all
</Directory>
Restart Apache
Note: if you have multiple Directory directives in your environment, you should consider doing the
same for all.
Implementation
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
Verification
HTTP 1.0 has a security weakness related to session hijacking. We can disable this by using the
mod_rewrite module.
Implementation
Implementation
Go to $Web_Server/conf directory
Open httpd.conf using vi
Add the following in httpd.conf
Timeout 60
5. SSL
Having SSL is an additional layer of security you are adding into Web Application. However, the
default SSL configuration leads to certain vulnerabilities and you should consider tweaking those
configurations.
As you might know, using a 2009-era PC cracking away for around 73 days you can reverse engineer
a 512-bit key.
So the higher key length you have, the more complicated 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 -out geekflare.csr -newkey rsa:2048 -nodes -keyout geekflare
It will generate a CSR which you will need to send to a certificate authority to sign it. Once you receive
the signed certificate file, you can add them in httpd-ssl.conf file
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.
Implementation:
Go to $Web_Server/conf/extra folder
Modify SSLCipherSuite directive in httpd-ssl.conf as below to accept only higher
encryption algorithms
SSLCipherSuite HIGH:!MEDIUM:!aNULL:!MD5:!RC4
Save the configuration file and restart apache server
Note: if you have many weak ciphers in your SSL auditing report, you can quickly reject them adding
at the ! beginning .
Any SSL v2/v3 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 TLS and reject SSL v2/v3 connection request.
Implementation
Go to $Web_Server/conf/extra folder
Modify SSLProtocol directive in httpd-ssl.conf as below to accept only TLS 1.0+
SSLProtocol –ALL +TLSv1 +TLSv1.1 +TLSv1.2
Once you are done with SSL configuration, it’s a good idea to test your web application with online
SSL/TLS Certificate tool to find any configuration error.
6. 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 can’t afford a commercial web
application firewall, this would be a 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 another malicious surface
activity
Integration with AV Scanning for File Uploads – identifies 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.
6.2 Configuration
In order to use Mod security feature with Apache, we have to load mod security module in
httpd.conf. The mod_unique_id module is a 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 a 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
Restart apache web server
Mod Security is now installed!
Next thing you have to do is to install Mod Security core rule to take full advantage of its feature.
Well done. Now, Apache Web server is protected by Mod Security web application firewall.
It’s important to understand what are the OWASP rules are provided for 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 implement in UAT before using these in a production
environment.
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.
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 can change based on your requirement. The log is controlled in
SecDefaultAction directive. Let’s 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.
Implementation
Implementation
XSS Attack:-
Open Firefox and access your application and put a <script> tag at the end of URL
Monitor the modsec_audit.log in apache/logs folder
You will notice Mod Security blocks request as it contains <script> tag which is the root of XSS attack.
Directory Traversal Attack:- Directory traversal attacks can create a lot of damage by taking
advantage of this vulnerabilities and access system related file. Ex – /etc/passwd, .htaccess, etc.
Let’s go one step ahead, how about keeping server name whatever you wish? It’s possible with
SecServerSignature directive in Mod Security. You see it’s interesting.
Note: to use Mod Security to manipulate Server Banner from a 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:
7. General Configuration
We will talk about some of the general configuration as best practice.
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
Configure Listen directive in httpd.conf with absolute IP and port as the shown example
below
Listen 10.10.10.1:80
By default, Apache is not configured to capture these data. You got to configure them manually as
follows.
Implementation
To capture time taken to serve the request and SESSION ID in an access log
Add %T & %sessionID in httpd.conf under LogFormat directive
LogFormat "%h %l %u %t "%{sessionID}C" "%r" %>s %b %T" common
You can refer http://httpd.apache.org/docs/2.2/mod/mod_log_config.html for a complete list of
parameter supported in LogFormat directive in Apache Web Server.
Best practice is to configure Apache with required modules in your web applications. Following
modules have security concerns, and you might be interested in disabling in httpd.conf of Apache
Web Server.
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
So that was some of the best practices you can use to secure your Apache web server.
If you are new to Apache HTTP, then I would recommend taking Apache HTTP administration course.
ABOUT THE AUTHOR
Hey,
My name is Chandan and I hope this guide helps you in securing Apache HTTP
server from common online vulnerabilities.
Let's be in touch.