sshmitm.interfaces.server module
- class sshmitm.interfaces.server.BaseServerInterface(session: Session)
Bases:
ServerInterface
,BaseModule
- class sshmitm.interfaces.server.ProxySFTPServer(channel: Channel, name: str, server: ServerInterface, sftp_si: Type[SFTPServerInterface], session: Session, *largs: Any, **kwargs: Any)
Bases:
SFTPServer
- finish_subsystem() None
Perform any cleanup at the end of a subsystem. The default implementation just closes the channel.
New in version 1.1.
- start_subsystem(name: str, transport: Transport, channel: Channel) None
Process an ssh subsystem in server mode. This method is called on a new object (and in a new thread) for each subsystem request. It is assumed that all subsystem logic will take place here, and when the subsystem is finished, this method will return. After this method returns, the channel is closed.
The combination of
transport
andchannel
are unique; this handler corresponds to exactly one .Channel on one .Transport.Note
It is the responsibility of this method to exit if the underlying .Transport is closed. This can be done by checking .Transport.is_active or noticing an EOF on the .Channel. If this method loops forever without checking for this case, your Python interpreter may refuse to exit because this thread will still be running.
- Parameters:
name (str) – name of the requested subsystem.
transport (.Transport) – the server-mode .Transport.
channel (.Channel) – the channel associated with this subsystem request.
- class sshmitm.interfaces.server.ServerInterface(session: Session)
Bases:
BaseServerInterface
ssh server implementation for SSH-MITM
- cancel_port_forward_request(address: str, port: int) None
The client would like to cancel a previous port-forwarding request. If the given address and port is being forwarded across this ssh connection, the port should be closed.
- Parameters:
address (str) – the forwarded address
port (int) – the forwarded port
- check_auth_interactive(username: str, submethods: Union[bytes, str]) Union[int, InteractiveQuery]
Begin an interactive authentication challenge, if supported. You should override this method in server mode if you want to support the
"keyboard-interactive"
auth type, which requires you to send a series of questions for the client to answer.Return
AUTH_FAILED
if this auth method isn’t supported. Otherwise, you should return an .InteractiveQuery object containing the prompts and instructions for the user. The response will be sent via a call to check_auth_interactive_response.The default implementation always returns
AUTH_FAILED
.- Parameters:
username (str) – the username of the authenticating client
submethods (str) – a comma-separated list of methods preferred by the client (usually empty)
- Returns:
AUTH_FAILED
if this auth method isn’t supported; otherwise an object containing queries for the user- Return type:
int or .InteractiveQuery
- check_auth_interactive_response(responses: List[str]) Union[int, InteractiveQuery]
Continue or finish an interactive authentication challenge, if supported. You should override this method in server mode if you want to support the
"keyboard-interactive"
auth type.Return
AUTH_FAILED
if the responses are not accepted,AUTH_SUCCESSFUL
if the responses are accepted and complete the authentication, orAUTH_PARTIALLY_SUCCESSFUL
if your authentication is stateful, and this set of responses is accepted for authentication, but more authentication is required. (In this latter case, get_allowed_auths will be called to report to the client what options it has for continuing the authentication.)If you wish to continue interactive authentication with more questions, you may return an .InteractiveQuery object, which should cause the client to respond with more answers, calling this method again. This cycle can continue indefinitely.
The default implementation always returns
AUTH_FAILED
.- Parameters:
responses – list of str responses from the client
- Returns:
AUTH_FAILED
if the authentication fails;AUTH_SUCCESSFUL
if it succeeds;AUTH_PARTIALLY_SUCCESSFUL
if the interactive auth is successful, but authentication must continue; otherwise an object containing queries for the user- Return type:
int or .InteractiveQuery
- check_auth_none(username: str) int
Determine if a client may open channels with no (further) authentication.
Return
AUTH_FAILED
if the client must authenticate, orAUTH_SUCCESSFUL
if it’s okay for the client to not authenticate.The default implementation always returns
AUTH_FAILED
.- Parameters:
username (str) – the username of the client.
- Returns:
AUTH_FAILED
if the authentication fails;AUTH_SUCCESSFUL
if it succeeds.- Return type:
int
- check_auth_password(username: str, password: str) int
Determine if a given username and password supplied by the client is acceptable for use in authentication.
Return
AUTH_FAILED
if the password is not accepted,AUTH_SUCCESSFUL
if the password is accepted and completes the authentication, orAUTH_PARTIALLY_SUCCESSFUL
if your authentication is stateful, and this key is accepted for authentication, but more authentication is required. (In this latter case, get_allowed_auths will be called to report to the client what options it has for continuing the authentication.)The default implementation always returns
AUTH_FAILED
.- Parameters:
username (str) – the username of the authenticating client.
password (str) – the password given by the client.
- Returns:
AUTH_FAILED
if the authentication fails;AUTH_SUCCESSFUL
if it succeeds;AUTH_PARTIALLY_SUCCESSFUL
if the password auth is successful, but authentication must continue.- Return type:
int
- check_auth_publickey(username: str, key: PKey) int
Determine if a given key supplied by the client is acceptable for use in authentication. You should override this method in server mode to check the username and key and decide if you would accept a signature made using this key.
Return
AUTH_FAILED
if the key is not accepted,AUTH_SUCCESSFUL
if the key is accepted and completes the authentication, orAUTH_PARTIALLY_SUCCESSFUL
if your authentication is stateful, and this password is accepted for authentication, but more authentication is required. (In this latter case, get_allowed_auths will be called to report to the client what options it has for continuing the authentication.)Note that you don’t have to actually verify any key signtature here. If you’re willing to accept the key, Paramiko will do the work of verifying the client’s signature.
The default implementation always returns
AUTH_FAILED
.- Parameters:
username (str) – the username of the authenticating client
key (.PKey) – the key object provided by the client
- Returns:
AUTH_FAILED
if the client can’t authenticate with this key;AUTH_SUCCESSFUL
if it can;AUTH_PARTIALLY_SUCCESSFUL
if it can authenticate with this key but must continue with authentication- Return type:
int
- check_channel_direct_tcpip_request(chanid: int, origin: Tuple[str, int], destination: Tuple[str, int]) int
Determine if a local port forwarding channel will be granted, and return
OPEN_SUCCEEDED
or an error code. This method is called in server mode when the client requests a channel, after authentication is complete.The
chanid
parameter is a small number that uniquely identifies the channel within a .Transport. A .Channel object is not created unless this method returnsOPEN_SUCCEEDED
– once a .Channel object is created, you can call .Channel.get_id to retrieve the channel ID.The origin and destination parameters are (ip_address, port) tuples that correspond to both ends of the TCP connection in the forwarding tunnel.
The return value should either be
OPEN_SUCCEEDED
(or0
) to allow the channel request, or one of the following error codes to reject it:OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
OPEN_FAILED_CONNECT_FAILED
OPEN_FAILED_UNKNOWN_CHANNEL_TYPE
OPEN_FAILED_RESOURCE_SHORTAGE
The default implementation always returns
OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
.- Parameters:
chanid (int) – ID of the channel
origin (tuple) – 2-tuple containing the IP address and port of the originator (client side)
destination (tuple) – 2-tuple containing the IP address and port of the destination (server side)
- Returns:
an int success or failure code (listed above)
- check_channel_env_request(channel: Channel, name: bytes, value: bytes) bool
Check whether a given environment variable can be specified for the given channel. This method should return
True
if the server is willing to set the specified environment variable. Note that some environment variables (e.g., PATH) can be exceedingly dangerous, so blindly allowing the client to set the environment is almost certainly not a good idea.The default implementation always returns
False
.- Parameters:
channel – the .Channel the env request arrived on
name (str) – name
value (str) – Channel value
- Returns:
A boolean
- check_channel_exec_request(channel: Channel, command: bytes) bool
Determine if a shell command will be executed for the client. If this method returns
True
, the channel should be connected to the stdin, stdout, and stderr of the shell command.The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the request arrived on.
command (str) – the command to execute.
- Returns:
True
if this channel is now hooked up to the stdin, stdout, and stderr of the executing command;False
if the command will not be executed.
New in version 1.1.
- check_channel_forward_agent_request(channel: Channel) bool
Determine if the client will be provided with an forward agent session. If this method returns
True
, the server will allow SSH Agent forwarding.The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the request arrived on
- Returns:
True
if the AgentForward was loaded;False
if not
If
True
is returned, the server should create anAgentServerProxy
to access the agent.
- check_channel_pty_request(channel: Channel, term: bytes, width: int, height: int, pixelwidth: int, pixelheight: int, modes: bytes) bool
Determine if a pseudo-terminal of the given dimensions (usually requested for shell access) can be provided on the given channel.
The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the pty request arrived on.
term (str) – type of terminal requested (for example,
"vt100"
).width (int) – width of screen in characters.
height (int) – height of screen in characters.
pixelwidth (int) – width of screen in pixels, if known (may be
0
if unknown).pixelheight (int) – height of screen in pixels, if known (may be
0
if unknown).
- Returns:
True
if the pseudo-terminal has been allocated;False
otherwise.
- check_channel_request(kind: str, chanid: int) int
Determine if a channel request of a given type will be granted, and return
OPEN_SUCCEEDED
or an error code. This method is called in server mode when the client requests a channel, after authentication is complete.If you allow channel requests (and an ssh server that didn’t would be useless), you should also override some of the channel request methods below, which are used to determine which services will be allowed on a given channel:
check_channel_pty_request
check_channel_shell_request
check_channel_subsystem_request
check_channel_window_change_request
check_channel_x11_request
check_channel_forward_agent_request
The
chanid
parameter is a small number that uniquely identifies the channel within a .Transport. A .Channel object is not created unless this method returnsOPEN_SUCCEEDED
– once a .Channel object is created, you can call .Channel.get_id to retrieve the channel ID.The return value should either be
OPEN_SUCCEEDED
(or0
) to allow the channel request, or one of the following error codes to reject it:OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
OPEN_FAILED_CONNECT_FAILED
OPEN_FAILED_UNKNOWN_CHANNEL_TYPE
OPEN_FAILED_RESOURCE_SHORTAGE
The default implementation always returns
OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
.- Parameters:
kind (str) – the kind of channel the client would like to open (usually
"session"
).chanid (int) – ID of the channel
- Returns:
an int success or failure code (listed above)
- check_channel_shell_request(channel: Channel) bool
Determine if a shell will be provided to the client on the given channel. If this method returns
True
, the channel should be connected to the stdin/stdout of a shell (or something that acts like a shell).The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the request arrived on.
- Returns:
True
if this channel is now hooked up to a shell;False
if a shell can’t or won’t be provided.
- check_channel_subsystem_request(channel: Channel, name: str) bool
Determine if a requested subsystem will be provided to the client on the given channel. If this method returns
True
, all future I/O through this channel will be assumed to be connected to the requested subsystem. An example of a subsystem issftp
.The default implementation checks for a subsystem handler assigned via .Transport.set_subsystem_handler. If one has been set, the handler is invoked and this method returns
True
. Otherwise it returnsFalse
.Note
Because the default implementation uses the .Transport to identify valid subsystems, you probably won’t need to override this method.
- Parameters:
channel (.Channel) – the .Channel the pty request arrived on.
name (str) – name of the requested subsystem.
- Returns:
True
if this channel is now hooked up to the requested subsystem;False
if that subsystem can’t or won’t be provided.
- check_channel_window_change_request(channel: Channel, width: int, height: int, pixelwidth: int, pixelheight: int) bool
Determine if the pseudo-terminal on the given channel can be resized. This only makes sense if a pty was previously allocated on it.
The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the pty request arrived on.
width (int) – width of screen in characters.
height (int) – height of screen in characters.
pixelwidth (int) – width of screen in pixels, if known (may be
0
if unknown).pixelheight (int) – height of screen in pixels, if known (may be
0
if unknown).
- Returns:
True
if the terminal was resized;False
if not.
- check_channel_x11_request(channel: Channel, single_connection: bool, auth_protocol: str, auth_cookie: ByteString, screen_number: int) bool
Determine if the client will be provided with an X11 session. If this method returns
True
, X11 applications should be routed through new SSH channels, using .Transport.open_x11_channel.The default implementation always returns
False
.- Parameters:
channel (.Channel) – the .Channel the X11 request arrived on
single_connection (bool) –
True
if only a single X11 channel should be opened, elseFalse
.auth_protocol (str) – the protocol used for X11 authentication
auth_cookie (str) – the cookie used to authenticate to X11
screen_number (int) – the number of the X11 screen to connect to
- Returns:
True
if the X11 session was opened;False
if not
- check_global_request(kind: str, msg: Message) Union[bool, Tuple[Union[bool, int, str], ...]]
Handle a global request of the given
kind
. This method is called in server mode and client mode, whenever the remote host makes a global request. If there are any arguments to the request, they will be inmsg
.There aren’t any useful global requests defined, aside from port forwarding, so usually this type of request is an extension to the protocol.
If the request was successful and you would like to return contextual data to the remote host, return a tuple. Items in the tuple will be sent back with the successful result. (Note that the items in the tuple can only be strings, ints, or bools.)
The default implementation always returns
False
, indicating that it does not support any global requests.Note
Port forwarding requests are handled separately, in check_port_forward_request.
- Parameters:
kind (str) – the kind of global request being made.
msg (.Message) – any extra arguments to the request.
- Returns:
True
or a tuple of data if the request was granted;False
otherwise.
- check_port_forward_request(address: str, port: int) int
Note that the if the client requested the port, we must handle it or return false. Only if it requested 0 as port we can open a random port (actually the OS will tell us which port). If it can’t be opened, we just return false.
- get_allowed_auths(username: str) str
Return a list of authentication methods supported by the server. This list is sent to clients attempting to authenticate, to inform them of authentication methods that might be successful.
The “list” is actually a string of comma-separated names of types of authentication. Possible values are
"password"
,"publickey"
, and"none"
.The default implementation always returns
"password"
.- Parameters:
username (str) – the username requesting authentication.
- Returns:
a comma-separated str of authentication types
- classmethod parser_arguments() None