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"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="security_tips.xml.meta">
24 <parentdocument href="./">Miscellaneous Documentation</parentdocument>
26 <title>Security Tips</title>
29 <p>Some hints and tips on security issues in setting up a web server.
30 Some of the suggestions will be general, others specific to Apache.</p>
33 <section id="uptodate"><title>Keep up to Date</title>
35 <p>The Apache HTTP Server has a good record for security and a
36 developer community highly concerned about security issues. But
37 it is inevitable that some problems -- small or large -- will be
38 discovered in software after it is released. For this reason, it
39 is crucial to keep aware of updates to the software. If you have
40 obtained your version of the HTTP Server directly from Apache, we
41 highly recommend you subscribe to the <a
42 href="http://httpd.apache.org/lists.html#http-announce">Apache
43 HTTP Server Announcements List</a> where you can keep informed of
44 new releases and security updates. Similar services are available
45 from most third-party distributors of Apache software.</p>
47 <p>Of course, most times that a web server is compromised, it is
48 not because of problems in the HTTP Server code. Rather, it comes
49 from problems in add-on code, CGI scripts, or the underlying
50 Operating System. You must therefore stay aware of problems and
51 updates with all the software on your system.</p>
57 <title>Denial of Service (DoS) attacks</title>
59 <p>All network servers can be subject to denial of service attacks
60 that attempt to prevent responses to clients by tying up the
61 resources of the server. It is not possible to prevent such
62 attacks entirely, but you can do certain things to mitigate the
63 problems that they create.</p>
65 <p>Often the most effective anti-DoS tool will be a firewall or
66 other operating-system configurations. For example, most
67 firewalls can be configured to restrict the number of simultaneous
68 connections from any individual IP address or network, thus
69 preventing a range of simple attacks. Of course this is no help
70 against Distributed Denial of Service attacks (DDoS).</p>
72 <p>There are also certain Apache HTTP Server configuration
73 settings that can help mitigate problems:</p>
76 <li>The <directive module="core">TimeOut</directive> directive
77 should be lowered on sites that are subject to DoS attacks.
78 Setting this to as low as a few seconds may be appropriate.
79 As <directive module="core">TimeOut</directive> is currently
80 used for several different operations, setting it to a low value
81 introduces problems with long running CGI scripts.</li>
83 <li>The <directive module="core">KeepAliveTimeout</directive>
84 directive may be also lowered on sites that are subject to DoS
85 attacks. Some sites even turn off the keepalives completely via
86 <directive module="core">KeepAlive</directive>, which has of course
87 other drawbacks on performance.</li>
89 <li>The values of various timeout-related directives provided by
90 other modules should be checked.</li>
93 <directive module="core">LimitRequestBody</directive>,
94 <directive module="core">LimitRequestFields</directive>,
95 <directive module="core">LimitRequestFieldSize</directive>,
96 <directive module="core">LimitRequestLine</directive>, and
97 <directive module="core">LimitXMLRequestBody</directive>
98 should be carefully configured to limit resource consumption
99 triggered by client input.</li>
101 <li>On operating systems that support it, make sure that you use
102 the <directive module="core">AcceptFilter</directive> directive
103 to offload part of the request processing to the operating
104 system. This is active by default in Apache httpd, but may
105 require reconfiguration of your kernel.</li>
107 <li>Tune the <directive
108 module="mpm_common">MaxClients</directive> directive to allow
109 the server to handle the maximum number of simultaneous
110 connections without running out of resources. See also the <a
111 href="perf-tuning.html">performance tuning
112 documentation</a>.</li>
114 <li>The use of a threaded <a href="../mpm.html">mpm</a> may
115 allow you to handle more simultaneous connections, thereby
116 mitigating DoS attacks. Further, the experimental
117 <module>event</module> mpm
118 uses asynchronous processing to avoid devoting a thread to each
119 connection. At the current point of time this
120 is work in progress and not fully implemented. Especially the
121 <module>event</module> mpm is currently incompatible with
122 <module>mod_ssl</module> and other input filters.</li>
124 <li>There are a number of third-party modules available through
126 href="http://modules.apache.org/">http://modules.apache.org/</a>
127 that can restrict certain client behaviors and thereby mitigate
135 <section id="serverroot">
137 <title>Permissions on ServerRoot Directories</title>
139 <p>In typical operation, Apache is started by the root user, and it
140 switches to the user defined by the <directive
141 module="mpm_common">User</directive> directive to serve hits. As is the
142 case with any command that root executes, you must take care that it is
143 protected from modification by non-root users. Not only must the files
144 themselves be writeable only by root, but so must the directories, and
145 parents of all directories. For example, if you choose to place
146 ServerRoot in <code>/usr/local/apache</code> then it is suggested that
147 you create that directory as root, with commands like these:</p>
150 mkdir /usr/local/apache <br />
151 cd /usr/local/apache <br />
152 mkdir bin conf logs <br />
153 chown 0 . bin conf logs <br />
154 chgrp 0 . bin conf logs <br />
155 chmod 755 . bin conf logs
158 <p>It is assumed that <code>/</code>, <code>/usr</code>, and
159 <code>/usr/local</code> are only modifiable by root. When you install the
160 <program>httpd</program> executable, you should ensure that it is
161 similarly protected:</p>
164 cp httpd /usr/local/apache/bin <br />
165 chown 0 /usr/local/apache/bin/httpd <br />
166 chgrp 0 /usr/local/apache/bin/httpd <br />
167 chmod 511 /usr/local/apache/bin/httpd
170 <p>You can create an htdocs subdirectory which is modifiable by other
171 users -- since root never executes any files out of there, and shouldn't
172 be creating files in there.</p>
174 <p>If you allow non-root users to modify any files that root either
175 executes or writes on then you open your system to root compromises.
176 For example, someone could replace the <program>httpd</program> binary so
177 that the next time you start it, it will execute some arbitrary code. If
178 the logs directory is writeable (by a non-root user), someone could replace
179 a log file with a symlink to some other system file, and then root
180 might overwrite that file with arbitrary data. If the log files
181 themselves are writeable (by a non-root user), then someone may be
182 able to overwrite the log itself with bogus data.</p>
188 <title>Server Side Includes</title>
190 <p>Server Side Includes (SSI) present a server administrator with
191 several potential security risks.</p>
193 <p>The first risk is the increased load on the server. All
194 SSI-enabled files have to be parsed by Apache, whether or not
195 there are any SSI directives included within the files. While this
196 load increase is minor, in a shared server environment it can become
199 <p>SSI files also pose the same risks that are associated with CGI
200 scripts in general. Using the <code>exec cmd</code> element, SSI-enabled
201 files can execute any CGI script or program under the permissions of the
202 user and group Apache runs as, as configured in
203 <code>httpd.conf</code>.</p>
205 <p>There are ways to enhance the security of SSI files while still
206 taking advantage of the benefits they provide.</p>
208 <p>To isolate the damage a wayward SSI file can cause, a server
209 administrator can enable <a href="../suexec.html">suexec</a> as
210 described in the <a href="#cgi">CGI in General</a> section.</p>
212 <p>Enabling SSI for files with <code>.html</code> or <code>.htm</code>
213 extensions can be dangerous. This is especially true in a shared, or high
214 traffic, server environment. SSI-enabled files should have a separate
215 extension, such as the conventional <code>.shtml</code>. This helps keep
216 server load at a minimum and allows for easier management of risk.</p>
218 <p>Another solution is to disable the ability to run scripts and
219 programs from SSI pages. To do this replace <code>Includes</code>
220 with <code>IncludesNOEXEC</code> in the <directive
221 module="core">Options</directive> directive. Note that users may
222 still use <code><--#include virtual="..." --></code> to execute CGI
223 scripts if these scripts are in directories designated by a <directive
224 module="mod_alias">ScriptAlias</directive> directive.</p>
230 <title>CGI in General</title>
232 <p>First of all, you always have to remember that you must trust the
233 writers of the CGI scripts/programs or your ability to spot potential
234 security holes in CGI, whether they were deliberate or accidental. CGI
235 scripts can run essentially arbitrary commands on your system with the
236 permissions of the web server user and can therefore be extremely
237 dangerous if they are not carefully checked.</p>
239 <p>All the CGI scripts will run as the same user, so they have potential
240 to conflict (accidentally or deliberately) with other scripts e.g. User
241 A hates User B, so he writes a script to trash User B's CGI database. One
242 program which can be used to allow scripts to run as different users is
243 <a href="../suexec.html">suEXEC</a> which is included with Apache as of
244 1.2 and is called from special hooks in the Apache server code. Another
245 popular way of doing this is with
246 <a href="http://cgiwrap.unixtools.org/">CGIWrap</a>.</p>
250 <section id="nsaliasedcgi">
252 <title>Non Script Aliased CGI</title>
254 <p>Allowing users to execute CGI scripts in any directory should only be
258 <li>You trust your users not to write scripts which will deliberately
259 or accidentally expose your system to an attack.</li>
260 <li>You consider security at your site to be so feeble in other areas,
261 as to make one more potential hole irrelevant.</li>
262 <li>You have no users, and nobody ever visits your server.</li>
267 <section id="saliasedcgi">
269 <title>Script Aliased CGI</title>
271 <p>Limiting CGI to special directories gives the admin control over what
272 goes into those directories. This is inevitably more secure than non
273 script aliased CGI, but only if users with write access to the
274 directories are trusted or the admin is willing to test each
275 new CGI script/program for potential security holes.</p>
277 <p>Most sites choose this option over the non script aliased CGI
282 <section id="dynamic">
284 <title>Other sources of dynamic content</title>
286 <p>Embedded scripting options which run as part of the server itself,
287 such as <code>mod_php</code>, <code>mod_perl</code>, <code>mod_tcl</code>,
288 and <code>mod_python</code>, run under the identity of the server itself
289 (see the <directive module="mpm_common">User</directive> directive), and
290 therefore scripts executed by these engines potentially can access anything
291 the server user can. Some scripting engines may provide restrictions, but
292 it is better to be safe and assume not.</p>
295 <section id="dynamicsec">
297 <title>Dynamic content security</title>
299 <p>When setting up dynamic content, such as <code>mod_php</code>,
300 <code>mod_perl</code> or <code>mod_python</code>, many security considerations
301 get out of the scope of <code>httpd</code> itself, and you need to consult
302 documentation from those modules. For example, PHP lets you setup <a
303 href="http://www.php.net/manual/en/ini.sect.safe-mode.php">Safe Mode</a>,
304 which is most usually disabled by default. Another example is <a
305 href="http://www.hardened-php.net/suhosin/">Suhosin</a>, a PHP addon for more
306 security. For more information about those, consult each project
309 <p>At the Apache level, a module named <a href="http://modsecurity.org/">mod_security</a>
310 can be seen as a HTTP firewall and, provided you configure it finely enough,
311 can help you enhance your dynamic content security.</p>
315 <section id="systemsettings">
317 <title>Protecting System Settings</title>
319 <p>To run a really tight ship, you'll want to stop users from setting
320 up <code>.htaccess</code> files which can override security features
321 you've configured. Here's one way to do it.</p>
323 <p>In the server configuration file, put</p>
326 <Directory /> <br />
327 AllowOverride None <br />
331 <p>This prevents the use of <code>.htaccess</code> files in all
332 directories apart from those specifically enabled.</p>
336 <section id="protectserverfiles">
338 <title>Protect Server Files by Default</title>
340 <p>One aspect of Apache which is occasionally misunderstood is the
341 feature of default access. That is, unless you take steps to change it,
342 if the server can find its way to a file through normal URL mapping
343 rules, it can serve it to clients.</p>
345 <p>For instance, consider the following example:</p>
348 # cd /; ln -s / public_html <br />
349 Accessing <code>http://localhost/~root/</code>
352 <p>This would allow clients to walk through the entire filesystem. To
353 work around this, add the following block to your server's
357 <Directory /> <br />
358 Order Deny,Allow <br />
363 <p>This will forbid default access to filesystem locations. Add
364 appropriate <directive module="core">Directory</directive> blocks to
365 allow access only in those areas you wish. For example,</p>
368 <Directory /usr/users/*/public_html> <br />
369 Order Deny,Allow <br />
370 Allow from all <br />
371 </Directory> <br />
372 <Directory /usr/local/httpd> <br />
373 Order Deny,Allow <br />
374 Allow from all <br />
378 <p>Pay particular attention to the interactions of <directive
379 module="core">Location</directive> and <directive
380 module="core">Directory</directive> directives; for instance, even
381 if <code><Directory /></code> denies access, a <code>
382 <Location /></code> directive might overturn it.</p>
384 <p>Also be wary of playing games with the <directive
385 module="mod_userdir">UserDir</directive> directive; setting it to
386 something like <code>./</code> would have the same effect, for root, as
387 the first example above. If you are using Apache 1.3 or above, we strongly
388 recommend that you include the following line in your server
389 configuration files:</p>
392 UserDir disabled root
397 <section id="watchyourlogs">
399 <title>Watching Your Logs</title>
401 <p>To keep up-to-date with what is actually going on against your server
402 you have to check the <a href="../logs.html">Log Files</a>. Even though
403 the log files only reports what has already happened, they will give you
404 some understanding of what attacks is thrown against the server and
405 allow you to check if the necessary level of security is present.</p>
407 <p>A couple of examples:</p>
410 grep -c "/jsp/source.jsp?/jsp/ /jsp/source.jsp??" access_log <br />
411 grep "client denied" error_log | tail -n 10
414 <p>The first example will list the number of attacks trying to exploit the
415 <a href="http://online.securityfocus.com/bid/4876/info/">Apache Tomcat
416 Source.JSP Malformed Request Information Disclosure Vulnerability</a>,
417 the second example will list the ten last denied clients, for example:</p>
420 [Thu Jul 11 17:18:39 2002] [error] [client foo.example.com] client denied
421 by server configuration: /usr/local/apache/htdocs/.htpasswd
424 <p>As you can see, the log files only report what already has happened, so
425 if the client had been able to access the <code>.htpasswd</code> file you
426 would have seen something similar to:</p>
429 foo.example.com - - [12/Jul/2002:01:59:13 +0200] "GET /.htpasswd HTTP/1.1"
432 <p>in your <a href="../logs.html#accesslog">Access Log</a>. This means
433 you probably commented out the following in your server configuration
437 <Files ~ "^\.ht"> <br />
438 Order allow,deny <br />