#!/usr/bin/env python3
"""Async HTTPS prober: GET / on targets, match product signatures.
Usage:
  prober.py map passA.tsv respA.tsv   # domain<TAB>ip input, SNI=domain
  prober.py ip  open443_ips.txt -     # raw IP input, no SNI mapping
Writes resp file (status/title/server/matches/location). Stats -> prober_stats.log
"""
import asyncio
import re
import socket
import sys
import time

import aiohttp
from aiohttp.resolver import AbstractResolver

SIGS = {
    "fortinet": r"(?i)fortigate|fortios|fortinet|fortilink|fortimail",
    "ivanti": r"(?i)dana-na|pulse connect|ivanti|Ivanti Connect",
    "netscaler": r"(?i)netscaler|citrix gateway|NSC_",
    "panos": r"(?i)global-?protect|palo alto|pan-os|PanOS",
    "sonicwall": r"(?i)sonicwall|secure mobile access|Virtual Office",
    "cisco": r"(?i)\+CSCOE\+|anyconnect|cisco adaptive security|cisco unified",
    "checkpoint": r"(?i)check point|SSL Network Extender",
    "f5": r"(?i)big-?ip|f5 networks",
    "beyondtrust": r"(?i)beyondtrust|bomgar",
    "exchange": r"(?i)outlook web|/owa/|X-OWA|exchange server",
    "sharepoint": r"(?i)sharepoint|sprequestguid",
    "vmware": r"(?i)vsphere|esxi|vcenter|ID_VC_Welcome|VMware",
    "zimbra": r"(?i)zimbra",
    "smartermail": r"(?i)smartermail",
    "gitlab": r"(?i)gitlab",
    "screenconnect": r"(?i)screenconnect",
    "whd": r"(?i)web help desk",
    "papercut": r"(?i)papercut",
    "atlassian": r"(?i)atlassian|jira|confluence",
    "weblogic": r"(?i)weblogic",
    "oracle": r"(?i)OA_HTML|e-?business suite|peoplesoft|oracle http",
    "freepbx": r"(?i)freepbx|switchvox|sangoma",
    "trueconf": r"(?i)trueconf",
    "oneview": r"(?i)hpe oneview",
    "ws1": r"(?i)workspace one|airwatch",
    "drupal": r"(?i)drupal\.settings|Drupal [7891]",
    "craft": r"(?i)craft cms",
    "zammad": r"(?i)zammad",
    "simplehelp": r"(?i)simplehelp",
    "openemr": r"(?i)openemr",
    "orthanc": r"(?i)orthanc",
}
COMP = [(n, re.compile(p)) for n, p in SIGS.items()]
TITLE = re.compile(r"(?is)<title[^>]*>\s*(.*?)\s*</title>")
WRITEALL = True

stats = {"done": 0, "hits": 0, "start": time.time()}
SEM = asyncio.Semaphore(900)


class MapResolver(AbstractResolver):
    def __init__(self, mapping):
        self.m = mapping

    async def resolve(self, host, port=0, family=socket.AF_INET):
        ip = self.m.get(host)
        if ip is None:
            raise OSError("nomap")
        return [{"hostname": host, "host": ip, "port": port,
                 "family": family, "proto": 0, "flags": 0}]

    async def close(self):
        pass


async def probe(sess, host, out, lock):
    try:
        async with sess.get(f"https://{host}/", allow_redirects=False) as r:
            raw = await r.content.read(65536)
            txt = raw.decode("utf-8", "replace")
            server = r.headers.get("Server", "")[:60]
            loc = r.headers.get("Location", "")[:80]
            blob = server + "\n" + txt
            m = [n for n, p in COMP if p.search(blob)]
            t = TITLE.search(txt)
            title = re.sub(r"\s+", " ", t.group(1)).strip()[:120] if t else ""
            stats["hits"] += 1 if m else 0
            if WRITEALL or m:
                async with lock:
                    out.write("\t".join([host, str(r.status), server, title,
                                         ",".join(m), loc]) + "\n")
    except Exception:
        pass
    finally:
        stats["done"] += 1


async def stats_log():
    with open("/root/recon/prober_stats.log", "a") as f:
        while True:
            el = time.time() - stats["start"]
            hps = stats["done"] / el if el else 0
            f.write(f"{time.strftime('%H:%M:%S')} done={stats['done']} "
                    f"hps={hps:.0f} hits={stats['hits']}\n")
            f.flush()
            await asyncio.sleep(30)


async def main():
    global WRITEALL
    mode, infile, outfile = sys.argv[1], sys.argv[2], sys.argv[3]
    WRITEALL = (mode == "map")
    if mode == "map":
        mapping = {}
        with open(infile, errors="replace") as f:
            for line in f:
                p = line.rstrip("\n").split("\t")
                if len(p) == 2:
                    mapping[p[0]] = p[1]
        resolver = MapResolver(mapping)
        hosts = list(mapping.keys())
    else:
        resolver = None
        hosts = iter(l.strip() for l in open(infile, errors="replace") if l.strip())

    conn = aiohttp.TCPConnector(limit=0, ssl=False, resolver=resolver)
    tout = aiohttp.ClientTimeout(total=9, connect=5, sock_read=6)
    hdr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
    lock = asyncio.Lock()
    stag = asyncio.create_task(stats_log())
    with open(outfile, "w") if outfile != "-" else open("/dev/null", "w") as out:
        async with aiohttp.ClientSession(connector=conn, timeout=tout,
                                         headers=hdr) as sess:
            pend = set()

            async def task(h):
                async with SEM:
                    await probe(sess, h, out, lock)

            for h in hosts:
                pend.add(asyncio.ensure_future(task(h)))
                if len(pend) >= 900:
                    _, pend = await asyncio.wait(
                        pend, return_when=asyncio.FIRST_COMPLETED)
            if pend:
                await asyncio.wait(pend)
    stag.cancel()
    with open("/root/recon/prober_stats.log", "a") as f:
        f.write(f"PASS COMPLETE done={stats['done']} hits={stats['hits']}\n")


if __name__ == "__main__":
    asyncio.run(main())
