Path: blob/master/modules/post/multi/gather/ping_sweep.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Multi Gather Ping Sweep',12'Description' => %q{ Performs IPv4 ping sweep using the OS included ping command.},13'License' => MSF_LICENSE,14'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],15'Platform' => %w[bsd linux osx solaris win],16'SessionTypes' => [ 'meterpreter', 'shell' ],17'Notes' => {18'Stability' => [CRASH_SAFE],19'SideEffects' => [],20'Reliability' => []21}22)23)24register_options(25[26OptAddressRange.new('RHOSTS', [true, 'IP Range to perform ping sweep against.']),27]28)29end3031# Run Method for when run command is issued32def run33iprange = datastore['RHOSTS']34print_status("Performing ping sweep for IP range #{iprange}")35iplst = []36begin37ipadd = Rex::Socket::RangeWalker.new(iprange)38numip = ipadd.num_ips39while (iplst.length < numip)40ipa = ipadd.next_ip41if !ipa42break43end4445iplst << ipa46end4748case session.platform49when 'windows'50count = ' -n 1 '51cmd = 'ping'52when 'solaris'53cmd = '/usr/sbin/ping'54else55count = ' -n -c 1 -W 2 '56cmd = 'ping'57end5859ip_found = []6061while !iplst.nil? && !iplst.empty?62a = []631.upto session.max_threads do64a << framework.threads.spawn("Module(#{refname})", false, iplst.shift) do |ip_add|65next if ip_add.nil?6667if session.platform =~ /solaris/i68r = cmd_exec(cmd, "-n #{ip_add} 1")69else70r = cmd_exec(cmd, count + ip_add)71end72if r =~ /(TTL|Alive)/i73print_good "\t#{ip_add} host found"74ip_found << ip_add75else76vprint_status("\t#{ip_add} host not found")77end78end79end80a.map(&:join)81end82rescue Rex::TimeoutError, Rex::Post::Meterpreter::RequestError83vprint_error(e.message)84rescue StandardError => e85print_status("The following error was encountered: #{e.class} #{e}")86end8788ip_found.each do |ip|89report_host(host: ip)90end91end92end939495