fix(console): prevent RHOSTS temp file deletion after services -R #20599
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: RHOSTS temp file deleted before module run when using
services ... -R
Summary
Using
services -p <ports> -u -R
could setRHOSTS
to afile:/tmp/msf-db-rhosts-*
path that vanished immediately upon running a module. This resulted in modules starting with an empty or ineffective target list. The root cause was that the temporary file was created viaRex::Quickfile
inset_rhosts_from_addrs
and the only Ruby reference to the object went out of scope. Under recent Ruby / Rex behavior, the file was unlinked when the object was garbage collected (often triggered right as a module initialized and allocated additional objects).Root Cause
set_rhosts_from_addrs
wrote the host list to aRex::Quickfile
, closed it, and returned without retaining a reference. IfRex::Quickfile
(or an underlying implementation analogous toTempfile
) registers a finalizer that unlinks the file, the OS file disappeared as soon as GC (garbage collector) finalized the object. GC frequently occurred when a module was launched due to new allocations, making the deletion appear to be caused by the module itself.Fix Implemented
We now retain references to created
Rex::Quickfile
instances in an instance variable@persisted_rhosts_files
, preventing garbage collection (and therefore unlink) for the lifetime of the console process. For small host lists (≤5 hosts), behavior is unchanged: the list is stored inline in theRHOSTS
datastore.Test Coverage
Note on Reproduction via
db_import
The issue is most easily observed after importing a larger host/service set from an Nmap XML file using
db_import <scan.xml>
. Imported scans commonly contain more than five hosts, causingset_rhosts_from_addrs
to choose the temp file path variant (file:/tmp/msf-db-rhosts-*
). More you have larger host/service more you have chance to trigger the garbage collector at the run.From namp scan:
Or create many fake hosts/services ( > 60 ):
On bash:
Note the displayed file path (e.g.
file:/tmp/msf-db-rhosts-...
).The file has been removed and the module didn't scan the targets. Because the garbage collector clean the file.
Apply the fix and rerun the step 1 and 2 above you will see that the scan run properly and the file is not clean by the GC.