【问题标题】:Undocumented sanitization in PHP file uploadPHP文件上传中未记录的清理
【发布时间】:2018-08-20 11:57:59
【问题描述】:

在处理文件上传时,根据PHP official documentation,文件名应针对目录遍历和可能的其他类型的攻击进行清理:

// basename() may prevent filesystem traversal attacks;
// further validation/sanitation of the filename may be appropriate
$name = basename($_FILES["pictures"]["name"][$key]);

尽管如此,我发现默认情况下,文件名在到达 PHP 脚本时已经过清理。

我有证据表明 Apache 收到了恶意文件名:filename="../file.png",而 PHP 脚本改为在 $_FILES 变量中读取经过清理的名称。

Apache 输入的低级转储:

mod_dumpio: dumpio_in (data-HEAP):
--------------------------eb8b65b665870e02
Content-Disposition: form-data;
name="attachment";
filename="../file.png" ← [Malicious file name]
Content-Type: image/png

PHP 脚本

echo $_FILES['attachment']['name']; ← [File name already sanitised: 'file.png']

我在 Apache 模块和 php-fpm 中都发现了这种行为,运行 PHP 从 5.5 到 7.2,我必须推断 PHP 解释器在将变量传递给脚本之前执行了这种清理。

所以,感谢 PHP 在我不知情和不同意的情况下为我做卫生。但是(这是我的问题),因为据我所知,此功能未记录在案,我想知道清理标准/正则表达式/算法,以确保它满足我的需求。 p>

【问题讨论】:

  • nginx、lighthttp、iis 和所有其他 Web 服务器是否都这样做?为了使您的应用程序安全且可移植,您不应指望 Web 服务器来清理任何内容。
  • 感谢@MagnusEriksson 的提示,但是 1. 如上所述,我怀疑它是由 PHP 解释器制作的,并且 2. 仍然需要知道我对内置输出的期望算法。例如,它可以(理论上)切断一些我想以不同方式处理的输入。如果不了解所应用的算法,我不知道。

标签: php security file-upload sanitization input-sanitization


【解决方案1】:

你要看看rfc1867.c,这似乎是你指的部分:

SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)

从评论看来,basename() 似乎用于消除虚假的反斜杠,这实际上可能是正确的(我想也许是“Hello\ World.txt” ?)。 但这是基于 IE 的行为,并且评论指出它可能会在未来被删除。

所以你不能依靠这种“净化”来继续存在。

...

    /* The \ check should technically be needed for win32 systems only where
     * it is a valid path separator. However, IE in all it's wisdom always sends
     * the full path of the file on the user's filesystem, which means that unless
     * the user does basename() they get a bogus file name. Until IE's user base drops
     * to nill or problem is fixed this code must remain enabled for all systems. */

    s = _basename(internal_encoding, filename TSRMLS_CC);
    if (!s) {
        s = filename;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2019-09-05
    相关资源
    最近更新 更多