文件系统对于任何一个站点来说都是相当重要的,程序员们都在不遗余力的保护着自己的系统不受侵犯。今天我们就为大家讲解了PHP保护文件系统的具体代码示例。曾经有一个 Web 站点泄露了保存在 Web 服务器的文件中的客户数据。该 Web 站点的一个访问者使用 URL 查看了包含数据的文件。虽然文件被放错了位置,但是这个例子强调了针对攻击者保护文件系统的重要性。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>if ($_POST['submit'] == 'Download') { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">file</font></span><span> = $_POST['fileName']; </span> </li> <li class=""><span> header("Content-Type: application/x-octet-stream"); </span></li> <li class="alt"><span> header("Content-Transfer-Encoding: binary"); </span></li> <li class=""> <span> header("Content-Disposition: attachment; </span><span class="attribute"><font color="#ff0000">filename</font></span><span>="" . $file . "";" ); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">fh</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fopen</font></span><span>($file, 'r'); </span> </li> <li class=""><span> while (! feof($fh)) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo(fread($fh, 1024)); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> fclose($fh); </span></li> <li class="alt"><span>} else { </span></li> <li class=""> <span> echo("</span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span></span></span></font></strong><span>"); </span> </li> <li class="alt"> <span> echo("title</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Guard your filesystem</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></font></strong><span>"); </span> </li> <li class=""> <span> echo("</span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span class="tag"><span class="tag-name">form</span></span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">id</font></span><span>="myFrom" </span><span class="attribute"><font color="#ff0000">action</font></span><span>="" . $_SERVER['PHP_SELF'] . </span> </li> <li class="alt"> <span> "" </span><span class="attribute"><font color="#ff0000">method</font></span><span>="post"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"); </span> </li> <li class=""> <span> echo("</span><strong><font color="#006699"><span class="tag"><span class="tag-name">div</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span></span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>="text" </span><span class="attribute"><font color="#ff0000">name</font></span><span>="fileName" </span><span class="attribute"><font color="#ff0000">value</font></span><span>=""); </span> </li> <li class="alt"><span> echo(isset($_REQUEST['fileName']) ? $_REQUEST['fileName'] : ''); </span></li> <li class=""> <span> echo("" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>"); </span> </li> <li class="alt"> <span> echo("</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>="submit" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="Download" </span><span class="attribute"><font color="#ff0000">name</font></span><span>="submit" </span><strong><font color="#006699"><span class="tag">/></span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span></font></strong><span>"); </span> </li> <li class=""> <span> echo("</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span>"); </span> </li> <li class="alt"><span>} </span></li> </ol>
正如您所见,清单 1 中比较危险的脚本将处理 Web 服务器拥有读取权限的所有文件,包括会话目录中的文件(请参阅 “保护会话数据”),甚至还包括一些系统文件(例如 /etc/passwd)。为了进行PHP保护文件系统演示,这个示例使用了一个可供用户键入文件名的文本框,但是可以在查询字符串中轻松地提供文件名。
同时配置用户输入和文件系统访问权十分危险,因此最好把应用程序设计为使用数据库和隐藏生成的文件名来避免同时配置。但是,这样做并不总是有效。清单 2 提供了验证文件名的示例例程。它将使用正则表达式以确保文件名中仅使用有效字符,并且特别检查圆点字符:..。
<ol class="dp-xml"> <li class="alt"><span><span>function isValidFileName($file) { </span></span></li> <li class=""><span> /* don't allow .. and allow any "word" character / */ </span></li> <li class="alt"><span> return preg_match('/^(((?:.)(?!.))|w)+$/', $file); </span></li> <li class=""><span>} </span></li> </ol>
以上就是本文为大家分享的PHP保护文件系统的具体代码编写。