278N/A<?
xml version="1.0" encoding="UTF-8" ?>
278N/A<!-- $LastChangedRevision$ --> 278N/A Copyright 2002-2004 The Apache Software Foundation 278N/A Licensed under the Apache License, Version 2.0 (the "License"); 278N/A you may not use this file except in compliance with the License. 278N/A You may obtain a copy of the License at 278N/A Unless required by applicable law or agreed to in writing, software 278N/A distributed under the License is distributed on an "AS IS" BASIS, 278N/A WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 278N/A See the License for the specific language governing permissions and 278N/A limitations under the License. 278N/A <
parentdocument href="./">Miscellaneous Documentation</
parentdocument>
278N/A <
title>Security Tips</
title>
278N/A <
p>Some hints and tips on security issues in setting up a web server.
278N/A Some of the suggestions will be general, others specific to Apache.</
p>
278N/A <
section id="uptodate"><
title>Keep up to Date</
title>
278N/A <
p>The Apache HTTP Server has a good record for security and a
278N/A developer community highly concerned about security issues. But
278N/A it is inevitable that some problems -- small or large -- will be
278N/A discovered in software after it is released. For this reason, it
278N/A is crucial to keep aware of updates to the software. If you have
278N/A obtained your version of the HTTP Server directly from Apache, we
278N/A highly recommend you subscribe to the <
a 278N/A HTTP Server Announcements List</
a> where you can keep informed of
278N/A new releases and security updates. Similar services are available
278N/A from most third-party distributors of Apache software.</
p>
278N/A <
p>Of course, most times that a web server is compromised, it is
278N/A not because of problems in the HTTP Server code. Rather, it comes
278N/A from problems in add-on code, CGI scripts, or the underlying
278N/A Operating System. You must therefore stay aware of problems and
278N/A updates with all the software on your system.</
p>
278N/A <
section id="serverroot">
278N/A <
title>Permissions on ServerRoot Directories</
title>
278N/A <
p>In typical operation, Apache is started by the root user, and it
278N/A switches to the user defined by the <
directive 278N/A module="mpm_common">User</
directive> directive to serve hits. As is the
278N/A case with any command that root executes, you must take care that it is
278N/A protected from modification by non-root users. Not only must the files
278N/A themselves be writeable only by root, but so must the directories, and
278N/A parents of all directories. For example, if you choose to place
278N/A that directory as root, with commands like these:</
p>
278N/A mkdir bin conf logs <
br />
278N/A chown 0 . bin conf logs <
br />
278N/A chgrp 0 . bin conf logs <
br />
278N/A chmod 755 . bin conf logs
278N/A <
p>It is assumed that /, /usr, and /
usr/
local are only modifiable by
278N/A root. When you install the <
program>httpd</
program> executable, you
278N/A should ensure that it is similarly protected:</
p>
278N/A <
p>You can create an htdocs subdirectory which is modifiable by other
278N/A users -- since root never executes any files out of there, and shouldn't
278N/A be creating files in there.</
p>
278N/A <
p>If you allow non-root users to modify any files that root either
278N/A executes or writes on then you open your system to root compromises.
278N/A For example, someone could replace the <
program>httpd</
program> binary so
278N/A that the next time you start it, it will execute some arbitrary code. If
278N/A the logs directory is writeable (by a non-root user), someone could replace
278N/A a log file with a symlink to some other system file, and then root
278N/A might overwrite that file with arbitrary data. If the log files
278N/A themselves are writeable (by a non-root user), then someone may be
278N/A able to overwrite the log itself with bogus data.</
p>
278N/A <
title>Server Side Includes</
title>
278N/A <
p>Server Side Includes (SSI) present a server administrator with
278N/A several potential security risks.</
p>
278N/A <
p>The first risk is the increased load on the server. All
278N/A SSI-enabled files have to be parsed by Apache, whether or not
278N/A there are any SSI directives included within the files. While this
278N/A load increase is minor, in a shared server environment it can become
278N/A <
p>SSI files also pose the same risks that are associated with CGI
278N/A scripts in general. Using the "exec cmd" element, SSI-enabled files
278N/A can execute any CGI script or program under the permissions of the
278N/A <
p>There are ways to enhance the security of SSI files while still
278N/A taking advantage of the benefits they provide.</
p>
278N/A <
p>To isolate the damage a wayward SSI file can cause, a server
278N/A described in the <
a href="#cgi">CGI in General</
a> section</
p>
278N/A <
p>Enabling SSI for files with .html or .htm extensions can be
278N/A dangerous. This is especially true in a shared, or high traffic,
278N/A server environment. SSI-enabled files should have a separate extension,
278N/A such as the conventional .shtml. This helps keep server load at a
278N/A minimum and allows for easier management of risk.</
p>
278N/A <
p>Another solution is to disable the ability to run scripts and
278N/A programs from SSI pages. To do this replace <
code>Includes</
code>
278N/A with <
code>IncludesNOEXEC</
code> in the <
directive 278N/A module="core">Options</
directive> directive. Note that users may
278N/A still use <--#include virtual="..." --> to execute CGI scripts if
278N/A these scripts are in directories desginated by a <
directive 278N/A module="mod_alias">ScriptAlias</
directive> directive.</
p>
278N/A <
title>CGI in General</
title>
278N/A <
p>First of all, you always have to remember that you must trust the
278N/A security holes in CGI, whether they were deliberate or accidental. CGI
278N/A scripts can run essentially arbitrary commands on your system with the
278N/A permissions of the web server user and can therefore be extremely
278N/A dangerous if they are not carefully checked.</
p>
278N/A <
p>All the CGI scripts will run as the same user, so they have potential
278N/A to conflict (accidentally or deliberately) with other scripts
e.g. User
278N/A A hates User B, so he writes a script to trash User B's CGI database. One
278N/A program which can be used to allow scripts to run as different users is
278N/A 1.2 and is called from special hooks in the Apache server code. Another
278N/A popular way of doing this is with
278N/A <
section id="nsaliasedcgi">
278N/A <
title>Non Script Aliased CGI</
title>
278N/A <
p>Allowing users to execute CGI scripts in any directory should only be
278N/A <
li>You trust your users not to write scripts which will deliberately
278N/A or accidentally expose your system to an attack.</
li>
278N/A <
li>You consider security at your site to be so feeble in other areas,
278N/A as to make one more potential hole irrelevant.</
li>
278N/A <
li>You have no users, and nobody ever visits your server.</
li>
278N/A <
section id="saliasedcgi">
278N/A <
title>Script Aliased CGI</
title>
278N/A <
p>Limiting CGI to special directories gives the admin control over what
278N/A goes into those directories. This is inevitably more secure than non
278N/A script aliased CGI, but only if users with write access to the
278N/A directories are trusted or the admin is willing to test each
278N/A <
p>Most sites choose this option over the non script aliased CGI
278N/A <
title>Other sources of dynamic content</
title>
278N/A Embedded scripting options which run as part of the server itself,
278N/A such as mod_php, mod_perl, mod_tcl, and mod_python, run under the
278N/A identity of the server itself (see the <
directive 278N/A module="mpm_common">User</
directive> directive), and therefore
278N/A scripts executed by these engines potentially can access anything the
server user can. Some scripting engines may provide restrictions, but
it is better to be safe and assume not.</
p>
<
section id="systemsettings">
<
title>Protecting System Settings</
title>
<
p>To run a really tight ship, you'll want to stop users from setting
up <
code>.htaccess</
code> files which can override security features
you've configured. Here's one way to do it.</
p>
<
p>In the server configuration file, put</
p>
<Directory /> <
br />
AllowOverride None <
br />
<
p>This prevents the use of <
code>.htaccess</
code> files in all
directories apart from those specifically enabled.</
p>
<
section id="protectserverfiles">
<
title>Protect Server Files by Default</
title>
<
p>One aspect of Apache which is occasionally misunderstood is the
feature of default access. That is, unless you take steps to change it,
if the server can find its way to a file through normal URL mapping
rules, it can serve it to clients.</
p>
<
p>For instance, consider the following example:</
p>
# cd /; ln -s / public_html <
br />
<
p>This would allow clients to walk through the entire filesystem. To
work around this, add the following block to your server's
<Directory /> <
br />
<
p>This will forbid default access to filesystem locations. Add
appropriate <
directive module="core">Directory</
directive> blocks to
allow access only in those areas you wish. For example,</
p>
<Directory /
usr/
users/*/public_html> <
br />
</Directory> <
br />
<
p>Pay particular attention to the interactions of <
directive module="core">Location</
directive> and <
directive module="core">Directory</
directive> directives; for instance, even
if <
code><Directory /></
code> denies access, a <
code>
<Location /></
code> directive might overturn it</
p>
<
p>Also be wary of playing games with the <
directive module="mod_userdir">UserDir</
directive> directive; setting it to
something like "./" would have the same effect, for root, as the first
example above. If you are using Apache 1.3 or above, we strongly
recommend that you include the following line in your server
<
section id="watchyourlogs">
<
title>Watching Your Logs</
title>
<
p>To keep up-to-date with what is actually going on against your server
you have to check the <
a href="/logs.html">Log Files</
a>. Even though
the log files only reports what has already happened, they will give you
some understanding of what attacks is thrown against the server and
allows you to check if the necessary level of security is present.</
p>
<
p>A couple of examples:</
p>
grep "client denied" error_log | tail -n 10
<
p>The first example will list the number of attacks trying to exploit the
Source.JSP Malformed Request Information Disclosure Vulnerability</
a>,
the second example will list the ten last denied clients, for example:</
p>
[Thu Jul 11 17:18:39 2002] [error] [client
foo.bar.com] client denied
<
p>As you can see, the log files only report what already has happened, so
if the client had been able to access the <
code>.htpasswd</
code> file you
would have seen something similar to:</
p>
<
p>in your <
a href="/logs.html#accesslog">Access Log</
a>. This means
you probably commented out the following in your server configuration
<Files ~ "^\.ht"> <
br />