sshmitm.plugins.scp.replace_file module

SCPReplaceFile: Replace file with another file during SCP transfer.

This module is a subclass of SCPForwarder from the sshmitm library and implements the handle_command and process_data methods to replace the original file with a specified file during SCP transfer.

Attributes: data_sent (bool): Flag to keep track of if the replacement file data has already been sent. file_stat (os.stat_result): Stat result of the replacement file. file_to_send (file): The replacement file that is to be sent during SCP transfer. args (argparse.Namespace): Namespace object containing the command-line arguments passed to the script. bytes_remaining (int): Bytes remaining to be sent. file_size (int): The size of the replacement file. file_command (str): The command of the file being transferred (e.g. ‘C’ for copying to the remote machine). file_mode (str): The mode of the file being transferred (e.g. ‘0644’). file_name (str): The name of the file being transferred. got_c_command (bool): Flag to keep track of if the ‘C’ command has been received.

Methods: parser_arguments(): Adds a required argument –scp-replace to the argument parser. init(session: sshmitm.session.Session): Initializes the SCPReplaceFile instance. handle_command(data: bytes) -> bytes: Handles the incoming SCP command and returns the modified SCP command. process_data(data: bytes) -> bytes: Processes the SCP data and returns the modified SCP data (i.e.the replacement file).

class sshmitm.plugins.scp.replace_file.SCPReplaceFile(session)

Bases: SCPForwarder

Replaces the transferred file with a different file during an SCP upload.

When an SCP client uploads a file to the server, this plugin intercepts the transfer and substitutes the specified replacement file, so the server receives the replacement instead of the original content.

Usage example

ssh-mitm server --scp-forwarder replace_file --scp-replace-file /path/to/replacement.bin

Notes

  • Only affects upload operations. Download transfers pass through unchanged.

  • File size and permissions are taken from the replacement file, not the original, so the client may observe a size mismatch if it checks.

Parameters:

session (Session)

__init__(session)

Resolves the replacement file path and opens it for reading.

Parameters:

session (Session) – the active SSH session being intercepted.

handle_command(data)
Parameters:

data (bytes)

Return type:

bytes

classmethod parser_arguments()
Return type:

None

process_data(data)
Parameters:

data (bytes)

Return type:

bytes