cvs: phpdoc /kr/chapters security.xml
| From: | WonHee Hwang | Date: | Tue, 09 Jan 2001 03:00:50 +0000 |
| Subject: | cvs: phpdoc /kr/chapters security.xml | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969333495@lists.php.net to get a copy of this message | ||
regina Mon Jan 8 19:00:50 2001 EDT
Added files:
/phpdoc/kr/chapters security.xml
Log:
PR:
Submitted by:
Reviewed by:
Obtained from:
Index: phpdoc/kr/chapters/security.xml +++ phpdoc/kr/chapters/security.xml <chapter id="security"> <title>Security</title> <simpara> PHPë ë§¤ì° ê°ë ¥í ì¸ì´ì´ë¤. ì´ ì¸í°í리í°ë ì¹ìë²ì 모ëë¡ ëì´ ìê±´ <acronym>CGI</acronym> binaryë¡ ëì´ ìê±´, ìì¤í ë´ì íì¼ì ì¬ì©í ì ìê³ , ì¬íì ëª ë ¹ì´ë íë¡ê·¸ë¨ì ì¤íí ì ììë¿ ìëë¼, ë¤í¸ìì íµí´ ë¤ë¥¸ ìë²ë¡ ì°ê²°ì í ìë ìë¤. ì´ë° 기ë¥ë¤ì ì ì를 ê°ì§ ì¬ì©ìë ì´ë³´ìê° ì´ë¤ íë¡ê·¸ë¨ì´ë ì¤íìì¼ ì¹ìë²ë¥¼ ë³´ìì ë¶ìí ìíë¡ ë§ë¤ ì ìë¤. PHPë ë¶ëª í Perlì´ë Cë³´ë¤ ì¢ë ìì í CGI íë¡ê·¸ë¨ì ë§ë¤ ì ìëë¡ ëì´ ìë¤. ì¬ë¬ì´ ì»´íì¼ì´ë ì¤íì ì¤ì ìµì ë¤ì ì ì í ì¡°í©íì¬ ì¬ì©íë©´, ìíë ì ëì ìì ë¡ìê³¼ ë³´ììì ì ì½ì ì¡°íìí¬ ì ìë¤. </simpara> <simpara> PHPë ìë§ì ë°©ë²ì¼ë¡ ì´ì©ì´ ê°ë¥íê³ , ì´ì ë°ë¥¸ ì¬ë¬ ê°ì§ ì¤ì ìµì ì´ ìì ì ìë¤. ë§ì ìµì ì í ì¬íë¤ì PHP를 ë³´ë¤ ë§ì 목ì ì¼ë¡ ì¬ì©í ì ìëë¡ íì§ë§, ì´ ìµì ë¤ì ì¡°í©ê³¼ ìë²ì ì¤ì ì ë°ë¼ ë³´ììì íì ì ë ¸ì¶ìí¬ ìë ìë¤. </simpara> <simpara> The configuration flexibility of PHP is equally rivalled by the code flexibility. PHP can be used to build complete server applications, with all the power of a shell user, or it can be used for simple server-side includes with little risk in a tightly controlled environment. How you build that environment, and how secure it is, is largely up to the PHP developer. </simpara> <simpara> This chapter starts by explaining the different configuration option combinations and the situations they can be safely used. It then describes different considerations in coding for different levels of security, and ends with some general security advice. </simpara> <sect1 id="security.cgi"> <title>Installed as CGI binary</title> <sect2 id="security.cgi.attacks"> <title>ê°ë¥í 공격 (Possible attacks)</title> <simpara> <acronym>CGI</acronym> binary ííì PHPë ì´ë¤ ì´ì ë¡ (ìíì¹ ê°ì) ìë² ìíí¸ì¨ì´ì 모ëë¡ ì¬ì©ë기를 ìíì§ ìë ê²½ì°ì ì¬ì©í ì ìê³ , PHP를 ë¤ë¥¸ ì¢ ë¥ì CGI wrapperì í¨ê» ìì í chrootì setuid íê²½ì ë§ë¤ì´ ë´ë ê³³ì ëì ì¬ì©í ì ìë¤. ì´ ì¤ì ì ë³´íµ ì¤íí PHP binary를 ì¹ìë²ì cgi-binëë í 리ì ì¤ì¹í´ì¼ íë¤. CERT ê¶ê³ ì¬í <ulink url="&url.cert;">CA-96.11</ulink>ì ë°ë¥´ë©´ 모ë ì¸í°í리í°ë¤ì cgi-binì ì¤ì¹íëë¡ ê¶ê³ ëë¤. ë¹ë¡ PHP binaryê° ë 립ì ì¸ ì¸í°í리í°ë¡ ì¬ì©ëë¤ í ì§ë¼ë, PHPë ì´ë° ì¤ì ìì ë°ì ì ìë 공격ì ë¤ìê³¼ ê°ì´ ë§ìë´ëë¡ ì¤ê³ëì´ ìë¤. : </simpara> <itemizedlist> <listitem> <simpara> ìì¤í íì¼ë¡ì ì ê·¼ : <filename role="url">http://my.host/cgi-bin/php?/etc/passwd</filename> </simpara> <simpara> ì´ urlë´ì 물ìí(?)ì´íì ìë ìì²ì CGI ì¸í°íì´ì¤ì ìí´ command line argumentë¡ ì¸í°í리í°ìê² ì ë¬ëë¤. ë³´íµ ì¸í°í리í°ë 커맨ë ë¼ì¸ì 첫 ë²ì§¸ argumentì íì¼ì ì½ì´ì ì¤í íë¤. </simpara> <simpara> CGI binaryë¡ ì¤í ë PHPë 모ë command line argumentë¤ì 모ë 무ìíë¤. </simpara> </listitem> <listitem> <simpara> ìë²ì ì¹ ë¬¸ìë¡ì ì ê·¼ : <filename role="url">http://my.host/cgi-bin/php/secret/doc.html</filename> </simpara> <simpara> ì´ urlì PHP binary ì´íì path ì ë³´(<filename role="uri">/secret/doc.html</filename>)ë ì¼ë°ì ì¼ë¡ <acronym>CGI</acronym> íë¡ê·¸ë¨ì ìí´ ì½ê³ í´ìë íì¼ ì´ë¦ì¼ë¡ ì¬ì©ëë¤. ë³´íµ ì¹ìë²ë ì¤ì ì§ìì(Apacheì ê²½ì° Action)를 ì¬ì©íì¬ <filename role="url">http://my.host/secret/script.php3</filename>ê°ì 문ì ìì²ì PHP ì¸í°í리í°ë¡ ë°ë¡ 리ëë í¸ íëë¡ í ì ìë¤. ì´ë ê² ì¤ì íë©´, ì¹ìë²ë ì°ì <filename role="uri">/secret</filename> ëë í 리ì ì ê·¼ ê¶íì ê²ì¬í íì <filename role="url">http://my.host/cgi-bin/php/secret/script.php3</filename>ë¡ ë¦¬ëë í¸ë¥¼ íë¤. ë¶ííë, ì´ ìì²ì´ ìëì formìì 주ì´ì§ ê²ì´ë¼ë©´, ì¹ìë²ë <filename role="uri">/secret/script.php3</filename> íì¼ì ëí ì ê·¼ ê¶í ê²ì¬ë¥¼ íì§ ìê³ , <filename role="uri">/cgi-bin/php</filename> íì¼ì ëí ê²ì¬ë§ì ííë¤. ì´ ë°©ë²ì ì¬ì©íë©´ <filename role="uri">/cgi-bin/php</filename>ì ì ê·¼ ê°ë¥í ì¬ì©ìë ì¹ìë²ìì 모ë ë³´í¸ë 문ìë¤ë ì ê·¼í ì ìë¤. </simpara> <simpara> ì´ë°ê²½ì° ìë² ë¬¸ì treeì ì ê·¼ ì ì½ì´ ìë ëë í ë¦¬ê° ìë¤ë©´, ì»´íì¼ìì <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.default"> <title>Case 1: ê³µì© íì¼ë§ ì ê³µíë ê²½ì° (only public files served)</title> <simpara> ë§ì½ ì¬ë¬ë¶ì ìë²ê° password ë ip 기ì¤ì ì ê·¼ íµì ì ê´í ì´ë í ì¬íë ìë¤ë©´, ì´ë¬í ì¤ì ìµì ë¤ì íì ìë¤. ë§ì½ ì¬ë¬ë¶ì ì¹ìë²ê° 리ëë í¸ë¥¼ íì©íì§ ìê±°ë, ìë²ê° í´ë¹ ìêµ¬ê° ìì íê² ë¦¬ëë í¸ë ì구ì¸ì§ ëí ì 보를 PHP binaryì ì ë¬í ì ìë ë°©ë²ì´ ìì ê²½ì°, config ì¤í¬ë¦½í¸ìì <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.php3</filename>ê°ì ì§ì ì ê·¼ ë°©ë²ì´ë <filename role="php">http://my.host/dir/script.php3</filename>ê°ì 리ëë ì ì ë°©ë² ì´ì¸ì, ë¤ë¥¸ ë¯¿ì§ ëª»í ë°©ë²ì ìí´ ë¶ë ¤ ì§ ë를 ëë¹íì¬ ëì± ì ê°êµ¬í´ ëì´ì¼ í©ëë¤ </simpara> <simpara> 리ëë ì ì ì를ë¤ì´ Apacheììë AddHandleê³¼ Action ì§ìì를 ì¬ì©íì¬ ì¤ì í ì ììµëë¤. (ìë를 ë³´ì¸ì.) </simpara> </sect2> <sect2 id="security.cgi.force-redirect"> <title>Case 2: --enable-force-cgi-redirect ìµì ì¬ì©</title> <simpara> ì´ ìµì ì ì£¼ê³ PHP를 ì»´íì¼ íë©´, ì¬ì©ìê° <filename role="php">http://my.host/cgi-bin/php/secretdir/script.php3</filename>ì ê°ì´ PHP를 ì§ì í¸ì¶íë ê²ì ë§ìì¤ë¤. ëì , ì´ ëª¨ëìì PHPë ì¬ì©ìì ì구(request)ê° ì¹ìë²ì 리ëë í¸ ê·ì¹ì íµê³¼í ê²½ì°ìë§ ëì í ê²ì´ë¤. </simpara> <simpara> ì¼ë°ì ì¼ë¡ ìíì¹ìì 리ëë í¸ ì¤ì ì ë¤ìê³¼ ê°ì ì§ìì를 ì¬ì©íì¬ íë¤. : </simpara> <programlisting role="apache-conf"> Action php3-script /cgi-bin/php AddHandler php3-script .php3 </programlisting> <simpara> ì´ ìµì ì ìíì¹ ì¹ìë²ììë§ ê²ì¦ëìë¤. ìíì¹ë 리ëë í¸ë ì구ì ëí´ìë <envar>REDIRECT_STATUS</envar> ë¼ë ë¹ íì¤ CGI íê²½ ë³ì를 ì¤ì íë¤. ë§ì½ ì¹ìë²ê° í´ë¹ 문ì ìêµ¬ê° ì§ì ì ê·¼(direct)ì¸ì§ 리ëë í¸(redirect)ì¸ì§ 구ë³í ì ìë ë°©ë²ì ì ê³µíì§ ìëë¤ë©´ ì¬ë¬ë¶ì ì´ ìµì ì ì¬ì©í ì ìì¼ë¯ë¡ ì´ ë¬¸ìì ìë CGI ë²ì ì ëí ë¤ë¥¸ ë°©ë²ì ì¬ì©í´ì¼ í ê²ì´ë¤. </simpara> </sect2> <sect2 id="security.cgi.doc-root"> <title>Case 3: doc_rootë user_dirì ì¤ì </title> <simpara> ì¹ìë² ë¬¸ì ëë í 리ì scriptë ì¤ííì¼ ê°ì Active content를 í¬í¨íë ê²ì ëëë¡ ë¶ìí ìí©ì ë§ë¤ ì ìë¤. ë§ì½ ì½ê°ì ì¤ì ì¤ë¥ë¡ ì¤í¬ë¦½í¸ê° ì¤íëì§ ìê³ HTML 문ìì²ë¼ ê·¸ëë¡ ì¶ë ¥ ëë¤ë©´, ê°ì¢ ì§ì ì¬ì°ì´ë Passwordê°ì ë³´ììì ì ë³´ê° ëì¤ëë ê²°ê³¼ê° ì´ëë ì§ ëª¨ë¥¸ë¤. ê·¸ë¬ë¯ë¡ ë§ì ìì¤í ê´ë¦¬ìë¤ì ì¤í¬ë¦½í¸ë¥¼ ìí ë³ëì ëë í 리를 ë§ë¤ì´ ëê³ , ì´ ëë í 리ë PHP CGI를 íµí´ìë§ ì ê·¼ì´ ê°ë¥íëë¡ ë§ë¤ì´ ëì ì¤íì ê°ë¥íì§ë§ ë³´ì¬ì§ì§ë ìëë¡ íê³ ìë¤. </simpara> <simpara> ëí, ììì ë§í ë°ì ê°ì´, 문ì ìêµ¬ê° ë¦¬ëë í¸ë ê²ì´ ìëë¼ê³ íì¸í´ì£¼ë ë°©ë²ì´ ìë¤ë©´, ì¹ìë²ì document rootìë ë³ëì script doc_root를 ì¤ì í´ ì£¼ë ê²ì´ ë°ëì íìíë¤. </simpara> <simpara> <link linkend="configuration.file">configuration file (php.ini)</link>ì <link linkend="ini.doc-root">doc_root</link> ì§ìì를 ì¤ì íê±°ë, <envar> PHP_DOCUMENT_ROOT</envar>ë¼ë íê²½ë³ì를 ì¤ì íì¬, PHP script document root를 ì¤ì í ì ìë¤. ë§ì½ ì´ê²ì´ ì¤ì ëì´ ìë¤ë©´, CGI ë²ì ì PHPë ì ê·¼íë ¤ë íì¼ì ì´ë¦ì ì´ <parameter>doc_root</parameter>ì requestì ìë ê²½ë¡ ì 보를 ì´ì©íì¬ ë§ë¤ì´ë¸ë¤. ë°ë¼ì ì¬ë¬ë¶ì ì´ ëë í 리 ë°ììë ì´ë í ì¤í¬ë¦½í¸ë ì¤íë ì ìëë¡ ë§ë¤ê² ëë¤. (ìëì ìë user_dir ë§ì ìì¸ì´ë¤) </simpara> <simpara> ì¬ê¸°ì ì¬ì©í ì ìë ëë¤ë¥¸ ìµì ì <link linkend="ini.user-dir">user_dir</link>ì´ë¤. user_dirì´ ì¤ì ëì´ ìì§ ìì¼ë©´ ì ê·¼ ê°ë¥í íì¼ì <parameter>doc_root</parameter> ë°ì ìë ê² ë¿ì´ë¤. <filename role="url">http://my.host/~user/doc.php3</filename>ê°ì urlë¡ë user home directoryì ìë íì¼ì ì´ ì ìë¤. ë¨ì§ doc_rootìëì <filename role="uri">~user/doc.php3</filename> íì¼ì ë¶ë¥¼ ë¿ì´ë¤. (ë¬¼ë¡ í¸ë°[<literal>~</literal>]ë¡ ììíë "~user"ë¼ë ëë í 리ì´ë¤.) </simpara> <simpara> ì를 ë¤ì´, ë§ì½ user_dirì´ <filename role="dir">public_php</filename>ë¡ ì¤ì ëì´ ìë¤ë©´, <filename role="url">http://my.host/~user/doc.php3</filename>ì ê°ì ì구ë userì home ëë í 리 ë°ì <filename role="dir">public_php</filename>ë¼ë ëë í 리 ë°ì ìë doc.php3ë¼ë íì¼ì í¸ì¶ í ê²ì´ë¤. ë§ì½ ì¬ì©ìì homeì´ <filename role="dir">/home/user</filename>ë¼ë©´, ì¤íëë íì¼ì <filename>/home/user/public_php/doc.php3</filename>ì´ ëë¤. </simpara> <simpara> <parameter>user_dir</parameter> íì¥ì <parameter>doc_root</parameter> ì¤ì ê³¼ ê´ë ¨ìì¼ë¯ë¡, ì¬ë¬ë¶ì document rootì user directory ì ê·¼ íµì 를 ë°ë¡ë°ë¡ í ì ìë¤. </simpara> </sect2> <sect2 id="security.cgi.shell"> <title>Case 4: PHP parser를 ì¹ í¸ë¦¬ ë°ì ë기 (parser outside of web tree)</title> <para> ë§¤ì° ì 뢰í ì ìë ë°©ë²ì¼ë¡ PHP parser binary를 ì¹ í¸ë¦¬ ë°ìë¤ ëë ë°©ë²ì´ë¤. ì를 ë¤ì´ <filename role="dir">/usr/local/bin</filename> ê°ì ê³³ì ëë¤. ì´ ìµì ì ë¨ì ì, 모ë PHP íì í¬í¨íê³ ìë íì¼ì 첫ë²ì§¸ ë¼ì¸ì ë¤ìê³¼ ê°ì´ ì ì´ì£¼ë ê² ë¿ì´ë¤. : <informalexample> <programlisting> #!/usr/local/bin/php </programlisting> </informalexample> ëí íì¼ì ì¤í ê°ë¥ì¼ë¡ ë§ë¤ì´ 주ì´ì¼ íë¤. ì¦, ìì ì ì¤íì ìí´ì <literal>#!</literal> shell-escape ë©ì¹´ëì¦ì ì¬ì©íë Perlì´ë sh í¹ì ë¤ë¥¸ ì¤í¬ë¦½í¸ ì¸ì´ì ê°ì ë°©ìì¼ë¡ ë¤ë£¨ë ê²ì´ë¤. </para> <para> ì´ ì¤ì ìì PHPê° <envar>PATH_INFO</envar>ì <envar>PATH_TRANSLATED</envar>ì 보를 ì íí ë¤ë£¨ë ¤ë©´, php parserê° <link linkend="install.configure.enable-discard-path">--enable-discard-path</link> ì¤ì ìµì ì¼ë¡ ì»´íì¼ ëì´ì¼ íë¤. </para> </sect2> </sect1> <sect1 id="security.apache"> <title>Installed as an Apache module</title> <simpara> When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the "nobody" user). This has several impacts on security and authorization. For example, if you are using PHP to access a database, unless that database has built-in access control, you will have to make the database accessable to the "nobody" user. This means a malicious script could access and modify the databse, even without a username and password. It's entirely possible that a web spider could stumble across a database adminisitror's web page, and drop all of your databases. You can protect against this with Apache authorization, or you can design your own access model using LDAP, .htaccess files, etc. and include that code as part of your PHP scripts. </simpara> <simpara> Often, once security is established to the point where the PHP user (in this case, the apache user) has very little risk, it is discovered that PHP now has been prevented from writing virus files to user directories. Or perhaps it has been prevented from accessing or changing a non-public database. It has equally been secured from writing files that it should, or entering database transactions. </simpara> <simpara> A frequent security mistake made at this point is to allow apache root permissions. </simpara> <simpara> Escalating the Apache user's permissions to root is extremely dangerous and may compromise the entire system, so sudo'ing, chroot'ing ,or otherwise running as root should not be considered by those who are not security professionals. </simpara> </sect1> <sect1 id="security.filesystem"> <title>Filesystem Security</title> <simpara> PHP is subject to the security built into most server systems with respect to permissions on a file and directory basis. This allows you to control which files in the filesystem may be read. Care should be taken with any files which are world readable to ensure that they are safe for reading by all users who have access to that filesystem. </simpara> <simpara> Since PHP was designed to allow user level access to the filesystem, it's entirely possible to write a PHP script that will allow you to read system files such as /etc/password, modify your ethernet connections, send massive printer jobs out, etc. This has some obvious implications, in that you need to ensure that the files that you read from and write to are the appropriate ones. </simpara> <simpara> Consider the following script, where a user indicates that they'd like to delete a file in their home directory. This assumes a situation where a PHP web interface is regularly used for file management, so the Apache user is allowed to delete files in the user home directories. </simpara> <para> <example> <title>Poor variable checking leads to....</title> <programlisting role="php"> <?php // remove a file from the user's home directory $username = $user_submitted_name; $homedir = "/home/$username"; $file_to_delete = "$userfile"; unlink ($homedir/$userfile); echo "$file_to_delete has been deleted!"; ?> </programlisting> </example> Since the username is postable from a user form, they can submit a username and file belonging to someone else, and delete files. In this case, you'd want to use some other form of authentication. Consider what could happen if the variables submitted were "../etc/" and "passwd". The code would then effectively read: <example> <title>... A filesystem attack</title> <programlisting role="php"> <?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> There are two important measures you should take to prevent these issues. <itemizedlist> <listitem> <simpara> Only allow limited permissions to the PHP web user binary. </simpara> </listitem> <listitem> <simpara> Check all variables which are submitted. </simpara> </listitem> </itemizedlist> Here is an improved script: <example> <title>More secure file name checking</title> <programlisting role="php"> <?php // removes a file from the hard drive that // the PHP user has access to. $username = $HTTP_REMOTE_USER; // use 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 = "$HTTP_REMOTE_USER $homedir $file_to_delete"; fputs ($fp, $logstring); fclose($fp); echo "$file_to_delete has been deleted!"; ?> </programlisting> </example> Alternately, you may prefer to write a more customized check: <example> <title>More secure file name checking</title> <programlisting role="php"> <?php $username = $HTTP_REMOTE_USER; $homedir = "/home/$username"; if (!ereg('^[^./][^/]*$', $userfile)) die('bad filename'); //die, do not process //etc... ?> </programlisting> </example> Depending on your operating system, there are a wide variety of files which you should be concerned about, including device entries (/dev/ or COM1), configuration files (/etc/ files and the .ini files), well known file storage areas (/home/, My Documents), etc. For this reason, it's usually easier to create a policy where you forbid everything except for what you explicitly allow. </para> </sect1> <sect1 id="security.errors"> <title>Error Reporting</title> <simpara> 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. </simpara> <simpara> 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. </simpara> <simpara> For example, the very style of 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. </simpara> <simpara> 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. </simpara> <simpara> 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. </simpara> <simpara> 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. </simpara> </sect1> <sect1 id="security.variables"> <title>User Submitted Data</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>Dangerous Variable Usage</title> <programlisting role="php"> <?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 not? 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.general"> <title>General considerations</title> <simpara> A completely secure system is a virtual impossibility, so an approach often used in the security profession is one of balancing risk and usability. If every variable submitted by a user required two forms of biometric validation (such as a retinal scan and a fingerprint), you would have an extremely high level of accountability. It would also take half an hour to fill out a fairly complex form, which would tend to encourage users to find ways of bypassing the security. </simpara> <simpara> The best security is often inobtrusive enough to suit the requirements without the user being prevented from accomplishing their work, or over-burdening the code author with excessive complexity. Indeed, some security attacks are merely exploits of this kind of overly built security, which tends to erode over time. </simpara> <simpara> A phrase worth remembering: A system is only as good as the weakest link in a chain. If all transactions are heavily logged based on time, location, transaction type, etc. but the user is only verified based on a single cookie, the validity of tying the users to the transaction log is severely weakened. </simpara> <simpara> When testing, keep in mind that you will not be able to test all possibilities for even the simplest of pages. The input you may expect will be completely unrelated to the input given by a disgruntled employee, a cracker with months of time on their hands, or a housecat walking across the keyboard. This is why it's best to look at the code from a logical perspective, to discern where unexpected data can be introduced, and then follow how it is modified, reduced, or amplified. </simpara> <simpara> The Internet is filled with people trying to make a name for themselves by breaking your code, crashing your site, posting inappropriate content, and otherwise making your day interesting. It doesn't matter if you have a small or large site, you are a target by simply being online, by having a server that can be connected to. Many cracking programs do not discern by size, they simply trawl massive IP blocks looking for victims. Try not to become one. </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 sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->
Index: phpdoc/kr/chapters/security.xml +++ phpdoc/kr/chapters/security.xml <chapter id="security"> <title>Security</title> <simpara> PHPë ë§¤ì° ê°ë ¥í ì¸ì´ì´ë¤. ì´ ì¸í°í리í°ë ì¹ìë²ì 모ëë¡ ëì´ ìê±´ <acronym>CGI</acronym> binaryë¡ ëì´ ìê±´, ìì¤í ë´ì íì¼ì ì¬ì©í ì ìê³ , ì¬íì ëª ë ¹ì´ë íë¡ê·¸ë¨ì ì¤íí ì ììë¿ ìëë¼, ë¤í¸ìì íµí´ ë¤ë¥¸ ìë²ë¡ ì°ê²°ì í ìë ìë¤. ì´ë° 기ë¥ë¤ì ì ì를 ê°ì§ ì¬ì©ìë ì´ë³´ìê° ì´ë¤ íë¡ê·¸ë¨ì´ë ì¤íìì¼ ì¹ìë²ë¥¼ ë³´ìì ë¶ìí ìíë¡ ë§ë¤ ì ìë¤. PHPë ë¶ëª í Perlì´ë Cë³´ë¤ ì¢ë ìì í CGI íë¡ê·¸ë¨ì ë§ë¤ ì ìëë¡ ëì´ ìë¤. ì¬ë¬ì´ ì»´íì¼ì´ë ì¤íì ì¤ì ìµì ë¤ì ì ì í ì¡°í©íì¬ ì¬ì©íë©´, ìíë ì ëì ìì ë¡ìê³¼ ë³´ììì ì ì½ì ì¡°íìí¬ ì ìë¤. </simpara> <simpara> PHPë ìë§ì ë°©ë²ì¼ë¡ ì´ì©ì´ ê°ë¥íê³ , ì´ì ë°ë¥¸ ì¬ë¬ ê°ì§ ì¤ì ìµì ì´ ìì ì ìë¤. ë§ì ìµì ì í ì¬íë¤ì PHP를 ë³´ë¤ ë§ì 목ì ì¼ë¡ ì¬ì©í ì ìëë¡ íì§ë§, ì´ ìµì ë¤ì ì¡°í©ê³¼ ìë²ì ì¤ì ì ë°ë¼ ë³´ììì íì ì ë ¸ì¶ìí¬ ìë ìë¤. </simpara> <simpara> The configuration flexibility of PHP is equally rivalled by the code flexibility. PHP can be used to build complete server applications, with all the power of a shell user, or it can be used for simple server-side includes with little risk in a tightly controlled environment. How you build that environment, and how secure it is, is largely up to the PHP developer. </simpara> <simpara> This chapter starts by explaining the different configuration option combinations and the situations they can be safely used. It then describes different considerations in coding for different levels of security, and ends with some general security advice. </simpara> <sect1 id="security.cgi"> <title>Installed as CGI binary</title> <sect2 id="security.cgi.attacks"> <title>ê°ë¥í 공격 (Possible attacks)</title> <simpara> <acronym>CGI</acronym> binary ííì PHPë ì´ë¤ ì´ì ë¡ (ìíì¹ ê°ì) ìë² ìíí¸ì¨ì´ì 모ëë¡ ì¬ì©ë기를 ìíì§ ìë ê²½ì°ì ì¬ì©í ì ìê³ , PHP를 ë¤ë¥¸ ì¢ ë¥ì CGI wrapperì í¨ê» ìì í chrootì setuid íê²½ì ë§ë¤ì´ ë´ë ê³³ì ëì ì¬ì©í ì ìë¤. ì´ ì¤ì ì ë³´íµ ì¤íí PHP binary를 ì¹ìë²ì cgi-binëë í 리ì ì¤ì¹í´ì¼ íë¤. CERT ê¶ê³ ì¬í <ulink url="&url.cert;">CA-96.11</ulink>ì ë°ë¥´ë©´ 모ë ì¸í°í리í°ë¤ì cgi-binì ì¤ì¹íëë¡ ê¶ê³ ëë¤. ë¹ë¡ PHP binaryê° ë 립ì ì¸ ì¸í°í리í°ë¡ ì¬ì©ëë¤ í ì§ë¼ë, PHPë ì´ë° ì¤ì ìì ë°ì ì ìë 공격ì ë¤ìê³¼ ê°ì´ ë§ìë´ëë¡ ì¤ê³ëì´ ìë¤. : </simpara> <itemizedlist> <listitem> <simpara> ìì¤í íì¼ë¡ì ì ê·¼ : <filename role="url">http://my.host/cgi-bin/php?/etc/passwd</filename> </simpara> <simpara> ì´ urlë´ì 물ìí(?)ì´íì ìë ìì²ì CGI ì¸í°íì´ì¤ì ìí´ command line argumentë¡ ì¸í°í리í°ìê² ì ë¬ëë¤. ë³´íµ ì¸í°í리í°ë 커맨ë ë¼ì¸ì 첫 ë²ì§¸ argumentì íì¼ì ì½ì´ì ì¤í íë¤. </simpara> <simpara> CGI binaryë¡ ì¤í ë PHPë 모ë command line argumentë¤ì 모ë 무ìíë¤. </simpara> </listitem> <listitem> <simpara> ìë²ì ì¹ ë¬¸ìë¡ì ì ê·¼ : <filename role="url">http://my.host/cgi-bin/php/secret/doc.html</filename> </simpara> <simpara> ì´ urlì PHP binary ì´íì path ì ë³´(<filename role="uri">/secret/doc.html</filename>)ë ì¼ë°ì ì¼ë¡ <acronym>CGI</acronym> íë¡ê·¸ë¨ì ìí´ ì½ê³ í´ìë íì¼ ì´ë¦ì¼ë¡ ì¬ì©ëë¤. ë³´íµ ì¹ìë²ë ì¤ì ì§ìì(Apacheì ê²½ì° Action)를 ì¬ì©íì¬ <filename role="url">http://my.host/secret/script.php3</filename>ê°ì 문ì ìì²ì PHP ì¸í°í리í°ë¡ ë°ë¡ 리ëë í¸ íëë¡ í ì ìë¤. ì´ë ê² ì¤ì íë©´, ì¹ìë²ë ì°ì <filename role="uri">/secret</filename> ëë í 리ì ì ê·¼ ê¶íì ê²ì¬í íì <filename role="url">http://my.host/cgi-bin/php/secret/script.php3</filename>ë¡ ë¦¬ëë í¸ë¥¼ íë¤. ë¶ííë, ì´ ìì²ì´ ìëì formìì 주ì´ì§ ê²ì´ë¼ë©´, ì¹ìë²ë <filename role="uri">/secret/script.php3</filename> íì¼ì ëí ì ê·¼ ê¶í ê²ì¬ë¥¼ íì§ ìê³ , <filename role="uri">/cgi-bin/php</filename> íì¼ì ëí ê²ì¬ë§ì ííë¤. ì´ ë°©ë²ì ì¬ì©íë©´ <filename role="uri">/cgi-bin/php</filename>ì ì ê·¼ ê°ë¥í ì¬ì©ìë ì¹ìë²ìì 모ë ë³´í¸ë 문ìë¤ë ì ê·¼í ì ìë¤. </simpara> <simpara> ì´ë°ê²½ì° ìë² ë¬¸ì treeì ì ê·¼ ì ì½ì´ ìë ëë í ë¦¬ê° ìë¤ë©´, ì»´íì¼ìì <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.default"> <title>Case 1: ê³µì© íì¼ë§ ì ê³µíë ê²½ì° (only public files served)</title> <simpara> ë§ì½ ì¬ë¬ë¶ì ìë²ê° password ë ip 기ì¤ì ì ê·¼ íµì ì ê´í ì´ë í ì¬íë ìë¤ë©´, ì´ë¬í ì¤ì ìµì ë¤ì íì ìë¤. ë§ì½ ì¬ë¬ë¶ì ì¹ìë²ê° 리ëë í¸ë¥¼ íì©íì§ ìê±°ë, ìë²ê° í´ë¹ ìêµ¬ê° ìì íê² ë¦¬ëë í¸ë ì구ì¸ì§ ëí ì 보를 PHP binaryì ì ë¬í ì ìë ë°©ë²ì´ ìì ê²½ì°, config ì¤í¬ë¦½í¸ìì <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.php3</filename>ê°ì ì§ì ì ê·¼ ë°©ë²ì´ë <filename role="php">http://my.host/dir/script.php3</filename>ê°ì 리ëë ì ì ë°©ë² ì´ì¸ì, ë¤ë¥¸ ë¯¿ì§ ëª»í ë°©ë²ì ìí´ ë¶ë ¤ ì§ ë를 ëë¹íì¬ ëì± ì ê°êµ¬í´ ëì´ì¼ í©ëë¤ </simpara> <simpara> 리ëë ì ì ì를ë¤ì´ Apacheììë AddHandleê³¼ Action ì§ìì를 ì¬ì©íì¬ ì¤ì í ì ììµëë¤. (ìë를 ë³´ì¸ì.) </simpara> </sect2> <sect2 id="security.cgi.force-redirect"> <title>Case 2: --enable-force-cgi-redirect ìµì ì¬ì©</title> <simpara> ì´ ìµì ì ì£¼ê³ PHP를 ì»´íì¼ íë©´, ì¬ì©ìê° <filename role="php">http://my.host/cgi-bin/php/secretdir/script.php3</filename>ì ê°ì´ PHP를 ì§ì í¸ì¶íë ê²ì ë§ìì¤ë¤. ëì , ì´ ëª¨ëìì PHPë ì¬ì©ìì ì구(request)ê° ì¹ìë²ì 리ëë í¸ ê·ì¹ì íµê³¼í ê²½ì°ìë§ ëì í ê²ì´ë¤. </simpara> <simpara> ì¼ë°ì ì¼ë¡ ìíì¹ìì 리ëë í¸ ì¤ì ì ë¤ìê³¼ ê°ì ì§ìì를 ì¬ì©íì¬ íë¤. : </simpara> <programlisting role="apache-conf"> Action php3-script /cgi-bin/php AddHandler php3-script .php3 </programlisting> <simpara> ì´ ìµì ì ìíì¹ ì¹ìë²ììë§ ê²ì¦ëìë¤. ìíì¹ë 리ëë í¸ë ì구ì ëí´ìë <envar>REDIRECT_STATUS</envar> ë¼ë ë¹ íì¤ CGI íê²½ ë³ì를 ì¤ì íë¤. ë§ì½ ì¹ìë²ê° í´ë¹ 문ì ìêµ¬ê° ì§ì ì ê·¼(direct)ì¸ì§ 리ëë í¸(redirect)ì¸ì§ 구ë³í ì ìë ë°©ë²ì ì ê³µíì§ ìëë¤ë©´ ì¬ë¬ë¶ì ì´ ìµì ì ì¬ì©í ì ìì¼ë¯ë¡ ì´ ë¬¸ìì ìë CGI ë²ì ì ëí ë¤ë¥¸ ë°©ë²ì ì¬ì©í´ì¼ í ê²ì´ë¤. </simpara> </sect2> <sect2 id="security.cgi.doc-root"> <title>Case 3: doc_rootë user_dirì ì¤ì </title> <simpara> ì¹ìë² ë¬¸ì ëë í 리ì scriptë ì¤ííì¼ ê°ì Active content를 í¬í¨íë ê²ì ëëë¡ ë¶ìí ìí©ì ë§ë¤ ì ìë¤. ë§ì½ ì½ê°ì ì¤ì ì¤ë¥ë¡ ì¤í¬ë¦½í¸ê° ì¤íëì§ ìê³ HTML 문ìì²ë¼ ê·¸ëë¡ ì¶ë ¥ ëë¤ë©´, ê°ì¢ ì§ì ì¬ì°ì´ë Passwordê°ì ë³´ììì ì ë³´ê° ëì¤ëë ê²°ê³¼ê° ì´ëë ì§ ëª¨ë¥¸ë¤. ê·¸ë¬ë¯ë¡ ë§ì ìì¤í ê´ë¦¬ìë¤ì ì¤í¬ë¦½í¸ë¥¼ ìí ë³ëì ëë í 리를 ë§ë¤ì´ ëê³ , ì´ ëë í 리ë PHP CGI를 íµí´ìë§ ì ê·¼ì´ ê°ë¥íëë¡ ë§ë¤ì´ ëì ì¤íì ê°ë¥íì§ë§ ë³´ì¬ì§ì§ë ìëë¡ íê³ ìë¤. </simpara> <simpara> ëí, ììì ë§í ë°ì ê°ì´, 문ì ìêµ¬ê° ë¦¬ëë í¸ë ê²ì´ ìëë¼ê³ íì¸í´ì£¼ë ë°©ë²ì´ ìë¤ë©´, ì¹ìë²ì document rootìë ë³ëì script doc_root를 ì¤ì í´ ì£¼ë ê²ì´ ë°ëì íìíë¤. </simpara> <simpara> <link linkend="configuration.file">configuration file (php.ini)</link>ì <link linkend="ini.doc-root">doc_root</link> ì§ìì를 ì¤ì íê±°ë, <envar> PHP_DOCUMENT_ROOT</envar>ë¼ë íê²½ë³ì를 ì¤ì íì¬, PHP script document root를 ì¤ì í ì ìë¤. ë§ì½ ì´ê²ì´ ì¤ì ëì´ ìë¤ë©´, CGI ë²ì ì PHPë ì ê·¼íë ¤ë íì¼ì ì´ë¦ì ì´ <parameter>doc_root</parameter>ì requestì ìë ê²½ë¡ ì 보를 ì´ì©íì¬ ë§ë¤ì´ë¸ë¤. ë°ë¼ì ì¬ë¬ë¶ì ì´ ëë í 리 ë°ììë ì´ë í ì¤í¬ë¦½í¸ë ì¤íë ì ìëë¡ ë§ë¤ê² ëë¤. (ìëì ìë user_dir ë§ì ìì¸ì´ë¤) </simpara> <simpara> ì¬ê¸°ì ì¬ì©í ì ìë ëë¤ë¥¸ ìµì ì <link linkend="ini.user-dir">user_dir</link>ì´ë¤. user_dirì´ ì¤ì ëì´ ìì§ ìì¼ë©´ ì ê·¼ ê°ë¥í íì¼ì <parameter>doc_root</parameter> ë°ì ìë ê² ë¿ì´ë¤. <filename role="url">http://my.host/~user/doc.php3</filename>ê°ì urlë¡ë user home directoryì ìë íì¼ì ì´ ì ìë¤. ë¨ì§ doc_rootìëì <filename role="uri">~user/doc.php3</filename> íì¼ì ë¶ë¥¼ ë¿ì´ë¤. (ë¬¼ë¡ í¸ë°[<literal>~</literal>]ë¡ ììíë "~user"ë¼ë ëë í 리ì´ë¤.) </simpara> <simpara> ì를 ë¤ì´, ë§ì½ user_dirì´ <filename role="dir">public_php</filename>ë¡ ì¤ì ëì´ ìë¤ë©´, <filename role="url">http://my.host/~user/doc.php3</filename>ì ê°ì ì구ë userì home ëë í 리 ë°ì <filename role="dir">public_php</filename>ë¼ë ëë í 리 ë°ì ìë doc.php3ë¼ë íì¼ì í¸ì¶ í ê²ì´ë¤. ë§ì½ ì¬ì©ìì homeì´ <filename role="dir">/home/user</filename>ë¼ë©´, ì¤íëë íì¼ì <filename>/home/user/public_php/doc.php3</filename>ì´ ëë¤. </simpara> <simpara> <parameter>user_dir</parameter> íì¥ì <parameter>doc_root</parameter> ì¤ì ê³¼ ê´ë ¨ìì¼ë¯ë¡, ì¬ë¬ë¶ì document rootì user directory ì ê·¼ íµì 를 ë°ë¡ë°ë¡ í ì ìë¤. </simpara> </sect2> <sect2 id="security.cgi.shell"> <title>Case 4: PHP parser를 ì¹ í¸ë¦¬ ë°ì ë기 (parser outside of web tree)</title> <para> ë§¤ì° ì 뢰í ì ìë ë°©ë²ì¼ë¡ PHP parser binary를 ì¹ í¸ë¦¬ ë°ìë¤ ëë ë°©ë²ì´ë¤. ì를 ë¤ì´ <filename role="dir">/usr/local/bin</filename> ê°ì ê³³ì ëë¤. ì´ ìµì ì ë¨ì ì, 모ë PHP íì í¬í¨íê³ ìë íì¼ì 첫ë²ì§¸ ë¼ì¸ì ë¤ìê³¼ ê°ì´ ì ì´ì£¼ë ê² ë¿ì´ë¤. : <informalexample> <programlisting> #!/usr/local/bin/php </programlisting> </informalexample> ëí íì¼ì ì¤í ê°ë¥ì¼ë¡ ë§ë¤ì´ 주ì´ì¼ íë¤. ì¦, ìì ì ì¤íì ìí´ì <literal>#!</literal> shell-escape ë©ì¹´ëì¦ì ì¬ì©íë Perlì´ë sh í¹ì ë¤ë¥¸ ì¤í¬ë¦½í¸ ì¸ì´ì ê°ì ë°©ìì¼ë¡ ë¤ë£¨ë ê²ì´ë¤. </para> <para> ì´ ì¤ì ìì PHPê° <envar>PATH_INFO</envar>ì <envar>PATH_TRANSLATED</envar>ì 보를 ì íí ë¤ë£¨ë ¤ë©´, php parserê° <link linkend="install.configure.enable-discard-path">--enable-discard-path</link> ì¤ì ìµì ì¼ë¡ ì»´íì¼ ëì´ì¼ íë¤. </para> </sect2> </sect1> <sect1 id="security.apache"> <title>Installed as an Apache module</title> <simpara> When PHP is used as an Apache module it inherits Apache's user permissions (typically those of the "nobody" user). This has several impacts on security and authorization. For example, if you are using PHP to access a database, unless that database has built-in access control, you will have to make the database accessable to the "nobody" user. This means a malicious script could access and modify the databse, even without a username and password. It's entirely possible that a web spider could stumble across a database adminisitror's web page, and drop all of your databases. You can protect against this with Apache authorization, or you can design your own access model using LDAP, .htaccess files, etc. and include that code as part of your PHP scripts. </simpara> <simpara> Often, once security is established to the point where the PHP user (in this case, the apache user) has very little risk, it is discovered that PHP now has been prevented from writing virus files to user directories. Or perhaps it has been prevented from accessing or changing a non-public database. It has equally been secured from writing files that it should, or entering database transactions. </simpara> <simpara> A frequent security mistake made at this point is to allow apache root permissions. </simpara> <simpara> Escalating the Apache user's permissions to root is extremely dangerous and may compromise the entire system, so sudo'ing, chroot'ing ,or otherwise running as root should not be considered by those who are not security professionals. </simpara> </sect1> <sect1 id="security.filesystem"> <title>Filesystem Security</title> <simpara> PHP is subject to the security built into most server systems with respect to permissions on a file and directory basis. This allows you to control which files in the filesystem may be read. Care should be taken with any files which are world readable to ensure that they are safe for reading by all users who have access to that filesystem. </simpara> <simpara> Since PHP was designed to allow user level access to the filesystem, it's entirely possible to write a PHP script that will allow you to read system files such as /etc/password, modify your ethernet connections, send massive printer jobs out, etc. This has some obvious implications, in that you need to ensure that the files that you read from and write to are the appropriate ones. </simpara> <simpara> Consider the following script, where a user indicates that they'd like to delete a file in their home directory. This assumes a situation where a PHP web interface is regularly used for file management, so the Apache user is allowed to delete files in the user home directories. </simpara> <para> <example> <title>Poor variable checking leads to....</title> <programlisting role="php"> <?php // remove a file from the user's home directory $username = $user_submitted_name; $homedir = "/home/$username"; $file_to_delete = "$userfile"; unlink ($homedir/$userfile); echo "$file_to_delete has been deleted!"; ?> </programlisting> </example> Since the username is postable from a user form, they can submit a username and file belonging to someone else, and delete files. In this case, you'd want to use some other form of authentication. Consider what could happen if the variables submitted were "../etc/" and "passwd". The code would then effectively read: <example> <title>... A filesystem attack</title> <programlisting role="php"> <?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> There are two important measures you should take to prevent these issues. <itemizedlist> <listitem> <simpara> Only allow limited permissions to the PHP web user binary. </simpara> </listitem> <listitem> <simpara> Check all variables which are submitted. </simpara> </listitem> </itemizedlist> Here is an improved script: <example> <title>More secure file name checking</title> <programlisting role="php"> <?php // removes a file from the hard drive that // the PHP user has access to. $username = $HTTP_REMOTE_USER; // use 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 = "$HTTP_REMOTE_USER $homedir $file_to_delete"; fputs ($fp, $logstring); fclose($fp); echo "$file_to_delete has been deleted!"; ?> </programlisting> </example> Alternately, you may prefer to write a more customized check: <example> <title>More secure file name checking</title> <programlisting role="php"> <?php $username = $HTTP_REMOTE_USER; $homedir = "/home/$username"; if (!ereg('^[^./][^/]*$', $userfile)) die('bad filename'); //die, do not process //etc... ?> </programlisting> </example> Depending on your operating system, there are a wide variety of files which you should be concerned about, including device entries (/dev/ or COM1), configuration files (/etc/ files and the .ini files), well known file storage areas (/home/, My Documents), etc. For this reason, it's usually easier to create a policy where you forbid everything except for what you explicitly allow. </para> </sect1> <sect1 id="security.errors"> <title>Error Reporting</title> <simpara> 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. </simpara> <simpara> 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. </simpara> <simpara> For example, the very style of 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. </simpara> <simpara> 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. </simpara> <simpara> 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. </simpara> <simpara> 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. </simpara> </sect1> <sect1 id="security.variables"> <title>User Submitted Data</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>Dangerous Variable Usage</title> <programlisting role="php"> <?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 not? 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.general"> <title>General considerations</title> <simpara> A completely secure system is a virtual impossibility, so an approach often used in the security profession is one of balancing risk and usability. If every variable submitted by a user required two forms of biometric validation (such as a retinal scan and a fingerprint), you would have an extremely high level of accountability. It would also take half an hour to fill out a fairly complex form, which would tend to encourage users to find ways of bypassing the security. </simpara> <simpara> The best security is often inobtrusive enough to suit the requirements without the user being prevented from accomplishing their work, or over-burdening the code author with excessive complexity. Indeed, some security attacks are merely exploits of this kind of overly built security, which tends to erode over time. </simpara> <simpara> A phrase worth remembering: A system is only as good as the weakest link in a chain. If all transactions are heavily logged based on time, location, transaction type, etc. but the user is only verified based on a single cookie, the validity of tying the users to the transaction log is severely weakened. </simpara> <simpara> When testing, keep in mind that you will not be able to test all possibilities for even the simplest of pages. The input you may expect will be completely unrelated to the input given by a disgruntled employee, a cracker with months of time on their hands, or a housecat walking across the keyboard. This is why it's best to look at the code from a logical perspective, to discern where unexpected data can be introduced, and then follow how it is modified, reduced, or amplified. </simpara> <simpara> The Internet is filled with people trying to make a name for themselves by breaking your code, crashing your site, posting inappropriate content, and otherwise making your day interesting. It doesn't matter if you have a small or large site, you are a target by simply being online, by having a server that can be connected to. Many cracking programs do not discern by size, they simply trawl massive IP blocks looking for victims. Try not to become one. </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 sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->