
关键代码如下
1 |
|
已知问题代码出现在mail()函数,首先定位到$result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params);
这行代码。
通过mailPassthru
跟踪到下面这段代码
1 | private function mailPassthru($to, $subject, $body, $header, $params) |
根据这段if (ini_get('safe_mode') or !$this->UseSendmailOptions or is_null($params)) { $result = @mail($to, $subject, $body, $header);
可以看到只有safe_mode
没有开启的情况下才存在漏洞。
假设这个函数没有开启,继续追踪到关键函数$result = @mail($to, $subject, $body, $header, $params);
接下来看一看这个函数到底是怎么绕过的,追踪$params
参数
在setFrom()
函数中发现这个参数
1 | public function setFrom($address, $name = '', $auto = true) |
函数中对address
参数进行了过滤,既然这样,那问题肯定就是出现在过滤函数中了,继续往下找
过滤函数validateAddress
1 | public static function validateAddress($address, $patternselect = null) |
看到这段可以分析出两个限制条件,php版本小于5.2.0并且不支持PCRE才有可能调用 $patternselect = 'noregex';
,
假设版本小于5.2.0并且不支持PCRE,看一看noregex
如何过滤的
1 | case 'noregex': |
只要存在@并且长度大于等于3就好了,那这样就可以任意构造这个参数了。
接下来继续分析怎么构造会造成漏洞
这个漏洞的核心是因为php mail()函数漏洞引起的
首先看一下这个函数http://www.w3school.com.cn/php/func_mail_mail.asp
的语法
1 | 语法 |
声明:下面这段摘自绿盟博客,解释的至少比我易懂
在Linux系统上,mail函数在底层实现中,默认调用Linux的sendmail程序发送邮件。在sendmail程序的参数中,有一个-X选项,用于记录所有的邮件进出流量至log文件中。
1 | > -X logfile |
通过-X指定log文件记录邮件流量,实际可以达到写文件的效果。
例如,如下php代码
1 | $to = '[email protected]'; |
执行后,查看log文件/var/www/html/rce.php
1 | 17220 <<< To: [email protected] |
发现被写入了包含在邮件标题或正文中的php代码,通过访问此log文件可以执行预先可控的php代码。
关于POC可以看一下这些文章
https://legalhackers.com/videos/PHPMailer-Exploit-Remote-Code-Exec-Vuln-CVE-2016-10033-PoC.html
http://www.leavesongs.com/PENETRATION/PHPMailer-CVE-2016-10033.html
https://legalhackers.com/advisories/PHPMailer-Exploit-Remote-Code-Exec-CVE-2016-10033-Vuln.html
http://blog.nsfocus.net/multiple-php-mail-function-vulnerability-analysis/?utm_source=tuicool&utm_medium=referral
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-10033
- Post title:PHPMailer < 5.2.18 Remote Code Execution(CVE-2016-10033)
- Post author:langu_xyz
- Create time:2017-01-05 21:00:00
- Post link:https://blog.langu.xyz/PHPMailer < 5.2.18 Remote Code Execution(CVE-2016-10033)/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.