PoC 代码,用于利用 CVE-2024-32830 下载文件
getimagesize 绕过要绕过 getimagesize 的限制,我们可以为 PHP 创建一个简单的 image/vnd.wap.wbmp 图像。
文件类型的检查非常简单:
// https://github.com/php/php-src/blob/0029d2b08bbd3cb3aa293d9c8d55bf31faa9e203/ext/standard/image.c#L917
static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
{
int i, width = 0, height = 0;
if (php_stream_rewind(stream)) {
return 0;
}
/* get type */
if (php_stream_getc(stream) != 0) {
return 0;
}
/* skip header */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
} while (i & 0x80);
/* get width */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
width = (width << 7) | (i & 0x7f);
/* maximum valid width for wbmp (although 127 may be a more accurate one) */
if (width > 2048) {
return 0;
}
} while (i & 0x80);
/* get height */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
height = (height << 7) | (i & 0x7f);
/* maximum valid height for wbmp (although 127 may be a more accurate one) */
if (height > 2048) {
return 0;
}
} while (i & 0x80);
if (!height || !width) {
return 0;
}
if (!check) {
(*result)->width = width;
(*result)->height = height;
}
return IMAGE_FILETYPE_WBMP;
}
构造有效图像的最简单方法是使用两个 NUL 字节,后跟两个 < 0x80 字节分别表示宽度和高度。
使用 php://filter 可以为任何文件实现这一点。
第一层需要应用 base64 编码,以确保所有数据都是 ASCII,从而满足 < 0x80 的约束。
第二层过滤器需要添加前两个 NUL 字节。这可以通过强制从 UTF-16BE 转换为 UTF-32BE 来实现。这将迫使 iconv 将每两个字节的块解释为有效的 UTF-16BE 字符,然后在其前添加两个 NUL 字节以使其成为 UTF-32BE。实际要使用的过滤器是:convert.iconv.utf-16be.utf-32be。
我们最终的有效载荷是 php://filter/convert.base64-encode/convert.iconv.utf-16be.utf-32be/resource=<file here>。