cvs: phpdoc-zh /security index.xml
| From: | Gregory Song | Date: | Mon, 23 Jun 2003 06:36:38 +0000 |
| Subject: | cvs: phpdoc-zh /security index.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-1545@lists.php.net to get a copy of this message | ||
gregory Mon Jun 23 02:36:38 2003 EDT
Added files:
/phpdoc-zh/security index.xml
Log:
chapters/security.xml is moved here.
Index: phpdoc-zh/security/index.xml +++ phpdoc-zh/security/index.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <!-- $Author: gregory $ --> <!-- EN-Revision: 1.58 Maintainer: Gregory Status: ready --> <chapter id="security.index"> <title>°²È«</title> <simpara> PHP ÊÇÒ»Öַdz£Ç¿´óµÄÓïÑÔ£¬Æä½âÊÍÆ÷²»¹ÜÊÇ×÷Ϊ WEB ·þÎñÆ÷µÄÄ£¿é»¹Êǵ¥¶ÀµØÒÔ <acronym>CGI</acronym> ³ÌÐòµÄÐÎʽÔËÐУ¬¶¼Äܹ»·ÃÎÊÎļþ£¬Ö´ÐÐÃüÁ»òÕß½¨Á¢µ½·þÎñÆ÷µÄÍøÂçÁ¬½Ó¡£ÔÚĬÈÏÇé¿öÏ£¬ÕâÐ©ÌØÐÔ¸øÔÚ WEB ÉÏÔËÐеĻúÖÆ´øÀ´Á˰²È«Òþ»¼¡£Õë¶ÔÕâÒ»ÎÊÌ⣬PHP ±»Éè¼Æ³ÉÒ»ÖÖ¸ü¼Ó°²È«µÄÓïÑÔ£¬Ê¹µÃÆä¸ú Perl ºÍ C ÓïÑÔ±ÈÆðÀ´£¬¸üÊʺÏÓÃÀ´±àд CGI ³ÌÐò¡£¼ÓÉÏÆäÓÐÁ¼ºÃµÄ±àÒëʱºÍÔËÐÐʱÉèÖÃÑ¡ÏîÒÔ¼°Êʵ±µÄ´úÂë±àд¹æÔò£¬PHP Äܹ»ÎªÄúÕÒµ½ÄúÕæÕýÐèÒªµÄ×ÔÓÉÓ밲ȫµÄ½áºÏµã¡£ </simpara> <simpara> ÓÉÓÚÎÒÃÇ¿ÉÒÔÔںܶ಻ͬµÄ·½ÃæÀûÓà PHP£¬Òò´ËËüÓкܶàÉèÖÃÑ¡ÏîÀ´¿ØÖÆÆäÐÐΪ¡£Ò»×éÅÓ´óµÄ¿ÉÑ¡²ÎÊýÄܹ»±£Ö¤Äú¿ÉÒÔ½« PHP ÓÃÓÚÐí¶à²»Í¬µÄÄ¿µÄ£¬µ«ÕâͬʱҲÒâζ×ÅÕâЩ²ÎÊýºÍ·þÎñ¶ËÅäÖõÄ×éºÏ»á´øÀ´Ò»Ð©°²È«ÎÊÌâ¡£ </simpara> <simpara> PHP µÄÅäÖÃÓëÆä´úÂëÏà±È£¬ÓÐ×ÅͬÑùµÄÁé»îÐÔ¡£PHP ¿ÉÒÔÓÃÀ´½¨Á¢ÍêÕûµÄ·þÎñ¶ËÓ¦ÓóÌÐò£¬ÓµÓÐËùÓÐÍâ¿ÇÓû§µÄȨÏÞ£»ËüÒ²¿ÉÒÔÔÚ±»Ñϸñ¿ØÖƵĻ·¾³ÏÂÓÃ×÷Ò»¸ö¼òµ¥µÄ·þÎñ¶Ë°üº¬£¬½ö³Ðµ£ºÜСµÄ·çÏÕ¡£ÄúÈçºÎ½¨Á¢¸Ã»·¾³£¬ÒÔ¼°Æä°²È«ÐÔÈçºÎ£¬Ôںܴó³Ì¶ÈÉÏÈ¡¾öÓÚ PHP µÄ¿ª·¢Õß¡£ </simpara> <simpara> ±¾Õ½«ÒÔһЩ×ܵݲȫ½¨Ò鿪ʼ£¬½âÊͲ»Í¬µÄÅäÖÃÑ¡Ïî×éºÏÒÔ¼°ËüÃÇÄܹ»±»°²È«µÄʹÓõÄÇé¿ö£¬È»ºóÕë¶Ô²»Í¬µÄ°²È«µÈ¼¶ÒªÇó£¬ÃèÊöһЩÔÚ±àд´úÂë¹ý³ÌÖвÉÈ¡µÄ°²È«²ßÂÔ¡£ </simpara> <sect1 id="security.general"> <title>Ò»°ã²ßÂÔ</title> <simpara> Ò»¸ö¾ø¶Ô°²È«µÄϵͳÊDz»¿ÉÄÜʵÏֵģ¬Òò´ËÒ»¸ö°²È«²ßÂԵĺËÐÄͨ³£¶¼ÊÇѰÇó·çÏÕÓë¿ÉÓÃÐÔÖ®¼äµÄƽºâµã¡£Èç¹ûÓû§Ìá½»µÄÿ¸ö±äÁ¿¶¼ÐèÒªÁ½ÖÖÉúÎïͳ¼ÆÑ§µÄУÑ飨ÀýÈçÊÓÍøÄ¤É¨ÃèºÍÖ¸ÎÆ¼ìÑ飩£¬ÄÇôÎÒÃǽ«»áÐèÒª½øÐм«Æä¸ß½×µÄ¼ÆËã¡£Õ⻹¿ÉÄÜÔì³ÉÎÒÃÇÐèÒª»¨·Ñ°ë¸öСʱÀ´Ìîдһ¸ö¼°Æä·±ËöµÄ±íµ¥£¬Ê¹µÃÓû§¸üÇãÏòÓÚѰÕÒһЩ½Ý¾¶À´ÈƹýÕâЩ°²È«»úÖÆ¡£ </simpara> <simpara> ×îºÃµÄ°²È«²ßÂÔͨ³£Äܹ»²»ÄÇôÃ÷ÏÔµØÊÊÓ¦»·¾³µÄÐèÇó£¬Ëü²»»á·Á°Óû§Íê³ÉËûÃǵŤ×÷£¬Ò²²»»áʹ´úÂë±àÐ´Ô±Ãæ¹ý·Ö¸ºµ£¸´ÔÓµÄÇéÐΡ£Êµ¼ÊÉÏ£¬Ò»Ð©°²È«¹¥»÷µÄ³É¹¦ÕýÊÇÕâÖÖ¹ý·ÖÈßÔӵݲȫ»úÖÆËæ×Åʱ¼äÖð½¥»Ù»µµÄ½á¹û¡£ </simpara> <simpara> ÎÒÃÇÓ¦¸Ã¼ÇסÕâÑùÒ»¾äºÜÓмÛÖµµÃ»°£ºÒ»¸öϵͳ³äÆäÁ¿½öÏ൱ÓÚÒ»ÌõËøÁ´ÖÐ×ÈõµÄ»·½Ú¡£Èç¹ûËùÓеÄÊÂÎñµÄʱ¼ä¡¢µØµã¡¢ÀàÐ͵ÈÐÅÏ¢¶¼±»ÏêϸµÄдÈëÈÕÖ¾£¬µ«¶ÔÓû§µÄÈÏÖ¤½ö½öÖ»ÊÇ»ùÓÚÒ»¸öµ¥Ò»µÄ cookie£¬ÄÇô½«Óû§¼ÇÈëÊÂÎñÈÕÖ¾µÄÓÐЧÐԾͱ»ÑÏÖØµØÏ÷ÈõÁË¡£ </simpara> <simpara> ÔÚ²âÊÔµÄʱºò£¬Çë¼Çס£¬¼´Ê±ÊǶÔ×î¼òµ¥µÄÒ³Ãæ£¬ÄúÒ²²»¿ÉÄܲâÊÔµ½ËùÓеĿÉÄÜÐÔ¡£ÄúËùÔ¤Áϵ½µÄÊäÈë¿ÉÄܺÍÀ´×ÔÓÚÒ»¸ö²»ÂúµÄ¹ÍÔ±£¬Ò»¸öºÚ¿ÍÓÃÊýÔÂʱ¼ä»òÕßÒ»Ö»²»Ð¡ÐIJȵ½¼üÅ̵ÄèËù½øÐеÄÊäÈëÏàÈ¥ÉõÔ¶¡£ÕâÒ²ÊÇΪʲô˵ÎÒÃÇ×îºÃÄܹ»´ÓÂß¼µÄÈ«¾ÖÉÏÀ´ÉóÊÓÎÒÃǵĴúÂ룬´Ó¶ø±æ±ðÄÄÀï³öÏÖ²»¿ÉÔ¤ÁϵÄÊý¾Ý£¬È»ºóÔÙ¿¼ÂÇÈçºÎÐ޸ġ¢¼õÉÙ»òÕß·Å´óËüÃÇ¡£ </simpara> <simpara> Internet ÉϳäÂúÁËÏëÊÔͼͨ¹ýÆÆ»µÄãµÄ´úÂë¡¢ºÚµôÄãµÄÍøÕ¾¡¢·¢²¼²»ÊʺϵÄÄÚÈݵÈ;¾¶À´ÑïÃûÁ¢ÍòµÄÈË¡£²»¹ÜÄãµÄÍøÕ¾ÊÇ´ó»¹ÊÇС£¬Ö»ÒªÄãÔÚÏߣ¬ÓÐÒ»²¿¿ÉÒÔÁ¬½ÓµÃµ½µÄ·þÎñÆ÷£¬Äã¾ÍÄܳÉΪĿ±ê¡£Ðí¶àºÚ¿Í³ÌÐò²¢²»·Ö±çÍøÕ¾µÄ´óС£¬ËûÃÇÖ»ÊǼòµ¥µØÔÚÒ»´óƬ IP ·¶Î§ÄÚËÑÂÞÎþÉüÕß¡£ÎÒÃÇÒª¾¡¿ÉÄܲ»ÒªÊ¹×Ô¼º³ÉΪÆäÖеÄÒ»Ô±¡£ </simpara> </sect1> <sect1 id="security.cgi-bin"> <title>°²×°Îª CGI ³ÌÐò</title> <sect2 id="security.cgi-bin.attacks"> <title>¿ÉÄÜÊܵ½µÄ¹¥»÷</title> <simpara> Èç¹ûÄú²»Ï£Íû½« PHP ÒÔÄ£¿éµÄ·½Ê½¼¯³Éµ½·þÎñÆ÷Èí¼þ£¨ÀýÈç Apache£©ÖУ¬»òÕßÏ£Íû½« PHP ÓÃÓÚ²»Í¬µÄ CGI Íâ¿ÇÀ´½¨Á¢°²È«µÄ chroot ºÍ setuid ½Å±¾»·¾³£¬½« PHP ÓÃ×÷ <acronym>CGI</acronym> ³ÌÐòÊǰ²×°Ê±µÄÒ»ÖÖÑ¡Ôñ¡£ÕâÑùµÄ°²×°Í¨³£¶¼ÐèÒª½«¿ÉÖ´ÐÐµÄ PHP ³ÌÐò·ÅÖõ½ cgi-bin Ŀ¼¡£CERT ¹ËÎÊ <ulink url="&url.cert;">CA-96.11</ulink> µÄ½¨Òé·´¶Ô½«ÈκνâÊÍÆ÷·ÅÈë cgi-bin Ŀ¼¡£¾¡¹Ü PHP µÄ¿ÉÖ´ÐгÌÐòÄܹ»±»ÓÃ×÷¶ÀÁ¢µÄ½âÊÍÆ÷£¬PHP µÄ°²È«»úÖÆÒ²ÄÜ·ÀÖ¹ÓÉÕâÑùµÄ°²×°Ëùµ¼ÖµĿÉÄܵĹ¥»÷£º </simpara> <itemizedlist> <listitem> <simpara> ·ÃÎÊϵͳÎļþ£º<filename role="url">http://my.host/cgi-bin/php?/etc/passwd</filename> </simpara> <simpara> ÔÚ URL ÖÐÎʺţ¨?£©ºóÃæµÄ²éѯÐÅÏ¢½«±» CGI ½Ó¿Ú×÷ΪÃüÁîÐвÎÊý´«¸ø½âÊÍÆ÷¡£Í¨³£Çé¿öÏ£¬½âÊÍÆ÷»á´ò¿ª²¢ÔËÐÐÓÉÃüÁîÐеÚÒ»¸ö²ÎÊýËùÖ¸Ã÷µÄÎļþ¡£ </simpara> <simpara> µ± PHP ÒÔ CGI ³ÌÐòµÄ·½Ê½±»¼¤»îʱ£¬Ëü½«¾Ü¾ø½âÊ͸ÃÃüÁîÐвÎÊý¡£ </simpara> </listitem> <listitem> <simpara> ·ÃÎÊ·þÎñÆ÷ÉϵÄÈκΠWEB Îĵµ£º<filename role="url">http://my.host/cgi-bin/php/secret/doc.html</filename> </simpara> <simpara> URL ÖÐ PHP ³ÌÐòÃû³ÆºóÃæµÄ·¾¶ÐÅÏ¢ <filename role="uri">/secret/doc.html</filename> ϰ¹ßÉÏÊÇÓÃÀ´Ö¸Ã÷ÓÉ <acronym>CGI</acronym> ³ÌÐò´ò¿ª²¢½âÊ͵ÄÎļþÃû¡£Í¨³£ WEB ·þÎñÆ÷»áͨ¹ýһЩµÄÅäÖÃÑ¡ÏApache: Action£©½«¶ÔÀàËÆÓÚ <filename role="url">http://my.host/secret/script.php</filename> ÎĵµµÄÇëÇóÖØ¶¨Ïòµ½ PHP ½âÊÍÆ÷¡£WEB ·þÎñÆ÷Ïȼì²éĿ¼ <filename role="uri">/secret</filename> µÄ·ÃÎÊȨÏÞ£¬È»ºóÔÙ½¨Á¢ÇëÇóµÄÖØ¶¨Ïò <filename role="url">http://my.host/cgi-bin/php/secret/script.php</filename>¡£µ«²»ÐÒµÄÊÇ£¬Èç¹ûÇëÇóÖ±½ÓÒÔÕâÖÖÐÎʽ¸ø³ö£¬ÄÇô WEB ·þÎñÆ÷¾Í²»»áÖ´ÐжÔÎļþ <filename role="uri">/secret/script.php</filename> µÄ·ÃÎÊȨÏÞ¼ì²é£¬¶ø½ö½öÖ»¼ì²éÎļþ <filename role="uri">/cgi-bin/php</filename> µÄ·ÃÎÊȨÏÞ¡£ÕâÑùÒ»À´£¬ÈκοÉÒÔ·ÃÎÊ <filename role="uri">/cgi-bin/php</filename> µÄÓû§¶¼¿ÉÒÔ·ÃÎÊÈκΠWEB ·þÎñÆ÷ÉÏÊÕµ½±£»¤µÄÎĵµ¡£ </simpara> <simpara> ÔÚ PHP ÖУ¬±àÒëʱÉèÖÃÑ¡Ïî <link linkend="install.configure.enable-force-cgi-redirect">--enable-force-cgi-redirect</link> ÒÔ¼°ÔËÐÐʱÉèÖÃÑ¡Ïî <link linkend="ini.doc-root">doc_root</link> ºÍ <link linkend="ini.user-dir">user_dir</link> Äܹ»ÓÃÀ´·ÀÖ¹ÕâÖÖ¹¥»÷£¬Èç¹û·þÎñ¶ËµÄĿ¼Ê÷´æÔÚÓзÃÎÊÏÞÖÆµÄĿ¼¡£ÒÔϽ«Ïêϸ½âÊ͸÷ÖÖ²»Í¬µÄ×éºÏ¡£ </simpara> </listitem> </itemizedlist> </sect2> <sect2 id="security.cgi-bin.default"> <title>·½·¨Ò»£º½ö¾Í¹«ÖÚÎļþÌṩ·þÎñ</title> <simpara> Èç¹ûÄúµÄ·þÎñÆ÷ÉÏûÓÐÈκβ»±»ÃÜÂë»òÕß»ùÓÚ IP ·ÃÎÊÏÞÖÆ¿ØÖƵÄÄÚÈÝ£¬ÔòÕâЩѡÏîÅäÖöÔÄú²¢Ã»ÓÐ×÷Óá£Èç¹ûÄúµÄ WEB ·þÎñÆ÷²»ÔÊÐíÄú½øÐÐÖØ¶¨Ïò£¬»òÕß·þÎñÆ÷ÎÞ·¨ºÍ PHP ³ÌÐòͨÐÅÒÔʶ±ð°²È«µÄÖØ¶¨ÏòÇëÇó£¬Äú¿ÉÒÔÔÚÔËÐÐÅäÖýű¾µÄʱºòʹÓà <link linkend="install.configure.enable-force-cgi-redirect">--enable-force-cgi-redirect</link> ²ÎÊý¡£Äú»¹ÐèҪȷÈÏÄúµÄ PHP ½Å±¾²»½ö½öÖ»ÒÀÀµÄ³Ò»ÖÖ·½·¨À´µ÷Óýű¾£¬²»¹ÜÊÇÖ±½Óµ÷Óà <filename role="php">http://my.host/cgi-bin/php/dir/script.php</filename>£¬»¹ÊÇÖØ¶¨Ïòµ½ <filename role="php">http://my.host/dir/script.php</filename>¡£ </simpara> <simpara> ÔÚ Apache ÖÐÖØ¶¨Ïò¿ÉÒÔʹÓÃÑ¡Ïî AddHandler ºÍ Action À´ÅäÖã¨Çë²Î¿¼ÏÂÎÄ£©¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.force-redirect"> <title>·½·¨¶þ£ºÊ¹Óà --enable-force-cgi-redirect ²ÎÊý</title> <simpara> ¸Ã±àÒëʱ²ÎÊý·ÀÖ¹ÈκÎÈËͨ¹ýÖ±½Ó·ÃÎÊÀàËÆÓÚ <filename role="php">http://my.host/cgi-bin/php/secretdir/script.php</filename> µÄ URL À´µ÷Óà PHP¡£Ò²¾ÍÊÇ˵£¬Ö»Óе±½Å±¾»ùÓںϷ¨µÄÖØ¶¨Ïò¹æÔò±»·ÃÎÊʱ£¬PHP ²Å¶ÔËüÃǽøÐнâÎö¡£ </simpara> <simpara> ͨ³£ Apache µÄÖØ¶¨ÏòÉèÖÃÊÇͨ¹ýÒÔÏÂÑ¡ÏîÍê³ÉµÄ£º </simpara> <programlisting role="apache-conf"> <![CDATA[ Action php-script /cgi-bin/php AddHandler php-script .php ]]> </programlisting> <simpara> ¸ÃÑ¡Ïî½ö½öÔÚ Apache WEB ·þÎñÆ÷ÏÂ×ö¹ý²âÊÔ£¬²¢ÇÒÒÀÀµ Apache Ϊ±»Öض¨ÏòµÄÇëÇóÉèÖ÷DZê×¼µÄ CGI »·¾³±äÁ¿ <envar>REDIRECT_STATUS</envar>¡£Èç¹ûÄúµÄ WEB ·þÎñÆ÷²»Ö§³ÖÈκη½Ê½ÒÔ»ñϤÇëÇóÊÇÖ±½ÓµÄ»¹ÊDZ»Öض¨ÏòµÄ£¬Äú½«ÎÞ·¨Ê¹ÓøÃÑ¡Ïî¡£Äú±ØÐëʹÓñ¾ÎÄÌá¼°µÄÆäËüÔËÐÐ CGI °æ±¾µÄ·½·¨¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.doc-root"> <title>·½·¨Èý£ºÉèÖà doc_root »òÕß user_dir</title> <simpara> ÔÚ WEB ·þÎñÆ÷µÄÎĵµÄ¿Â¼ÖзÅÖÃÖîÈç½Å±¾ºÍ¿ÉÖ´ÐгÌÐòµÈÖ÷¶¯µÄÄÚÈÝ£¬³£³£±»ÈÏΪÊÇÒ»ÖÖ²»°²È«µÄ×ö·¨¡£Èç¹ûÓÉÓÚһЩÉèÖõĴíÎó£¬ÕâЩ½Å±¾Ã»Óб»Ö´ÐжøÏñͨ³£µÄ HTML ÎĵµÒ»Ñù±»ÏÔʾ£¬Õ⽫¿ÉÄܵ¼ÖÂ֪ʶ²úȨ»òÕßÖîÈçÃÜÂëµÈ°²È«ÐÅÏ¢µÄй©¡£Òò´ËºÜ¶àϵͳ¹ÜÀíÔ±ÄþԸΪ½Å±¾½¨Á¢ÁíÍâÒ»¸öÖ»Äܱ» PHP CGI ·ÃÎʵÄĿ¼½á¹¹£¬Ê¹µÃËüÃÇÄܹ»±»ÕýÈ·½âÎö£¬ÓÖ²»»á³öÏÖÉÏÊöµÄÎÊÌâ¡£ </simpara> <simpara> ͬÑù£¬Èç¹ûÈçÇ°ÃæÕ½ÚÃèÊöµÄÈ·±£ÇëÇó²»±»Öض¨ÏòµÄ·½·¨ÎÞЧ£¬ÄÇôÎÒÃǾÍÓбØÒª½¨Á¢Ò»¸öÇø±ðÓÚ WEB Îĵµ¸ùĿ¼½Å±¾µÄ doc_root Ŀ¼¡£ </simpara> <simpara> Äú¿ÉÒÔͨ¹ý<link linkend="configuration.file">ÅäÖÃÎļþ</link>ÖеÄÑ¡Ïî <link linkend="ini.doc-root">doc_root</link> »òÕß»·¾³±äÁ¿ <envar>PHP_DOCUMENT_ROOT</envar> À´ÉèÖà PHP ½Å±¾ÎĵµµÄ¸ùĿ¼¡£Èç¹û¸ÃÑ¡Ïî±»ÉèÖã¬ÄÇô PHP µÄ CGI °æ±¾½«×ÜÊÇֻͨ¹ý <parameter>doc_root</parameter> ºÍÇëÇóµÄ·¾¶ÐÅÏ¢À´´ò¿ªÎĵµ£¬ÕâÑùÄú¾Í¿ÉÒÔÈ·±£½Å±¾²»»áÔÚ¸ÃĿ¼ÒÔÍâÖ´ÐУ¨³ýÁËÒÔϽ«Ìá¼°µÄ <parameter>user_dir</parameter> Ŀ¼£©¡£ </simpara> <simpara> ÁíÍâÒ»¸öÕâÀï¿ÉÄÜÓõõ½µÄÑ¡ÏîÊÇ <link linkend="ini.user-dir">user_dir</link>¡£Èç¹û user_dir ûÓб»ÉèÖã¬ÄÇôΨһ¿ØÖƱ»´ò¿ªµÄÎļþÃûµÄÑ¡Ïî¾ÍÊÇ <parameter>doc_root</parameter>¡£´ò¿ªÒ»¸öÀàËÆÓÚ <filename role="url">http://my.host/~user/doc.php</filename> ½«²»»áµ¼Ö´ò¿ªÒ»¸öÓû§Ö÷Ŀ¼ÖеÄÎļþ£¬¶øÊÇ doc_root ÏÂÒ»¸öÃûΪ <filename role="uri">~user/doc.php</filename> µÄÎļþ£¨¼´Ò»¸öÒÔ²¨À˺Å[<literal>~</literal>]¿ªÍ·µÄĿ¼Ãû£©¡£ </simpara> <simpara> Èç¹û user_dir ±»ÉèÖÃΪ£¬ÀýÈ磬 <filename role="dir">public_php</filename>£¬ÄÇôÀàËÆÓÚ <filename role="url">http://my.host/~user/doc.php</filename> µØÇëÇ󽫴ò¿ªÒ»¸ö¸ÃÓû§Ö÷Ŀ¼ÏÂÃûΪ <filename role="dir">public_php</filename> µÄĿ¼ÏµÄÃûΪ <filename>doc.php</filename> µÄÎļþ¡£Èç¹ûÓû§µÄÖ÷Ŀ¼ÊÇ <filename role="dir">/home/user</filename>£¬ÄÇô±»Ö´ÐеÄÎļþÊÇ <filename>/home/user/public_php/doc.php</filename>¡£ </simpara> <simpara> <parameter>user_dir</parameter> À©Õ¹²¢²»ÒÀÀµÓÚ <parameter>doc_root</parameter> Ñ¡ÏîµÄÉ趨£¬Òò´ËÄú¿ÉÒÔ¶ÀÁ¢¿ØÖÆÎĵµ¸ùĿ¼ºÍÓû§·ÃÎÊĿ¼¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.shell"> <title>·½·¨ËÄ£ºÔÚ WEB Ŀ¼Ê÷ÒÔÍâ½øÐÐ PHP ½âÎö</title> <para> Ò»ÖÖ°²È«ÐԷdz£¸ßµÄ·½·¨ÊÇ°Ñ PHP ½âÎö³ÌÐò·ÅÖõ½ WEB ÎļþĿ¼Ê÷ÒÔÍâµÄij¸öµØ·½£¬ÀýÈ磬·ÅÖõ½ <filename role="dir">/usr/local/bin</filename>¡£ÕâÖÖ×ö·¨Î¨Ò»µÄ±×²¡¾ÍÊÇÄúÏÖÔÚÐèÒªÔÚËùÓк¬ÓÐ PHP ±ê¼Ç·ûÎļþµÄµÚÒ»ÐÐÌí¼ÓÀàËÆÓÚÒÔϵÄÄÚÈÝ£º <informalexample> <programlisting> <![CDATA[ #!/usr/local/bin/php ]]> </programlisting> </informalexample> ÄúÐèҪʹµÃÕâЩÎļþ³ÉΪ¿ÉÖ´ÐÐÎļþ¡£Ò²¾ÍÊÇ˵£¬Ïñ¶Ô´ýÈÎºÎÆäËü CGI ½Å±¾Ò»ÑùÀ´¶Ô´ýËüÃÇ¡£ÕâЩ CGI ½Å±¾¿ÉÄÜÊÇÓà Perl¡¢sh »òÕßÈÎºÎÆäËüʹÓÃµÄ <literal>#!</literal> shell-escape »úÖÆÀ´Æô¶¯ËüÃÇ×ÔÉíµÄ½Å±¾ÓïÑÔÀ´±àдµÄ¡£ </para> <para> ҪʹÕâ¸ö·½·¨ÖÐ PHP Äܹ»ÕýÈ·´¦Àí <envar>PATH_INFO</envar> ºÍ <envar>PATH_TRANSLATED</envar> ÐÅÏ¢£¬ÔÚ±àÒë PHP ½âÎöÆ÷ʱ±ØÐëʹÓÃÅäÖòÎÊý <link linkend="install.configure.enable-discard-path">--enable-discard-path</link>¡£ </para> </sect2> </sect1> <sect1 id="security.apache"> <title>°²×°Îª Apache Ä£¿é</title> <simpara> µ± PHP ±»ÓÃ×÷ Apache µÄÄ£¿éʱ£¬Ëü½«¼Ì³Ð Apache µÄÓû§È¨ÏÞ£¨µäÐÍÇé¿öΪÓû§¡°nobody¡±£©¡£ Õ⽫¶Ô°²È«¼°ÊÚȨ»úÖÆ²úÉúһЩ³å»÷¡£ÀýÈ磬Èç¹ûÄúʹÓà PHP À´·ÃÎÊÊý¾Ý¿â£¬³ý·ÇÊý¾Ý¿â±¾ÉíÓÐÄÚ½¨µÄ·ÃÎÊ¿ØÖÆ£¬·ñÔòÄú½«Ê¹µÃÊý¾Ý¿âÄܹ»±»Óû§¡°nobody¡±·ÃÎÊ¡£ÕâÒâζ×ŶñÒâµÄ½Å±¾Äܹ»·ÃÎʲ¢ÐÞ¸ÄÊý¾Ý¿â£¬ÉõÖÁ²»ÐèÒªÓû§ÃûºÍÃÜÂë¡£ÍøÂçºÚ¿ÍÎÞÒâÖзÃÎʵ½ÁËÊý¾Ý¿â¹ÜÀíÔ±µÄ WEB Ò³Ãæ²¢Í¨¹ýÕâÀïɾ³ýËùÓеÄÊý¾Ý¿âÊÇÍêÈ«ÓпÉÄÜ·¢ÉúµÄ¡£Äú¿ÉÒÔÀûÓà Apache µÄÈÏÖ¤»úÖÆÀ´·ÀÖ¹ÕâЩµÄ·¢Éú£¬»òÕßÒ²¿ÉÒÔÀûÓà LDAP »ò &htaccess; ÎļþµÈÀ´Éè¼ÆÄú×Ô¼ºµÄ·ÃÎÊģʽ£¬²¢½«ÕâЩ´úÂë°üº¬ÎªÄú PHP ½Å±¾µÄÒ»²¿·Ö¡£ </simpara> <simpara> ͨ³££¬Ò»µ©°²È«»úÖÆ½¨Á¢²¢Ê¹µÃ PHP µÄÓÃÓÚ£¨ÔÚÕâÖÖÇé¿öÏ£¬Îª Apache Óû§£©½öΪ´Ë³Ðµ£ºÜСµÄ·çÏÕʱ£¬ÎÒÃÇ·¢ÏÖ PHP ´Ëʱ±»½ûÖ¹ÍùÓû§Ä¿Â¼Ð´ÈëÈκÎÎļþ£¬»òÕß»¹ÓпÉÄܱ»½ûÖ¹·ÃÎʺ͸ü¸ÄÊý¾Ý¿â¡£ÎÞÂÛÍù·ÀÖ¹µÄ¶ÔÏóÖÐдÈëÎļþµÄºÃ»µÒÔ¼°½øÈëµÄÊý¾Ý¿âÊÂÎñµÄºÃ»µ£¬Æä°²È«ÐÔ¶¼ÊÇͬµÈµÄ¡£ </simpara> <simpara> ÔÚÕâ¸öʱºò£¬Ò»¸öƵ·±³öÏֵݲȫ´íÎóÊǸø Apache root ȨÏÞ£¬»òÕßÓÃÆäËü·½·¨ÌáÉý Apache µÄÄÜÁ¦¡£ </simpara> <simpara> ¸ø Apache Óû§¸³Óè root ȨÏÞÊǼ°ÆäΣÏյ쬶øÇÒÓпÉÄÜ»áÁ¬ÀÛÕû¸öϵͳ¡£Òò´Ë£¬½øÐÐ sudo¡¢chroot£¬»òÕßÒÔ root Õ˺ÅÔËÐеȲÙ×÷²»Ó¦¸Ã¿¼ÂÇÈÃÄÇЩ·Ç°²È«×¨¼ÒÀ´Ö´ÐС£ </simpara> <simpara> »¹ÓÐһЩ¸ü¼òµ¥µÄÇé¿ö¡£Äú¿ÉÒÔʹÓà <link linkend="ini.open-basedir">open_basedir</link> À´¿ØÖƺÍÏÞÖÆ PHP Äܹ»Ê¹ÓõÄĿ¼¡£Äú»¹¿ÉÒÔ½¨Á¢ Apache µÄרÓÃÇøÓò£¬ÒÔ½«ËùÓлùÓÚ WEB µÄ»î¶¯¶¼ÏÞÖÆµ½·ÇÓû§¡¢ÏµÍ³ºÍÎļþ¡£ </simpara> </sect1> <sect1 id="security.filesystem"> <title>Îļþϵͳ°²È«</title> <simpara> PHP ÊÜ´ó¶àÊý·þÎñÆ÷ϵͳ°²È«»úÖÆµÄ¿ØÖÆ£¬ÕâЩ°²È«»úÖÆÍ¨³£ÊÇ»ùÓÚÎļþºÍĿ¼Ðí¿É»ù´¡µÄ¡£ÕâʹµÃÄú¿ÉÒÔ¿ØÖÆÎļþϵͳÖеÄÄÄЩÎļþ¿ÉÒÔ±»¶ÁÈ¡¡£ÄúÐèÒªÌØ±ð×¢ÒâËùÓÐÈ«¾Ö¿É¶ÁµÄÎļþ£¬Òª±£Ö¤ËùÓзÃÎʸÃÎļþϵͳµÄÓû§¶ÔËüÃǵĶÁÈ¡Êǰ²È«µÄ¡£ </simpara> <simpara> ÓÉÓÚ PHP ÔÚÉè¼ÆµÄʱºò£¬ÔÊÐíÓû§¼¶·ÃÎÊÎļþϵͳ£¬Òò´Ë±àдһ¸ö PHP ½Å±¾Ê¹µÃÄúÄܹ»¶ÁÈ¡ÖîÈç /etc/passwd£¬¸ü¸ÄÄúµÄÒÔÌ«ÍøÁ¬½Ó£¬·¢ËÍ·±ÖصĴòÓ¡»úÈÎÎñµÈÊÇÍêÈ«ÓпÉÄܵġ£ÕâÀïÓÐһЩÃ÷ÏԵĹØÁª£¬ÄúÐèÒª±£Ö¤Äú¶ÁÈ¡ºÍдÈëµÄÊÇÕýÈ·µÄÎļþ¡£ </simpara> <simpara> ÔÚÒÔϽű¾ÖУ¬Ò»¸öÓû§Ï£Íû¸Ã½Å±¾Äܹ»É¾³ýËüÃÇÖ÷Ŀ¼ÖеÄÒ»¸öÎļþ¡£Õâ½ö½öÊǼÙÉèÁËÒ»ÖÖ PHP WEB ½Ó¿Úͨ³£±»ÓÃ×÷Îļþ¹ÜÀíµÄÇé¿ö£¬Òò´Ë Apache Óû§±»ÔÊÐíɾ³ýÓû§Ö÷Ŀ¼µÄÎļþ¡£ </simpara> <para> <example> <title>±¡ÈõµÄ±äÁ¿¼ìÑ齫µ¼Ö¡¡</title> <programlisting role="php"> <![CDATA[ <?php // remove a file from the user's home directory $username = $_POST['user_submitted_name']; $homedir = "/home/$username"; $file_to_delete = "$userfile"; unlink ("$homedir/$userfile"); echo "$file_to_delete has been deleted!"; ?> ]]> </programlisting> </example> ÓÉÓÚÓû§ÃûÊÇ¿ÉÒÔ´ÓÓû§±íµ¥·¢Ë͵ģ¬Òò´ËËûÃÇ¿ÉÒÔÌá½»Ò»¸öÓû§ÃûºÍÊôÓÚÆäËûÈ˵ÄÎļþ£¬Ê¹µÃÕâЩÎļþ±»É¾³ý¡£ÔÚ¸ÃÇé¿öÏ£¬Äú»áÐèҪʹÓÃÁíÍâµÄ±íµ¥À´ÊÚȨ¡£¿¼ÂÇÒ»ÏÂÈç¹û±»Ìá½»µÄ±äÁ¿ÊÇ¡°../etc/¡±ºÍ¡°passwd¡±½«»á·¢Éúʲô£¿Õâ¸öʱºòÆä´úÂë¿ÉÒÔ±»Àí½âΪ£º <example> <title>Ò»¸öÎļþϵͳ¹¥»÷</title> <programlisting role="php"> <![CDATA[ <?php // removes a file from anywhere on the hard drive that // the PHP user has access to. If PHP has root access: $username = "../etc/"; $homedir = "/home/../etc/"; $file_to_delete = "passwd"; unlink ("/home/../etc/passwd"); echo "/home/../etc/passwd has been deleted!"; ?> ]]> </programlisting> </example> ÓÐÁ½ÖÖÄú±ØÐë²ÉÈ¡µÄ´ëÊ©À´±ÜÃâÕâЩÎÊÌâ¡£ <itemizedlist> <listitem> <simpara> ¶Ô PHP WEB Óû§½ö¸³ÓèÓÐÏÞµÄȨÏÞ¡£ </simpara> </listitem> <listitem> <simpara> ¼ìÑéËùÓÐÌá½»µÄ±äÁ¿¡£ </simpara> </listitem> </itemizedlist> ÒÔÏÂÊǾ¹ý¸Ä½øµÄ½Å±¾£º <example> <title>¸ü¼Ó°²È«µÄÎļþÃû¼ìÑé</title> <programlisting role="php"> <![CDATA[ <?php // removes a file from the hard drive that // the PHP user has access to. $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim $homedir = "/home/$username"; $file_to_delete = basename("$userfile"); // strip paths unlink ($homedir/$file_to_delete); $fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion $logstring = "$username $homedir $file_to_delete"; fputs ($fp, $logstring); fclose($fp); echo "$file_to_delete has been deleted!"; ?> ]]> </programlisting> </example> ¾¡¹ÜÈç´Ë£¬¸Ã½Å±¾»¹ÊÇÓÐһЩÎÊÌâ¡£Èç¹ûÄúµÄÊÚȨϵͳÔÊÐíÓû§½¨Á¢ËûÃÇ×Ô¼ºµÄÓû§µÇ¼£¬¶øÄ³Óû§Ñ¡ÔñµÇ¼¡°../etc/¡±£¬ÏµÍ³½«±»Ôٴα©Â¶¡£»ùÓÚÕâÒ»µã£¬Äú¿ÉÄܸüÔ¸Òâ±àдһ¸ö¸üºÃµÄ¼ìÑ飺 <example> <title>¸ü°²È«µÄÎļþÃû¼ìÑé</title> <programlisting role="php"> <![CDATA[ <?php $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim $homedir = "/home/$username"; if (!ereg('^[^./][^/]*$', $userfile)) die('bad filename'); //die, do not process if (!ereg('^[^./][^/]*$', $username)) die('bad username'); //die, do not process //etc... ?> ]]> </programlisting> </example> </para> <para> ¸ù¾ÝÄúµÄ²Ù×÷ϵͳµÄ²»Í¬£¬ÄúÐèÒª¿¼ÂÇÐí¶à¸÷ÖÖ¸÷ÑùµÄÎļþ£¬°üÀ¨É豸ÏîÄ¿£¨/dev/ »ò COM1£©£¬ÉèÖÃÎļþ£¨/etc/ ÎļþºÍ .ini Îļþ£©£¬Í¨³£µÄÎļþ´æ´¢Çø£¨/home/£¬My Documents£©µÈ¡£»ùÓÚÕâÒ»µã£¬½¨Á¢Ò»¸ö²ßÂÔ½ûÖ¹ËùÓгýÁËÄúÃ÷È·µÄÊÚȨµÄÐÐΪ¿ÉÄÜÒª¸ü¼òµ¥Ò»Ð©¡£ </para> </sect1> <sect1 id="security.database"> <title>Êý¾Ý¿â°²È«</title> <simpara> ÏÖÔÚ£¬ÎªÁËʹµÃÍøÕ¾Äܹ»Ìṩ¸÷ÖÖ¸÷Ñù¶¯Ì¬µÄÄÚÈÝ£¬Êý¾Ý¿âÒѾ³ÉΪËùÓлùÓÚ WEB Ó¦ÓóÌÐò×îÖØÒªµÄ×é¼þ¡£ÓÉÓÚһЩʮ·ÖÃô¸Ð»òÕß±£ÃܵÄÐÅÏ¢¿ÉÄÜ»á´æ´¢ÔÚÕâÑùµÄÊý¾Ý¿âÖУ¬Òò´Ë£¬ÄúÐèÒª·Ç³£É÷ÖØµÄ¿¼ÂÇÈçºÎ±£»¤ËüÃÇ¡£ </simpara> <simpara> ΪÁËÄܹ»´æ´¢»òÕß¼ìË÷ÐÅÏ¢£¬ÄúÐèÒªÁ¬½Óµ½Êý¾Ý¿â£¬·¢ËÍÒ»¸öºÏ·¨µÄ²éѯÃüÁµÃµ½½á¹û£¬È»ºó¹Ø±ÕÁ¬½Ó¡£Ä¿Ç°£¬ÔÚÕâ¸ö½»»¥¹ý³ÌÖÐ×î³£ÓõIJéѯÓïÑÔÊÇ¡°½á¹¹»¯²éѯÓïÑÔ£¨Structured Query Language, SQL£©¡±¡£Çë²ÎÔÄÒÔÏÂÄÚÈÝÒÔÁ˽âºÚ¿ÍÈçºÎ<link linkend="security.database.sql-injection">¡°ÀûÓà SQL ²éѯ¹¥»÷¡±</link>¡£ </simpara> <simpara> ÕýÈçÄúËùÖªµÀµÄ£¬PHP ×ÔÉí²¢ÎÞ·¨±£»¤ÄúµÄÊý¾Ý¿â¡£ÒÔÏÂÕ½ڵÄÄÚÈÝÖ¼ÔÚ½éÉÜÓйØÈçºÎÀûÓà PHP ½Å±¾·ÃÎʺͲÙ×÷Êý¾Ý¿âµÄ×î»ù´¡µÄ֪ʶ¡£ </simpara> <simpara> Çë¼ÇסÕâ¸ö¼òµ¥µÄ·¨Ôò£º¾¡¿ÉÄÜÉî²ã´ÎµÄ·ÀÓù¡£ÎªÁ˱£»¤ÄúµÄÊý¾Ý¿âËù²ÉÈ¡µÃ´ëÊ©ºÍ³¡ËùÔ½¶à£¬¹¥»÷Õß±©Â¶¡¢ÀÄÓÃÊý¾Ý¿âÖд洢µÄ±£ÃÜÐÅÏ¢µÄ¿ÉÄÜÐÔ¾ÍԽС¡£Ç뾡¿ÉÄܸüºÃµÄÉè¼ÆÊý¾Ý¿â½á¹¹ºÍÓ¦ÓóÌÐò¡£ </simpara> <sect2 id="security.database.design"> <title>Éè¼ÆÊý¾Ý¿â</title> <simpara> Ò»°ãÀ´ËµµÚÒ»²½×ÜÊǽ¨Á¢Êý¾Ý¿â£¬³ý·ÇÄúÏ£ÍûʹÓÃÒѾ´æÔڵĵÚÈý·½Êý¾Ý¿â¡£µ±Ò»¸öÊý¾Ý¿â±»½¨Á¢ºó£¬Ëü½«±»Ö¸¶¨¸øÒ»¸öËùÓÐÕߣ¬¼´ÔËÐн¨Á¢Êý¾Ý¿âÓï¾äµÄÓû§¡£Í¨³££¬Ö»ÓÐËùÓÐÕߣ¨»òÕß³¬¼¶Óû§£©²ÅÄܶԸÃÊý¾Ý¿âÖеĶÔÏó½øÐÐÈκβÙ×÷£¬ÎªÁËÄÜÈÃÆäËüÓû§Ê¹ÓøÃÊý¾Ý¿â£¬ÐèÒª½øÐÐȨÏÞÉèÖᣠ</simpara> <simpara> Ó¦ÓóÌÐò¾ö²»ÄÜÓÃËùÓÐÕß»òÕß³¬¼¶Óû§µÄÕ˺ÅÀ´Á¬½Óµ½Êý¾Ý¿â£¬ÒòΪÕâЩÓû§¿ÉÒÔÖ´ÐÐÈκβéѯ£¬ÀýÈ磬ÐÞ¸ÄÊý¾Ý½á¹¹£¨Èçɾ³ý±í¸ñ£©»òÕßɾ³ýËùÓеÄÄÚÈÝ¡£ </simpara> <simpara> Äú¿ÉÒÔΪӦÓóÌÐò²»Í¬µÄ²¿·Ö½¨Á¢²»Í¬µÄÊý¾Ý¿âÕ˺ţ¬Ê¹µÃËüÃÇÖ°ÄܶÔÊý¾Ý¿â¶ÔÏóÐÐʹ·Ç³£ÓÐÏÞµÄȨÏÞ¡£¶ÔÕâЩÕ˺ÅÓ¦¸ÃÖ»¸³Óè×îÐèÒªµÄȨÏÞ£¬Í¬Ê±Ó¦¸Ã·ÀÖ¹ÏàͬµÄÓû§Äܹ»ÔÚ²»Í¬µÄʹÓÃÇé¿öÓëÊý¾Ý¿â½øÐн»Á÷¡£ÕâÒ²¾ÍÊÇ˵£¬Èç¹ûijһ¸öÈëÇÖÕßÀûÓÃÕâЩÕ˺ÅÖеÄijһ¸ö»ñµÃÁË·ÃÎÊÄúÊý¾Ý¿âµÄȨÏÞ£¬ËûÃÇÒ²½ö½öÄܹ»Ó°Ïìµ½ÄúµÄÓ¦ÓóÌÐòÁ¦ËùÄܼ°µÄ·¶Î§¡£ </simpara> <simpara> Äú×îºÃ²»Òª°ÑËùÓеÄÊÂÎï¹ý³Ì¶¼·ÅÔÚ WEB Ó¦ÓóÌÐò£¨¼´ÄúµÄ½Å±¾£©ÖÐÀ´ÊµÊ©£¬¶ø³ä·ÖµÄÀûÓÃÊý¾Ý¿â½á¹¹£¬Ê¹ÓÃÊÓͼ£¨view£©¡¢´¥·¢Æ÷£¨trigger£©»òÕß¹æÔò£¨rule£©¡£Èç¹ûÐèÒª¶Ôϵͳ½øÐÐÉý¼¶£¬ÔòÐèÒª¶ÔÊý¾Ý¿â¿ª±ÙеĶ˿ڣ¬Òò´ËÄúÐèÒª¶Ôÿһ¸ö¶ÀÁ¢µÄÊý¾Ý¿â¿Í»§ÖØÐÂÉèÖÃȨÏÞ¡£×ÜÖ®£¬´¥·¢Æ÷¿ÉÒÔ±»ÓÃÀ´Í¸Ã÷µØºÍ×Ô¶¯µØ´¦Àí×ֶΣ¬¸ÃÌØÐÔ¿ÉÒÔÔÚµ÷ÊÔÓ¦ÓóÌÐòµØÎÊÌâ»òÕß×·×Ùºǫ́ÊÂÎïʱÌṩ±ãÀûÐÅÏ¢¡£ </simpara> </sect2> <sect2 id="security.database.connection"> <title>Á¬½ÓÊý¾Ý¿â</title> <simpara> Äú¿ÉÄÜÏ£Íûͨ¹ý SSL ½¨Á¢Á¬½ÓÀ´¼ÓÃܿͻ§¶ËºÍ·þÎñ¶ËÖ®¼äµÄͨѶÒÔÔö¼Ó°²È«ÐÔ£¬»òÕßÄú¿ÉÒÔʹÓà ssh À´¼ÓÃܿͻ§¶ËºÍÊý¾Ý¿â·þÎñÆ÷Ö®¼äµÄÍøÂçÁ¬½Ó¡£Èç¹ûÄúʵʩÁËÕâЩ´ëÊ©£¬Ôò¼àÊÓÄúµÄÍøÂçÁ÷Á¿ÒÔ¼°ÒÔÕâÖÖ·½Ê½»ñÈ¡ÐÅÏ¢½«±ä³ÉÊ®·Ö¸´ÔӵŤ×÷¡£ </simpara> <!--simpara> If your database server has native SSL support, consider to use <link linkend="ref.openssl">OpenSSL functions</link> in communication between PHP and database via SSL. </simpara--> </sect2> <sect2 id="security.database.storage"> <title>¼ÓÃܴ洢ģÐÍ</title> <simpara> SSL/SSH protects data travelling from the client to the server, SSL/SSH does not protect the persistent data stored in a database. SSL is an on-the-wire protocol. </simpara> <simpara> Once an attacker gains access to your database directly (bypassing the webserver), the stored sensitive data may be exposed or misused, unless the information is protected by the database itself. Encrypting the data is a good way to mitigate this threat, but very few databases offer this type of data encryption. </simpara> <simpara> The easiest way to work around this problem is to first create your own encryption package, and then use it from within your PHP scripts. PHP can assist you in this case with its several extensions, such as <link linkend="ref.mcrypt">Mcrypt</link> and <link linkend="ref.mhash">Mhash</link>, covering a wide variety of encryption algorithms. The script encrypts the data be stored first, and decrypts it when retrieving. See the references for further examples how encryption works. </simpara> <simpara> In case of truly hidden data, if its raw representation is not needed (i.e. not be displayed), hashing may be also taken into consideration. The well-known example for the hashing is storing the MD5 hash of a password in a database, instead of the password itself. See also <function>crypt</function> and <function>md5</function>. </simpara> <example> <title>ʹÓþ¹ý¹þÏ£ÔËËãµÄÃÜÂë×Ö¶Î</title> <programlisting role="php"> <![CDATA[ // storing password hash $query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');", addslashes($username), md5($password)); $result = pg_exec($connection, $query); // querying if user submitted the right password $query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';", addslashes($username), md5($password)); $result = pg_exec($connection, $query); if (pg_numrows($result) > 0) { echo "Welcome, $username!"; } else { echo "Authentication failed for $username."; } ]]> </programlisting> </example> </sect2> <sect2 id="security.database.sql-injection"> <title>SQL ¹¥»÷</title> <simpara> Many web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands. </simpara> <simpara> Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately. </simpara> <para> Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database. <example> <title> ½«½á¹û¼¯·ÖÀëµ½Ò³ÃæÖУ¬È»ºó´´Ô쳬¼¶Óû§(PostgreSQL and MySQL) </title> <programlisting role="php"> <![CDATA[ $offset = argv[0]; // beware, no input validation! $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // with PostgreSQL $result = pg_exec($conn, $query); // with MySQL $result = mysql_query($query); ]]> </programlisting> </example> Normal users click on the 'next', 'prev' links where the <varname>$offset</varname> is encoded into the URL. The script expects that the incoming <varname>$offset</varname> is decimal number. However, someone tries to break in with appending <function>urlencode</function>'d form of the following to the URL <informalexample> <programlisting> <![CDATA[ // in case of PostgreSQL 0; insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd) select 'crack', usesysid, 't','t','crack' from pg_shadow where usename='postgres'; -- // in case of MySQL 0; UPDATE user SET Password=PASSWORD('crack') WHERE user='root'; FLUSH PRIVILEGES; ]]> </programlisting> </informalexample> If it happened, then the script would present a superuser access to him. Note that <literal>0;</literal> is to supply a valid offset to the original query and to terminate it. </para> <note> <para> It is common technique to force the SQL parser to ignore the rest of the query written by the developer with <literal>--</literal> which is the comment sign in SQL. </para> </note> <para> A feasible way to gain passwords is to circumvent your search result pages. What the attacker needs only is to try if there is any submitted variable used in SQL statement which is not handled properly. These filters can be set commonly in a preceding form to customize <literal>WHERE, ORDER BY, LIMIT</literal> and <literal>OFFSET</literal> clauses in <literal>SELECT</literal> statements. If your database supports the <literal>UNION</literal> construct, the attacker may try to append an entire query to the original one to list passwords from an arbitrary table. Using encrypted password fields is strongly encouraged. <example> <title> ÁгöÎÄÕ£¬ÒÔ¼°Ò»Ð©ÃÜÂ루ÈκÎÊý¾Ý¿â·þÎñÆ÷£© </title> <programlisting role="php"> <![CDATA[ $query = "SELECT id, name, inserted, size FROM products WHERE size = '$size' ORDER BY $order LIMIT $limit, $offset;"; $result = odbc_exec($conn, $query); ]]> </programlisting> </example> The static part of the query can be combined with another <literal>SELECT</literal> statement which reveals all passwords: <informalexample> <programlisting> <![CDATA[ ' union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable; -- ]]> </programlisting> </informalexample> If this query (playing with the <literal>'</literal> and <literal>--</literal>) were assigned to one of the variables used in <varname>$query</varname>, the query beast awakened. </para> <para> SQL UPDATEs are also subject to attacking your database. These queries are also threatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the <literal>SET</literal> clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examing the form variable names, or just simply brute forcing. There are not so many naming convention for fields storing passwords or usernames. <example> <title> ÀûÓÃÖØÉèÃÜÂëÀ´»ñÈ¡¸ü¶àµÄȨÏÞ£¨ÈκÎÊý¾Ý¿â·þÎñÆ÷£© </title> <programlisting role="php"> <![CDATA[ $query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';"; ]]> </programlisting> </example> But a malicious user sumbits the value <literal>' or uid like'%admin%'; --</literal> to <varname>$uid</varname> to change the admin's password, or simply sets <varname>$pwd</varname> to <literal>"hehehe', admin='yes', trusted=100 "</literal> (with a trailing space) to gain more privileges. Then, the query will be twisted: <informalexample> <programlisting role="php"> <![CDATA[ // $uid == ' or uid like'%admin%'; -- $query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --"; // $pwd == "hehehe', admin='yes', trusted=100 " $query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE ...;" ]]> </programlisting> </informalexample> </para> <para> A frightening example how operating system level commands can be accessed on some database hosts. <example> <title>¹¥»÷Êý¾Ý¿âÖ÷»úµÄ²Ù×÷ϵͳ (MSSQL Server)</title> <programlisting role="php"> <![CDATA[ $query = "SELECT * FROM products WHERE id LIKE '%$prod%'"; $result = mssql_query($query); ]]> </programlisting> </example> If attacker submits the value <literal>a%' exec master..xp_cmdshell 'net user test testpass /ADD' --</literal> to <varname>$prod</varname>, then the <varname>$query</varname> will be: <informalexample> <programlisting role="php"> <![CDATA[ $query = "SELECT * FROM products WHERE id LIKE '%a%' exec master..xp_cmdshell 'net user test testpass /ADD'--"; $result = mssql_query($query); ]]> </programlisting> </informalexample> MSSQL Server executes the SQL statements in the batch including a command to add a new user to the local accounts database. If this application were running as <literal>sa</literal> and the MSSQLSERVER service is running with sufficient privileges, the attacker would now have an account with which to access this machine. </para> <note> <para> Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be so vulnerable in other manner. </para> </note> <sect3 id="security.database.avoiding"> <title>Ô¤·ÀµÄ¼¼ÇÉ</title> <simpara> You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management system or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one. </simpara> <simpara> These attacks are mainly based on exploiting the code not being written with security in mind. Never trust on any kind of input, especially which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters. </simpara> <itemizedlist> <listitem> <simpara> Never connect to the database as a superuser or as the database owner. Use always customized users with very limited privileges. </simpara> </listitem> <listitem> <simpara> Check if the given input has the expected data type. PHP has a wide range of input validating functions, from the simplest ones found in <link linkend="ref.variables">Variable Functions</link> and in <link linkend="ref.ctype">Character Type Functions</link> (e.g. <function>is_numeric</function>, <function>ctype_digit</function> respectively) onwards the <link linkend="ref.pcre">Perl compatible Regular Expressions</link> support. </simpara> </listitem> <listitem> <para> If the application waits for numerical input, consider to verify data with <function>is_numeric</function>, or silently change its type using <function>settype</function>, or use its numeric representation by <function>sprintf</function>. <example> <title>¸ü¼Ó°²È«µÄ·ÖÒ³²éѯÓï¾ä</title> <programlisting role="php"> <![CDATA[ settype($offset, 'integer'); $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // please note %d in the format string, using %s would be meaningless $query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;", $offset); ]]> </programlisting> </example> </para> </listitem> <listitem> <simpara> Quote each non numeric user input which is passed to the database with <function>addslashes</function> or <function>addcslashes</function>. See <link linkend="security.database.storage">the first example</link>. As the examples shows, quotes burnt into the static part of the query is not enough, and can be easily hacked. </simpara> </listitem> <listitem> <simpara> Do not print out any database specific information, especially about the schema, by fair means or foul. See also <link linkend="security.errors">Error Reporting</link> and <link linkend="ref.errorfunc">Error Handling and Logging Functions</link>. </simpara> </listitem> <listitem> <simpara> You may use stored procedures and previously defined cursors to abstract data access so that users do not directly access tables or views, but this solution has another impacts. </simpara> </listitem> </itemizedlist> <simpara> Besides these, you benefit from logging queries either within your script or by the database itself, if it supports. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself, but through the information it contains. The more detail is generally better. </simpara> </sect3> </sect2> </sect1> <sect1 id="security.errors"> <title>´íÎ󱨸æ</title> <para> With PHP security, there are two sides to error reporting. One is beneficial to increasing security, the other is detrimental. </para> <para> A standard attack tactic involves profiling a system by feeding it improper data, and checking for the kinds, and contexts, of the errors which are returned. This allows the system cracker to probe for information about the server, to determine possible weaknesses. For example, if an attacker had gleaned information about a page based on a prior form submission, they may attempt to override variables, or modify them: <example> <title>ÓÃ×Ô¶¨ÒåµÄ HTML Ò³ÃæÀ´¹¥»÷±äÁ¿</title> <programlisting role="php"> <![CDATA[ <form method="POST" action="attacktarget?username=badfoo&password=badfoo"> <input type="hidden" name="username" value="badfoo"> <input type="hidden" name="password" value="badfoo"> </form> ]]> </programlisting> </example> </para> <para> The PHP errors which are normally returned can be quite helpful to a developer who is trying to debug a script, indicating such things as the function or file that failed, the PHP file it failed in, and the line number which the failure occured in. This is all information that can be exploited. It is not uncommon for a php developer to use <function>show_source</function>, <function>highlight_string</function>, or <function>highlight_file</function> as a debugging measure, but in a live site, this can expose hidden variables, unchecked syntax, and other dangerous information. Especially dangerous is running code from known sources with built-in debugging handlers, or using common debugging techniques. If the attacker can determine what general technique you are using, they may try to brute-force a page, by sending various common debugging strings: <example> <title>ÀûÓÃÆÕͨµÄµ÷ÊÔ±äÁ¿</title> <programlisting role="php"> <![CDATA[ <form method="POST" action="attacktarget?errors=Y&showerrors=1"&debug=1"> <input type="hidden" name="errors" value="Y"> <input type="hidden" name="showerrors" value="1"> <input type="hidden" name="debug" value="1"> </form> ]]> </programlisting> </example> </para> <para> Regardless of the method of error handling, the ability to probe a system for errors leads to providing an attacker with more information. </para> <para> For example, the very style of a generic PHP error indicates a system is running PHP. If the attacker was looking at an .html page, and wanted to probe for the back-end (to look for known weaknesses in the system), by feeding it the wrong data they may be able to determine that a system was built with PHP. </para> <para> A function error can indicate whether a system may be running a specific database engine, or give clues as to how a web page or programmed or designed. This allows for deeper investigation into open database ports, or to look for specific bugs or weaknesses in a web page. By feeding different pieces of bad data, for example, an attacker can determine the order of authentication in a script, (from the line number errors) as well as probe for exploits that may be exploited in different locations in the script. </para> <para> A filesystem or general PHP error can indicate what permissions the webserver has, as well as the structure and organization of files on the web server. Developer written error code can aggravate this problem, leading to easy exploitation of formerly "hidden" information. </para> <para> There are three major solutions to this issue. The first is to scrutinize all functions, and attempt to compensate for the bulk of the errors. The second is to disable error reporting entirely on the running code. The third is to use PHP's custom error handling functions to create your own error handler. Depending on your security policy, you may find all three to be applicable to your situation. </para> <para> One way of catching this issue ahead of time is to make use of PHP's own <function>error_reporting</function>, to help you secure your code and find variable usage that may be dangerous. By testing your code, prior to deployment, with E_ALL, you can quickly find areas where your variables may be open to poisoning or modification in other ways. Once you are ready for deployment, by using E_NONE, you insulate your code from probing. <example> <title>ÀûÓà E_ALL À´ÕÒ³öΣÏյıäÁ¿</title> <programlisting role="php"> <![CDATA[ <?php if ($username) { // Not initialized or checked before usage $good_login = 1; } if ($good_login == 1) { // If above test fails, not initialized or checked before usage readfile ("/highly/sensitive/data/index.html"); } ?> ]]> </programlisting> </example> </para> </sect1> <sect1 id="security.registerglobals"> <title>Using Register Globals</title> <para> Perhaps the most controversial change in PHP is when the default value for the PHP directive <link linkend="ini.register-globals"> register_globals</link> went from ON to OFF in PHP <ulink url="&url.php.release4.2.0;">4.2.0</ulink>. Reliance on this directive was quite common and many people didn't even know it existed and assumed it's just how PHP works. This page will explain how one can write insecure code with this directive but keep in mind that the directive itself isn't insecure but rather it's the misuse of it. </para> <para> When on, register_globals will inject (poison) your scripts will all sorts of variables, like request variables from html forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this. Let's demonstrate with an example misuse of register_globals: </para> <para> <example> <title>Example misuse with register_globals = on</title> <programlisting role="php"> <![CDATA[ <?php // define $authorized = true only if user is authenticated if (authenticated_user()) { $authorized = true; } // Because we didn't first initialize $authorized as false, this might be // defined through register_globals, like from GET auth.php?authorized=1 // So, anyone can be seen as authenticated! if ($authorized) { include "/highly/sensitive/data.php"; } ?> ]]> </programlisting> </example> </para> <para> When register_globals = on, our logic above may be compromised. When off, <varname>$authorized</varname> can't be set via request so it'll be fine, although it really is generally a good programming practice to initialize variables first. For example, in our example above we might have first done <literal>$authorized = false</literal>. Doing this first means our above code would work with register_globals on or off as users by default would be unauthorized. </para> <para> Another example is that of <link linkend="ref.session">sessions</link>. When register_globals = on, we could also use <varname>$username</varname> in our example below but again you must realize that <varname>$username</varname> could also come from other means, such as GET (through the URL). </para> <para> <example> <title>Example use of sessions with register_globals on or off</title> <programlisting role="php"> <![CDATA[ <?php // We wouldn't know where $username came from but do know $_SESSION is // for session data if (isset($_SESSION['username'])) { echo "Hello <b>{$_SESSION['username']}</b>"; } else { echo "Hello <b>Guest</b><br />"; echo "Would you like to login?"; } ?> ]]> </programlisting> </example> </para> <para> It's even possible to take preventative measures to warn when forging is being attempted. If you know ahead of time exactly where a variable should be coming from, you can check to see if the submitted data is coming from an inappropriate kind of submission. While it doesn't guarantee that data has not been forged, it does require an attacker to guess the right kind of forging. If you don't care where the request data comes from, you can use <varname>$_REQUEST</varname> as it contains a mix of GET, POST and COOKIE data. See also the manual section on using <link linkend="language.variables.external">variables from outside of PHP</link>. </para> <para> <example> <title>Detecting simple variable poisoning</title> <programlisting role="php"> <![CDATA[ <?php if (isset($_COOKIE['MAGIC_COOKIE'])) { // MAGIC_COOKIE comes from a cookie. // Be sure to validate the cookie data! } elseif (isset($_GET['MAGIC_COOKIE']) || isset($_POST['MAGIC_COOKIE'])) { mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']); echo "Security violation, admin has been alerted."; exit; } else { // MAGIC_COOKIE isn't set through this REQUEST } ?> ]]> </programlisting> </example> </para> <para> Of course, simply turning off register_globals does not mean your code is secure. For every piece of data that is submitted, it should also be checked in other ways. Always validate your user data and initialize your variables! To check for unitialized variables you may turn up <function>error_reporting</function> to show <constant>E_NOTICE</constant> level errors. </para> ¬e.superglobals; </sect1> <sect1 id="security.variables"> <title>Óû§Ìá½»µÄÊý¾Ý</title> <para> The greatest weakness in many PHP programs is not inherent in the language itself, but merely an issue of code not being written with security in mind. For this reason, you should always take the time to consider the implications of a given piece of code, to ascertain the possible damage if an unexpected variable is submitted to it. <example> <title>ΣÏյıäÁ¿Ó÷¨</title> <programlisting role="php"> <![CDATA[ <?php // remove a file from the user's home directory... or maybe // somebody else's? unlink ($evil_var); // Write logging of their access... or maybe an /etc/passwd entry? fputs ($fp, $evil_var); // Execute something trivial.. or rm -rf *? system ($evil_var); exec ($evil_var); ?> ]]> </programlisting> </example> You should always carefully examine your code to make sure that any variables being submitted from a web browser are being properly checked, and ask yourself the following questions: <itemizedlist> <listitem> <simpara> Will this script only affect the intended files? </simpara> </listitem> <listitem> <simpara> Can unusual or undesirable data be acted upon? </simpara> </listitem> <listitem> <simpara> Can this script be used in unintended ways? </simpara> </listitem> <listitem> <simpara> Can this be used in conjunction with other scripts in a negative manner? </simpara> </listitem> <listitem> <simpara> Will any transactions be adequately logged? </simpara> </listitem> </itemizedlist> By adequately asking these questions while writing the script, rather than later, you prevent an unfortunate re-write when you need to increase your security. By starting out with this mindset, you won't guarantee the security of your system, but you can help improve it. </para> <para> You may also want to consider turning off register_globals, magic_quotes, or other convenience settings which may confuse you as to the validity, source, or value of a given variable. Working with PHP in error_reporting(E_ALL) mode can also help warn you about variables being used before they are checked or initialized (so you can prevent unusual data from being operated upon). </para> </sect1> <sect1 id="security.hiding"> <title>Òþ²Ø PHP</title> <para> In general, security by obscurity is one of the weakest forms of security. But in some cases, every little bit of extra security is desirable. </para> <para> A few simple techniques can help to hide PHP, possibly slowing down an attacker who is attempting to discover weaknesses in your system. By setting expose_php = off in your &php.ini; file, you reduce the amount of information available to them. </para> <para> Another tactic is to configure web servers such as apache to parse different filetypes through PHP, either with an &htaccess; directive, or in the apache configuration file itself. You can then use misleading file extensions: <example> <title>½« PHP Òþ²ØÎªÁíÒ»ÖÖÓïÑÔ</title> <programlisting role="apache-conf"> <![CDATA[ # Make PHP code look like other code types AddType application/x-httpd-php .asp .py .pl ]]> </programlisting> </example> »òÕßÍêÈ«µØÒþ²Ø£º <example> <title>¶Ô PHP ʹÓÃδ֪À©Õ¹Ãû</title> <programlisting role="apache-conf"> <![CDATA[ # Make PHP code look like unknown types AddType application/x-httpd-php .bop .foo .133t ]]> </programlisting> </example> Or hide it as html code, which has a slight performance hit because all html will be parsed through the PHP engine: <example> <title>¶Ô PHP ʹÓà HTML À©Õ¹Ãû</title> <programlisting role="apache-conf"> <![CDATA[ # Make all PHP code look like html AddType application/x-httpd-php .htm .html ]]> </programlisting> </example> For this to work effectively, you must rename your PHP files with the above extensions. While it is a form of security through obscurity, it's a minor preventative measure with few drawbacks. </para> </sect1> <sect1 id="security.current"> <title>¸üе½×îа汾</title> <simpara> PHP, like any other large system, is under constant scrutiny and improvement. Each new version will often include both major and minor changes to enhance and repair security flaws, configuration mishaps, and other issues that will affect the overall security and stability of your system. </simpara> <simpara> Like other system-level scripting languages and programs, the best approach is to update often, and maintain awareness of the latest versions and their changes. </simpara> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
Index: phpdoc-zh/security/index.xml +++ phpdoc-zh/security/index.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <!-- $Author: gregory $ --> <!-- EN-Revision: 1.58 Maintainer: Gregory Status: ready --> <chapter id="security.index"> <title>°²È«</title> <simpara> PHP ÊÇÒ»Öַdz£Ç¿´óµÄÓïÑÔ£¬Æä½âÊÍÆ÷²»¹ÜÊÇ×÷Ϊ WEB ·þÎñÆ÷µÄÄ£¿é»¹Êǵ¥¶ÀµØÒÔ <acronym>CGI</acronym> ³ÌÐòµÄÐÎʽÔËÐУ¬¶¼Äܹ»·ÃÎÊÎļþ£¬Ö´ÐÐÃüÁ»òÕß½¨Á¢µ½·þÎñÆ÷µÄÍøÂçÁ¬½Ó¡£ÔÚĬÈÏÇé¿öÏ£¬ÕâÐ©ÌØÐÔ¸øÔÚ WEB ÉÏÔËÐеĻúÖÆ´øÀ´Á˰²È«Òþ»¼¡£Õë¶ÔÕâÒ»ÎÊÌ⣬PHP ±»Éè¼Æ³ÉÒ»ÖÖ¸ü¼Ó°²È«µÄÓïÑÔ£¬Ê¹µÃÆä¸ú Perl ºÍ C ÓïÑÔ±ÈÆðÀ´£¬¸üÊʺÏÓÃÀ´±àд CGI ³ÌÐò¡£¼ÓÉÏÆäÓÐÁ¼ºÃµÄ±àÒëʱºÍÔËÐÐʱÉèÖÃÑ¡ÏîÒÔ¼°Êʵ±µÄ´úÂë±àд¹æÔò£¬PHP Äܹ»ÎªÄúÕÒµ½ÄúÕæÕýÐèÒªµÄ×ÔÓÉÓ밲ȫµÄ½áºÏµã¡£ </simpara> <simpara> ÓÉÓÚÎÒÃÇ¿ÉÒÔÔںܶ಻ͬµÄ·½ÃæÀûÓà PHP£¬Òò´ËËüÓкܶàÉèÖÃÑ¡ÏîÀ´¿ØÖÆÆäÐÐΪ¡£Ò»×éÅÓ´óµÄ¿ÉÑ¡²ÎÊýÄܹ»±£Ö¤Äú¿ÉÒÔ½« PHP ÓÃÓÚÐí¶à²»Í¬µÄÄ¿µÄ£¬µ«ÕâͬʱҲÒâζ×ÅÕâЩ²ÎÊýºÍ·þÎñ¶ËÅäÖõÄ×éºÏ»á´øÀ´Ò»Ð©°²È«ÎÊÌâ¡£ </simpara> <simpara> PHP µÄÅäÖÃÓëÆä´úÂëÏà±È£¬ÓÐ×ÅͬÑùµÄÁé»îÐÔ¡£PHP ¿ÉÒÔÓÃÀ´½¨Á¢ÍêÕûµÄ·þÎñ¶ËÓ¦ÓóÌÐò£¬ÓµÓÐËùÓÐÍâ¿ÇÓû§µÄȨÏÞ£»ËüÒ²¿ÉÒÔÔÚ±»Ñϸñ¿ØÖƵĻ·¾³ÏÂÓÃ×÷Ò»¸ö¼òµ¥µÄ·þÎñ¶Ë°üº¬£¬½ö³Ðµ£ºÜСµÄ·çÏÕ¡£ÄúÈçºÎ½¨Á¢¸Ã»·¾³£¬ÒÔ¼°Æä°²È«ÐÔÈçºÎ£¬Ôںܴó³Ì¶ÈÉÏÈ¡¾öÓÚ PHP µÄ¿ª·¢Õß¡£ </simpara> <simpara> ±¾Õ½«ÒÔһЩ×ܵݲȫ½¨Ò鿪ʼ£¬½âÊͲ»Í¬µÄÅäÖÃÑ¡Ïî×éºÏÒÔ¼°ËüÃÇÄܹ»±»°²È«µÄʹÓõÄÇé¿ö£¬È»ºóÕë¶Ô²»Í¬µÄ°²È«µÈ¼¶ÒªÇó£¬ÃèÊöһЩÔÚ±àд´úÂë¹ý³ÌÖвÉÈ¡µÄ°²È«²ßÂÔ¡£ </simpara> <sect1 id="security.general"> <title>Ò»°ã²ßÂÔ</title> <simpara> Ò»¸ö¾ø¶Ô°²È«µÄϵͳÊDz»¿ÉÄÜʵÏֵģ¬Òò´ËÒ»¸ö°²È«²ßÂԵĺËÐÄͨ³£¶¼ÊÇѰÇó·çÏÕÓë¿ÉÓÃÐÔÖ®¼äµÄƽºâµã¡£Èç¹ûÓû§Ìá½»µÄÿ¸ö±äÁ¿¶¼ÐèÒªÁ½ÖÖÉúÎïͳ¼ÆÑ§µÄУÑ飨ÀýÈçÊÓÍøÄ¤É¨ÃèºÍÖ¸ÎÆ¼ìÑ飩£¬ÄÇôÎÒÃǽ«»áÐèÒª½øÐм«Æä¸ß½×µÄ¼ÆËã¡£Õ⻹¿ÉÄÜÔì³ÉÎÒÃÇÐèÒª»¨·Ñ°ë¸öСʱÀ´Ìîдһ¸ö¼°Æä·±ËöµÄ±íµ¥£¬Ê¹µÃÓû§¸üÇãÏòÓÚѰÕÒһЩ½Ý¾¶À´ÈƹýÕâЩ°²È«»úÖÆ¡£ </simpara> <simpara> ×îºÃµÄ°²È«²ßÂÔͨ³£Äܹ»²»ÄÇôÃ÷ÏÔµØÊÊÓ¦»·¾³µÄÐèÇó£¬Ëü²»»á·Á°Óû§Íê³ÉËûÃǵŤ×÷£¬Ò²²»»áʹ´úÂë±àÐ´Ô±Ãæ¹ý·Ö¸ºµ£¸´ÔÓµÄÇéÐΡ£Êµ¼ÊÉÏ£¬Ò»Ð©°²È«¹¥»÷µÄ³É¹¦ÕýÊÇÕâÖÖ¹ý·ÖÈßÔӵݲȫ»úÖÆËæ×Åʱ¼äÖð½¥»Ù»µµÄ½á¹û¡£ </simpara> <simpara> ÎÒÃÇÓ¦¸Ã¼ÇסÕâÑùÒ»¾äºÜÓмÛÖµµÃ»°£ºÒ»¸öϵͳ³äÆäÁ¿½öÏ൱ÓÚÒ»ÌõËøÁ´ÖÐ×ÈõµÄ»·½Ú¡£Èç¹ûËùÓеÄÊÂÎñµÄʱ¼ä¡¢µØµã¡¢ÀàÐ͵ÈÐÅÏ¢¶¼±»ÏêϸµÄдÈëÈÕÖ¾£¬µ«¶ÔÓû§µÄÈÏÖ¤½ö½öÖ»ÊÇ»ùÓÚÒ»¸öµ¥Ò»µÄ cookie£¬ÄÇô½«Óû§¼ÇÈëÊÂÎñÈÕÖ¾µÄÓÐЧÐԾͱ»ÑÏÖØµØÏ÷ÈõÁË¡£ </simpara> <simpara> ÔÚ²âÊÔµÄʱºò£¬Çë¼Çס£¬¼´Ê±ÊǶÔ×î¼òµ¥µÄÒ³Ãæ£¬ÄúÒ²²»¿ÉÄܲâÊÔµ½ËùÓеĿÉÄÜÐÔ¡£ÄúËùÔ¤Áϵ½µÄÊäÈë¿ÉÄܺÍÀ´×ÔÓÚÒ»¸ö²»ÂúµÄ¹ÍÔ±£¬Ò»¸öºÚ¿ÍÓÃÊýÔÂʱ¼ä»òÕßÒ»Ö»²»Ð¡ÐIJȵ½¼üÅ̵ÄèËù½øÐеÄÊäÈëÏàÈ¥ÉõÔ¶¡£ÕâÒ²ÊÇΪʲô˵ÎÒÃÇ×îºÃÄܹ»´ÓÂß¼µÄÈ«¾ÖÉÏÀ´ÉóÊÓÎÒÃǵĴúÂ룬´Ó¶ø±æ±ðÄÄÀï³öÏÖ²»¿ÉÔ¤ÁϵÄÊý¾Ý£¬È»ºóÔÙ¿¼ÂÇÈçºÎÐ޸ġ¢¼õÉÙ»òÕß·Å´óËüÃÇ¡£ </simpara> <simpara> Internet ÉϳäÂúÁËÏëÊÔͼͨ¹ýÆÆ»µÄãµÄ´úÂë¡¢ºÚµôÄãµÄÍøÕ¾¡¢·¢²¼²»ÊʺϵÄÄÚÈݵÈ;¾¶À´ÑïÃûÁ¢ÍòµÄÈË¡£²»¹ÜÄãµÄÍøÕ¾ÊÇ´ó»¹ÊÇС£¬Ö»ÒªÄãÔÚÏߣ¬ÓÐÒ»²¿¿ÉÒÔÁ¬½ÓµÃµ½µÄ·þÎñÆ÷£¬Äã¾ÍÄܳÉΪĿ±ê¡£Ðí¶àºÚ¿Í³ÌÐò²¢²»·Ö±çÍøÕ¾µÄ´óС£¬ËûÃÇÖ»ÊǼòµ¥µØÔÚÒ»´óƬ IP ·¶Î§ÄÚËÑÂÞÎþÉüÕß¡£ÎÒÃÇÒª¾¡¿ÉÄܲ»ÒªÊ¹×Ô¼º³ÉΪÆäÖеÄÒ»Ô±¡£ </simpara> </sect1> <sect1 id="security.cgi-bin"> <title>°²×°Îª CGI ³ÌÐò</title> <sect2 id="security.cgi-bin.attacks"> <title>¿ÉÄÜÊܵ½µÄ¹¥»÷</title> <simpara> Èç¹ûÄú²»Ï£Íû½« PHP ÒÔÄ£¿éµÄ·½Ê½¼¯³Éµ½·þÎñÆ÷Èí¼þ£¨ÀýÈç Apache£©ÖУ¬»òÕßÏ£Íû½« PHP ÓÃÓÚ²»Í¬µÄ CGI Íâ¿ÇÀ´½¨Á¢°²È«µÄ chroot ºÍ setuid ½Å±¾»·¾³£¬½« PHP ÓÃ×÷ <acronym>CGI</acronym> ³ÌÐòÊǰ²×°Ê±µÄÒ»ÖÖÑ¡Ôñ¡£ÕâÑùµÄ°²×°Í¨³£¶¼ÐèÒª½«¿ÉÖ´ÐÐµÄ PHP ³ÌÐò·ÅÖõ½ cgi-bin Ŀ¼¡£CERT ¹ËÎÊ <ulink url="&url.cert;">CA-96.11</ulink> µÄ½¨Òé·´¶Ô½«ÈκνâÊÍÆ÷·ÅÈë cgi-bin Ŀ¼¡£¾¡¹Ü PHP µÄ¿ÉÖ´ÐгÌÐòÄܹ»±»ÓÃ×÷¶ÀÁ¢µÄ½âÊÍÆ÷£¬PHP µÄ°²È«»úÖÆÒ²ÄÜ·ÀÖ¹ÓÉÕâÑùµÄ°²×°Ëùµ¼ÖµĿÉÄܵĹ¥»÷£º </simpara> <itemizedlist> <listitem> <simpara> ·ÃÎÊϵͳÎļþ£º<filename role="url">http://my.host/cgi-bin/php?/etc/passwd</filename> </simpara> <simpara> ÔÚ URL ÖÐÎʺţ¨?£©ºóÃæµÄ²éѯÐÅÏ¢½«±» CGI ½Ó¿Ú×÷ΪÃüÁîÐвÎÊý´«¸ø½âÊÍÆ÷¡£Í¨³£Çé¿öÏ£¬½âÊÍÆ÷»á´ò¿ª²¢ÔËÐÐÓÉÃüÁîÐеÚÒ»¸ö²ÎÊýËùÖ¸Ã÷µÄÎļþ¡£ </simpara> <simpara> µ± PHP ÒÔ CGI ³ÌÐòµÄ·½Ê½±»¼¤»îʱ£¬Ëü½«¾Ü¾ø½âÊ͸ÃÃüÁîÐвÎÊý¡£ </simpara> </listitem> <listitem> <simpara> ·ÃÎÊ·þÎñÆ÷ÉϵÄÈκΠWEB Îĵµ£º<filename role="url">http://my.host/cgi-bin/php/secret/doc.html</filename> </simpara> <simpara> URL ÖÐ PHP ³ÌÐòÃû³ÆºóÃæµÄ·¾¶ÐÅÏ¢ <filename role="uri">/secret/doc.html</filename> ϰ¹ßÉÏÊÇÓÃÀ´Ö¸Ã÷ÓÉ <acronym>CGI</acronym> ³ÌÐò´ò¿ª²¢½âÊ͵ÄÎļþÃû¡£Í¨³£ WEB ·þÎñÆ÷»áͨ¹ýһЩµÄÅäÖÃÑ¡ÏApache: Action£©½«¶ÔÀàËÆÓÚ <filename role="url">http://my.host/secret/script.php</filename> ÎĵµµÄÇëÇóÖØ¶¨Ïòµ½ PHP ½âÊÍÆ÷¡£WEB ·þÎñÆ÷Ïȼì²éĿ¼ <filename role="uri">/secret</filename> µÄ·ÃÎÊȨÏÞ£¬È»ºóÔÙ½¨Á¢ÇëÇóµÄÖØ¶¨Ïò <filename role="url">http://my.host/cgi-bin/php/secret/script.php</filename>¡£µ«²»ÐÒµÄÊÇ£¬Èç¹ûÇëÇóÖ±½ÓÒÔÕâÖÖÐÎʽ¸ø³ö£¬ÄÇô WEB ·þÎñÆ÷¾Í²»»áÖ´ÐжÔÎļþ <filename role="uri">/secret/script.php</filename> µÄ·ÃÎÊȨÏÞ¼ì²é£¬¶ø½ö½öÖ»¼ì²éÎļþ <filename role="uri">/cgi-bin/php</filename> µÄ·ÃÎÊȨÏÞ¡£ÕâÑùÒ»À´£¬ÈκοÉÒÔ·ÃÎÊ <filename role="uri">/cgi-bin/php</filename> µÄÓû§¶¼¿ÉÒÔ·ÃÎÊÈκΠWEB ·þÎñÆ÷ÉÏÊÕµ½±£»¤µÄÎĵµ¡£ </simpara> <simpara> ÔÚ PHP ÖУ¬±àÒëʱÉèÖÃÑ¡Ïî <link linkend="install.configure.enable-force-cgi-redirect">--enable-force-cgi-redirect</link> ÒÔ¼°ÔËÐÐʱÉèÖÃÑ¡Ïî <link linkend="ini.doc-root">doc_root</link> ºÍ <link linkend="ini.user-dir">user_dir</link> Äܹ»ÓÃÀ´·ÀÖ¹ÕâÖÖ¹¥»÷£¬Èç¹û·þÎñ¶ËµÄĿ¼Ê÷´æÔÚÓзÃÎÊÏÞÖÆµÄĿ¼¡£ÒÔϽ«Ïêϸ½âÊ͸÷ÖÖ²»Í¬µÄ×éºÏ¡£ </simpara> </listitem> </itemizedlist> </sect2> <sect2 id="security.cgi-bin.default"> <title>·½·¨Ò»£º½ö¾Í¹«ÖÚÎļþÌṩ·þÎñ</title> <simpara> Èç¹ûÄúµÄ·þÎñÆ÷ÉÏûÓÐÈκβ»±»ÃÜÂë»òÕß»ùÓÚ IP ·ÃÎÊÏÞÖÆ¿ØÖƵÄÄÚÈÝ£¬ÔòÕâЩѡÏîÅäÖöÔÄú²¢Ã»ÓÐ×÷Óá£Èç¹ûÄúµÄ WEB ·þÎñÆ÷²»ÔÊÐíÄú½øÐÐÖØ¶¨Ïò£¬»òÕß·þÎñÆ÷ÎÞ·¨ºÍ PHP ³ÌÐòͨÐÅÒÔʶ±ð°²È«µÄÖØ¶¨ÏòÇëÇó£¬Äú¿ÉÒÔÔÚÔËÐÐÅäÖýű¾µÄʱºòʹÓà <link linkend="install.configure.enable-force-cgi-redirect">--enable-force-cgi-redirect</link> ²ÎÊý¡£Äú»¹ÐèҪȷÈÏÄúµÄ PHP ½Å±¾²»½ö½öÖ»ÒÀÀµÄ³Ò»ÖÖ·½·¨À´µ÷Óýű¾£¬²»¹ÜÊÇÖ±½Óµ÷Óà <filename role="php">http://my.host/cgi-bin/php/dir/script.php</filename>£¬»¹ÊÇÖØ¶¨Ïòµ½ <filename role="php">http://my.host/dir/script.php</filename>¡£ </simpara> <simpara> ÔÚ Apache ÖÐÖØ¶¨Ïò¿ÉÒÔʹÓÃÑ¡Ïî AddHandler ºÍ Action À´ÅäÖã¨Çë²Î¿¼ÏÂÎÄ£©¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.force-redirect"> <title>·½·¨¶þ£ºÊ¹Óà --enable-force-cgi-redirect ²ÎÊý</title> <simpara> ¸Ã±àÒëʱ²ÎÊý·ÀÖ¹ÈκÎÈËͨ¹ýÖ±½Ó·ÃÎÊÀàËÆÓÚ <filename role="php">http://my.host/cgi-bin/php/secretdir/script.php</filename> µÄ URL À´µ÷Óà PHP¡£Ò²¾ÍÊÇ˵£¬Ö»Óе±½Å±¾»ùÓںϷ¨µÄÖØ¶¨Ïò¹æÔò±»·ÃÎÊʱ£¬PHP ²Å¶ÔËüÃǽøÐнâÎö¡£ </simpara> <simpara> ͨ³£ Apache µÄÖØ¶¨ÏòÉèÖÃÊÇͨ¹ýÒÔÏÂÑ¡ÏîÍê³ÉµÄ£º </simpara> <programlisting role="apache-conf"> <![CDATA[ Action php-script /cgi-bin/php AddHandler php-script .php ]]> </programlisting> <simpara> ¸ÃÑ¡Ïî½ö½öÔÚ Apache WEB ·þÎñÆ÷ÏÂ×ö¹ý²âÊÔ£¬²¢ÇÒÒÀÀµ Apache Ϊ±»Öض¨ÏòµÄÇëÇóÉèÖ÷DZê×¼µÄ CGI »·¾³±äÁ¿ <envar>REDIRECT_STATUS</envar>¡£Èç¹ûÄúµÄ WEB ·þÎñÆ÷²»Ö§³ÖÈκη½Ê½ÒÔ»ñϤÇëÇóÊÇÖ±½ÓµÄ»¹ÊDZ»Öض¨ÏòµÄ£¬Äú½«ÎÞ·¨Ê¹ÓøÃÑ¡Ïî¡£Äú±ØÐëʹÓñ¾ÎÄÌá¼°µÄÆäËüÔËÐÐ CGI °æ±¾µÄ·½·¨¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.doc-root"> <title>·½·¨Èý£ºÉèÖà doc_root »òÕß user_dir</title> <simpara> ÔÚ WEB ·þÎñÆ÷µÄÎĵµÄ¿Â¼ÖзÅÖÃÖîÈç½Å±¾ºÍ¿ÉÖ´ÐгÌÐòµÈÖ÷¶¯µÄÄÚÈÝ£¬³£³£±»ÈÏΪÊÇÒ»ÖÖ²»°²È«µÄ×ö·¨¡£Èç¹ûÓÉÓÚһЩÉèÖõĴíÎó£¬ÕâЩ½Å±¾Ã»Óб»Ö´ÐжøÏñͨ³£µÄ HTML ÎĵµÒ»Ñù±»ÏÔʾ£¬Õ⽫¿ÉÄܵ¼ÖÂ֪ʶ²úȨ»òÕßÖîÈçÃÜÂëµÈ°²È«ÐÅÏ¢µÄй©¡£Òò´ËºÜ¶àϵͳ¹ÜÀíÔ±ÄþԸΪ½Å±¾½¨Á¢ÁíÍâÒ»¸öÖ»Äܱ» PHP CGI ·ÃÎʵÄĿ¼½á¹¹£¬Ê¹µÃËüÃÇÄܹ»±»ÕýÈ·½âÎö£¬ÓÖ²»»á³öÏÖÉÏÊöµÄÎÊÌâ¡£ </simpara> <simpara> ͬÑù£¬Èç¹ûÈçÇ°ÃæÕ½ÚÃèÊöµÄÈ·±£ÇëÇó²»±»Öض¨ÏòµÄ·½·¨ÎÞЧ£¬ÄÇôÎÒÃǾÍÓбØÒª½¨Á¢Ò»¸öÇø±ðÓÚ WEB Îĵµ¸ùĿ¼½Å±¾µÄ doc_root Ŀ¼¡£ </simpara> <simpara> Äú¿ÉÒÔͨ¹ý<link linkend="configuration.file">ÅäÖÃÎļþ</link>ÖеÄÑ¡Ïî <link linkend="ini.doc-root">doc_root</link> »òÕß»·¾³±äÁ¿ <envar>PHP_DOCUMENT_ROOT</envar> À´ÉèÖà PHP ½Å±¾ÎĵµµÄ¸ùĿ¼¡£Èç¹û¸ÃÑ¡Ïî±»ÉèÖã¬ÄÇô PHP µÄ CGI °æ±¾½«×ÜÊÇֻͨ¹ý <parameter>doc_root</parameter> ºÍÇëÇóµÄ·¾¶ÐÅÏ¢À´´ò¿ªÎĵµ£¬ÕâÑùÄú¾Í¿ÉÒÔÈ·±£½Å±¾²»»áÔÚ¸ÃĿ¼ÒÔÍâÖ´ÐУ¨³ýÁËÒÔϽ«Ìá¼°µÄ <parameter>user_dir</parameter> Ŀ¼£©¡£ </simpara> <simpara> ÁíÍâÒ»¸öÕâÀï¿ÉÄÜÓõõ½µÄÑ¡ÏîÊÇ <link linkend="ini.user-dir">user_dir</link>¡£Èç¹û user_dir ûÓб»ÉèÖã¬ÄÇôΨһ¿ØÖƱ»´ò¿ªµÄÎļþÃûµÄÑ¡Ïî¾ÍÊÇ <parameter>doc_root</parameter>¡£´ò¿ªÒ»¸öÀàËÆÓÚ <filename role="url">http://my.host/~user/doc.php</filename> ½«²»»áµ¼Ö´ò¿ªÒ»¸öÓû§Ö÷Ŀ¼ÖеÄÎļþ£¬¶øÊÇ doc_root ÏÂÒ»¸öÃûΪ <filename role="uri">~user/doc.php</filename> µÄÎļþ£¨¼´Ò»¸öÒÔ²¨À˺Å[<literal>~</literal>]¿ªÍ·µÄĿ¼Ãû£©¡£ </simpara> <simpara> Èç¹û user_dir ±»ÉèÖÃΪ£¬ÀýÈ磬 <filename role="dir">public_php</filename>£¬ÄÇôÀàËÆÓÚ <filename role="url">http://my.host/~user/doc.php</filename> µØÇëÇ󽫴ò¿ªÒ»¸ö¸ÃÓû§Ö÷Ŀ¼ÏÂÃûΪ <filename role="dir">public_php</filename> µÄĿ¼ÏµÄÃûΪ <filename>doc.php</filename> µÄÎļþ¡£Èç¹ûÓû§µÄÖ÷Ŀ¼ÊÇ <filename role="dir">/home/user</filename>£¬ÄÇô±»Ö´ÐеÄÎļþÊÇ <filename>/home/user/public_php/doc.php</filename>¡£ </simpara> <simpara> <parameter>user_dir</parameter> À©Õ¹²¢²»ÒÀÀµÓÚ <parameter>doc_root</parameter> Ñ¡ÏîµÄÉ趨£¬Òò´ËÄú¿ÉÒÔ¶ÀÁ¢¿ØÖÆÎĵµ¸ùĿ¼ºÍÓû§·ÃÎÊĿ¼¡£ </simpara> </sect2> <sect2 id="security.cgi-bin.shell"> <title>·½·¨ËÄ£ºÔÚ WEB Ŀ¼Ê÷ÒÔÍâ½øÐÐ PHP ½âÎö</title> <para> Ò»ÖÖ°²È«ÐԷdz£¸ßµÄ·½·¨ÊÇ°Ñ PHP ½âÎö³ÌÐò·ÅÖõ½ WEB ÎļþĿ¼Ê÷ÒÔÍâµÄij¸öµØ·½£¬ÀýÈ磬·ÅÖõ½ <filename role="dir">/usr/local/bin</filename>¡£ÕâÖÖ×ö·¨Î¨Ò»µÄ±×²¡¾ÍÊÇÄúÏÖÔÚÐèÒªÔÚËùÓк¬ÓÐ PHP ±ê¼Ç·ûÎļþµÄµÚÒ»ÐÐÌí¼ÓÀàËÆÓÚÒÔϵÄÄÚÈÝ£º <informalexample> <programlisting> <![CDATA[ #!/usr/local/bin/php ]]> </programlisting> </informalexample> ÄúÐèҪʹµÃÕâЩÎļþ³ÉΪ¿ÉÖ´ÐÐÎļþ¡£Ò²¾ÍÊÇ˵£¬Ïñ¶Ô´ýÈÎºÎÆäËü CGI ½Å±¾Ò»ÑùÀ´¶Ô´ýËüÃÇ¡£ÕâЩ CGI ½Å±¾¿ÉÄÜÊÇÓà Perl¡¢sh »òÕßÈÎºÎÆäËüʹÓÃµÄ <literal>#!</literal> shell-escape »úÖÆÀ´Æô¶¯ËüÃÇ×ÔÉíµÄ½Å±¾ÓïÑÔÀ´±àдµÄ¡£ </para> <para> ҪʹÕâ¸ö·½·¨ÖÐ PHP Äܹ»ÕýÈ·´¦Àí <envar>PATH_INFO</envar> ºÍ <envar>PATH_TRANSLATED</envar> ÐÅÏ¢£¬ÔÚ±àÒë PHP ½âÎöÆ÷ʱ±ØÐëʹÓÃÅäÖòÎÊý <link linkend="install.configure.enable-discard-path">--enable-discard-path</link>¡£ </para> </sect2> </sect1> <sect1 id="security.apache"> <title>°²×°Îª Apache Ä£¿é</title> <simpara> µ± PHP ±»ÓÃ×÷ Apache µÄÄ£¿éʱ£¬Ëü½«¼Ì³Ð Apache µÄÓû§È¨ÏÞ£¨µäÐÍÇé¿öΪÓû§¡°nobody¡±£©¡£ Õ⽫¶Ô°²È«¼°ÊÚȨ»úÖÆ²úÉúһЩ³å»÷¡£ÀýÈ磬Èç¹ûÄúʹÓà PHP À´·ÃÎÊÊý¾Ý¿â£¬³ý·ÇÊý¾Ý¿â±¾ÉíÓÐÄÚ½¨µÄ·ÃÎÊ¿ØÖÆ£¬·ñÔòÄú½«Ê¹µÃÊý¾Ý¿âÄܹ»±»Óû§¡°nobody¡±·ÃÎÊ¡£ÕâÒâζ×ŶñÒâµÄ½Å±¾Äܹ»·ÃÎʲ¢ÐÞ¸ÄÊý¾Ý¿â£¬ÉõÖÁ²»ÐèÒªÓû§ÃûºÍÃÜÂë¡£ÍøÂçºÚ¿ÍÎÞÒâÖзÃÎʵ½ÁËÊý¾Ý¿â¹ÜÀíÔ±µÄ WEB Ò³Ãæ²¢Í¨¹ýÕâÀïɾ³ýËùÓеÄÊý¾Ý¿âÊÇÍêÈ«ÓпÉÄÜ·¢ÉúµÄ¡£Äú¿ÉÒÔÀûÓà Apache µÄÈÏÖ¤»úÖÆÀ´·ÀÖ¹ÕâЩµÄ·¢Éú£¬»òÕßÒ²¿ÉÒÔÀûÓà LDAP »ò &htaccess; ÎļþµÈÀ´Éè¼ÆÄú×Ô¼ºµÄ·ÃÎÊģʽ£¬²¢½«ÕâЩ´úÂë°üº¬ÎªÄú PHP ½Å±¾µÄÒ»²¿·Ö¡£ </simpara> <simpara> ͨ³££¬Ò»µ©°²È«»úÖÆ½¨Á¢²¢Ê¹µÃ PHP µÄÓÃÓÚ£¨ÔÚÕâÖÖÇé¿öÏ£¬Îª Apache Óû§£©½öΪ´Ë³Ðµ£ºÜСµÄ·çÏÕʱ£¬ÎÒÃÇ·¢ÏÖ PHP ´Ëʱ±»½ûÖ¹ÍùÓû§Ä¿Â¼Ð´ÈëÈκÎÎļþ£¬»òÕß»¹ÓпÉÄܱ»½ûÖ¹·ÃÎʺ͸ü¸ÄÊý¾Ý¿â¡£ÎÞÂÛÍù·ÀÖ¹µÄ¶ÔÏóÖÐдÈëÎļþµÄºÃ»µÒÔ¼°½øÈëµÄÊý¾Ý¿âÊÂÎñµÄºÃ»µ£¬Æä°²È«ÐÔ¶¼ÊÇͬµÈµÄ¡£ </simpara> <simpara> ÔÚÕâ¸öʱºò£¬Ò»¸öƵ·±³öÏֵݲȫ´íÎóÊǸø Apache root ȨÏÞ£¬»òÕßÓÃÆäËü·½·¨ÌáÉý Apache µÄÄÜÁ¦¡£ </simpara> <simpara> ¸ø Apache Óû§¸³Óè root ȨÏÞÊǼ°ÆäΣÏյ쬶øÇÒÓпÉÄÜ»áÁ¬ÀÛÕû¸öϵͳ¡£Òò´Ë£¬½øÐÐ sudo¡¢chroot£¬»òÕßÒÔ root Õ˺ÅÔËÐеȲÙ×÷²»Ó¦¸Ã¿¼ÂÇÈÃÄÇЩ·Ç°²È«×¨¼ÒÀ´Ö´ÐС£ </simpara> <simpara> »¹ÓÐһЩ¸ü¼òµ¥µÄÇé¿ö¡£Äú¿ÉÒÔʹÓà <link linkend="ini.open-basedir">open_basedir</link> À´¿ØÖƺÍÏÞÖÆ PHP Äܹ»Ê¹ÓõÄĿ¼¡£Äú»¹¿ÉÒÔ½¨Á¢ Apache µÄרÓÃÇøÓò£¬ÒÔ½«ËùÓлùÓÚ WEB µÄ»î¶¯¶¼ÏÞÖÆµ½·ÇÓû§¡¢ÏµÍ³ºÍÎļþ¡£ </simpara> </sect1> <sect1 id="security.filesystem"> <title>Îļþϵͳ°²È«</title> <simpara> PHP ÊÜ´ó¶àÊý·þÎñÆ÷ϵͳ°²È«»úÖÆµÄ¿ØÖÆ£¬ÕâЩ°²È«»úÖÆÍ¨³£ÊÇ»ùÓÚÎļþºÍĿ¼Ðí¿É»ù´¡µÄ¡£ÕâʹµÃÄú¿ÉÒÔ¿ØÖÆÎļþϵͳÖеÄÄÄЩÎļþ¿ÉÒÔ±»¶ÁÈ¡¡£ÄúÐèÒªÌØ±ð×¢ÒâËùÓÐÈ«¾Ö¿É¶ÁµÄÎļþ£¬Òª±£Ö¤ËùÓзÃÎʸÃÎļþϵͳµÄÓû§¶ÔËüÃǵĶÁÈ¡Êǰ²È«µÄ¡£ </simpara> <simpara> ÓÉÓÚ PHP ÔÚÉè¼ÆµÄʱºò£¬ÔÊÐíÓû§¼¶·ÃÎÊÎļþϵͳ£¬Òò´Ë±àдһ¸ö PHP ½Å±¾Ê¹µÃÄúÄܹ»¶ÁÈ¡ÖîÈç /etc/passwd£¬¸ü¸ÄÄúµÄÒÔÌ«ÍøÁ¬½Ó£¬·¢ËÍ·±ÖصĴòÓ¡»úÈÎÎñµÈÊÇÍêÈ«ÓпÉÄܵġ£ÕâÀïÓÐһЩÃ÷ÏԵĹØÁª£¬ÄúÐèÒª±£Ö¤Äú¶ÁÈ¡ºÍдÈëµÄÊÇÕýÈ·µÄÎļþ¡£ </simpara> <simpara> ÔÚÒÔϽű¾ÖУ¬Ò»¸öÓû§Ï£Íû¸Ã½Å±¾Äܹ»É¾³ýËüÃÇÖ÷Ŀ¼ÖеÄÒ»¸öÎļþ¡£Õâ½ö½öÊǼÙÉèÁËÒ»ÖÖ PHP WEB ½Ó¿Úͨ³£±»ÓÃ×÷Îļþ¹ÜÀíµÄÇé¿ö£¬Òò´Ë Apache Óû§±»ÔÊÐíɾ³ýÓû§Ö÷Ŀ¼µÄÎļþ¡£ </simpara> <para> <example> <title>±¡ÈõµÄ±äÁ¿¼ìÑ齫µ¼Ö¡¡</title> <programlisting role="php"> <![CDATA[ <?php // remove a file from the user's home directory $username = $_POST['user_submitted_name']; $homedir = "/home/$username"; $file_to_delete = "$userfile"; unlink ("$homedir/$userfile"); echo "$file_to_delete has been deleted!"; ?> ]]> </programlisting> </example> ÓÉÓÚÓû§ÃûÊÇ¿ÉÒÔ´ÓÓû§±íµ¥·¢Ë͵ģ¬Òò´ËËûÃÇ¿ÉÒÔÌá½»Ò»¸öÓû§ÃûºÍÊôÓÚÆäËûÈ˵ÄÎļþ£¬Ê¹µÃÕâЩÎļþ±»É¾³ý¡£ÔÚ¸ÃÇé¿öÏ£¬Äú»áÐèҪʹÓÃÁíÍâµÄ±íµ¥À´ÊÚȨ¡£¿¼ÂÇÒ»ÏÂÈç¹û±»Ìá½»µÄ±äÁ¿ÊÇ¡°../etc/¡±ºÍ¡°passwd¡±½«»á·¢Éúʲô£¿Õâ¸öʱºòÆä´úÂë¿ÉÒÔ±»Àí½âΪ£º <example> <title>Ò»¸öÎļþϵͳ¹¥»÷</title> <programlisting role="php"> <![CDATA[ <?php // removes a file from anywhere on the hard drive that // the PHP user has access to. If PHP has root access: $username = "../etc/"; $homedir = "/home/../etc/"; $file_to_delete = "passwd"; unlink ("/home/../etc/passwd"); echo "/home/../etc/passwd has been deleted!"; ?> ]]> </programlisting> </example> ÓÐÁ½ÖÖÄú±ØÐë²ÉÈ¡µÄ´ëÊ©À´±ÜÃâÕâЩÎÊÌâ¡£ <itemizedlist> <listitem> <simpara> ¶Ô PHP WEB Óû§½ö¸³ÓèÓÐÏÞµÄȨÏÞ¡£ </simpara> </listitem> <listitem> <simpara> ¼ìÑéËùÓÐÌá½»µÄ±äÁ¿¡£ </simpara> </listitem> </itemizedlist> ÒÔÏÂÊǾ¹ý¸Ä½øµÄ½Å±¾£º <example> <title>¸ü¼Ó°²È«µÄÎļþÃû¼ìÑé</title> <programlisting role="php"> <![CDATA[ <?php // removes a file from the hard drive that // the PHP user has access to. $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim $homedir = "/home/$username"; $file_to_delete = basename("$userfile"); // strip paths unlink ($homedir/$file_to_delete); $fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion $logstring = "$username $homedir $file_to_delete"; fputs ($fp, $logstring); fclose($fp); echo "$file_to_delete has been deleted!"; ?> ]]> </programlisting> </example> ¾¡¹ÜÈç´Ë£¬¸Ã½Å±¾»¹ÊÇÓÐһЩÎÊÌâ¡£Èç¹ûÄúµÄÊÚȨϵͳÔÊÐíÓû§½¨Á¢ËûÃÇ×Ô¼ºµÄÓû§µÇ¼£¬¶øÄ³Óû§Ñ¡ÔñµÇ¼¡°../etc/¡±£¬ÏµÍ³½«±»Ôٴα©Â¶¡£»ùÓÚÕâÒ»µã£¬Äú¿ÉÄܸüÔ¸Òâ±àдһ¸ö¸üºÃµÄ¼ìÑ飺 <example> <title>¸ü°²È«µÄÎļþÃû¼ìÑé</title> <programlisting role="php"> <![CDATA[ <?php $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim $homedir = "/home/$username"; if (!ereg('^[^./][^/]*$', $userfile)) die('bad filename'); //die, do not process if (!ereg('^[^./][^/]*$', $username)) die('bad username'); //die, do not process //etc... ?> ]]> </programlisting> </example> </para> <para> ¸ù¾ÝÄúµÄ²Ù×÷ϵͳµÄ²»Í¬£¬ÄúÐèÒª¿¼ÂÇÐí¶à¸÷ÖÖ¸÷ÑùµÄÎļþ£¬°üÀ¨É豸ÏîÄ¿£¨/dev/ »ò COM1£©£¬ÉèÖÃÎļþ£¨/etc/ ÎļþºÍ .ini Îļþ£©£¬Í¨³£µÄÎļþ´æ´¢Çø£¨/home/£¬My Documents£©µÈ¡£»ùÓÚÕâÒ»µã£¬½¨Á¢Ò»¸ö²ßÂÔ½ûÖ¹ËùÓгýÁËÄúÃ÷È·µÄÊÚȨµÄÐÐΪ¿ÉÄÜÒª¸ü¼òµ¥Ò»Ð©¡£ </para> </sect1> <sect1 id="security.database"> <title>Êý¾Ý¿â°²È«</title> <simpara> ÏÖÔÚ£¬ÎªÁËʹµÃÍøÕ¾Äܹ»Ìṩ¸÷ÖÖ¸÷Ñù¶¯Ì¬µÄÄÚÈÝ£¬Êý¾Ý¿âÒѾ³ÉΪËùÓлùÓÚ WEB Ó¦ÓóÌÐò×îÖØÒªµÄ×é¼þ¡£ÓÉÓÚһЩʮ·ÖÃô¸Ð»òÕß±£ÃܵÄÐÅÏ¢¿ÉÄÜ»á´æ´¢ÔÚÕâÑùµÄÊý¾Ý¿âÖУ¬Òò´Ë£¬ÄúÐèÒª·Ç³£É÷ÖØµÄ¿¼ÂÇÈçºÎ±£»¤ËüÃÇ¡£ </simpara> <simpara> ΪÁËÄܹ»´æ´¢»òÕß¼ìË÷ÐÅÏ¢£¬ÄúÐèÒªÁ¬½Óµ½Êý¾Ý¿â£¬·¢ËÍÒ»¸öºÏ·¨µÄ²éѯÃüÁµÃµ½½á¹û£¬È»ºó¹Ø±ÕÁ¬½Ó¡£Ä¿Ç°£¬ÔÚÕâ¸ö½»»¥¹ý³ÌÖÐ×î³£ÓõIJéѯÓïÑÔÊÇ¡°½á¹¹»¯²éѯÓïÑÔ£¨Structured Query Language, SQL£©¡±¡£Çë²ÎÔÄÒÔÏÂÄÚÈÝÒÔÁ˽âºÚ¿ÍÈçºÎ<link linkend="security.database.sql-injection">¡°ÀûÓà SQL ²éѯ¹¥»÷¡±</link>¡£ </simpara> <simpara> ÕýÈçÄúËùÖªµÀµÄ£¬PHP ×ÔÉí²¢ÎÞ·¨±£»¤ÄúµÄÊý¾Ý¿â¡£ÒÔÏÂÕ½ڵÄÄÚÈÝÖ¼ÔÚ½éÉÜÓйØÈçºÎÀûÓà PHP ½Å±¾·ÃÎʺͲÙ×÷Êý¾Ý¿âµÄ×î»ù´¡µÄ֪ʶ¡£ </simpara> <simpara> Çë¼ÇסÕâ¸ö¼òµ¥µÄ·¨Ôò£º¾¡¿ÉÄÜÉî²ã´ÎµÄ·ÀÓù¡£ÎªÁ˱£»¤ÄúµÄÊý¾Ý¿âËù²ÉÈ¡µÃ´ëÊ©ºÍ³¡ËùÔ½¶à£¬¹¥»÷Õß±©Â¶¡¢ÀÄÓÃÊý¾Ý¿âÖд洢µÄ±£ÃÜÐÅÏ¢µÄ¿ÉÄÜÐÔ¾ÍԽС¡£Ç뾡¿ÉÄܸüºÃµÄÉè¼ÆÊý¾Ý¿â½á¹¹ºÍÓ¦ÓóÌÐò¡£ </simpara> <sect2 id="security.database.design"> <title>Éè¼ÆÊý¾Ý¿â</title> <simpara> Ò»°ãÀ´ËµµÚÒ»²½×ÜÊǽ¨Á¢Êý¾Ý¿â£¬³ý·ÇÄúÏ£ÍûʹÓÃÒѾ´æÔڵĵÚÈý·½Êý¾Ý¿â¡£µ±Ò»¸öÊý¾Ý¿â±»½¨Á¢ºó£¬Ëü½«±»Ö¸¶¨¸øÒ»¸öËùÓÐÕߣ¬¼´ÔËÐн¨Á¢Êý¾Ý¿âÓï¾äµÄÓû§¡£Í¨³££¬Ö»ÓÐËùÓÐÕߣ¨»òÕß³¬¼¶Óû§£©²ÅÄܶԸÃÊý¾Ý¿âÖеĶÔÏó½øÐÐÈκβÙ×÷£¬ÎªÁËÄÜÈÃÆäËüÓû§Ê¹ÓøÃÊý¾Ý¿â£¬ÐèÒª½øÐÐȨÏÞÉèÖᣠ</simpara> <simpara> Ó¦ÓóÌÐò¾ö²»ÄÜÓÃËùÓÐÕß»òÕß³¬¼¶Óû§µÄÕ˺ÅÀ´Á¬½Óµ½Êý¾Ý¿â£¬ÒòΪÕâЩÓû§¿ÉÒÔÖ´ÐÐÈκβéѯ£¬ÀýÈ磬ÐÞ¸ÄÊý¾Ý½á¹¹£¨Èçɾ³ý±í¸ñ£©»òÕßɾ³ýËùÓеÄÄÚÈÝ¡£ </simpara> <simpara> Äú¿ÉÒÔΪӦÓóÌÐò²»Í¬µÄ²¿·Ö½¨Á¢²»Í¬µÄÊý¾Ý¿âÕ˺ţ¬Ê¹µÃËüÃÇÖ°ÄܶÔÊý¾Ý¿â¶ÔÏóÐÐʹ·Ç³£ÓÐÏÞµÄȨÏÞ¡£¶ÔÕâЩÕ˺ÅÓ¦¸ÃÖ»¸³Óè×îÐèÒªµÄȨÏÞ£¬Í¬Ê±Ó¦¸Ã·ÀÖ¹ÏàͬµÄÓû§Äܹ»ÔÚ²»Í¬µÄʹÓÃÇé¿öÓëÊý¾Ý¿â½øÐн»Á÷¡£ÕâÒ²¾ÍÊÇ˵£¬Èç¹ûijһ¸öÈëÇÖÕßÀûÓÃÕâЩÕ˺ÅÖеÄijһ¸ö»ñµÃÁË·ÃÎÊÄúÊý¾Ý¿âµÄȨÏÞ£¬ËûÃÇÒ²½ö½öÄܹ»Ó°Ïìµ½ÄúµÄÓ¦ÓóÌÐòÁ¦ËùÄܼ°µÄ·¶Î§¡£ </simpara> <simpara> Äú×îºÃ²»Òª°ÑËùÓеÄÊÂÎï¹ý³Ì¶¼·ÅÔÚ WEB Ó¦ÓóÌÐò£¨¼´ÄúµÄ½Å±¾£©ÖÐÀ´ÊµÊ©£¬¶ø³ä·ÖµÄÀûÓÃÊý¾Ý¿â½á¹¹£¬Ê¹ÓÃÊÓͼ£¨view£©¡¢´¥·¢Æ÷£¨trigger£©»òÕß¹æÔò£¨rule£©¡£Èç¹ûÐèÒª¶Ôϵͳ½øÐÐÉý¼¶£¬ÔòÐèÒª¶ÔÊý¾Ý¿â¿ª±ÙеĶ˿ڣ¬Òò´ËÄúÐèÒª¶Ôÿһ¸ö¶ÀÁ¢µÄÊý¾Ý¿â¿Í»§ÖØÐÂÉèÖÃȨÏÞ¡£×ÜÖ®£¬´¥·¢Æ÷¿ÉÒÔ±»ÓÃÀ´Í¸Ã÷µØºÍ×Ô¶¯µØ´¦Àí×ֶΣ¬¸ÃÌØÐÔ¿ÉÒÔÔÚµ÷ÊÔÓ¦ÓóÌÐòµØÎÊÌâ»òÕß×·×Ùºǫ́ÊÂÎïʱÌṩ±ãÀûÐÅÏ¢¡£ </simpara> </sect2> <sect2 id="security.database.connection"> <title>Á¬½ÓÊý¾Ý¿â</title> <simpara> Äú¿ÉÄÜÏ£Íûͨ¹ý SSL ½¨Á¢Á¬½ÓÀ´¼ÓÃܿͻ§¶ËºÍ·þÎñ¶ËÖ®¼äµÄͨѶÒÔÔö¼Ó°²È«ÐÔ£¬»òÕßÄú¿ÉÒÔʹÓà ssh À´¼ÓÃܿͻ§¶ËºÍÊý¾Ý¿â·þÎñÆ÷Ö®¼äµÄÍøÂçÁ¬½Ó¡£Èç¹ûÄúʵʩÁËÕâЩ´ëÊ©£¬Ôò¼àÊÓÄúµÄÍøÂçÁ÷Á¿ÒÔ¼°ÒÔÕâÖÖ·½Ê½»ñÈ¡ÐÅÏ¢½«±ä³ÉÊ®·Ö¸´ÔӵŤ×÷¡£ </simpara> <!--simpara> If your database server has native SSL support, consider to use <link linkend="ref.openssl">OpenSSL functions</link> in communication between PHP and database via SSL. </simpara--> </sect2> <sect2 id="security.database.storage"> <title>¼ÓÃܴ洢ģÐÍ</title> <simpara> SSL/SSH protects data travelling from the client to the server, SSL/SSH does not protect the persistent data stored in a database. SSL is an on-the-wire protocol. </simpara> <simpara> Once an attacker gains access to your database directly (bypassing the webserver), the stored sensitive data may be exposed or misused, unless the information is protected by the database itself. Encrypting the data is a good way to mitigate this threat, but very few databases offer this type of data encryption. </simpara> <simpara> The easiest way to work around this problem is to first create your own encryption package, and then use it from within your PHP scripts. PHP can assist you in this case with its several extensions, such as <link linkend="ref.mcrypt">Mcrypt</link> and <link linkend="ref.mhash">Mhash</link>, covering a wide variety of encryption algorithms. The script encrypts the data be stored first, and decrypts it when retrieving. See the references for further examples how encryption works. </simpara> <simpara> In case of truly hidden data, if its raw representation is not needed (i.e. not be displayed), hashing may be also taken into consideration. The well-known example for the hashing is storing the MD5 hash of a password in a database, instead of the password itself. See also <function>crypt</function> and <function>md5</function>. </simpara> <example> <title>ʹÓþ¹ý¹þÏ£ÔËËãµÄÃÜÂë×Ö¶Î</title> <programlisting role="php"> <![CDATA[ // storing password hash $query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');", addslashes($username), md5($password)); $result = pg_exec($connection, $query); // querying if user submitted the right password $query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';", addslashes($username), md5($password)); $result = pg_exec($connection, $query); if (pg_numrows($result) > 0) { echo "Welcome, $username!"; } else { echo "Authentication failed for $username."; } ]]> </programlisting> </example> </sect2> <sect2 id="security.database.sql-injection"> <title>SQL ¹¥»÷</title> <simpara> Many web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands. </simpara> <simpara> Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately. </simpara> <para> Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database. <example> <title> ½«½á¹û¼¯·ÖÀëµ½Ò³ÃæÖУ¬È»ºó´´Ô쳬¼¶Óû§(PostgreSQL and MySQL) </title> <programlisting role="php"> <![CDATA[ $offset = argv[0]; // beware, no input validation! $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // with PostgreSQL $result = pg_exec($conn, $query); // with MySQL $result = mysql_query($query); ]]> </programlisting> </example> Normal users click on the 'next', 'prev' links where the <varname>$offset</varname> is encoded into the URL. The script expects that the incoming <varname>$offset</varname> is decimal number. However, someone tries to break in with appending <function>urlencode</function>'d form of the following to the URL <informalexample> <programlisting> <![CDATA[ // in case of PostgreSQL 0; insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd) select 'crack', usesysid, 't','t','crack' from pg_shadow where usename='postgres'; -- // in case of MySQL 0; UPDATE user SET Password=PASSWORD('crack') WHERE user='root'; FLUSH PRIVILEGES; ]]> </programlisting> </informalexample> If it happened, then the script would present a superuser access to him. Note that <literal>0;</literal> is to supply a valid offset to the original query and to terminate it. </para> <note> <para> It is common technique to force the SQL parser to ignore the rest of the query written by the developer with <literal>--</literal> which is the comment sign in SQL. </para> </note> <para> A feasible way to gain passwords is to circumvent your search result pages. What the attacker needs only is to try if there is any submitted variable used in SQL statement which is not handled properly. These filters can be set commonly in a preceding form to customize <literal>WHERE, ORDER BY, LIMIT</literal> and <literal>OFFSET</literal> clauses in <literal>SELECT</literal> statements. If your database supports the <literal>UNION</literal> construct, the attacker may try to append an entire query to the original one to list passwords from an arbitrary table. Using encrypted password fields is strongly encouraged. <example> <title> ÁгöÎÄÕ£¬ÒÔ¼°Ò»Ð©ÃÜÂ루ÈκÎÊý¾Ý¿â·þÎñÆ÷£© </title> <programlisting role="php"> <![CDATA[ $query = "SELECT id, name, inserted, size FROM products WHERE size = '$size' ORDER BY $order LIMIT $limit, $offset;"; $result = odbc_exec($conn, $query); ]]> </programlisting> </example> The static part of the query can be combined with another <literal>SELECT</literal> statement which reveals all passwords: <informalexample> <programlisting> <![CDATA[ ' union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable; -- ]]> </programlisting> </informalexample> If this query (playing with the <literal>'</literal> and <literal>--</literal>) were assigned to one of the variables used in <varname>$query</varname>, the query beast awakened. </para> <para> SQL UPDATEs are also subject to attacking your database. These queries are also threatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the <literal>SET</literal> clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examing the form variable names, or just simply brute forcing. There are not so many naming convention for fields storing passwords or usernames. <example> <title> ÀûÓÃÖØÉèÃÜÂëÀ´»ñÈ¡¸ü¶àµÄȨÏÞ£¨ÈκÎÊý¾Ý¿â·þÎñÆ÷£© </title> <programlisting role="php"> <![CDATA[ $query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';"; ]]> </programlisting> </example> But a malicious user sumbits the value <literal>' or uid like'%admin%'; --</literal> to <varname>$uid</varname> to change the admin's password, or simply sets <varname>$pwd</varname> to <literal>"hehehe', admin='yes', trusted=100 "</literal> (with a trailing space) to gain more privileges. Then, the query will be twisted: <informalexample> <programlisting role="php"> <![CDATA[ // $uid == ' or uid like'%admin%'; -- $query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --"; // $pwd == "hehehe', admin='yes', trusted=100 " $query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE ...;" ]]> </programlisting> </informalexample> </para> <para> A frightening example how operating system level commands can be accessed on some database hosts. <example> <title>¹¥»÷Êý¾Ý¿âÖ÷»úµÄ²Ù×÷ϵͳ (MSSQL Server)</title> <programlisting role="php"> <![CDATA[ $query = "SELECT * FROM products WHERE id LIKE '%$prod%'"; $result = mssql_query($query); ]]> </programlisting> </example> If attacker submits the value <literal>a%' exec master..xp_cmdshell 'net user test testpass /ADD' --</literal> to <varname>$prod</varname>, then the <varname>$query</varname> will be: <informalexample> <programlisting role="php"> <![CDATA[ $query = "SELECT * FROM products WHERE id LIKE '%a%' exec master..xp_cmdshell 'net user test testpass /ADD'--"; $result = mssql_query($query); ]]> </programlisting> </informalexample> MSSQL Server executes the SQL statements in the batch including a command to add a new user to the local accounts database. If this application were running as <literal>sa</literal> and the MSSQLSERVER service is running with sufficient privileges, the attacker would now have an account with which to access this machine. </para> <note> <para> Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be so vulnerable in other manner. </para> </note> <sect3 id="security.database.avoiding"> <title>Ô¤·ÀµÄ¼¼ÇÉ</title> <simpara> You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management system or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one. </simpara> <simpara> These attacks are mainly based on exploiting the code not being written with security in mind. Never trust on any kind of input, especially which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters. </simpara> <itemizedlist> <listitem> <simpara> Never connect to the database as a superuser or as the database owner. Use always customized users with very limited privileges. </simpara> </listitem> <listitem> <simpara> Check if the given input has the expected data type. PHP has a wide range of input validating functions, from the simplest ones found in <link linkend="ref.variables">Variable Functions</link> and in <link linkend="ref.ctype">Character Type Functions</link> (e.g. <function>is_numeric</function>, <function>ctype_digit</function> respectively) onwards the <link linkend="ref.pcre">Perl compatible Regular Expressions</link> support. </simpara> </listitem> <listitem> <para> If the application waits for numerical input, consider to verify data with <function>is_numeric</function>, or silently change its type using <function>settype</function>, or use its numeric representation by <function>sprintf</function>. <example> <title>¸ü¼Ó°²È«µÄ·ÖÒ³²éѯÓï¾ä</title> <programlisting role="php"> <![CDATA[ settype($offset, 'integer'); $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // please note %d in the format string, using %s would be meaningless $query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;", $offset); ]]> </programlisting> </example> </para> </listitem> <listitem> <simpara> Quote each non numeric user input which is passed to the database with <function>addslashes</function> or <function>addcslashes</function>. See <link linkend="security.database.storage">the first example</link>. As the examples shows, quotes burnt into the static part of the query is not enough, and can be easily hacked. </simpara> </listitem> <listitem> <simpara> Do not print out any database specific information, especially about the schema, by fair means or foul. See also <link linkend="security.errors">Error Reporting</link> and <link linkend="ref.errorfunc">Error Handling and Logging Functions</link>. </simpara> </listitem> <listitem> <simpara> You may use stored procedures and previously defined cursors to abstract data access so that users do not directly access tables or views, but this solution has another impacts. </simpara> </listitem> </itemizedlist> <simpara> Besides these, you benefit from logging queries either within your script or by the database itself, if it supports. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself, but through the information it contains. The more detail is generally better. </simpara> </sect3> </sect2> </sect1> <sect1 id="security.errors"> <title>´íÎ󱨸æ</title> <para> With PHP security, there are two sides to error reporting. One is beneficial to increasing security, the other is detrimental. </para> <para> A standard attack tactic involves profiling a system by feeding it improper data, and checking for the kinds, and contexts, of the errors which are returned. This allows the system cracker to probe for information about the server, to determine possible weaknesses. For example, if an attacker had gleaned information about a page based on a prior form submission, they may attempt to override variables, or modify them: <example> <title>ÓÃ×Ô¶¨ÒåµÄ HTML Ò³ÃæÀ´¹¥»÷±äÁ¿</title> <programlisting role="php"> <![CDATA[ <form method="POST" action="attacktarget?username=badfoo&password=badfoo"> <input type="hidden" name="username" value="badfoo"> <input type="hidden" name="password" value="badfoo"> </form> ]]> </programlisting> </example> </para> <para> The PHP errors which are normally returned can be quite helpful to a developer who is trying to debug a script, indicating such things as the function or file that failed, the PHP file it failed in, and the line number which the failure occured in. This is all information that can be exploited. It is not uncommon for a php developer to use <function>show_source</function>, <function>highlight_string</function>, or <function>highlight_file</function> as a debugging measure, but in a live site, this can expose hidden variables, unchecked syntax, and other dangerous information. Especially dangerous is running code from known sources with built-in debugging handlers, or using common debugging techniques. If the attacker can determine what general technique you are using, they may try to brute-force a page, by sending various common debugging strings: <example> <title>ÀûÓÃÆÕͨµÄµ÷ÊÔ±äÁ¿</title> <programlisting role="php"> <![CDATA[ <form method="POST" action="attacktarget?errors=Y&showerrors=1"&debug=1"> <input type="hidden" name="errors" value="Y"> <input type="hidden" name="showerrors" value="1"> <input type="hidden" name="debug" value="1"> </form> ]]> </programlisting> </example> </para> <para> Regardless of the method of error handling, the ability to probe a system for errors leads to providing an attacker with more information. </para> <para> For example, the very style of a generic PHP error indicates a system is running PHP. If the attacker was looking at an .html page, and wanted to probe for the back-end (to look for known weaknesses in the system), by feeding it the wrong data they may be able to determine that a system was built with PHP. </para> <para> A function error can indicate whether a system may be running a specific database engine, or give clues as to how a web page or programmed or designed. This allows for deeper investigation into open database ports, or to look for specific bugs or weaknesses in a web page. By feeding different pieces of bad data, for example, an attacker can determine the order of authentication in a script, (from the line number errors) as well as probe for exploits that may be exploited in different locations in the script. </para> <para> A filesystem or general PHP error can indicate what permissions the webserver has, as well as the structure and organization of files on the web server. Developer written error code can aggravate this problem, leading to easy exploitation of formerly "hidden" information. </para> <para> There are three major solutions to this issue. The first is to scrutinize all functions, and attempt to compensate for the bulk of the errors. The second is to disable error reporting entirely on the running code. The third is to use PHP's custom error handling functions to create your own error handler. Depending on your security policy, you may find all three to be applicable to your situation. </para> <para> One way of catching this issue ahead of time is to make use of PHP's own <function>error_reporting</function>, to help you secure your code and find variable usage that may be dangerous. By testing your code, prior to deployment, with E_ALL, you can quickly find areas where your variables may be open to poisoning or modification in other ways. Once you are ready for deployment, by using E_NONE, you insulate your code from probing. <example> <title>ÀûÓà E_ALL À´ÕÒ³öΣÏյıäÁ¿</title> <programlisting role="php"> <![CDATA[ <?php if ($username) { // Not initialized or checked before usage $good_login = 1; } if ($good_login == 1) { // If above test fails, not initialized or checked before usage readfile ("/highly/sensitive/data/index.html"); } ?> ]]> </programlisting> </example> </para> </sect1> <sect1 id="security.registerglobals"> <title>Using Register Globals</title> <para> Perhaps the most controversial change in PHP is when the default value for the PHP directive <link linkend="ini.register-globals"> register_globals</link> went from ON to OFF in PHP <ulink url="&url.php.release4.2.0;">4.2.0</ulink>. Reliance on this directive was quite common and many people didn't even know it existed and assumed it's just how PHP works. This page will explain how one can write insecure code with this directive but keep in mind that the directive itself isn't insecure but rather it's the misuse of it. </para> <para> When on, register_globals will inject (poison) your scripts will all sorts of variables, like request variables from html forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this. Let's demonstrate with an example misuse of register_globals: </para> <para> <example> <title>Example misuse with register_globals = on</title> <programlisting role="php"> <![CDATA[ <?php // define $authorized = true only if user is authenticated if (authenticated_user()) { $authorized = true; } // Because we didn't first initialize $authorized as false, this might be // defined through register_globals, like from GET auth.php?authorized=1 // So, anyone can be seen as authenticated! if ($authorized) { include "/highly/sensitive/data.php"; } ?> ]]> </programlisting> </example> </para> <para> When register_globals = on, our logic above may be compromised. When off, <varname>$authorized</varname> can't be set via request so it'll be fine, although it really is generally a good programming practice to initialize variables first. For example, in our example above we might have first done <literal>$authorized = false</literal>. Doing this first means our above code would work with register_globals on or off as users by default would be unauthorized. </para> <para> Another example is that of <link linkend="ref.session">sessions</link>. When register_globals = on, we could also use <varname>$username</varname> in our example below but again you must realize that <varname>$username</varname> could also come from other means, such as GET (through the URL). </para> <para> <example> <title>Example use of sessions with register_globals on or off</title> <programlisting role="php"> <![CDATA[ <?php // We wouldn't know where $username came from but do know $_SESSION is // for session data if (isset($_SESSION['username'])) { echo "Hello <b>{$_SESSION['username']}</b>"; } else { echo "Hello <b>Guest</b><br />"; echo "Would you like to login?"; } ?> ]]> </programlisting> </example> </para> <para> It's even possible to take preventative measures to warn when forging is being attempted. If you know ahead of time exactly where a variable should be coming from, you can check to see if the submitted data is coming from an inappropriate kind of submission. While it doesn't guarantee that data has not been forged, it does require an attacker to guess the right kind of forging. If you don't care where the request data comes from, you can use <varname>$_REQUEST</varname> as it contains a mix of GET, POST and COOKIE data. See also the manual section on using <link linkend="language.variables.external">variables from outside of PHP</link>. </para> <para> <example> <title>Detecting simple variable poisoning</title> <programlisting role="php"> <![CDATA[ <?php if (isset($_COOKIE['MAGIC_COOKIE'])) { // MAGIC_COOKIE comes from a cookie. // Be sure to validate the cookie data! } elseif (isset($_GET['MAGIC_COOKIE']) || isset($_POST['MAGIC_COOKIE'])) { mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']); echo "Security violation, admin has been alerted."; exit; } else { // MAGIC_COOKIE isn't set through this REQUEST } ?> ]]> </programlisting> </example> </para> <para> Of course, simply turning off register_globals does not mean your code is secure. For every piece of data that is submitted, it should also be checked in other ways. Always validate your user data and initialize your variables! To check for unitialized variables you may turn up <function>error_reporting</function> to show <constant>E_NOTICE</constant> level errors. </para> ¬e.superglobals; </sect1> <sect1 id="security.variables"> <title>Óû§Ìá½»µÄÊý¾Ý</title> <para> The greatest weakness in many PHP programs is not inherent in the language itself, but merely an issue of code not being written with security in mind. For this reason, you should always take the time to consider the implications of a given piece of code, to ascertain the possible damage if an unexpected variable is submitted to it. <example> <title>ΣÏյıäÁ¿Ó÷¨</title> <programlisting role="php"> <![CDATA[ <?php // remove a file from the user's home directory... or maybe // somebody else's? unlink ($evil_var); // Write logging of their access... or maybe an /etc/passwd entry? fputs ($fp, $evil_var); // Execute something trivial.. or rm -rf *? system ($evil_var); exec ($evil_var); ?> ]]> </programlisting> </example> You should always carefully examine your code to make sure that any variables being submitted from a web browser are being properly checked, and ask yourself the following questions: <itemizedlist> <listitem> <simpara> Will this script only affect the intended files? </simpara> </listitem> <listitem> <simpara> Can unusual or undesirable data be acted upon? </simpara> </listitem> <listitem> <simpara> Can this script be used in unintended ways? </simpara> </listitem> <listitem> <simpara> Can this be used in conjunction with other scripts in a negative manner? </simpara> </listitem> <listitem> <simpara> Will any transactions be adequately logged? </simpara> </listitem> </itemizedlist> By adequately asking these questions while writing the script, rather than later, you prevent an unfortunate re-write when you need to increase your security. By starting out with this mindset, you won't guarantee the security of your system, but you can help improve it. </para> <para> You may also want to consider turning off register_globals, magic_quotes, or other convenience settings which may confuse you as to the validity, source, or value of a given variable. Working with PHP in error_reporting(E_ALL) mode can also help warn you about variables being used before they are checked or initialized (so you can prevent unusual data from being operated upon). </para> </sect1> <sect1 id="security.hiding"> <title>Òþ²Ø PHP</title> <para> In general, security by obscurity is one of the weakest forms of security. But in some cases, every little bit of extra security is desirable. </para> <para> A few simple techniques can help to hide PHP, possibly slowing down an attacker who is attempting to discover weaknesses in your system. By setting expose_php = off in your &php.ini; file, you reduce the amount of information available to them. </para> <para> Another tactic is to configure web servers such as apache to parse different filetypes through PHP, either with an &htaccess; directive, or in the apache configuration file itself. You can then use misleading file extensions: <example> <title>½« PHP Òþ²ØÎªÁíÒ»ÖÖÓïÑÔ</title> <programlisting role="apache-conf"> <![CDATA[ # Make PHP code look like other code types AddType application/x-httpd-php .asp .py .pl ]]> </programlisting> </example> »òÕßÍêÈ«µØÒþ²Ø£º <example> <title>¶Ô PHP ʹÓÃδ֪À©Õ¹Ãû</title> <programlisting role="apache-conf"> <![CDATA[ # Make PHP code look like unknown types AddType application/x-httpd-php .bop .foo .133t ]]> </programlisting> </example> Or hide it as html code, which has a slight performance hit because all html will be parsed through the PHP engine: <example> <title>¶Ô PHP ʹÓà HTML À©Õ¹Ãû</title> <programlisting role="apache-conf"> <![CDATA[ # Make all PHP code look like html AddType application/x-httpd-php .htm .html ]]> </programlisting> </example> For this to work effectively, you must rename your PHP files with the above extensions. While it is a form of security through obscurity, it's a minor preventative measure with few drawbacks. </para> </sect1> <sect1 id="security.current"> <title>¸üе½×îа汾</title> <simpara> PHP, like any other large system, is under constant scrutiny and improvement. Each new version will often include both major and minor changes to enhance and repair security flaws, configuration mishaps, and other issues that will affect the overall security and stability of your system. </simpara> <simpara> Like other system-level scripting languages and programs, the best approach is to update often, and maintain awareness of the latest versions and their changes. </simpara> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->