2006-08-12 08:31:38 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2006-08-12 23:08:20 +00:00
|
|
|
# This module provides methods for scanning modules
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
module Auxiliary::Scanner
|
|
|
|
|
2006-08-12 23:08:20 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
|
|
|
# Initializes an instance of a recon auxiliary module
|
|
|
|
#
|
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
2009-10-26 03:49:07 +00:00
|
|
|
register_options([
|
2006-08-12 23:08:20 +00:00
|
|
|
OptAddressRange.new('RHOSTS', [ true, "The target address range or CIDR identifier"]),
|
2007-04-24 06:27:39 +00:00
|
|
|
OptInt.new('THREADS', [ true, "The number of concurrent threads", 1 ] )
|
2006-08-12 08:31:38 +00:00
|
|
|
], Auxiliary::Scanner)
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 23:08:20 +00:00
|
|
|
# RHOST should not be used in scanner modules, only RHOSTS
|
|
|
|
deregister_options('RHOST')
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-10-26 03:49:07 +00:00
|
|
|
register_advanced_options([
|
|
|
|
OptBool.new('ShowProgress', [true, 'Display progress messages during a scan', true]),
|
|
|
|
OptInt.new('ShowProgressPercent', [true, 'The interval in percent that progress should be shown', 10])
|
|
|
|
], Auxiliary::Scanner)
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
2006-08-12 23:08:20 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
|
|
|
# The command handler when launched from the console
|
|
|
|
#
|
|
|
|
def run
|
2006-12-10 06:04:51 +00:00
|
|
|
|
2009-10-26 03:49:07 +00:00
|
|
|
@show_progress = datastore['ShowProgress']
|
|
|
|
@show_percent = datastore['ShowProgressPercent'].to_i
|
|
|
|
|
|
|
|
ar = Rex::Socket::RangeWalker.new(datastore['RHOSTS'])
|
2009-12-04 19:37:14 +00:00
|
|
|
@range_count = ar.length
|
2009-10-26 03:49:07 +00:00
|
|
|
@range_done = 0
|
|
|
|
@range_percent = 0
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-10-26 03:49:07 +00:00
|
|
|
threads_max = datastore['THREADS'].to_i
|
2007-04-24 06:27:39 +00:00
|
|
|
tl = []
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-06-19 21:46:40 +00:00
|
|
|
#
|
|
|
|
# Sanity check threading on different platforms
|
|
|
|
#
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-06-19 21:46:40 +00:00
|
|
|
if(Rex::Compat.is_windows)
|
2010-05-01 16:26:24 +00:00
|
|
|
if(threads_max > 32)
|
2009-06-19 21:46:40 +00:00
|
|
|
print_error("Warning: The Windows platform cannot reliably support more than 16 threads")
|
|
|
|
print_error("Thread count has been adjusted to 16")
|
2010-05-01 16:26:24 +00:00
|
|
|
threads_max = 32
|
2009-06-19 21:46:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if(Rex::Compat.is_cygwin)
|
|
|
|
if(threads_max > 200)
|
|
|
|
print_error("Warning: The Cygwin platform cannot reliably support more than 200 threads")
|
|
|
|
print_error("Thread count has been adjusted to 200")
|
|
|
|
threads_max = 200
|
|
|
|
end
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-12-10 06:04:51 +00:00
|
|
|
begin
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
if (self.respond_to?('run_range'))
|
2009-10-26 03:49:07 +00:00
|
|
|
# No automated progress reporting for run_range
|
2006-08-12 08:31:38 +00:00
|
|
|
return run_range(datastore['RHOSTS'])
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
if (self.respond_to?('run_host'))
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-07-16 02:27:18 +00:00
|
|
|
tl = []
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2009-07-16 02:27:18 +00:00
|
|
|
while (true)
|
2007-04-24 06:27:39 +00:00
|
|
|
# Spawn threads for each host
|
2009-06-19 21:46:40 +00:00
|
|
|
while (tl.length < threads_max)
|
2007-04-24 06:27:39 +00:00
|
|
|
ip = ar.next_ip
|
|
|
|
break if not ip
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-05-29 03:18:00 +00:00
|
|
|
tl << Thread.new(ip.dup) do |tip|
|
|
|
|
targ = tip
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod = self.replicant
|
|
|
|
nmod.datastore['RHOST'] = targ
|
2009-07-16 02:27:18 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
begin
|
2009-03-29 03:11:56 +00:00
|
|
|
nmod.run_host(targ)
|
2010-04-06 02:05:50 +00:00
|
|
|
rescue ::Rex::ConnectionError, ::Rex::ConnectionProxyError, ::Errno::ECONNRESET, ::Errno::EINTR, ::Rex::TimeoutError, ::Timeout::Error
|
2009-10-18 18:03:02 +00:00
|
|
|
rescue ::Interrupt,::NoMethodError, ::RuntimeError, ::ArgumentError, ::NameError
|
2007-04-24 06:27:39 +00:00
|
|
|
raise $!
|
|
|
|
rescue ::Exception => e
|
2009-10-18 18:03:02 +00:00
|
|
|
print_status("Error: #{targ}: #{e.class} #{e.message}")
|
2009-05-21 23:37:07 +00:00
|
|
|
elog("Error running against host #{targ}: #{e.message}\n#{e.backtrace.join("\n")}")
|
2010-05-01 16:26:24 +00:00
|
|
|
ensure
|
|
|
|
nmod.cleanup
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
# Exit once we run out of hosts
|
|
|
|
if(tl.length == 0)
|
|
|
|
break
|
|
|
|
end
|
2009-07-16 02:27:18 +00:00
|
|
|
|
|
|
|
# Assume that the oldest thread will be one of the
|
|
|
|
# first to finish and wait for it. After that's
|
|
|
|
# done, remove any finished threads from the list
|
|
|
|
# and continue on. This will open up at least one
|
|
|
|
# spot for a new thread
|
2009-10-26 03:49:07 +00:00
|
|
|
tla = tl.length
|
2009-07-16 02:27:18 +00:00
|
|
|
tl.first.join
|
|
|
|
tl.delete_if { |t| not t.alive? }
|
2009-10-26 03:49:07 +00:00
|
|
|
tlb = tl.length
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-10-26 03:49:07 +00:00
|
|
|
@range_done += tla - tlb
|
|
|
|
scanner_show_progress()
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if (self.respond_to?('run_batch'))
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
if (! self.respond_to?('run_batch_size'))
|
|
|
|
print_status("This module needs to export run_batch_size()")
|
|
|
|
return
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
size = run_batch_size()
|
|
|
|
|
|
|
|
ar = Rex::Socket::RangeWalker.new(datastore['RHOSTS'])
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-07-16 02:27:18 +00:00
|
|
|
tl = []
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
while(true)
|
2009-11-17 12:45:34 +00:00
|
|
|
nohosts = false
|
2009-06-19 21:46:40 +00:00
|
|
|
while (tl.length < threads_max)
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
batch = []
|
|
|
|
|
|
|
|
# Create batches from each set
|
|
|
|
while (batch.length < size)
|
|
|
|
ip = ar.next_ip
|
2007-12-29 17:16:22 +00:00
|
|
|
if (not ip)
|
|
|
|
nohosts = true
|
|
|
|
break
|
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
batch << ip
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
# Create a thread for each batch
|
|
|
|
if (batch.length > 0)
|
2009-11-13 20:28:36 +00:00
|
|
|
thread = Thread.new(batch) do |bat|
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod = self.replicant
|
2009-07-16 00:20:54 +00:00
|
|
|
mybatch = bat.dup
|
2007-04-24 06:27:39 +00:00
|
|
|
begin
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod.run_batch(mybatch)
|
2010-04-06 02:05:50 +00:00
|
|
|
rescue ::Rex::ConnectionError, ::Rex::ConnectionProxyError, ::Errno::ECONNRESET, ::Errno::EINTR, ::Rex::TimeoutError, ::Timeout::Error
|
2009-10-18 18:03:02 +00:00
|
|
|
rescue ::Interrupt,::NoMethodError, ::RuntimeError, ::ArgumentError, ::NameError
|
2007-04-24 06:27:39 +00:00
|
|
|
raise $!
|
|
|
|
rescue ::Exception => e
|
2009-03-28 05:49:33 +00:00
|
|
|
print_status("Error: #{mybatch[0]}-#{mybatch[-1]}: #{e}")
|
2010-05-01 16:26:24 +00:00
|
|
|
ensure
|
|
|
|
nmod.cleanup
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
thread[:batch_size] = batch.length
|
2009-11-13 20:28:36 +00:00
|
|
|
tl << thread
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
# Exit once we run out of hosts
|
2007-12-29 17:16:22 +00:00
|
|
|
if (tl.length == 0 or nohosts)
|
2007-04-24 06:27:39 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2007-12-29 17:16:22 +00:00
|
|
|
|
|
|
|
# Exit if there are no more pending threads
|
|
|
|
if (tl.length == 0)
|
|
|
|
break
|
2009-11-17 12:45:34 +00:00
|
|
|
end
|
2009-07-16 02:27:18 +00:00
|
|
|
|
|
|
|
# Assume that the oldest thread will be one of the
|
|
|
|
# first to finish and wait for it. After that's
|
|
|
|
# done, remove any finished threads from the list
|
|
|
|
# and continue on. This will open up at least one
|
|
|
|
# spot for a new thread
|
2009-10-26 03:49:07 +00:00
|
|
|
tla = 0
|
|
|
|
tl.map {|t| tla += t[:batch_size] }
|
2009-07-16 02:27:18 +00:00
|
|
|
tl.first.join
|
|
|
|
tl.delete_if { |t| not t.alive? }
|
2009-10-26 03:49:07 +00:00
|
|
|
tlb = 0
|
|
|
|
tl.map {|t| tlb += t[:batch_size] }
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2009-10-26 03:54:44 +00:00
|
|
|
@range_done += tla - tlb
|
2009-11-17 12:45:34 +00:00
|
|
|
scanner_show_progress()
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
return
|
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2009-04-10 04:05:05 +00:00
|
|
|
print_error("This module defined no run_host, run_range or run_batch methods")
|
2009-11-17 12:45:34 +00:00
|
|
|
|
2006-12-10 06:04:51 +00:00
|
|
|
rescue ::Interrupt
|
|
|
|
print_status("Caught interrupt from the console...")
|
|
|
|
return
|
2007-04-24 06:27:39 +00:00
|
|
|
ensure
|
|
|
|
tl.each do |t|
|
|
|
|
begin
|
|
|
|
t.kill
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
2006-12-10 06:04:51 +00:00
|
|
|
end
|
2009-10-26 03:49:07 +00:00
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2010-01-14 18:20:32 +00:00
|
|
|
def scanner_progress
|
2009-10-26 03:49:07 +00:00
|
|
|
pct = (@range_done / @range_count.to_f) * 100
|
2010-01-14 18:20:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def scanner_show_progress
|
|
|
|
pct = scanner_progress
|
2009-10-26 03:49:07 +00:00
|
|
|
if(pct >= (@range_percent + @show_percent))
|
|
|
|
@range_percent = @range_percent + @show_percent
|
|
|
|
tdlen = @range_count.to_s.length
|
|
|
|
print_status("Scanned #{"%.#{tdlen}d" % @range_done} of #{@range_count} hosts (#{"%.3d" % pct.to_i}% complete)")
|
|
|
|
end
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-11-03 09:17:08 +00:00
|
|
|
end
|
2009-11-17 12:45:34 +00:00
|
|
|
|