]> granicus.if.org Git - apache/blob - docs/manual/misc/security_tips.xml
New XML.
[apache] / docs / manual / misc / security_tips.xml
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
5 <manualpage>
6   <relativepath href=".." />
7
8   <title>Security Tips</title>
9   
10   <summary>
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>
13   </summary>
14   
15   <section id="serverroot">
16   
17     <title>Permissions on ServerRoot Directories</title>
18     
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>
28     
29     <example>
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
36     </example>
37     
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>
41     
42     <example>
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
47     </example>
48     
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>
52     
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>
62     
63   </section>
64   
65   <section id="ssi">
66   
67     <title>Server Side Includes</title>
68     
69     <p>Server Side Includes (SSI) present a server administrator with 
70     several potential security risks.</p>
71     
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 
76     significant.</p>
77     
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>
82     
83     <p>There are ways to enhance the security of SSI files while still 
84     taking advantage of the benefits they provide.</p>
85     
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>
89     
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>
95     
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 &gt;--#include virtual="..." --&lt; to execute CGI scripts if 
101     these scripts are in directories desginated by a <directive
102     module="mod_alias">ScriptAlias</directive> directive.</p>
103     
104   </section>
105   
106   <section id="cgi">
107   
108     <title>CGI in General</title>
109     
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>
116     
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>
125     
126   </section>
127   
128   <section id="nsaliasedcgi">
129   
130     <title>Non Script Aliased CGI</title>
131     
132     <p>Allowing users to execute CGI scripts in any directory should only be 
133     considered if;</p>
134     
135     <ul>
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>
141     </ul>
142     
143   </section>
144   
145   <section id="saliasedcgi">
146   
147     <title>Script Aliased CGI</title>
148     
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>
154     
155     <p>Most sites choose this option over the non script aliased CGI 
156     approach.</p>
157     
158   </section>
159   
160   <section id="systemsettings">
161   
162     <title>Protecting System Settings</title>
163     
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>
167     
168     <p>In the server configuration file, put</p>
169     
170     <example>
171       &lt;Directory /&gt; <br />
172         AllowOverride None <br />
173       &lt;/Directory&gt;
174     </example>
175     
176     <p>This prevents the use of <code>.htaccess</code> files in all 
177     directories apart from those specifically enabled.</p>
178     
179   </section>
180   
181   <section id="protectserverfiles">
182   
183     <title>Protect Server Files by Default</title>
184     
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>
189     
190     <p>For instance, consider the following example:</p>
191     
192     <example>
193       # cd /; ln -s / public_html <br />
194       Accessing <code>http://localhost/~root/</code>
195     </example>
196     
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 
199     configuration:</p>
200     
201     <example>
202       &lt;Directory /&gt; <br />
203       Order Deny,Allow <br />
204       Deny from all <br />
205       &lt;/Directory&gt;
206     </example>
207     
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>
211     
212     <example>
213       &lt;Directory /usr/users/*/public_html&gt; <br />
214         Order Deny,Allow <br />
215         Allow from all <br />
216       &lt;/Directory&gt; <br />
217       &lt;Directory /usr/local/httpd&gt; <br />
218         Order Deny,Allow <br />
219         Allow from all <br />
220       &lt;/Directory&gt;
221     </example>
222     
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>&lt;Directory /&gt;</code> denies access, a <code>
227     &lt;Location /&gt;</code> directive might overturn it</p>
228     
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>
235     
236     <example>
237       UserDir disabled root
238     </example>
239     
240   </section>
241   
242   <section id="watchyourlogs">
243   
244     <title>Watching Your Logs</title>
245     
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>
251     
252     <p>A couple of examples:</p>
253     
254     <example>
255       grep -c "/jsp/source.jsp?/jsp/ /jsp/source.jsp??" access_log <br />
256       grep "client denied" error_log | tail -n 10
257     </example>
258     
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>
263     
264     <example>
265       [Thu Jul 11 17:18:39 2002] [error] [client foo.bar.com] client denied 
266       by server configuration: /usr/local/apache/htdocs/.htpasswd
267     </example>
268     
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>
272     
273     <example>
274       foo.bar.com - - [12/Jul/2002:01:59:13 +0200] "GET /.htpasswd HTTP/1.1"
275     </example>
276     
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 
279     file:</p>
280     
281     <example>
282       &lt;Files ~ "^\.ht"&gt; <br />
283         Order allow,deny <br />
284         Deny from all <br />
285       &lt;Files&gt;
286     </example>
287     
288   </section>
289   
290 </manualpage>