import concurrent.futures
import http.client
import os
import re
import ssl

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
OUT = "/root/recon/cp_pages"
os.makedirs(OUT, exist_ok=True)
DOM = re.compile(r"\b(?:[a-z0-9][a-z0-9-]{1,62}\.)+(?:com|net|org|gov|edu|us|local|corp|lan|mil|biz|info|io)\b", re.I)
KEY = re.compile(r"(saml|entra|azure|okta|adfs|realm|domain|aaa|ldap|radius|office mode|endpoint|snx|mobile access|snap)", re.I)


def ctx():
    c = ssl.create_default_context()
    c.check_hostname = False
    c.verify_mode = ssl.CERT_NONE
    c.set_ciphers("DEFAULT@SECLEVEL=1")
    return c


def fetch(ip, host, path, timeout=10):
    try:
        conn = http.client.HTTPSConnection(ip, 443, timeout=timeout, context=ctx())
        conn.request("GET", path, headers={"Host": host, "User-Agent": UA, "Accept": "*/*"})
        r = conn.getresponse()
        b = r.read(300000).decode("utf-8", "replace")
        loc = r.getheader("Location") or ""
        conn.close()
        if r.status in (301, 302, 303, 307) and loc:
            nxt = loc if loc.startswith("/") else "/" + loc.split("/", 3)[-1]
            return fetch(ip, host, nxt, timeout)
        return r.status, b
    except Exception:
        return None, ""


def host_of(subject):
    m = re.search(r"CN\s*=\s*([^,]+)", subject)
    if not m:
        return ""
    cn = m.group(1).strip().lstrip("*.")
    parts = cn.split(".")
    for a, b in zip(parts, parts[1:]):
        if b.lower() in ("com", "net", "org", "gov", "edu", "us", "local", "mil", "biz", "info", "io"):
            idx = parts.index(a)
            return ".".join(parts[idx:idx + 3])
    return cn


def one(line):
    f = line.rstrip("\n").split("\t")
    ip = f[0]
    subj = f[1] if len(f) > 1 else ""
    host = host_of(subj) or ip
    titles = []
    hints = set()
    doms = set()
    for path in ("/", "/sslvpn/Login/Login", "/clients/"):
        st, b = fetch(ip, host, path)
        if not b:
            continue
        t = re.search(r"<title[^>]*>(.*?)</title>", b, re.S | re.I)
        if t:
            titles.append(t.group(1).strip()[:60])
        for m in KEY.finditer(b):
            hints.add(m.group(1).lower())
        for m in DOM.finditer(b):
            d = m.group(0).lower()
            if not re.match(r"^\d", d) and "w3.org" not in d and "jquery" not in d:
                doms.add(d)
        if "saml" in " ".join(hints) or doms:
            open(os.path.join(OUT, ip + ".html"), "w").write(b[:150000])
    return "%s\t%s\t%s\t%s\t%s" % (ip, host, " | ".join(dict.fromkeys(titles))[:90],
                                   ",".join(sorted(hints))[:90],
                                   ",".join(sorted(doms)[:8]))


rows = [l for l in open("/root/recon/cp_vuln_orgs.tsv", encoding="utf-8", errors="replace") if l.strip()]
print("gateways", len(rows), flush=True)
with open("/root/recon/cp_portal.tsv", "w") as f:
    with concurrent.futures.ThreadPoolExecutor(max_workers=24) as pool:
        for r in pool.map(one, rows):
            f.write(r + "\n")
            f.flush()
print("done", flush=True)
