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

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.

__init__(*args, **kwargs)[source]

Initializes the ManagerFolder instance.

init_for_challenge(*args, **kwargs)[source]

Sets up attributes and folders for a challenge.

handling_global_config()[source]

Loads global configuration for folder names.

initializing_all_ancestors(*args, **kwargs)[source]

Initializes ancestor classes (placeholder).

get_parent()[source]

Determines and sets the parent directory of the calling file.

setup_named_folders()[source]

Creates and assigns paths for named folders.

create_parent_folder()[source]

Creates parent folders if they do not exist.

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.

check_empty_folder(folder)[source]

Checks if a folder is empty.

get_challenge_file()[source]

Assigns the challenge file path.

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.

exec_on_files(folder, func, *args, **kwargs)[source]

Executes a function on all files in a folder.

Parameters:
  • files (list[str])

  • func (callable)

search_files(directory, exclude_dirs, search_string, save=False, display=False)[source]

Searches for a string in files within a directory.

get_self_functions()[source]

Returns a list of callable methods of the class.

get_function_reference(function, file)[source]

Finds references to a function in a file.

find_function_from_file(file_path, function_name)[source]

Finds and returns the source code of a function from a file.

folfil(folder, file)[source]

Returns the full path of a file within a folder.

folders_file(*folders, file)[source]

Returns the full path of a file within nested folders.

challenge_folder_structure(*args, **kwargs)[source]

Checks the structure of challenge folders.

Recursively applies a function to folders and files.

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”])

verbose[source]
Path[source]
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.

file[source]

The file associated with the challenge.

Type:

str

debug[source]

Indicates whether debug mode is enabled.

Type:

bool

folders_name_list[source]

A custom list of folder names, if provided.

Type:

list or None

folders_names_must[source]

The default list of required folder names.

Type:

list

handling_global_config()[source]
initializing_all_ancestors(*args, **kwargs)[source]
Description:

Initializes all the ancestors of the class

get_current_dir()[source]
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.

self.parent[source]

The resolved parent directory of the calling file.

Type:

Path or None

self.file_called_frame[source]

The stack frame of the calling file.

Type:

list

self.file_called_path[source]

The file path of the calling file.

Type:

Path

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)

check_empty_folder(folder)[source]
Description:

Check if the folder is empty

check_folder_exists(folder)[source]
Parameters:

folder (str)

Return type:

str | bool

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

Parameters:
  • solution_name (str, optional) – Name of the solution file. Defaults to “solution.py”.

  • save (bool, optional) – Save the solution file. Defaults to False.

Returns:

Path of the solution file if save is True

Return type:

str

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:
  • file (str) – File to search for the pattern

  • func (function, optional) – Function to search for the pattern. Defaults to None.

  • display (bool, optional) – Display the output. Defaults to False.

  • save (bool, optional) – Save the output. Defaults to False.

Returns:

List of output if save is True

Return type:

list

exec_on_folder(folder, func, *args, **kwargs)[source]

Description: Execute a function on all the files in the folder with the arguments provided

Parameters:
  • folder (str) – Folder to execute the function

  • func (function) – Function to execute

Returns:

List of output of the function

Return type:

list

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

Parameters:
  • folder (str) – Folder to execute the function

  • func (function) – Function to execute

Returns:

List of output of the function

Return type:

list

exec_on_files(files, func, *args, **kwargs)[source]

Description: Execute a function on all the file list with the arguments provided

Parameters:
  • files (list) – List of files to execute the function

  • func (function) – Function to execute

Returns:

List of output of the function

Return type:

list

search_files(directory, exclude_dirs, search_string, save=False, display=False)[source]

Description: Search for a string in the files in the directory

Parameters:
  • directory (str) – Directory to search for the string

  • exclude_dirs (list) – List of directories to exclude

  • search_string (str) – String to search for

  • save (bool, optional) – Save the output. Defaults to False.

  • display (bool, optional) – Display the output. Defaults to False.

Returns:

List of output if save is True

Return type:

list

folfil(folder, file)[source]
Description:

Get the full path of the file in the folder

Parameters:
  • folder (str) – Folder to get the file

  • file (str) – File to get the full path

Returns:

Full path of the file

Return type:

str

folders_file(*folders, file)[source]
Description:

Get the full path of the file in the folder

Parameters:

folders (list) – List of folders to get the file

Returns:

Full path of the file

Return type:

str

challenge_folder_structure(*args, **kwargs)[source]
Description:

Recursively search

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:

tuple

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.

Parameters:
  • source (str) – Source folder to copy

  • destination (str) – Destination folder to copy to

Returns:

None

download_automove(category, challenge_name, challenge_path, checker=False, verbose=False)[source]

Description: Moves the downloaded files to the challenge folder structure

Parameters:
  • category (str) – Category of the challenge

  • challenge_name (str) – Name of the challenge to search in the downloads

  • challenge_path (str) – Path of the challenge to move

  • checker (bool)

  • verbose (bool)