-
Notifications
You must be signed in to change notification settings - Fork 14.4k
fix(post/windows/manage/remove_host): handle whitespace parsing robustly #20273
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
fix(post/windows/manage/remove_host): handle whitespace parsing robustly #20273
Conversation
This seems like it fixes the problem that was originally reported where it wasn't matching lines with spaces. Your edge cases do highlight what I would imagine is a separate bug entirely.
Based on the module description:
And the description of the Would you be up for fixing that too or should I file another bug report for it? |
I will fix that too! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to go through and verify the issue has been fixed. Thanks for your work on this.
msf6 post(windows/manage/remove_host) > set DOMAIN testnet1Interrupt: use the 'exit' command to quit
msf6 post(windows/manage/remove_host) > show options
Module options (post/windows/manage/remove_host):
Name Current Setting Required Description
---- --------------- -------- -----------
DOMAIN vmware yes Domain name to remove from the hosts file.
SESSION -1 yes The session to run this module on
View the full module info with the info, or info -d command.
msf6 post(windows/manage/remove_host) > set DOMAIN testnet1
DOMAIN => testnet1
msf6 post(windows/manage/remove_host) > run
[*] Removing hosts file entry pointing to testnet1
[+] Done!
[*] Post module execution completed
msf6 post(windows/manage/remove_host) > set DOMAIN testnet2
DOMAIN => testnet2
msf6 post(windows/manage/remove_host) > run
[*] Removing hosts file entry pointing to testnet2
[+] Done!
[*] Post module execution completed
msf6 post(windows/manage/remove_host) > show options
Module options (post/windows/manage/remove_host):
Name Current Setting Required Description
---- --------------- -------- -----------
DOMAIN testnet2 yes Domain name to remove from the hosts file.
SESSION -1 yes The session to run this module on
View the full module info with the info, or info -d command.
msf6 post(windows/manage/remove_host) > run
[*] Removing hosts file entry pointing to testnet2
[+] Done!
[*] Post module execution completed
msf6 post(windows/manage/remove_host) > run
[*] Removing hosts file entry pointing to testnet2
[+] Done!
[*] Post module execution completed
msf6 post(windows/manage/remove_host) >
Release NotesThis fixes multiple issues in the |
"multiple issues" is more appropriate. It fixes far more than two issues. I count at least 4:
|
FWIW, it looks like there's still an issue that'll come up if the C: isn't the system root since we're not pulling it from the environment. |
@smcintyre-r7 i have seen this as well. I could create a new issue for that and would add a fix to modify the hosts file on a variable path. Like this: %SystemRoot%\System32\drivers\etc\hosts |
See #20157
This PR fixes a bug in the
post/windows/manage/remove_host
module that caused it to incorrectly assume hosts file entries use only tab-separated fields. In reality, Windows hosts files can use any type of whitespace (spaces, tabs, or mixed) and may include trailing whitespace or inline comments.The original logic only matched tab-separated entries, ignoring entries with spaces or trailing whitespace, and did not handle inline comments correctly. Additionally, because it used regex matching, it could (in rare cases) remove unintended entries if the specified domain contained regex-like characters (e.g.,
.
,*
).This fix updates the parsing logic to:
Example hosts file
Below is an example hosts file with various edge cases that this PR correctly handles:
If you run the module with:
set DOMAIN asdf
it will remove only these lines:
while leaving all other lines untouched, including:
If you run the module with:
set DOMAIN shop.ebay.com
it will remove only this line:
while leaving all other lines untouched, including:
Verification
To test the fix:
msfconsole
use post/windows/manage/remove_host
set DOMAIN <target host name>
set SESSION <session ID>
run
shop4ebay.com
vs.shop.ebay.com
), comments, and unrelated entries remain untouchedScreenshots:
Before
Afterwards
Thanks for reviewing!