【PHP代码审计】Null字符问题

PHP基于C语言编写,所以可能对于某些字符的处理,在实际运行中可能会有意想不到的结果

漏洞代码:

<?php
$file  =  $_GET [ 'file' ];  // "../../etc/passwd\0"
if ( file_exists ( '/home/wwwrun/' . $file . '.php' )) {
     // file_exists will return true as the file /home/wwwrun/../../etc/passwd exists
     include  '/home/wwwrun/' . $file . '.php' ;
     // the file /etc/passwd will be included
}
?>

防御方法:


<?php
$file  =  $_GET [ 'file' ]; 

// 对字符串进行白名单检查
switch ( $file ) {
    case  'main' :
    case  'foo' :
    case  'bar' :
        include  '/home/wwwrun/include/' . $file . '.php' ;
        break;
    default:
        include  '/home/wwwrun/include/main.php' ;
}
?>

版权归原作者所有.仅供学习交流使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容