Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close ssh session on error #19656

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sjanusz-r7
Copy link
Contributor

@sjanusz-r7 sjanusz-r7 commented Nov 17, 2024

This PR ensures that an SSH session dies when it encounters errors. For example, the shell request can fail asynchronously, leading to an SSH session that cannot be interacted with, as was the case with a Windows VM that has installed OpenSSH version 8.1.0.0 with choco install openssh --version 8.1.0-beta1.
As this shell request happens asynchronously, we end up with a session that has been reported as open and that the credential works, but the session then dies afterwards.

This PR also allows for the passing of an error_callback method, allowing us to log out the errors to the user:

  metasploit-framework git:(close-ssh-session-on-error) bundle exec ruby ./msfconsole -qx 'run'
[*] 192.168.112.222:22 - Starting bruteforce
[+] 192.168.112.222:22 - Success: 'win10:win10' 'Microsoft Windows 10 Pro 10.0.19045 N/A Build 19045'
[-] SSH Command Stream encountered an error: Shell/exec channel request failed (Server Version: SSH-2.0-OpenSSH_for_Windows_8.1)
[*] SSH session 1 opened (x:49198 -> x:22) at 2024-11-18 17:26:48 +0000
[*] Scanned 1 of 1 hosts (100% complete)
[*] Scan completed, 1 credential was successful.

Successful logins
=================

    Host             Public  Private
    ----             ------  -------
    x                  win10   win10


[*] 1 session was opened successfully.
[*] Auxiliary module execution completed
[*] x - SSH session 1 closed.  Reason: Died

Verification

List the steps needed to make sure this thing works

  • Start msfconsole
  • Ensure that running the ssh_login module vs. an Ubuntu VM works as expected
  • Ensure that running the ssh_login module vs. a Windows VM with an OpenSSH version newer than 8.1.0.0 works as expected
  • Ensure that running the ssh_login module vs. a Windows VM with an OpenSSH version of 8.1.0.0 makes the session die.

Before

An SSH session is broken but has not been killed. When interacting with it, no data is sent through the shell channel as it has died silently.

After

Session is killed, and an error is logged with the server version if the error_callback is provided as the command stream has no access to print_error itself, so it is passed in.

@sjanusz-r7 sjanusz-r7 force-pushed the close-ssh-session-on-error branch 2 times, most recently from 0a8ade6 to ee316e7 Compare November 21, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants