Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/multi/recon/reverse_lookup.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Common7include Msf::Exploit::Deprecated8moved_from 'post/windows/gather/reverse_lookup'9moved_from 'post/windows/recon/resolve_ip'1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Reverse Lookup IP Addresses',16'Description' => %q{17This module reverse resolves an IP address or IP address range to hostnames.18},19'License' => MSF_LICENSE,20'Author' => [ 'mubix' ],21'Platform' => %w[win unix linux osx solaris bsd],22'SessionTypes' => %w[meterpreter powershell shell],23'Notes' => {24'Stability' => [CRASH_SAFE],25'Reliability' => [],26'SideEffects' => []27},28'Compat' => {29'Meterpreter' => {30'Commands' => %w[31stdapi_railgun_api32stdapi_railgun_memread33]34}35}36)37)38register_options([39OptAddress.new('ADDRESS', [ false, 'IP address to resolve']),40OptAddressRange.new('RANGE', [ false, 'IP address range to resolve'])41])42end4344def resolve_ip(ip)45return unless Rex::Socket.dotted_ip?(ip)4647session.platform == 'windows' ? win_resolve_ip(ip) : unix_resolve_ip(ip)48end4950def unix_resolve_ip(ip)51return unless Rex::Socket.dotted_ip?(ip)5253res = cmd_exec("host #{ip}")5455return if res.blank?5657hostname = res.split("\n").first.split(' ').last5859if hostname.blank? || !hostname.ends_with?('.')60print_error("Failed to resolve #{ip}")61return62end6364hostname.chomp('.')65end6667def win_resolve_ip(ip)68return unless Rex::Socket.dotted_ip?(ip)6970case session.type71when 'powershell'72host = cmd_exec("[System.Net.Dns]::GetHostEntry('#{ip}').HostName").to_s7374if host.blank?75print_error("Failed to resolve #{ip}")76return77end7879return host80when 'meterpreter'81ip_ino = Rex::Socket.addr_aton(ip)8283result = client.railgun.ws2_32.gethostbyaddr(ip_ino, ip_ino.size, 2)8485if result.blank? || result['return'] == 086print_error("Failed to resolve #{ip}")87return88end8990memtext = client.railgun.memread(result['return'], 255)9192unless memtext.include?(ip_ino)93print_error("Failed to resolve #{ip}")94return95end9697host = memtext.split(ip_ino)[1].split("\00")[0]9899if host.blank?100print_error("Failed to resolve #{ip}")101return102end103104return host105else106fail_with(Failure::BadConfig, "Unsupported sesssion type #{session.type}")107end108rescue Rex::Post::Meterpreter::RequestError, Errno::ETIMEDOUT109print_error("Failed to resolve #{ip}")110nil111end112113def run114address = datastore['ADDRESS']115range = datastore['RANGE']116117fail_with(Failure::BadConfig, 'ADDRESS or RANGE option must be set.') if address.blank? && range.blank?118119if session.platform == 'windows'120if session.type == 'meterpreter'121# Add ws2_32 just in case it isn't there...122session.railgun.ws2_32123124# Check if gethostbyaddr is available125modhandle = session.railgun.kernel32.GetModuleHandleA('ws2_32.dll')126if modhandle['return'] == 0127fail_with(Failure::Unknown, 'WS2_32 is not available at this time, exiting')128end129130procaddr = session.railgun.kernel32.GetProcAddress(modhandle['return'], 'gethostbyaddr')131if procaddr['return'] == 0132fail_with(Failure::Unknown, 'WS2_32 was loaded but does not have the gethostbyaddr function, exiting')133end134end135else136fail_with(Failure::NoTarget, "`host' command not found") unless command_exists?('host')137end138139unless address.blank?140print_status("Resolving #{address}")141host = resolve_ip(address)142print_good("#{address} resolves to #{host}") unless host.blank?143end144145unless range.blank?146rex_range = Rex::Socket::RangeWalker.new(range)147print_status("Resolving #{range} (#{rex_range.num_ips} hosts)")148rex_range.each do |ip|149host = resolve_ip(ip)150print_good("#{ip} resolves to #{host}") unless host.blank?151end152end153end154end155156157