FastCGI - Port 9000

Basic info

What is FastCGI?

FastCGI is a binary protocol that allows web servers to communicate with application servers (typically PHP-FPM) to process dynamic content. It's an evolution of CGI (Common Gateway Interface) designed to:

  • Persist processes instead of spawning them per request

  • Reduce overhead of process creation

  • Scale better with high-traffic applications

  • Support multiple languages (PHP, Python, Perl, Ruby, etc.)

FastCGI vs CGI

Architecture

Typical Web Stack with FastCGI:

Process Flow:

  1. Client requests http://server/index.php

  2. Nginx receives request

  3. Nginx forwards to PHP-FPM via FastCGI (port 9000)

  4. PHP-FPM executes script

  5. PHP-FPM returns result to Nginx

  6. Nginx sends response to client

PHP-FPM

PHP-FPM (FastCGI Process Manager) is the most common FastCGI implementation:

  • Manages pool of PHP worker processes

  • Handles FastCGI protocol

  • Usually listens on localhost:9000

  • Can listen on Unix socket (/var/run/php-fpm.sock)

Configuration Files:

Default Port

Port 9000 - FastCGI (typically PHP-FPM)

Important Notes:

  • Usually binds to localhost only (127.0.0.1)

  • Exposed FastCGI is a critical vulnerability

  • No authentication by default

  • Binary protocol (not HTTP)

FastCGI Protocol Overview

Protocol Structure

FastCGI Record Format:

Record Types

FastCGI Parameters

Key Parameters (CGI Environment Variables):

PHP-Specific Parameters:

Reconnaissance & Enumeration

Port Scanning

Basic Nmap Scan

Sample Output:

Note: Nmap often shows "unknown" service for FastCGI

Service Fingerprinting

Using cgi-fcgi (libfcgi)

Sample Status Page Output:

Using netcat (Limited)

Check for Exposed FastCGI

Local Testing:

Remote Testing:

Shodan Queries

Note: FastCGI is rarely indexed by Shodan since it's usually localhost-only

Remote Code Execution (RCE)

Method 1: Direct FastCGI RCE (Exposed Port)

Concept: Abuse PHP_VALUE to set auto_prepend_file with PHP code

Bash Script Exploitation:

What This Does:

  1. Sets PHP_VALUE to modify PHP configuration

  2. Enables allow_url_include and allow_url_fopen

  3. Uses auto_prepend_file to execute code before script

  4. Uses data:// stream wrapper with base64-encoded payload

  5. Executes when accessing existing PHP file

Testing:

Method 2: Python FastCGI Exploit

Advanced Python Script:

Usage:

Method 3: SSRF to FastCGI (Gopher Protocol)

Concept: Use SSRF vulnerability to access localhost:9000

Gopher Payload Builder:

Usage in SSRF:

Method 4: Nginx Misconfiguration Exploitation

Vulnerable Nginx Configuration:

With PHP Configuration:

Exploitation:

Path Traversal Exploitation:

Known Vulnerabilities & CVEs

CVE-2024-xxxx: libfcgi Integer Overflow

Affected: libfcgi <= 2.4.4 (2024)

Impact: Heap overflow β†’ Remote Code Execution

Description:

  • Integer overflow in nameLen/valueLen parsing

  • Affects 32-bit builds (common in IoT/embedded)

  • Leads to heap corruption

  • RCE when FastCGI socket is reachable

Vulnerable Code:

Exploitation:

Mitigation:

  • Update to libfcgi > 2.4.4

  • Use 64-bit systems

  • Validate input lengths

CVE-2024-9026: PHP-FPM Log Manipulation

Affected: PHP-FPM with catch_workers_output = yes

Impact: Log injection/truncation

Description:

  • When catch_workers_output is enabled

  • Attackers can inject/truncate log entries

  • Up to 4 bytes per log line

  • Can hide attack indicators

  • Poison logs for SIEM evasion

Exploitation:

Mitigation:

  • Update PHP-FPM

  • Disable catch_workers_output if not needed

  • Monitor logs for corruption

  • Use centralized logging

Nginx + cgi.fix_pathinfo Misconfiguration

Not a CVE, but widespread vulnerability

Vulnerable Configuration:

Impact:

  • Path traversal

  • Arbitrary file execution

  • Source code disclosure

Example Attack:

Proper Configuration:

Defense & Hardening

Secure PHP-FPM Configuration

Bind to Localhost Only:

Security Settings:

Secure Nginx Configuration

Safe PHP Handling:

Disable Path Info (Secure):

Network Security

Firewall Rules:

Monitor Connections:

Monitoring & Detection

Log Monitoring:

Intrusion Detection:

File Integrity Monitoring:

Regular Security Practices

Tools & Scripts

Essential Tools

  1. cgi-fcgi - FastCGI client (libfcgi)

  2. Gopherus - SSRF payload generator

  3. curl - HTTP client (for testing)

  4. nmap - Port scanning

  5. Custom Python scripts - Protocol manipulation

Install cgi-fcgi

Complete Exploitation Framework

Cheat Sheet

Quick Reference

Important Files

Key Parameters

Conclusion

FastCGI, while designed for performance, introduces significant security risks when exposed or misconfigured. The combination of no default authentication, powerful parameter injection capabilities, and common misconfigurations makes it a critical target for attackers.

Key Takeaways:

  1. Never expose FastCGI externally - Bind to localhost only

  2. Use Unix sockets - More secure than TCP sockets

  3. Validate file existence - Nginx must check files exist

  4. Disable cgi.fix_pathinfo - Set to 0 in php.ini

  5. Restrict PHP settings - Disable dangerous functions

  6. Monitor FastCGI connections - Alert on external access

  7. Keep software updated - Patch libfcgi and PHP-FPM

  8. Use strong Nginx config - Include try_files directive

  9. Regular security audits - Check binding and config

  10. Defense in depth - Firewall + config + monitoring

Attack Vectors:

  • Direct FastCGI access (if exposed)

  • SSRF to localhost:9000 (gopher protocol)

  • Nginx misconfiguration (path traversal)

  • Parameter injection (PHP_VALUE abuse)

  • Integer overflow (CVE-2024 libfcgi)

  • Log manipulation (CVE-2024-9026)

Remember to only perform these techniques during authorized security assessments. Unauthorized access is illegal and unethical.

Additional Resources

circle-check

Last updated