「Web安全」php://filter 的浅略底层分析

新建一个 php 文件:a.php
<?php
$a = "a.txt";
include("php://filter/resource=" . $a);
在同一目录下新建一个文件:a.txt(内容为 <?php phpinfo();?> 的 base64 编码)
PD9waHAgcGhwaW5mbygpOz8+
在对应文件的相关函数下个断点:

【资料】
贴一下关键代码:
php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
{
...
if (!strncasecmp(path, "php://", 6)) {
}
if (!strncasecmp(path, "temp", 4)) {
}
if (!strcasecmp(path, "memory")) {
}
if (!strcasecmp(path, "output")) {
}
if (!strcasecmp(path, "input")) {
}
if (!strcasecmp(path, "stdin")) {
} else if (!strcasecmp(path, "stdout")) {
} else if (!strcasecmp(path, "stderr")) {
} else if (!strncasecmp(path, "fd/", 3)) {
} else if (!strncasecmp(path, "filter/", 7)) {
/* Save time/memory when chain isn't specified */
if (strchr(mode, 'r') || strchr(mode, '+')) {
mode_rw |= PHP_STREAM_FILTER_READ;
}
if (strchr(mode, 'w') || strchr(mode, '+') || strchr(mode, 'a')) {
mode_rw |= PHP_STREAM_FILTER_WRITE;
}
pathdup = estrndup(path + 6, strlen(path + 6));
p = strstr(pathdup, "/resource=");
if (!p) {
zend_throw_error(NULL, "No URL resource specified");
efree(pathdup);
return NULL;
}
if (!(stream = php_stream_open_wrapper(p + 10, mode, options, opened_path))) {
efree(pathdup);
return NULL;
}
*p = '\0';
p = php_strtok_r(pathdup + 1, "/", &token);
while (p) {
if (!strncasecmp(p, "read=", 5)) {
php_stream_apply_filter_list(stream, p + 5, 1, 0);
} else if (!strncasecmp(p, "write=", 6)) {
php_stream_apply_filter_list(stream, p + 6, 0, 1);
} else {
php_stream_apply_filter_list(stream, p, mode_rw & PHP_STREAM_FILTER_READ, mode_rw & PHP_STREAM_FILTER_WRITE);
}
p = php_strtok_r(NULL, "/", &token);
}
efree(pathdup);
if (EG(exception)) {
php_stream_close(stream);
return NULL;
}
return stream;
} else {
/* invalid php://thingy */
php_error_docref(NULL, E_WARNING, "Invalid php:// URL specified");
return NULL;
}
...
return stream;
}
我们先来测试一下它的逻辑吧,我们设置变量 a 为 a/../a.txt,那么就相当于我们 include 了一个 php://filter/resource=a/../a.txt,在 filter 的判断断个点,逐步调过去:

首先是判断读或写,这里不用管,继续往下看。

这里有两个字符串的操作,并分别赋值给了 pathdup 和 p,对了,path 最开始是我们转入 include 的值,也就是 php://filter/resource=a/../a.txt,在前面有一个 php:// 是否存在的判断,如果存在则将 path 的指针向后移 6 位,这里再将 path 指针向后移 6 位的地址为 pathdup,也就是将 filter 之后一位的地址赋给 pathdup,而 p 则是用 strstr 函数来获取到第一个 /resource= 的地址,然后会判断 p 是否被赋值,如果没有的话就报错,有的话就继续往下走。
接下来是第一个关键点,这里调用了 php_stream_open_wrapper 来判断文件是否存在,它这里对于文件的判断是将 p 指针向后移动 10 位,也就是取 /resource= 之后的那一段,也就是我们这里的 a/../a.txt,虽然我们没有创建 a 这个文件夹,但是可以目录穿越直接穿回来,所以这里已经把文件读取出来将流赋给了 stream 变量。

然后 *p = '0' 将 p 清空,就到了最有意思的一段了,也是第二个关键点。

大概讲一下这里的意思吧,它会把 pathdup 指针向后移动一位之后的字符串以 / 作为分割,每个被分割的部分都会被丢入
php_stream_apply_filter_list 函数中进行判断,也就是说,这里我们本来 pathdup 是 /resource=a/../a.txt,会被分割为 resource=a & .. & a.txt,每个都会被丢入
php_stream_apply_filter_list 中,那么这个函数是干什么的呢,我们可以跟进一下:

我们把中间最重要的一段单独提出来:
if (read_chain) {
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
php_stream_filter_append(&stream->readfilters, temp_filter);
} else {
php_error_docref(NULL, E_WARNING, "Unable to create filter (%s)", p);
}
}
不难发现,它会尝试以这个字符串为过滤器名去创建这个过滤器,如果创建成功,则会对我们之前读取的文件流进行过滤器的相关操作,而如果不存在的话,则会弹一个警告:

所以到这里我们也就理解了它的搞法,不难构造出 php://filter/resource=
a/convert.base64-decode/../../a.txt:

不局限于 base64 编码,也可以尝试些别的。
PS:先前测的时候发现似乎从 5.x 到 8.x 都是这样的…… 参考文献
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36