主页
软件技术
返回
页面显示PHP源码

        函数1:show_source("filename");

        函数2:highlight_file ("filename");

        <?php

        $file = $_GET['f'];

        $file = $file ? $file : __FILE__; highlight_file($file);

        ?>

        函数3:highlight_string("string");

        <?php

        highlight_string('<?php phpinfo(); ?>'); ?>

        执行后,在浏览器中输出:

        <code><font color="#000000">

        <font color="#0000BB">&lt;?php phpinfo</font><font color="#007700">(); </font><font

        color="#0000BB">?&gt;</font>

        </font>

        </code>

        但是这样执行之后,如果程序员不仔细检查,会有很多的漏洞,给一些别有用心的人以可成之机~ 下面来看看具体的解决办法:

        代码大致功能就是通过URL中提交一个变量名为f的变量接受需要高亮显示代码的php文件路径,然后程序在调用highlight_file函数来显示该文件。然后把该文件保存到我的个人主目录的public_html目录下,文件名为test.php。

        现在我们使用firefox打开:~heiyeluren/test.php,返回的是我们

        test.php的文件内容,现在我们存取passwd文件看看:

        ~heiyeluren/test.php?f=/etc/passwd

        马上页面里就显示了很多用户信息:

        root:x:0:0:root:/root:/bin/bash

        bin:x:1:1:bin:/bin:/sbin/nologin

        daemon:x:2:2:daemon:/sbin:/sbin/nologin

        ......

        我们再查看php的配置文件php.ini的信息:

        ~heiyeluren/test.php?f=/usr/local/php/lib/php.ini

        马上显示了:

        [PHP]

        ;;;;;;;;;;;

        ; WARNING ;

        ;;;;;;;;;;;

        ; This is the default settings file for new PHP installations. ; By default, PHP installs itself with a configuration suitable for ; development purposes, and *NOT* for production purposes. ; For several security-oriented considerations that should be taken ; before going online with your site, please consult php.ini-recommended ; and

        ......

        看来只要权限允许,很多文件都能够直接读取,对我们系统构成了巨大威胁。换句话说,如果把上面代码插入到当前web系统中其他任意一个能够直接访问的php文件中,通过特殊的变脸进行激活,而且一般程序员是不会那么仔细的检查每个PHP程序文件,那么恶意用户就能够随时随地查看我们的系统文件。

        [ 系统防范 ]

        那么,如果防止,或者说拒绝类似的问题呢,因为,同样的,php的文件存取函数非常多,比如file、file_get_contents、readfile等函数,我们如何防止这些函数带来的威胁呢,

        解决方法一:

        如果系统中只是跟数据库进行交互,那么完全可以屏蔽掉这些文件存取函数,象fopen、file、file_get_contents、readfile、opendir等函数,方法就是在php的配置文件php.ini中进行禁止,php.ini中有一个选项叫做disable_functions,我们可以把需要屏蔽的函数放到里面: disable_functions = highlight_file,fopen,file,file_get_contents,readfile,opendir 那么上面那些函数就无法使用了,比如你调用了highlight_file函数,那么php引擎会提示你: Warning: highlight_file() has been disabled for security reasons in

        /home/heiyeluren/public_html/test.php on line 5

        当然,我们不能一概而杀,只是说你可以禁止掉那些基本不怎么使用的函数,比如highlight_file我觉得就使用的比较少。

        解决方法二:

        第一种方法太强制性了,函数禁止后将无法访问该函数,总是不是那么的适合,对于一些空间提供商来讲,是不合理的,那么还有一个解决方法,还是配置我们的php.ini,打开php的安全模式:

        safe_mode = On

        当然,如果你需要,最好再配置一下open_basedir之类的选项来更好的控制,具体可参考php手册。

        当我们打开了php的安全模式后,我们再来访问一下/etc/passwd,提交URL: ~heiyeluren/test.php?f=/etc/passwd

        那么浏览器中马上就显示:

        Warning: highlight_file() [function.highlight-file]: SAFE MODE Restriction in effect.

        The script whose uid is 500 is not allowed to access /etc/passwd owned by uid 0 in

        /home/heiyeluren/public_html/test.php on line 5

        解决方法三:

        在不损失我们PHP强大功能前提下,那些函数都能够正常使用,那防范方法就是设置我们系统中各个目录的各种不同访问权限,比如我们的/etc/passwd文件。比如默认是这个权限: [~]# ls -al /usr/local/php/lib/php.ini -rw-r--r-- 1 root root 41489 5 12:40 /usr/local/php/lib/php.ini

        我们设置设置一下不是随便能够读的:

        [~]# chmod 640 /usr/local/php/lib/php.ini

        [~]# ls -al /usr/local/php/lib/php.ini

        -rw-r----- 1 root root 1865 Nov 27 01:16 /usr/local/php/lib/php.ini

        现在,我们在访问

        ~heiyeluren/test.php?f=/usr/local/php/lib/php.ini 看看:

        Warning: highlight_file(/usr/local/php/lib/php.ini) [function.highlight-file]: failed to open stream: Permission denied in /home/heiyeluren/public_html/test.php on line 5

        Warning: highlight_file() [function.highlight-file]: Failed opening '/usr/local/php/lib/php.ini' for highlighting in

        /home/heiyeluren/public_html/test.php on line 5

        警告说没有权限读取/usr/local/php/lib/php.ini这个文件,顺利达到我们的目的。

        


加强基础设施建设时期的工程项目管理
牛长春:历史文化建筑的保护规划关乎一座城市的未来
09年一级建造师考试《法规及相关知识》辅导资料(133)
赵彬:如何在雾霾天保护自己和家人
工程项目管理的三控制
房地产项目的规划设计管理(1)
2015年一级建造师《专业工程管理与实务》复习资料(58)
浅谈如何有效的进行工程造价的控制
信息发布:名易软件http://www.myidp.net