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="mod_reqtimeout">RequestReadTimeout</directive>
77 directive allows to limit the time a client may take to send the
80 <li>The <directive module="core">TimeOut</directive> directive
81 should be lowered on sites that are subject to DoS attacks.
82 Setting this to as low as a few seconds may be appropriate.
83 As <directive module="core">TimeOut</directive> is currently
84 used for several different operations, setting it to a low value
85 introduces problems with long running CGI scripts.</li>
87 <li>The <directive module="core">KeepAliveTimeout</directive>
88 directive may be also lowered on sites that are subject to DoS
89 attacks. Some sites even turn off the keepalives completely via
90 <directive module="core">KeepAlive</directive>, which has of course
91 other drawbacks on performance.</li>
93 <li>The values of various timeout-related directives provided by
94 other modules should be checked.</li>
97 <directive module="core">LimitRequestBody</directive>,
98 <directive module="core">LimitRequestFields</directive>,
99 <directive module="core">LimitRequestFieldSize</directive>,
100 <directive module="core">LimitRequestLine</directive>, and
101 <directive module="core">LimitXMLRequestBody</directive>
102 should be carefully configured to limit resource consumption
103 triggered by client input.</li>
105 <li>On operating systems that support it, make sure that you use
106 the <directive module="core">AcceptFilter</directive> directive
107 to offload part of the request processing to the operating
108 system. This is active by default in Apache httpd, but may
109 require reconfiguration of your kernel.</li>
111 <li>Tune the <directive
112 module="mpm_common">MaxRequestWorkers</directive> directive to allow
113 the server to handle the maximum number of simultaneous
114 connections without running out of resources. See also the <a
115 href="perf-tuning.html">performance tuning
116 documentation</a>.</li>
118 <li>The use of a threaded <a href="../mpm.html">mpm</a> may
119 allow you to handle more simultaneous connections, thereby
120 mitigating DoS attacks. Further, the
121 <module>event</module> mpm
122 uses asynchronous processing to avoid devoting a thread to each
123 connection. Due to the nature of the OpenSSL library the
124 <module>event</module> mpm is currently incompatible with
125 <module>mod_ssl</module> and other input filters. In these
126 cases it falls back to the behaviour of the
127 <module>worker</module> mpm.</li>
129 <li>There are a number of third-party modules available through
131 href="http://modules.apache.org/">http://modules.apache.org/</a>
132 that can restrict certain client behaviors and thereby mitigate
140 <section id="serverroot">
142 <title>Permissions on ServerRoot Directories</title>
144 <p>In typical operation, Apache is started by the root user, and it
145 switches to the user defined by the <directive
146 module="mpm_common">User</directive> directive to serve hits. As is the
147 case with any command that root executes, you must take care that it is
148 protected from modification by non-root users. Not only must the files
149 themselves be writeable only by root, but so must the directories, and
150 parents of all directories. For example, if you choose to place
151 ServerRoot in <code>/usr/local/apache</code> then it is suggested that
152 you create that directory as root, with commands like these:</p>
155 mkdir /usr/local/apache <br />
156 cd /usr/local/apache <br />
157 mkdir bin conf logs <br />
158 chown 0 . bin conf logs <br />
159 chgrp 0 . bin conf logs <br />
160 chmod 755 . bin conf logs
163 <p>It is assumed that <code>/</code>, <code>/usr</code>, and
164 <code>/usr/local</code> are only modifiable by root. When you install the
165 <program>httpd</program> executable, you should ensure that it is
166 similarly protected:</p>
169 cp httpd /usr/local/apache/bin <br />
170 chown 0 /usr/local/apache/bin/httpd <br />
171 chgrp 0 /usr/local/apache/bin/httpd <br />
172 chmod 511 /usr/local/apache/bin/httpd
175 <p>You can create an htdocs subdirectory which is modifiable by other
176 users -- since root never executes any files out of there, and shouldn't
177 be creating files in there.</p>
179 <p>If you allow non-root users to modify any files that root either
180 executes or writes on then you open your system to root compromises.
181 For example, someone could replace the <program>httpd</program> binary so
182 that the next time you start it, it will execute some arbitrary code. If
183 the logs directory is writeable (by a non-root user), someone could replace
184 a log file with a symlink to some other system file, and then root
185 might overwrite that file with arbitrary data. If the log files
186 themselves are writeable (by a non-root user), then someone may be
187 able to overwrite the log itself with bogus data.</p>
193 <title>Server Side Includes</title>
195 <p>Server Side Includes (SSI) present a server administrator with
196 several potential security risks.</p>
198 <p>The first risk is the increased load on the server. All
199 SSI-enabled files have to be parsed by Apache, whether or not
200 there are any SSI directives included within the files. While this
201 load increase is minor, in a shared server environment it can become
204 <p>SSI files also pose the same risks that are associated with CGI
205 scripts in general. Using the <code>exec cmd</code> element, SSI-enabled
206 files can execute any CGI script or program under the permissions of the
207 user and group Apache runs as, as configured in
208 <code>httpd.conf</code>.</p>
210 <p>There are ways to enhance the security of SSI files while still
211 taking advantage of the benefits they provide.</p>
213 <p>To isolate the damage a wayward SSI file can cause, a server
214 administrator can enable <a href="../suexec.html">suexec</a> as
215 described in the <a href="#cgi">CGI in General</a> section.</p>
217 <p>Enabling SSI for files with <code>.html</code> or <code>.htm</code>
218 extensions can be dangerous. This is especially true in a shared, or high
219 traffic, server environment. SSI-enabled files should have a separate
220 extension, such as the conventional <code>.shtml</code>. This helps keep
221 server load at a minimum and allows for easier management of risk.</p>
223 <p>Another solution is to disable the ability to run scripts and
224 programs from SSI pages. To do this replace <code>Includes</code>
225 with <code>IncludesNOEXEC</code> in the <directive
226 module="core">Options</directive> directive. Note that users may
227 still use <code><--#include virtual="..." --></code> to execute CGI
228 scripts if these scripts are in directories designated by a <directive
229 module="mod_alias">ScriptAlias</directive> directive.</p>
235 <title>CGI in General</title>
237 <p>First of all, you always have to remember that you must trust the
238 writers of the CGI scripts/programs or your ability to spot potential
239 security holes in CGI, whether they were deliberate or accidental. CGI
240 scripts can run essentially arbitrary commands on your system with the
241 permissions of the web server user and can therefore be extremely
242 dangerous if they are not carefully checked.</p>
244 <p>All the CGI scripts will run as the same user, so they have potential
245 to conflict (accidentally or deliberately) with other scripts e.g. User
246 A hates User B, so he writes a script to trash User B's CGI database. One
247 program which can be used to allow scripts to run as different users is
248 <a href="../suexec.html">suEXEC</a> which is included with Apache as of
249 1.2 and is called from special hooks in the Apache server code. Another
250 popular way of doing this is with
251 <a href="http://cgiwrap.sourceforge.net/">CGIWrap</a>.</p>
255 <section id="nsaliasedcgi">
257 <title>Non Script Aliased CGI</title>
259 <p>Allowing users to execute CGI scripts in any directory should only be
263 <li>You trust your users not to write scripts which will deliberately
264 or accidentally expose your system to an attack.</li>
265 <li>You consider security at your site to be so feeble in other areas,
266 as to make one more potential hole irrelevant.</li>
267 <li>You have no users, and nobody ever visits your server.</li>
272 <section id="saliasedcgi">
274 <title>Script Aliased CGI</title>
276 <p>Limiting CGI to special directories gives the admin control over what
277 goes into those directories. This is inevitably more secure than non
278 script aliased CGI, but only if users with write access to the
279 directories are trusted or the admin is willing to test each
280 new CGI script/program for potential security holes.</p>
282 <p>Most sites choose this option over the non script aliased CGI
287 <section id="dynamic">
289 <title>Other sources of dynamic content</title>
291 <p>Embedded scripting options which run as part of the server itself,
292 such as <code>mod_php</code>, <code>mod_perl</code>, <code>mod_tcl</code>,
293 and <code>mod_python</code>, run under the identity of the server itself
294 (see the <directive module="mpm_common">User</directive> directive), and
295 therefore scripts executed by these engines potentially can access anything
296 the server user can. Some scripting engines may provide restrictions, but
297 it is better to be safe and assume not.</p>
300 <section id="dynamicsec">
302 <title>Dynamic content security</title>
304 <p>When setting up dynamic content, such as <code>mod_php</code>,
305 <code>mod_perl</code> or <code>mod_python</code>, many security considerations
306 get out of the scope of <code>httpd</code> itself, and you need to consult
307 documentation from those modules. For example, PHP lets you setup <a
308 href="http://www.php.net/manual/en/ini.sect.safe-mode.php">Safe Mode</a>,
309 which is most usually disabled by default. Another example is <a
310 href="http://www.hardened-php.net/suhosin/">Suhosin</a>, a PHP addon for more
311 security. For more information about those, consult each project
314 <p>At the Apache level, a module named <a href="http://modsecurity.org/">mod_security</a>
315 can be seen as a HTTP firewall and, provided you configure it finely enough,
316 can help you enhance your dynamic content security.</p>
320 <section id="systemsettings">
322 <title>Protecting System Settings</title>
324 <p>To run a really tight ship, you'll want to stop users from setting
325 up <code>.htaccess</code> files which can override security features
326 you've configured. Here's one way to do it.</p>
328 <p>In the server configuration file, put</p>
331 <Directory /> <br />
332 AllowOverride None <br />
336 <p>This prevents the use of <code>.htaccess</code> files in all
337 directories apart from those specifically enabled.</p>
341 <section id="protectserverfiles">
343 <title>Protect Server Files by Default</title>
345 <p>One aspect of Apache which is occasionally misunderstood is the
346 feature of default access. That is, unless you take steps to change it,
347 if the server can find its way to a file through normal URL mapping
348 rules, it can serve it to clients.</p>
350 <p>For instance, consider the following example:</p>
353 # cd /; ln -s / public_html <br />
354 Accessing <code>http://localhost/~root/</code>
357 <p>This would allow clients to walk through the entire filesystem. To
358 work around this, add the following block to your server's
362 <Directory /> <br />
363 Order Deny,Allow <br />
368 <p>This will forbid default access to filesystem locations. Add
369 appropriate <directive module="core">Directory</directive> blocks to
370 allow access only in those areas you wish. For example,</p>
373 <Directory /usr/users/*/public_html> <br />
374 Order Deny,Allow <br />
375 Allow from all <br />
376 </Directory> <br />
377 <Directory /usr/local/httpd> <br />
378 Order Deny,Allow <br />
379 Allow from all <br />
383 <p>Pay particular attention to the interactions of <directive
384 module="core">Location</directive> and <directive
385 module="core">Directory</directive> directives; for instance, even
386 if <code><Directory /></code> denies access, a <code>
387 <Location /></code> directive might overturn it.</p>
389 <p>Also be wary of playing games with the <directive
390 module="mod_userdir">UserDir</directive> directive; setting it to
391 something like <code>./</code> would have the same effect, for root, as
392 the first example above. We strongly
393 recommend that you include the following line in your server
394 configuration files:</p>
397 UserDir disabled root
402 <section id="watchyourlogs">
404 <title>Watching Your Logs</title>
406 <p>To keep up-to-date with what is actually going on against your server
407 you have to check the <a href="../logs.html">Log Files</a>. Even though
408 the log files only reports what has already happened, they will give you
409 some understanding of what attacks is thrown against the server and
410 allow you to check if the necessary level of security is present.</p>
412 <p>A couple of examples:</p>
415 grep -c "/jsp/source.jsp?/jsp/ /jsp/source.jsp??" access_log <br />
416 grep "client denied" error_log | tail -n 10
419 <p>The first example will list the number of attacks trying to exploit the
420 <a href="http://online.securityfocus.com/bid/4876/info/">Apache Tomcat
421 Source.JSP Malformed Request Information Disclosure Vulnerability</a>,
422 the second example will list the ten last denied clients, for example:</p>
425 [Thu Jul 11 17:18:39 2002] [error] [client foo.example.com] client denied
426 by server configuration: /usr/local/apache/htdocs/.htpasswd
429 <p>As you can see, the log files only report what already has happened, so
430 if the client had been able to access the <code>.htpasswd</code> file you
431 would have seen something similar to:</p>
434 foo.example.com - - [12/Jul/2002:01:59:13 +0200] "GET /.htpasswd HTTP/1.1"
437 <p>in your <a href="../logs.html#accesslog">Access Log</a>. This means
438 you probably commented out the following in your server configuration
442 <Files ".ht*"> <br />
443 Order allow,deny <br />
450 <section id="merging">
452 <title>Merging of configuration sections</title>
454 <p> The merging of configuration sections is complicated and sometimes
455 directive specific. Always test your changes when creating dependencies
456 on how directives are merged.</p>
458 <p> For modules that don't implement any merging logic, such as
459 <directive>mod_access_compat</directive>, the behavior in later sections
460 depends on whether the later section has any directives
461 from the module. The configuration is inherited until a change is made,
462 at which point the configuration is <em>replaced</em> and not merged.</p>