From 6b8a956c54237cead7a783082fe2ae028c9f4bf6 Mon Sep 17 00:00:00 2001 From: Mitul16 Date: Fri, 18 Jun 2021 16:17:53 +0530 Subject: [PATCH] Fixed ChannelError constructor calls There is a missing argument to the ChannelError constructor - ch (channel). Because of this, many explicitly passed error messages are simply rejected. There is a minor typo correction as well, 'writiers' -> 'writers'. --- pwncat/channel/connect.py | 12 +++++++++++- pwncat/channel/ssh.py | 14 +++++++------- pwncat/manager.py | 6 +++++- pwncat/platform/linux.py | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pwncat/channel/connect.py b/pwncat/channel/connect.py index a40214d..e839b4c 100644 --- a/pwncat/channel/connect.py +++ b/pwncat/channel/connect.py @@ -37,7 +37,17 @@ class Connect(Socket): ) as progress: progress.add_task("connecting", total=1, start=False) # Connect to the remote host - client = socket.create_connection((host, port)) + + # If we get an invalid host from the user, that cannot be resolved + # then we capture the GAI (getaddrinfo) exception and raise it as ChannelError + # so that it is handled properly by the parent methods + + try: + client = socket.create_connection((host, port)) + except socket.gaierror: + raise ChannelError(self, "invalid host provided") + except ConnectionRefusedError: + raise ChannelError(self, "connection refused, check your port") progress.log( f"connection to " diff --git a/pwncat/channel/ssh.py b/pwncat/channel/ssh.py index 0937571..8da056f 100644 --- a/pwncat/channel/ssh.py +++ b/pwncat/channel/ssh.py @@ -34,7 +34,7 @@ class Ssh(Channel): port = 22 if not user or user is None: - raise ChannelError("you must specify a user") + raise ChannelError(self, "you must specify a user") if password is None and identity is None: password = prompt("Password: ", is_password=True) @@ -43,7 +43,7 @@ class Ssh(Channel): # Connect to the remote host's ssh server sock = socket.create_connection((host, port)) except Exception as exc: - raise ChannelError(str(exc)) + raise ChannelError(self, str(exc)) # Create a paramiko SSH transport layer around the socket t = paramiko.Transport(sock) @@ -51,7 +51,7 @@ class Ssh(Channel): t.start_client() except paramiko.SSHException: sock.close() - raise ChannelError("ssh negotiation failed") + raise ChannelError(self, "ssh negotiation failed") if identity is not None: try: @@ -67,23 +67,23 @@ class Ssh(Channel): try: key = paramiko.RSAKey.from_private_key_file(identity, password) except paramiko.ssh_exception.SSHException: - raise ChannelError("invalid private key or passphrase") + raise ChannelError(self, "invalid private key or passphrase") # Attempt authentication try: t.auth_publickey(user, key) except paramiko.ssh_exception.AuthenticationException as exc: - raise ChannelError(str(exc)) + raise ChannelError(self, str(exc)) else: try: t.auth_password(user, password) except paramiko.ssh_exception.AuthenticationException as exc: - raise ChannelError(str(exc)) + raise ChannelError(self, str(exc)) if not t.is_authenticated(): t.close() sock.close() - raise ChannelError("authentication failed") + raise ChannelError(self, "authentication failed") # Open an interactive session chan = t.open_session() diff --git a/pwncat/manager.py b/pwncat/manager.py index 20b1e82..ea9da5e 100644 --- a/pwncat/manager.py +++ b/pwncat/manager.py @@ -653,7 +653,11 @@ class Manager: specified platform. """ - session = Session(self, platform, channel, **kwargs) + try: + session = Session(self, platform, channel, **kwargs) + except ChannelError as exc: + self.log(f"[red]error:[/red] {exc}") + return None # Increment the ``session_id`` variable upon adding a new session # Session constructor will automatically grab the current diff --git a/pwncat/platform/linux.py b/pwncat/platform/linux.py index 72ae26a..f604f85 100644 --- a/pwncat/platform/linux.py +++ b/pwncat/platform/linux.py @@ -1249,7 +1249,7 @@ class Linux(Platform): except MissingBinary: pass else: - raise PlatformError("no available gtfobins writiers") + raise PlatformError("no available gtfobins writers") popen = self.Popen( payload, @@ -1278,7 +1278,7 @@ class Linux(Platform): except MissingBinary: pass else: - raise PlatformError("no available gtfobins writiers") + raise PlatformError("no available gtfobins writers") popen = self.Popen( payload,