import concurrent.futures, http.client, re, ssl, subprocess

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"


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 subj(ip):
    try:
        p = subprocess.run(["openssl", "s_client", "-connect", "%s:443" % ip, "-showcerts",
                            "-servername", ip], input="", capture_output=True, text=True, timeout=12)
        s = re.search(r"s:\s*(.+)", p.stdout)
        return s.group(1)[:95] if s else ""
    except Exception:
        return ""


def title(ip):
    for path in ("/", "/sslvpn/Login/Login"):
        try:
            conn = http.client.HTTPSConnection(ip, 443, timeout=8, context=ctx())
            conn.request("GET", path, headers={"Host": ip, "User-Agent": UA})
            r = conn.getresponse()
            b = r.read(20000).decode("utf-8", "replace")
            conn.close()
            m = re.search(r"<title[^>]*>(.*?)</title>", b, re.S | re.I)
            t = " ".join(m.group(1).split())[:60] if m else ""
            brand = ""
            low = b.lower()
            for k in ("check point", "mobile access", "remote access vpn", "network extender"):
                if k in low:
                    brand = k
                    break
            if t or brand:
                return "%s|%s|%d" % (t, brand, r.status)
        except Exception:
            continue
    return ""


ips = [l.split()[0] for l in open("/root/recon/t_vuln.txt") if l.strip()]
print("targets", len(ips), flush=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as pool:
    ss = list(pool.map(subj, ips))
    tt = list(pool.map(title, ips))
with open("/root/recon/cp_vuln_orgs.tsv", "w") as f:
    for ip, s, t in zip(ips, ss, tt):
        f.write("%s\t%s\t%s\n" % (ip, s, t))
print("finished", flush=True)
