Skip to content

fix: handle ActiveRecord::RecordInvalid in ssh_creds when workspace is reset#21536

Open
4ravind-b wants to merge 1 commit into
rapid7:masterfrom
4ravind-b:fix/ssh-creds-workspace-reset-crash
Open

fix: handle ActiveRecord::RecordInvalid in ssh_creds when workspace is reset#21536
4ravind-b wants to merge 1 commit into
rapid7:masterfrom
4ravind-b:fix/ssh-creds-workspace-reset-crash

Conversation

@4ravind-b
Copy link
Copy Markdown
Contributor

Fixes #21480

When workspace -D is run while a session is active, the session's
database record gets deleted but the session itself stays alive. When
post/multi/gather/ssh_creds tries to save credentials, it fails with
ActiveRecord::RecordInvalid: Session can't be blank and crashes the module.

This fix adds a rescue for ActiveRecord::RecordInvalid so the module
prints a warning and continues instead of crashing.

Verification

  • Start msfconsole
  • Get a session on target
  • Run post/multi/gather/ssh_creds — works fine
  • Run workspace -D
  • Run post/multi/gather/ssh_creds again
  • Verify module prints a warning instead of crashing
  • Verify files are still downloaded successfully

@4ravind-b
Copy link
Copy Markdown
Contributor Author

Verified on Metasploitable2.

Before this fix: Running the module after deleting the workspace with workspace -D caused the module to crash and print a full stack trace.

After this fix:

msf post(multi/gather/ssh_creds) > workspace -D
[*] Deleted workspace: default
[*] Recreated the default workspace

msf post(multi/gather/ssh_creds) > run
[*] Finding .ssh directories
[*] Looting 3 .ssh directories
[*] Looting /home/msfadmin/.ssh directory
[+] Downloaded /home/msfadmin/.ssh/authorized_keys -> ...
[+] Downloaded /home/msfadmin/.ssh/id_rsa -> ...
[!] Could not save credential to database: Validation failed: Session can't be blank
[+] Downloaded /home/msfadmin/.ssh/id_rsa.pub -> ...
[*] Looting /home/user/.ssh directory
[+] Downloaded /home/user/.ssh/id_dsa -> ...
[!] Could not save credential to database: Validation failed: Session can't be blank
[*] Looting /root/.ssh directory
[+] Downloaded /root/.ssh/authorized_keys -> ...
[*] Post module execution completed

The module no longer crashes. Instead, it prints a warning when credential storage fails and continues processing the remaining files. All SSH files are successfully downloaded, post-module execution completes normally, and the session remains active.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

post/multi/gather/ssh_creds & workspace -D = crash

2 participants