ctfsolver.managers.manager_folder¶
manager_folder.py
This module provides the ManagerFolder class for managing folder structures and file operations in the context of CTF (Capture The Flag) challenges. It offers utilities for initializing challenge folders, preparing and cleaning up files, searching for patterns, and introspecting Python files for function definitions.
- Classes:
ManagerFolder: Handles creation, management, and introspection of challenge-related folders and files.
- Typical usage example:
manager = ManagerFolder(file=”challenge.py”, verbose=True) manager.create_parent_folder() manager.prepare_space(files=[“input.txt”, “output.txt”])
CONFIG (dict): Global configuration dictionary imported from ctfsolver.config.
- Dependencies:
pathlib, inspect, os, ast, collections.defaultdict
Classes¶
ManagerFolder provides utilities for managing folder structures and files for CTF (Capture The Flag) challenges. |
Module Contents¶
- class ManagerFolder(*args, **kwargs)[source]¶
ManagerFolder provides utilities for managing folder structures and files for CTF (Capture The Flag) challenges. This class handles the creation, organization, and manipulation of challenge-related directories and files, including payloads, data, and solution scripts. It offers methods for searching files, executing functions on files, cleaning up empty directories, and extracting function definitions from Python files.
verbose (bool): Enables verbose output for debugging and logging. Path (type): Reference to the pathlib.Path class for file system operations. parent (Path): The resolved parent directory of the calling file. file (str): The filename associated with the challenge. folders_name_list (list): List of folder names to be managed. folders (defaultdict): Mapping of folder names to their Path objects. folder_payloads (Path): Path to the payloads folder. folder_data (Path): Path to the data folder. folder_files (Path): Path to the files folder. challenge_file (Path): Path to the challenge file. solution_file (Path): Path to the solution file.
- prepare_space(files=None, folder=None, test_text='flag{test}')[source]¶
Prepares challenge space by creating files and folders.
- clean_folders(folders
list = None): Removes empty folders.
- get_solution_file(*args, solution_name='solution.py', save=False, display=False, **kwargs)[source]¶
Retrieves the solution file path.
- search_for_pattern_in_file(file, func=None, display=False, save=False, *args, **kwargs)[source]¶
Searches for a pattern in a file.
- search_files(directory, exclude_dirs, search_string, save=False, display=False)[source]¶
Searches for a string in files within a directory.
- find_function_from_file(file_path, function_name)[source]¶
Finds and returns the source code of a function from a file.
- recursive_folder_search(function, *args, path=None, **kwargs)[source]¶
Recursively applies a function to folders and files.
- single_folder_search(*args, **kwargs)[source]¶
Applies a function to the contents of a single folder.
Example
manager = ManagerFolder(file=”challenge.txt”, verbose=True) manager.prepare_space(files=[“input.txt”, “output.txt”])
- init_for_challenge(*args, **kwargs)[source]¶
Initializes the class for the challenge. This method sets up the necessary attributes and folders required for the challenge. :param *args: Variable length argument list. :param **kwargs: Arbitrary keyword arguments.
file (str): The file associated with the challenge.
debug (bool, optional): Flag to enable or disable debug mode. Defaults to False.
folders_name_list (list, optional): A custom list of folder names. Defaults to None.
- initializing_all_ancestors(*args, **kwargs)[source]¶
- Description:
Initializes all the ancestors of the class
- get_parent()[source]¶
- Description:
Retrieves the parent directory of the file that invoked the current class.
This method determines the file path of the script that instantiated the class, resolves its parent directory, and adjusts the result based on a predefined list of folder names.
- Behavior:
If the parent directory’s name is in self.folders_name_list, the method sets self.parent to the grandparent directory instead.
Note
Ensure that self.folders_name_list is defined and contains the folder names to be checked before calling this method.
- setup_named_folders()[source]¶
Initializes and sets up named folder paths as attributes and in a dictionary. This method creates Path objects for the ‘data’, ‘files’, and ‘payloads’ folders relative to the parent directory, and assigns them to corresponding attributes. It also initializes a defaultdict to store folder paths for each name in self.folders_name_list, mapping each folder name to its Path object.
- Attributes set:
folder_payloads (Path): Path to the ‘payloads’ folder. folder_data (Path): Path to the ‘data’ folder. folder_files (Path): Path to the ‘files’ folder. folders (defaultdict): Dictionary mapping folder names to their Path objects.
- Raises:
AttributeError – If self.parent or self.folders_name_list is not defined.
- create_parent_folder()[source]¶
- Description:
Create the parent folder of the file that called the class if they don’t exist
- create_ctf_structure(category, site, name, verbose=False, download=False, **kwargs)[source]¶
- Description:
Create the CTF folder structure
- prepare_space(files=None, folder=None, test_text='flag{test}')[source]¶
Creates files with specified content in a given folder if they do not already exist. :param files: List of filenames to create. Defaults to an empty list. :type files: list, optional :param folder: Path to the folder where files will be created. :type folder: str or Path, optional :param Defaults to self.folder_files.: :param test_text: Content to write into each created file. Defaults to “flag{test}”. :type test_text: str, optional
- Returns:
None
- clean_folders(folders=None)[source]¶
- Description:
Clean the space by deleting the folders that remain empty
- Parameters:
folders (list)
- get_challenge_file()[source]¶
- Description:
Get the challenge file and assign it to the self.challenge_file for ease of access
- get_solution_file(*args, solution_name='solution.py', save=False, display=False, **kwargs)[source]¶
- Description:
Get the solution file and assign it to the self.solution_file for ease of access
- search_for_pattern_in_file(file, func=None, display=False, save=False, *args, **kwargs)[source]¶
Description: Search for a pattern in the file and return the output
- Parameters:
- Returns:
List of output if save is True
- Return type:
- exec_on_folder(folder, func, *args, **kwargs)[source]¶
Description: Execute a function on all the files in the folder with the arguments provided
- exec_on_folder_files(folder, func, func_args=[], func_kwargs={}, *args, **kwargs)[source]¶
Description: Execute a function on all the files in the folder with the arguments provided
- exec_on_files(files, func, *args, **kwargs)[source]¶
Description: Execute a function on all the file list with the arguments provided
- search_files(directory, exclude_dirs, search_string, save=False, display=False)[source]¶
Description: Search for a string in the files in the directory
- Parameters:
- Returns:
List of output if save is True
- Return type:
- recursive_folder_search(function, *args, path=None, **kwargs)[source]¶
- Description:
Recursively search for the file in the folder
- single_folder_search(*args, **kwargs)[source]¶
Searches a single folder and applies a specified function to its contents. :param *args: Additional positional arguments to pass to the specified function. :param **kwargs: Additional keyword arguments, including:
exclude (list, optional): A list of directory names to exclude from the search.
function (callable, optional): A function to apply to the folder’s contents. The function should accept the following arguments: root (str), dirs (list), files (list), *args, and **kwargs.
path (str, optional): The path of the folder to search.
- Returns:
- A tuple containing:
root (str): The root directory of the search.
dirs (list): A list of subdirectories in the root directory, excluding those in the exclude list.
files (list): A list of files in the root directory.
- Return type:
- delete_folder(folder)[source]¶
Description: Deletes the folder and all its contents.
- Parameters:
folder (str) – Folder to delete
- Returns:
None
- copy_folder(source, destination)[source]¶
Description: Copies the folder and all its contents to the destination.