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

Catch exception when updating non-existing session #18833

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

zeroSteiner
Copy link
Contributor

@zeroSteiner zeroSteiner commented Feb 13, 2024

Fix #18561 by catching ActiveRecord::RecordNotFound exceptions to prevent them from being printed to the screen. These exceptions are raised when the workspace is deleted before the session is closed.

It shouldn't be necessary to log the exception because it was already logged by:

Testing

  • Open a Meterpreter session
  • Background the Meterpreter session
  • Delete the current workspace by running workspace -D
  • Kill the session by running sessions -K
  • Do not see an exception that is thrown

Demo

msf6 exploit(windows/smb/psexec) > 
[*] 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...
[*] Sending stage (201798 bytes) to 192.168.159.10
[+] 192.168.159.10:445 - Service start timed out, OK if running a command or non-service executable...
[*] Meterpreter session 1 opened (192.168.159.128:4444 -> 192.168.159.10:63450) at 2024-02-13 16:28:08 -0500

msf6 exploit(windows/smb/psexec) > workspace -D
[*] Deleted workspace: default
[*] Recreated the default workspace
msf6 exploit(windows/smb/psexec) > sessions -K
[*] Killing all sessions...
[*] 192.168.159.10 - Meterpreter session 1 closed.
msf6 exploit(windows/smb/psexec) > 

@jheysel-r7 jheysel-r7 self-assigned this Feb 23, 2024
@jheysel-r7
Copy link
Contributor

Thanks for the fix @zeroSteiner! Looks good 👍

Before

msf6 payload(linux/x64/meterpreter/reverse_tcp) > [*] Sending stage (3045380 bytes) to 172.16.199.158
[*] Meterpreter session 1 opened (172.16.199.158:4444 -> 172.16.199.158:34100) at 2024-02-23 10:01:42 -0900
msf6 payload(linux/x64/meterpreter/reverse_tcp) > workspace -D
[*] Deleted workspace: default
[*] Recreated the default workspace
msf6 payload(linux/x64/meterpreter/reverse_tcp) > sessions -K
[*] Killing all sessions...
[*] 172.16.199.158 - Meterpreter session 1 closed.
[-] Session manipulation failed: Couldn't find Mdm::Session with 'id'=103 ["/home/msfuser/.rvm/gems/ruby-3.0.5@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/core.rb:284:in `find'", "/home/msfuser/git/metasploit-framework/lib/msf/core/db_manager/session.rb:194:in `block in update_session'", "/home/msfuser/.rvm/gems/ruby-3.0.5@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:215:in `with_connection'", "/home/msfuser/git/metasploit-framework/lib/msf/core/db_manager/session.rb:191:in `update_session'", "/home/msfuser/git/metasploit-framework/lib/metasploit/framework/data_service/proxy/session_data_proxy.rb:46:in `block in update_session'", "/home/msfuser/git/metasploit-framework/lib/metasploit/framework/data_service/proxy/core.rb:164:in `data_service_operation'", "/home/msfuser/git/metasploit-framework/lib/metasploit/framework/data_service/proxy/session_data_proxy.rb:34:in `update_session'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session.rb:231:in `block in cleanup'", "/home/msfuser/.rvm/gems/ruby-3.0.5@metasploit-framework/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:215:in `with_connection'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session.rb:230:in `cleanup'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session/interactive.rb:102:in `cleanup'", "/home/msfuser/git/metasploit-framework/lib/msf/base/sessions/meterpreter.rb:306:in `cleanup'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session_manager.rb:258:in `deregister'", "/home/msfuser/git/metasploit-framework/lib/msf/base/sessions/meterpreter.rb:368:in `kill'", "/home/msfuser/git/metasploit-framework/lib/msf/ui/console/command_dispatcher/core.rb:1725:in `block in cmd_sessions'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session_manager.rb:196:in `each'", "/home/msfuser/git/metasploit-framework/lib/msf/core/session_manager.rb:196:in `each'", "/home/msfuser/git/metasploit-framework/lib/msf/ui/console/command_dispatcher/core.rb:1717:in `cmd_sessions'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:581:in `run_command'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:530:in `block in run_single'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:524:in `each'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:524:in `run_single'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/shell.rb:165:in `block in run'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/shell.rb:309:in `block in with_history_manager_context'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/shell/history_manager.rb:33:in `with_context'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/shell.rb:306:in `with_history_manager_context'", "/home/msfuser/git/metasploit-framework/lib/rex/ui/text/shell.rb:133:in `run'", "/home/msfuser/git/metasploit-framework/lib/metasploit/framework/command/console.rb:54:in `start'", "/home/msfuser/git/metasploit-framework/lib/metasploit/framework/command/base.rb:82:in `start'", "./msfconsole:23:in `<main>'"]

After

msf6 payload(linux/x64/meterpreter/reverse_tcp) >
[*] Sending stage (3045380 bytes) to 172.16.199.158
[*] Meterpreter session 1 opened (172.16.199.158:4444 -> 172.16.199.158:54592) at 2024-02-23 10:08:05 -0900

msf6 payload(linux/x64/meterpreter/reverse_tcp) > workspace -D
[*] Deleted workspace: default
[*] Recreated the default workspace
msf6 payload(linux/x64/meterpreter/reverse_tcp) > sessions -K
[*] Killing all sessions...
[*] 172.16.199.158 - Meterpreter session 1 closed.

@jheysel-r7 jheysel-r7 merged commit 03e5b36 into rapid7:master Feb 23, 2024
49 checks passed
@jheysel-r7 jheysel-r7 added rn-enhancement release notes enhancement enhancement labels Feb 23, 2024
@jheysel-r7
Copy link
Contributor

jheysel-r7 commented Feb 23, 2024

Release Notes

Fixes a module crash when updating a non-existent session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement rn-enhancement release notes enhancement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Killing sessions crashes after workspace deltion
2 participants