1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
5 <manualpage metafile="security_tips.xml.meta">
6 <relativepath href=".." />
7 <parentdocument href="./">Miscellaneous Documentation</parentdocument>
9 <title>Security Tips</title>
12 <p>Some hints and tips on security issues in setting up a web server.
13 Some of the suggestions will be general, others specific to Apache.</p>
16 <section id="uptodate"><title>Keep up to Date</title>
18 <p>The Apache HTTP Server has a good record for security and a
19 developer community highly concerned about security issues. But
20 it is inevitable that some problems -- small or large -- will be
21 discovered in software after it is released. For this reason, it
22 is crucial to keep aware of updates to the software. If you have
23 obtained your version of the HTTP Server directly from Apache, we
24 highly recommend you subscribe to the <a
25 href="http://httpd.apache.org/lists.html#http-announce">Apache
26 HTTP Server Announcements List</a> where you can keep informed of
27 new releases and security updates. Similar services are available
28 from most third-party distributors of Apache software.</p>
30 <p>Of course, most times that a web server is compromised, it is
31 not because of problems in the HTTP Server code. Rather, it comes
32 from problems in add-on code, CGI scripts, or the underlying
33 Operating System. You must therefore stay aware of problems and
34 updates with all the software on your system.</p>
38 <section id="serverroot">
40 <title>Permissions on ServerRoot Directories</title>
42 <p>In typical operation, Apache is started by the root user, and it
43 switches to the user defined by the <directive
44 module="mpm_common">User</directive> directive to serve hits. As is the
45 case with any command that root executes, you must take care that it is
46 protected from modification by non-root users. Not only must the files
47 themselves be writeable only by root, but so must the directories, and
48 parents of all directories. For example, if you choose to place
49 ServerRoot in /usr/local/apache then it is suggested that you create
50 that directory as root, with commands like these:</p>
53 mkdir /usr/local/apache <br />
54 cd /usr/local/apache <br />
55 mkdir bin conf logs <br />
56 chown 0 . bin conf logs <br />
57 chgrp 0 . bin conf logs <br />
58 chmod 755 . bin conf logs
61 <p>It is assumed that /, /usr, and /usr/local are only modifiable by
62 root. When you install the httpd executable, you should ensure that
63 it is similarly protected:</p>
66 cp httpd /usr/local/apache/bin <br />
67 chown 0 /usr/local/apache/bin/httpd <br />
68 chgrp 0 /usr/local/apache/bin/httpd <br />
69 chmod 511 /usr/local/apache/bin/httpd
72 <p>You can create an htdocs subdirectory which is modifiable by other
73 users -- since root never executes any files out of there, and shouldn't
74 be creating files in there.</p>
76 <p>If you allow non-root users to modify any files that root either
77 executes or writes on then you open your system to root compromises.
78 For example, someone could replace the httpd binary so that the next
79 time you start it, it will execute some arbitrary code. If the logs
80 directory is writeable (by a non-root user), someone could replace
81 a log file with a symlink to some other system file, and then root
82 might overwrite that file with arbitrary data. If the log files
83 themselves are writeable (by a non-root user), then someone may be
84 able to overwrite the log itself with bogus data.</p>
90 <title>Server Side Includes</title>
92 <p>Server Side Includes (SSI) present a server administrator with
93 several potential security risks.</p>
95 <p>The first risk is the increased load on the server. All
96 SSI-enabled files have to be parsed by Apache, whether or not
97 there are any SSI directives included within the files. While this
98 load increase is minor, in a shared server environment it can become
101 <p>SSI files also pose the same risks that are associated with CGI
102 scripts in general. Using the "exec cmd" element, SSI-enabled files
103 can execute any CGI script or program under the permissions of the
104 user and group Apache runs as, as configured in httpd.conf.</p>
106 <p>There are ways to enhance the security of SSI files while still
107 taking advantage of the benefits they provide.</p>
109 <p>To isolate the damage a wayward SSI file can cause, a server
110 administrator can enable <a href="../suexec.html">suexec</a> as
111 described in the <a href="#cgi">CGI in General</a> section</p>
113 <p>Enabling SSI for files with .html or .htm extensions can be
114 dangerous. This is especially true in a shared, or high traffic,
115 server environment. SSI-enabled files should have a separate extension,
116 such as the conventional .shtml. This helps keep server load at a
117 minimum and allows for easier management of risk.</p>
119 <p>Another solution is to disable the ability to run scripts and
120 programs from SSI pages. To do this replace <code>Includes</code>
121 with <code>IncludesNOEXEC</code> in the <directive
122 module="core">Options</directive> directive. Note that users may
123 still use <--#include virtual="..." --> to execute CGI scripts if
124 these scripts are in directories desginated by a <directive
125 module="mod_alias">ScriptAlias</directive> directive.</p>
131 <title>CGI in General</title>
133 <p>First of all, you always have to remember that you must trust the
134 writers of the CGI scripts/programs or your ability to spot potential
135 security holes in CGI, whether they were deliberate or accidental. CGI
136 scripts can run essentially arbitrary commands on your system with the
137 permissions of the web server user and can therefore be extremely
138 dangerous if they are not carefully checked.</p>
140 <p>All the CGI scripts will run as the same user, so they have potential
141 to conflict (accidentally or deliberately) with other scripts e.g. User
142 A hates User B, so he writes a script to trash User B's CGI database. One
143 program which can be used to allow scripts to run as different users is
144 <a href="../suexec.html">suEXEC</a> which is included with Apache as of
145 1.2 and is called from special hooks in the Apache server code. Another
146 popular way of doing this is with
147 <a href="http://cgiwrap.unixtools.org/">CGIWrap</a>.</p>
151 <section id="nsaliasedcgi">
153 <title>Non Script Aliased CGI</title>
155 <p>Allowing users to execute CGI scripts in any directory should only be
159 <li>You trust your users not to write scripts which will deliberately
160 or accidentally expose your system to an attack.</li>
161 <li>You consider security at your site to be so feeble in other areas,
162 as to make one more potential hole irrelevant.</li>
163 <li>You have no users, and nobody ever visits your server.</li>
168 <section id="saliasedcgi">
170 <title>Script Aliased CGI</title>
172 <p>Limiting CGI to special directories gives the admin control over what
173 goes into those directories. This is inevitably more secure than non
174 script aliased CGI, but only if users with write access to the
175 directories are trusted or the admin is willing to test each
176 new CGI script/program for potential security holes.</p>
178 <p>Most sites choose this option over the non script aliased CGI
183 <section id="dynamic">
185 <title>Other sources of dynamic content</title>
188 Embedded scripting options which run as part of the server itself,
189 such as mod_php, mod_perl, mod_tcl, and mod_python, run under the
190 identity of the server itself (see the <directive
191 module="mpm_common">User</directive> directive), and therefore
192 scripts executed by these engines potentially can access anything the
193 server user can. Some scripting engines may provide restrictions, but
194 it is better to be safe and assume not.</p>
198 <section id="systemsettings">
200 <title>Protecting System Settings</title>
202 <p>To run a really tight ship, you'll want to stop users from setting
203 up <code>.htaccess</code> files which can override security features
204 you've configured. Here's one way to do it.</p>
206 <p>In the server configuration file, put</p>
209 <Directory /> <br />
210 AllowOverride None <br />
214 <p>This prevents the use of <code>.htaccess</code> files in all
215 directories apart from those specifically enabled.</p>
219 <section id="protectserverfiles">
221 <title>Protect Server Files by Default</title>
223 <p>One aspect of Apache which is occasionally misunderstood is the
224 feature of default access. That is, unless you take steps to change it,
225 if the server can find its way to a file through normal URL mapping
226 rules, it can serve it to clients.</p>
228 <p>For instance, consider the following example:</p>
231 # cd /; ln -s / public_html <br />
232 Accessing <code>http://localhost/~root/</code>
235 <p>This would allow clients to walk through the entire filesystem. To
236 work around this, add the following block to your server's
240 <Directory /> <br />
241 Order Deny,Allow <br />
246 <p>This will forbid default access to filesystem locations. Add
247 appropriate <directive module="core">Directory</directive> blocks to
248 allow access only in those areas you wish. For example,</p>
251 <Directory /usr/users/*/public_html> <br />
252 Order Deny,Allow <br />
253 Allow from all <br />
254 </Directory> <br />
255 <Directory /usr/local/httpd> <br />
256 Order Deny,Allow <br />
257 Allow from all <br />
261 <p>Pay particular attention to the interactions of <directive
262 module="core">Location</directive> and <directive
263 module="core">Directory</directive> directives; for instance, even
264 if <code><Directory /></code> denies access, a <code>
265 <Location /></code> directive might overturn it</p>
267 <p>Also be wary of playing games with the <directive
268 module="mod_userdir">UserDir</directive> directive; setting it to
269 something like "./" would have the same effect, for root, as the first
270 example above. If you are using Apache 1.3 or above, we strongly
271 recommend that you include the following line in your server
272 configuration files:</p>
275 UserDir disabled root
280 <section id="watchyourlogs">
282 <title>Watching Your Logs</title>
284 <p>To keep up-to-date with what is actually going on against your server
285 you have to check the <a href="../logs.html">Log Files</a>. Even though
286 the log files only reports what has already happend, they will give you
287 some understanding of what attacks is thrown against the server and
288 allows you to check if the necessary level of security is present.</p>
290 <p>A couple of examples:</p>
293 grep -c "/jsp/source.jsp?/jsp/ /jsp/source.jsp??" access_log <br />
294 grep "client denied" error_log | tail -n 10
297 <p>The first example will list the number of attacks trying to exploit the
298 <a href="http://online.securityfocus.com/bid/4876/info/">Apache Tomcat
299 Source.JSP Malformed Request Information Disclosure Vulnerability</a>,
300 the second example will list the ten last denied clients, for example:</p>
303 [Thu Jul 11 17:18:39 2002] [error] [client foo.bar.com] client denied
304 by server configuration: /usr/local/apache/htdocs/.htpasswd
307 <p>As you can see, the log files only report what already has happend, so
308 if the client had been able to access the <code>.htpasswd</code> file you
309 would have seen something similar to:</p>
312 foo.bar.com - - [12/Jul/2002:01:59:13 +0200] "GET /.htpasswd HTTP/1.1"
315 <p>in your <a href="../logs.html#accesslog">Access Log</a>. This means
316 you probably commented out the following in your server configuration
320 <Files ~ "^\.ht"> <br />
321 Order allow,deny <br />