security.filesystem.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 156 行 · 第 1/2 页

HTML
156
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Filesystem Security</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="security.apache.html">Installed as an Apache module</a></div> <div class="next" style="text-align: right; float: right;"><a href="security.database.html">Database Security</a></div> <div class="up"><a href="security.html">Security</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div>   <h1>Filesystem Security</h1>   <p class="simpara">    PHP is subject to the security built into most server systems with    respect to permissions on a file and directory basis. This allows    you to control which files in the filesystem may be read. Care    should be taken with any files which are world readable to ensure    that they are safe for reading by all users who have access to that    filesystem.   </p>   <p class="simpara">    Since PHP was designed to allow user level access to the filesystem,    it&#039;s entirely possible to write a PHP script that will allow you    to read system files such as /etc/passwd, modify your ethernet    connections, send massive printer jobs out, etc. This has some    obvious implications, in that you need to ensure that the files    that you read from and write to are the appropriate ones.   </p>   <p class="simpara">    Consider the following script, where a user indicates that they&#039;d    like to delete a file in their home directory. This assumes a    situation where a PHP web interface is regularly used for file    management, so the Apache user is allowed to delete files in    the user home directories.   </p>   <p class="para">    <div class="example">     <p><b>Example #1 Poor variable checking leads to....</b></p>     <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;remove&nbsp;a&nbsp;file&nbsp;from&nbsp;the&nbsp;user's&nbsp;home&nbsp;directory<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_submitted_name'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$userfile&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_submitted_filename'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$homedir&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/home/$username"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #DD0000">"$homedir/$userfile"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;file&nbsp;has&nbsp;been&nbsp;deleted!"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>   Since the username and the filename are postable from a user form,    they can submit a username and a filename belonging to someone else,    and delete it even if they&#039;re not supposed to be allowed to do so.   In this case, you&#039;d want to use some other form of authentication.   Consider what could happen if the variables submitted were   &quot;../etc/&quot; and &quot;passwd&quot;. The code would then effectively read:    <div class="example">     <p><b>Example #2 ... A filesystem attack</b></p>     <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;removes&nbsp;a&nbsp;file&nbsp;from&nbsp;anywhere&nbsp;on&nbsp;the&nbsp;hard&nbsp;drive&nbsp;that<br />//&nbsp;the&nbsp;PHP&nbsp;user&nbsp;has&nbsp;access&nbsp;to.&nbsp;If&nbsp;PHP&nbsp;has&nbsp;root&nbsp;access:<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_submitted_name'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;"../etc"<br /></span><span style="color: #0000BB">$userfile&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'user_submitted_filename'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;"passwd"<br /></span><span style="color: #0000BB">$homedir&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/home/$username"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"/home/../etc"<br /><br /></span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #DD0000">"$homedir/$userfile"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;"/home/../etc/passwd"<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;file&nbsp;has&nbsp;been&nbsp;deleted!"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>    There are two important measures you should take to prevent these    issues.    <ul class="itemizedlist">     <li class="listitem">      <span class="simpara">       Only allow limited permissions to the PHP web user binary.      </span>     </li>     <li class="listitem">      <span class="simpara">       Check all variables which are submitted.      </span>     </li>    </ul>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?