ctfsolver.managers.manager_connections

manager_connections.py

This module provides the ManagerConnections class for managing connections to CTF challenges, supporting both local and remote connection types using the pwntools library.

Classes:

ManagerConnections: Handles connection setup, interaction, and communication with CTF challenges.

Typical usage example:

manager = ManagerConnections(url=”example.com”, port=1337, conn=”remote”) manager.initiate_connection() output = manager.recv_lines(number=3, display=True, save=True) manager.send_menu(choice=1, menu_num=3, menu_text=”Your choice:”)

pwn[source]

Reference to the pwntools library.

Type:

module

url[source]

Remote URL for connection.

Type:

str

port[source]

Remote port for connection.

Type:

int

conn_type[source]

Type of connection (“local” or “remote”).

Type:

str

conn[source]

Active connection object.

menu_num[source]

Number of menu options.

Type:

int

menu_text[source]

Text prompt for menu selection.

Type:

str

debug[source]

Debug mode flag.

Type:

bool

ValueError[source]

If required menu parameters are not provided.

DeprecationWarning[source]

If deprecated methods are used.

EOFError[source]

If the connection closes unexpectedly during data reception.

Classes

ManagerConnections

Manages connections to CTF challenges, supporting both local and remote modes.

Module Contents

class ManagerConnections(*args, **kwargs)[source]

Manages connections to CTF challenges, supporting both local and remote modes. This class provides methods to initiate and manage connections to CTF challenges, either by spawning a local process or connecting to a remote host. It also offers utilities for interacting with typical menu-driven CTF binaries, including sending choices, receiving lines, and handling menu prompts. .. attribute:: pwn

The pwntools module or object used for process and remote connections.

url[source]

The remote host URL or IP address.

Type:

str

port[source]

The remote host port.

Type:

int

conn_type[source]

Type of connection, either ‘local’ or ‘remote’.

Type:

str

conn[source]

The active connection object (process or remote).

menu_num[source]

Number of menu options expected.

Type:

int

menu_text[source]

Text prompt expected before sending a menu choice.

Type:

str

debug[source]

Flag to enable debug mode.

Type:

bool

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

Initializes the ManagerConnections instance with connection parameters.

Return type:

None

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

Initiates the connection based on the specified connection type.

Return type:

None

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

Connects to the challenge locally or remotely, depending on conn_type.

Return type:

None

recv_menu(number=1, display=False, save=False)[source]

Deprecated. Use recv_lines instead.

recv_lines(number=1, display=False, save=False)[source]

Receives a specified number of lines from the connection.

send_menu(choice, menu_num=None, menu_text=None, display=False, save=False)[source]

Sends a choice to a menu-driven binary, handling menu prompts and output.

recv_send(text, lines=None, text_until=None, display=False, save=False)[source]

Receives lines and/or text until a prompt, then sends a response.

send(text, encode=True)[source]

Sends text to the connection, optionally encoding it.

Return type:

None

recv_until(text, **kwargs)[source]

Receives data until a specified delimiter is encountered.

Return type:

bytes

pwn[source]
url[source]
port[source]
conn_type[source]
conn = None[source]
menu_num = None[source]
menu_text = None[source]
debug[source]
initiate_connection(*args, **kwargs)[source]

Initiates a connection using the specified connection type and parameters. :param *args: Variable length argument list to be passed to the connection method. :param **kwargs: Arbitrary keyword arguments to be passed to the connection method.

Returns:

None

Return type:

None

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

Connects to the challenge based on the connection type. If the connection type is remote, it connects to the url and port provided. If the connection type is local, it starts a process with the file provided.

local:
kwargs :

argv: Any | None = None, shell: bool = False, executable: Any | None = None, cwd: Any | None = None, env: Any | None = None, ignore_environ: Any | None = None, stdin: int = PIPE, stdout: PTY | int = PTY if not IS_WINDOWS else PIPE, stderr: int = STDOUT, close_fds: bool = True, preexec_fn: Any = lambda : None, raw: bool = True, aslr: Any | None = None, setuid: Any | None = None, where: str = ‘local’, display: Any | None = None, alarm: Any | None = None, creationflags: int = 0

Return type:

None

recv_menu(number=1, display=False, save=False)[source]
recv_lines(number=1, display=False, save=False, *args, **kwargs)[source]
Description:

Receives the output of the menu based on the number of lines provided. If display is True, it prints the output of everything received. If save is True, it saves the output in a list and returns it.

Parameters:
  • number (int, optional) – Number of lines to receive . Defaults to 1.

  • display (bool, optional) – Displayes the lines received. Defaults to False.

  • save (bool, optional) – Saves the lines received to a list. Defaults to False.

Returns:

list of the lines received if save is True

Return type:

list

send_menu(choice, menu_num=None, menu_text=None, display=False, save=False)[source]
Description:

Gets the menu num either from the class or from the function call and saves it to the class. Gets the menu text that the menu is providing, receives until the menu asks for choice and then send out the choice. If save is True, it saves the output of the menu in a list and returns it. If display is True, it prints the output of everything received.

Parameters:
  • choice (int or str) – Choice to send to the menu

  • menu_num (int, optional) – Number of options printed in the menu. Defaults to None.

  • menu_text (str, optional) – Text that the menu asks before sending your choice. Defaults to None.

  • display (bool, optional) – Variable to print every received line. Defaults to False.

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

Returns:

List of output of the menu if save is True

Return type:

list

recv_send(text, lines=None, text_until=None, display=False, save=False)[source]
Description:

Receives lines and sends a response. It can receive a number or lines, and/or specific text. If save is True, it saves the output of the menu in a list and returns it. If display is True, it prints the output of everything received.

Parameters:
  • choice (int or str) – Choice to send to the menu

  • menu_num (int, optional) – Number of options printed in the menu. Defaults to None.

  • menu_text (str, optional) – Text that the menu asks before sending your choice. Defaults to None.

  • display (bool, optional) – Variable to print every received line. Defaults to False.

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

Returns:

List of output of the menu if save is True

Return type:

list

send(text, encode=True)[source]
Description:

Sends the text to the connection after it encodes it. Wrapper for self.conn.sendline(str(text).encode())

Parameters:

text (str) – Text to send

Return type:

None

recv_until(text, **kwargs)[source]
Description:

Receive data until one of delims`(text) provided is encountered. It encodes the text before sending it. Wrapper for self.conn.recvuntil(text.encode()) Can also drop the ending if drop is True. If the request is not satisfied before ``timeout` seconds pass, all data is buffered and an empty string ('') is returned.

Parameters:
  • text (str) – Text to receive until

  • **kwargs – Additional keyword arguments to pass to the recv - drop (bool, optional): Drop the ending. If True it is removed from the end of the return value. Defaults to False. - timeout (int, optional): Timeout in seconds. Defaults to default.

Raises:

exceptions.EOFError – The connection closed before the request could be satisfied

Returns:

A string containing bytes received from the socket, or '' if a timeout occurred while waiting.

Return type:

bytes