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"?>
6 <relativepath href=".." />
8 <title>Security Tips</title>
11 <p>Some hints and tips on security issues in setting up a web server.
12 Some of the suggestions will be general, others specific to Apache.</p>
15 <section id="serverroot">
17 <title>Permissions on ServerRoot Directories</title>
19 <p>In typical operation, Apache is started by the root user, and it
20 switches to the user defined by the <directive
21 module="mpm_common">User</directive> directive to serve hits. As is the
22 case with any command that root executes, you must take care that it is
23 protected from modification by non-root users. Not only must the files
24 themselves be writeable only by root, but so must the directories, and
25 parents of all directories. For example, if you choose to place
26 ServerRoot in /usr/local/apache then it is suggested that you create
27 that directory as root, with commands like these:</p>
30 mkdir /usr/local/apache <br />
31 cd /usr/local/apache <br />
32 mkdir bin conf logs <br />
33 chown 0 . bin conf logs <br />
34 chgrp 0 . bin conf logs <br />
35 chmod 755 . bin conf logs
38 <p>It is assumed that /, /usr, and /usr/local are only modifiable by
39 root. When you install the httpd executable, you should ensure that
40 it is similarly protected:</p>
43 cp httpd /usr/local/apache/bin <br />
44 chown 0 /usr/local/apache/bin/httpd <br />
45 chgrp 0 /usr/local/apache/bin/httpd <br />
46 chmod 511 /usr/local/apache/bin/httpd
49 <p>You can create an htdocs subdirectory which is modifiable by other
50 users -- since root never executes any files out of there, and shouldn't
51 be creating files in there.</p>
53 <p>If you allow non-root users to modify any files that root either
54 executes or writes on then you open your system to root compromises.
55 For example, someone could replace the httpd binary so that the next
56 time you start it, it will execute some arbitrary code. If the logs
57 directory is writeable (by a non-root user), someone could replace
58 a log file with a symlink to some other system file, and then root
59 might overwrite that file with arbitrary data. If the log files
60 themselves are writeable (by a non-root user), then someone may be
61 able to overwrite the log itself with bogus data.</p>
67 <title>Server Side Includes</title>
69 <p>Server Side Includes (SSI) present a server administrator with
70 several potential security risks.</p>
72 <p>The first risk is the increased load on the server. All
73 SSI-enabled files have to be parsed by Apache, whether or not
74 there are any SSI directives included within the files. While this
75 load increase is minor, in a shared server environment it can become
78 <p>SSI files also pose the same risks that are associated with CGI
79 scripts in general. Using the "exec cmd" element, SSI-enabled files
80 can execute any CGI script or program under the permissions of the
81 user and group Apache runs as, as configured in httpd.conf.</p>
83 <p>There are ways to enhance the security of SSI files while still
84 taking advantage of the benefits they provide.</p>
86 <p>To isolate the damage a wayward SSI file can cause, a server
87 administrator can enable <a href="../suexec.html">suexec</a> as
88 described in the <a href="#cgi">CGI in General</a> section</p>
90 <p>Enabling SSI for files with .html or .htm extensions can be
91 dangerous. This is especially true in a shared, or high traffic,
92 server environment. SSI-enabled files should have a separate extension,
93 such as the conventional .shtml. This helps keep server load at a
94 minimum and allows for easier management of risk.</p>
96 <p>Another solution is to disable the ability to run scripts and
97 programs from SSI pages. To do this replace <code>Includes</code>
98 with <code>IncludesNOEXEC</code> in the <directive
99 module="core">Options</directive> directive. Note that users may
100 still use >--#include virtual="..." --< to execute CGI scripts if
101 these scripts are in directories desginated by a <directive
102 module="mod_alias">ScriptAlias</directive> directive.</p>
108 <title>CGI in General</title>
110 <p>First of all, you always have to remember that you must trust the
111 writers of the CGI scripts/programs or your ability to spot potential
112 security holes in CGI, whether they were deliberate or accidental. CGI
113 scripts can run essentially arbitrary commands on your system with the
114 permissions of the web server user and can therefore be extremely
115 dangerous if they are not carefully checked.</p>
117 <p>All the CGI scripts will run as the same user, so they have potential
118 to conflict (accidentally or deliberately) with other scripts e.g. User
119 A hates User B, so he writes a script to trash User B's CGI database. One
120 program which can be used to allow scripts to run as different users is
121 <a href="../suexec.html">suEXEC</a> which is included with Apache as of
122 1.2 and is called from special hooks in the Apache server code. Another
123 popular way of doing this is with
124 <a href="http://cgiwrap.unixtools.org/">CGIWrap</a>.</p>
128 <section id="nsaliasedcgi">
130 <title>Non Script Aliased CGI</title>
132 <p>Allowing users to execute CGI scripts in any directory should only be
136 <li>You trust your users not to write scripts which will deliberately
137 or accidentally expose your system to an attack.</li>
138 <li>You consider security at your site to be so feeble in other areas,
139 as to make one more potential hole irrelevant.</li>
140 <li>You have no users, and nobody ever visits your server.</li>
145 <section id="saliasedcgi">
147 <title>Script Aliased CGI</title>
149 <p>Limiting CGI to special directories gives the admin control over what
150 goes into those directories. This is inevitably more secure than non
151 script aliased CGI, but only if users with write access to the
152 directories are trusted or the admin is willing to test each
153 new CGI script/program for potential security holes.</p>
155 <p>Most sites choose this option over the non script aliased CGI
160 <section id="systemsettings">
162 <title>Protecting System Settings</title>
164 <p>To run a really tight ship, you'll want to stop users from setting
165 up <code>.htaccess</code> files which can override security features
166 you've configured. Here's one way to do it.</p>
168 <p>In the server configuration file, put</p>
171 <Directory /> <br />
172 AllowOverride None <br />
176 <p>This prevents the use of <code>.htaccess</code> files in all
177 directories apart from those specifically enabled.</p>
181 <section id="protectserverfiles">
183 <title>Protect Server Files by Default</title>
185 <p>One aspect of Apache which is occasionally misunderstood is the
186 feature of default access. That is, unless you take steps to change it,
187 if the server can find its way to a file through normal URL mapping
188 rules, it can serve it to clients.</p>
190 <p>For instance, consider the following example:</p>
193 # cd /; ln -s / public_html <br />
194 Accessing <code>http://localhost/~root/</code>
197 <p>This would allow clients to walk through the entire filesystem. To
198 work around this, add the following block to your server's
202 <Directory /> <br />
203 Order Deny,Allow <br />
208 <p>This will forbid default access to filesystem locations. Add
209 appropriate <directive module="core">Directory</directive> blocks to
210 allow access only in those areas you wish. For example,</p>
213 <Directory /usr/users/*/public_html> <br />
214 Order Deny,Allow <br />
215 Allow from all <br />
216 </Directory> <br />
217 <Directory /usr/local/httpd> <br />
218 Order Deny,Allow <br />
219 Allow from all <br />
223 <p>Pay particular attention to the interactions of <directive
224 module="core">Location</directive> and <directive
225 module="core">Directory</directive> directives; for instance, even
226 if <code><Directory /></code> denies access, a <code>
227 <Location /></code> directive might overturn it</p>
229 <p>Also be wary of playing games with the <directive
230 module="mod_userdir">UserDir</directive> directive; setting it to
231 something like "./" would have the same effect, for root, as the first
232 example above. If you are using Apache 1.3 or above, we strongly
233 recommend that you include the following line in your server
234 configuration files:</p>
237 UserDir disabled root
242 <section id="watchyourlogs">
244 <title>Watching Your Logs</title>
246 <p>To keep up-to-date with what is actually going on against your server
247 you have to check the <a href="../logs.html">Log Files</a>. Even though
248 the log files only reports what has already happend, they will give you
249 some understanding of what attacks is thrown against the server and
250 allows you to check if the necessary level of security is present.</p>
252 <p>A couple of examples:</p>
255 grep -c "/jsp/source.jsp?/jsp/ /jsp/source.jsp??" access_log <br />
256 grep "client denied" error_log | tail -n 10
259 <p>The first example will list the number of attacks trying to exploit the
260 <a href="http://online.securityfocus.com/bid/4876/info/">Apache Tomcat
261 Source.JSP Malformed Request Information Disclosure Vulnerability</a>,
262 the second example will list the ten last denied clients, for example:</p>
265 [Thu Jul 11 17:18:39 2002] [error] [client foo.bar.com] client denied
266 by server configuration: /usr/local/apache/htdocs/.htpasswd
269 <p>As you can see, the log files only report what already has happend, so
270 if the client had been able to access the <code>.htpasswd</code> file you
271 would have seen something similar to:</p>
274 foo.bar.com - - [12/Jul/2002:01:59:13 +0200] "GET /.htpasswd HTTP/1.1"
277 <p>in your <a href="../logs.html#accesslog">Access Log</a>. This means
278 you probably commented out the following in your server configuration
282 <Files ~ "^\.ht"> <br />
283 Order allow,deny <br />