RIPS Analysis

The total amount of detected vulnerabilities is very high. Luckily, the majority of the detected vulnerabilities are inside the administration control panel, such that attackers either need to steal a valid account first or they have to trick an administrator into visiting a malicious website that triggers one of the critical vulnerabilities. For example, a remote command execution vulnerability could be triggered by a less critical cross-site scripting vulnerability. By chaining both vulnerabilities, the severity is increased drastically and can lead to full server compromise.

This is not necessary for all bugs though. In some cases, it is possible to trigger vulnerable code without prior authentication. After we have reported the vulnerabilities detected by RIPS, we were notified that a file creation vulnerability (included in our reports) was abused in the wild to infect and hack many FreePBX systems around the world. Hence, we urge all FreePBX users to update their installation to the latest version.

The truncated analysis results are available in our RIPS demo application. Please note that we limited the results to the issues described in this post in order to ensure a fix is available.

Case Study

A vulnerability that can be exploited directly may be more critical than a vulnerability that requires user interaction - but it is also less spectacular. For this reason, we present a combination of bugs that demonstrate how vulnerabilities can be chained together to increase their impact.

Example 1: Remote Command Execution

One interesting way to inject arbitrary system commands in FreePBX lies in the signature verification code. It was intended to make the system more secure, but in the end it had the opposite effect. The following code summaries highlight the flow of user input into a security critical operation.




public function handleupload($uploaded_file) {
    global $amp_conf;

    $filename = $amp_conf['AMPWEBROOT'] . '/admin/modules/_cache/' . $uploaded_file['name'];

public function _process_archive($filename, $progress_callback = '') {


public function verifyFile($filename, $retry = true) {
    $this->runGPG("–verify {$filename}");

public function runGPG($params, $stdin = null) {
    $gpgdir = $this->getGpgLocation();
    $homediropt = "--homedir {$gpgdir}";

    $cmd = $this->gpg . " {$homediropt} " . $this->gpgopts . " --status-fd 3 {$params}";
    proc_open($cmd, $fds, $pipes, '/tmp', $this->gpgenv);

The superglobal variable $_FILES has two array keys that can be tainted by a malicious user: the file name and its type. In this case, the name of an uploaded file is concatenated unsanitized to an OS system command in the verifyFile() method of the GPG class. As a result, an attacker can inject arbitrary system commands into a file name which are then executed with proc_open().

Although the summarized data flow above looks straight-forward, it takes effort to detect this vulnerability manually because the user input is passed through thousands of lines of code in several different files and functions until it is used in the security sensitive operation. RIPS was able to identify the vulnerable data flow within seconds.

In order to exploit this vulnerability, an attacker needs to know the circumstances that lead to the execution of the vulnerable code. In this case there is a file extension check in the function _process_archive(). The method runGPG() is only executed when the file extension matches gpg. So one possibility to execute arbitrary system commands is to upload a file that contains backticks in its name and ends with .gpg, e.g. `command`.gpg .

The file name is also used to create a temporary cache file on the disk. The file system (ext4) only supports names up to 255 characters, so if the command is too long it will not be executed. Several other command execution vulnerabilities were detected.

Example 2: Cross-Site Scripting

FreePBX has a cross-site request forgery protection. If the action parameter is set the referrer has to match the address of the server.


if (!isset($no_auth) && $action != '' && $amp_conf['CHECKREFERER']) {
    if (isset($_SERVER['HTTP_REFERER'])) {
        $referer = parse_url($_SERVER['HTTP_REFERER']);
        $server = trim($_SERVER['SERVER_NAME']);
        $refererok = ($referer['host'] == $server);
    } else {
        $refererok = false;
    if (!$refererok) {
        $display = 'badrefer';

This prevents attackers from abusing existing sessions of logged-in administrators because they are not able to spoof the referrer. While this is not the best approach to prevent cross-site request forgery, it is still effective. All remote command execution vulnerabilities that were found have an action parameter and thus are protected by this piece of code. However, RIPS also detected an abundance of cross-site scripting vulnerabilities that do not have an action parameter and thus can be used to nullify the referrer check.

Here is one simplified example of user input that is embedded into a JavaScript context without proper sanitization.


<?php $tech = htmlspecialchars($_REQUEST['tech']); ?>
var tech = '<?php echo !empty($tech) ? strtolower($tech) : strtolower($_REQUEST['tech']) ?>';

The request parameter tech is assigned to the $tech variable. Note, that it gets encoded with htmlspecialchars which prevents the use of the &, ", <, and > characters 1. Although in many scenarios this would be sufficient to prevent cross-site scripting, here, the user input is embedded into a JavaScript context with single quotes. We can simply end the current string with a single quote and inject arbitrary JavaScript instructions afterwards.

For exploitation, an attacker can embed and load an URL such as http://target/admin/config.php?display=trunks&tech=rips%27;alert%281%29;%27%3C/script%3E in the background of a web page. If they trick an administrator into visiting the malicious page, the JavaScript code is printed by the PHP code, executed in the context of the targeted user, and thus the attacker can perform any action in the FreePBX administration panel effectively bypassing the referrer check. The payload is converted to lower case characters though, which makes it a little more difficult to exploit because we cannot call JavaScript functions that contain upper case letters (i.e. most of them) - but it is certainly possible as demonstrated in the following video.

The final payload does not require upper case nor encoded letters and the file name has a length of 163 characters. All filters are bypassed and the command execution vulnerability is triggered by a click of our proof-of-concept link. The full payload will not be published.


There are many vulnerabilities in FreePBX that can have diverse reasons. For one, the project is in development for more than 12 years. Everyone that worked on a project for a longer time knows that priorities shift, unforeseen requirements emerge and the landscapes of languages change. Old libraries are replaced by new libraries, different coding styles and patterns come into fashion and the like.

With close to 600,000 lines of code, FreePBX is also a large and complex project. Too large and complex for a single person to know all details and specifics. There are many different people writing and maintaining modules and core systems of FreePBX. This makes it difficult to review all changes in full detail and bad code slips through from time to time. Unlike many other types of bugs, security vulnerabilities often remain unnoticed unless they are triggered on purpose. Furthermore, there are many different types of vulnerabilities that require in-depth studying of the attackers possibilities and effective remmediation methods.

As writing secure software is challenging and mistakes will always happen, an important thing is how vendors react when notified about problems. We would like to thank the team of FreePBX at Sangoma Technologies Corporation that worked with us on resolving the issues. They responded fast, professional, and fixed the most critical vulnerabilities in a fast manner.

Follow us on Twitter to be notified when the next gift of our advent calendar is opened!

APAV Time Table

Date Author Title
24 Dec 2016 Johannes Dahse What we learned from our Advent Calendar
23 Dec 2016 Hendrik Buchwald e107 2.1.2: SQL Injection through Object Injection
22 Dec 2016 Daniel Peeren Security Compliance with Static Code Analysis
21 Dec 2016 Martin Bednorz AbanteCart 1.2.8 - Multiple SQL Injections
20 Dec 2016 Martin Bednorz Kliqqi From Cross-Site Request Forgery to Code Execution
19 Dec 2016 Robin Peraglie osClass 3.6.1: Remote Code Execution via Image File
18 Dec 2016 Daniel Peeren Continuous Integration - Jenkins at your service
17 Dec 2016 Johannes Dahse OpenConf 5.30 - Multi-Step Remote Command Execution
16 Dec 2016 Robin Peraglie Redaxo 5.2.0: Remote Code Execution via CSRF
15 Dec 2016 Dennis Detering Guest Post: Vtiger 6.5.0 - SQL Injection
14 Dec 2016 Hendrik Buchwald The State of Wordpress Security
13 Dec 2016 Johannes Dahse phpBB 2.0.23 - From Variable Tampering to SQL Injection
12 Dec 2016 Martin Bednorz Teampass Unauthenticated SQL Injection
11 Dec 2016 Daniel Peeren Rescanning Applications with RIPS
10 Dec 2016 Hendrik Buchwald Non-Exploitable Security Issues
9 Dec 2016 Hendrik Buchwald Precurio 2.1: Remote Command Execution via Xinha Plugin
8 Dec 2016 Martin Bednorz PHPKit 1.6.6: Code Execution for Privileged Users
7 Dec 2016 Hendrik Buchwald Serendipity 2.0.3: From File Upload to Code Execution
6 Dec 2016 Robin Peraglie Roundcube 1.2.2: Command Execution via Email
5 Dec 2016 Hendrik Buchwald Expression Engine 3.4.2: Code Reuse Attack
4 Dec 2016 Johannes Dahse Introducing the RIPS analysis engine
3 Dec 2016 Martin Bednorz eFront 3.6.15: Steal your professors password
2 Dec 2016 Martin Bednorz Coppermine 1.5.42: Second-Order Command Execution
1 Dec 2016 Hendrik Buchwald FreePBX 13: From Cross-Site Scripting to Remote Command Execution
25 Nov 2016 Martin Bednorz Announcing the Advent of PHP Application Vulnerabilities

Disclaimer: The information provided here is for educational purposes only. It is your responsibility to obey all applicable local, state and federal laws. RIPS Technologies GmbH assumes no liability and is not responsible for any misuse or damages caused by direct or indirect use of the information provided.