Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/msf/ui/console/command_dispatcher/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def set_rhosts_from_addrs(rhosts)
# create the output file and assign it to the RHOSTS variable
rhosts_file.write(rhosts.join("\n")+"\n")
rhosts_file.close
# Keep a reference so Ruby's GC doesn't finalize and unlink the temp file
# before a module has a chance to read it.
@persisted_rhosts_files ||= []
@persisted_rhosts_files << rhosts_file
Comment on lines +92 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we had a chance to look through the tempfile API a bit and what we noticed is that when the file is created in this way, it's automatically removed by the garbage collector. If however, it's created using the .create method, it will not automatically delete the file.

That means that instead of keeping a reference here, we should be able to achieve the same fix by adjusting L87 from rhosts_file = Rex::Quickfile.new("msf-db-rhosts-") to rhosts_file = Rex::Quickfile.create("msf-db-rhosts-"). GitHub won't let me suggest changes on lines you didn't write. With that in place, I'd then remove these lines since we don't need to explicitly keep a reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with "create" is that the file is not removed once the Metasploit console is exited, so the temp file will stay on the filesystem. Is this something we want?

else
# For short lists, just set it directly
mydatastore['RHOSTS'] = rhosts.join(" ")
Expand Down