cvs: phpdoc-zh /security filesystem.xml
| From: | liuming | Date: | Wed, 09 May 2007 12:17:36 +0000 |
| Subject: | cvs: phpdoc-zh /security filesystem.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-2895@lists.php.net to get a copy of this message | ||
lm92 Wed May 9 12:17:36 2007 UTC
Modified files:
/phpdoc-zh/security filesystem.xml
Log:
updated
http://cvs.php.net/viewvc.cgi/phpdoc-zh/security/filesystem.xml?r1=1.3&r2=1.4&diff_format=u
Index: phpdoc-zh/security/filesystem.xml
diff -u phpdoc-zh/security/filesystem.xml:1.3 phpdoc-zh/security/filesystem.xml:1.4
--- phpdoc-zh/security/filesystem.xml:1.3 Wed Aug 24 19:09:02 2005
+++ phpdoc-zh/security/filesystem.xml Wed May 9 12:17:36 2007
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="gb2312"?>
-<!-- $Revision: 1.3 $ -->
-<!-- $Author: dallas $ -->
-<!-- EN-Revision: 1.3 Maintainer: lm92 Status: ready -->
+<!-- $Revision: 1.4 $ -->
+<!-- $Author: lm92 $ -->
+<!-- EN-Revision: 1.5 Maintainer: lm92 Status: ready -->
<!-- CREDITS: dallas -->
<chapter id="security.filesystem">
<title>Îļþϵͳ°²È«</title>
@@ -26,26 +26,28 @@
<?php
// ´ÓÓû§Ä¿Â¼ÖÐɾ³ýÖ¸¶¨µÄÎļþ
$username = $_POST['user_submitted_name'];
+$userfile = $_POST['user_submitted_filename'];
$homedir = "/home/$username";
-$file_to_delete = "$userfile";
unlink ("$homedir/$userfile");
-echo "$file_to_delete has been deleted!";
+echo "The file has been deleted!";
?>
]]>
</programlisting>
</example>
- ¼ÈÈ» username
±äÁ¿¿ÉÒÔͨ¹ýÓû§±íµ¥À´Ìá½»£¬ÄǾͿÉÒÔÌá½»±ðÈ˵ÄÓû§ÃûºÍÎļþÃû£¬²¢É¾³ý¸ÃÎļþ¡£ÕâÖÖÇé¿öÏ£¬¾ÍÒª¿¼ÂÇÆäËü·½Ê½µÄÈÏÖ¤¡£ÏëÏë¿´Èç¹ûÌá½»µÄ±äÁ¿ÊÇ¡°../etc/¡±ºÍ¡°passwd¡±»á·¢Éúʲô¡£ÉÏÃæµÄ´úÂë¾ÍµÈͬÓÚ£º
+ ¼ÈÈ» username ºÍ filename
±äÁ¿¿ÉÒÔͨ¹ýÓû§±íµ¥À´Ìá½»£¬ÄǾͿÉÒÔÌá½»±ðÈ˵ÄÓû§ÃûºÍÎļþÃû£¬ÉõÖÁ¿ÉÒÔɾµô±¾À´²»Ó¦¸ÃÈÃËûÃÇɾ³ýµÄÎļþ¡£ÕâÖÖÇé¿öÏ£¬¾ÍÒª¿¼ÂÇÆäËü·½Ê½µÄÈÏÖ¤¡£ÏëÏë¿´Èç¹ûÌá½»µÄ±äÁ¿ÊÇ¡°../etc/¡±ºÍ¡°passwd¡±»á·¢Éúʲô¡£ÉÏÃæµÄ´úÂë¾ÍµÈͬÓÚ£º
<example>
<title>¡¡Îļþϵͳ¹¥»÷</title>
<programlisting role="php">
<![CDATA[
<?php
// ɾ³ýÓ²ÅÌÖÐÈκΠPHP ÓзÃÎÊȨÏÞµÄÎļþ¡£Èç¹û PHP ÓÐ root
ȨÏÞ£º
-$username = "../etc/";
-$homedir = "/home/../etc/";
-$file_to_delete = "passwd";
-unlink ("/home/../etc/passwd");
-echo "/home/../etc/passwd has been deleted!";
+$username = $_POST['user_submitted_name']; // "../etc"
+$userfile = $_POST['user_submitted_filename']; // "passwd"
+$homedir = "/home/$username"; // "/home/../etc"
+
+unlink("$homedir/$userfile"); // "/home/../etc/passwd"
+
+echo "The file has been deleted!";
?>
]]>
</programlisting>
@@ -71,18 +73,22 @@
<?php
// ɾ³ýÓ²ÅÌÖÐ PHP ÓÐȨ·ÃÎʵÄÎļþ
$username = $_SERVER['REMOTE_USER']; // ʹÓÃÈÏÖ¤»úÖÆ
-
+$userfile = basename($_POST['user_submitted_filename']);
$homedir = "/home/$username";
-$file_to_delete = basename("$userfile"); // È¥³ý±äÁ¿ÖеÄ·¾¶
-unlink ($homedir/$file_to_delete);
+$filepath = "$homedir/$userfile";
-$fp = fopen("/home/logging/filedelete.log","+a"); // ¼Ç¼ɾ³ý¶¯×÷
-$logstring = "$username $homedir $file_to_delete";
+
+if (file_exists($filepath) && unlink($filepath)) {
+ $logstring = "Deleted $filepath\n";
+} else {
+ $logstring = "Failed to delete $filepath\n";
+}
+$fp = fopen("/home/logging/filedelete.log", "a");
fwrite ($fp, $logstring);
fclose($fp);
-echo "$file_to_delete has been deleted!";
+echo htmlentities($logstring, ENT_QUOTES);
?>
]]>
</programlisting>
@@ -94,13 +100,13 @@
<![CDATA[
<?php
$username = $_SERVER['REMOTE_USER']; // ʹÓÃÈÏÖ¤»úÖÆ
+$userfile = $_POST['user_submitted_filename'];
$homedir = "/home/$username";
-if (!ereg('^[^./][^/]*$', $userfile))
- die('bad filename'); // Í£Ö¹Ö´ÐдúÂë
-
-if (!ereg('^[^./][^/]*$', $username))
- die('bad username'); // Í£Ö¹Ö´ÐдúÂë
+$filepath = "$homedir/$userfile";
+if (!ctype_alnum($username) || !preg_match('/^(?:[a-z0-9_-]|\.(?!\.))+$/iD', $userfile))
{
+ die("Bad username/filename");
+}
//ºóÂÔ¡¡
?>
]]>
@@ -112,6 +118,56 @@
»òÕß COM1£©¡¢ÅäÖÃÎļþ£¨/ect/ ÎļþºÍ
.iniÎļþ£©¡¢³£ÓÃµÄ´æ´¢ÇøÓò£¨/home/ »òÕß My
Documents£©µÈµÈ¡£ÓÉÓÚ´ËÔÒò£¬½¨Á¢Ò»¸ö²ßÂÔ½ûÖ¹ËùÓÐȨÏÞ¶øÖ»¿ª·ÅÃ÷È·ÔÊÐíµÄͨ³£¸üÈÝÒ×Щ¡£
</para>
+ <sect1 id="security.filesystem.nullbytes">
+ <title>Null ×Ö·ûÎÊÌâ</title>
+ <simpara>
+ ÓÉÓÚ PHP µÄÎļþϵͳ²Ù×÷ÊÇ»ùÓÚ C
ÓïÑԵĺ¯ÊýµÄ£¬ËùÒÔËü¿ÉÄÜ»áÒÔÄúÒâÏë²»µ½µÄ·½Ê½´¦Àí
+ Null ×Ö·û¡£
+ Null×Ö·ûÔÚ C
ÓïÑÔÖÐÓÃÓÚ±êʶ×Ö·û´®½áÊø£¬Ò»¸öÍêÕûµÄ×Ö·û´®ÊÇ´ÓÆä¿ªÍ·µ½Óö¼û
+ Null ×Ö·ûΪֹ¡£
+
+ ÒÔÏ´úÂëÑÝʾÁËÀàËÆµÄ¹¥»÷£º
+ </simpara>
+ <example>
+ <title>»á±» Null ×Ö·ûÎÊÌâ¹¥»÷µÄ´úÂë</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+$file = $_GET['file']; // "../../etc/passwd\0"
+if (file_exists('/home/wwwrun/'.$file.'.php')) {
+ // file_exists will return true as the file /home/wwwrun/../../etc/passwd exists
+ include '/home/wwwrun/'.$file.'.php';
+ // the file /etc/passwd will be included
+}
+?>
+]]>
+ </programlisting>
+ </example>
+ <para>
+
Òò´Ë£¬ÈκÎÓÃÓÚ²Ù×÷ÎļþϵͳµÄ×Ö·û´®£¨Òë×¢£ºÌرðÊdzÌÐòÍⲿÊäÈëµÄ×Ö·û´®£©¶¼±ØÐë¾¹ýÊʵ±µÄ¼ì²é¡£ÒÔÏÂÊÇÉÏÊöÀý×ӵĸĽø°æ±¾£º
+ </para>
+ <example>
+ <title>ÑéÖ¤ÊäÈëµÄÕýÈ·×ö·¨</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+$file = $_GET['file'];
+
+// ¶Ô×Ö·û´®½øÐа×Ãûµ¥¼ì²é
+switch ($file) {
+ case 'main':
+ case 'foo':
+ case 'bar':
+ include '/home/wwwrun/include/'.$file.'.php';
+ break;
+ default:
+ include '/home/wwwrun/include/main.php';
+}
+?>
+]]>
+ </programlisting>
+ </example>
+ </sect1>
</chapter>
<!-- Keep this comment at the end of the file