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
|
|
|
|
|
2006-08-12 23:08:20 +00:00
|
|
|
register_options(
|
2006-08-12 08:31:38 +00:00
|
|
|
[
|
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)
|
2006-08-12 23:08:20 +00:00
|
|
|
|
|
|
|
# RHOST should not be used in scanner modules, only RHOSTS
|
|
|
|
deregister_options('RHOST')
|
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
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
tl = []
|
|
|
|
|
2006-12-10 06:04:51 +00:00
|
|
|
begin
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
if (self.respond_to?('run_range'))
|
|
|
|
return run_range(datastore['RHOSTS'])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (self.respond_to?('run_host'))
|
|
|
|
ar = Rex::Socket::RangeWalker.new(datastore['RHOSTS'])
|
2007-04-24 06:27:39 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
tl = []
|
|
|
|
|
|
|
|
# Spawn threads for each host
|
|
|
|
while (tl.length < datastore['THREADS'])
|
|
|
|
ip = ar.next_ip
|
|
|
|
break if not ip
|
|
|
|
tl << Thread.new do
|
2009-03-28 05:49:33 +00:00
|
|
|
targ = ip.dup
|
|
|
|
nmod = self.replicant
|
|
|
|
nmod.datastore['RHOST'] = targ
|
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
begin
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod.run_host(nmod.rhost)
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
2009-03-28 05:49:33 +00:00
|
|
|
rescue ::Rex::ConnectionError
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Exception => e
|
2009-03-28 05:49:33 +00:00
|
|
|
print_status("Error: #{targ}: #{e} #{e.backtrace}")
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Exit once we run out of hosts
|
|
|
|
if(tl.length == 0)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for the threads
|
|
|
|
tl.each { |t| t.join }
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if (self.respond_to?('run_batch'))
|
|
|
|
|
|
|
|
if (! self.respond_to?('run_batch_size'))
|
|
|
|
print_status("This module needs to export run_batch_size()")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
size = run_batch_size()
|
|
|
|
|
|
|
|
ar = Rex::Socket::RangeWalker.new(datastore['RHOSTS'])
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
while(true)
|
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
tl = []
|
2007-12-29 17:16:22 +00:00
|
|
|
|
|
|
|
nohosts = false
|
2007-04-24 06:27:39 +00:00
|
|
|
while (tl.length < datastore['THREADS'])
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-12-29 17:16:22 +00:00
|
|
|
|
2007-04-24 06:27:39 +00:00
|
|
|
# Create a thread for each batch
|
|
|
|
if (batch.length > 0)
|
|
|
|
tl << Thread.new do
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod = self.replicant
|
|
|
|
mybatch = batch.dup
|
2007-04-24 06:27:39 +00:00
|
|
|
begin
|
2009-03-28 05:49:33 +00:00
|
|
|
nmod.run_batch(mybatch)
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
2009-03-28 05:49:33 +00:00
|
|
|
rescue ::Rex::ConnectionError
|
2007-04-24 06:27:39 +00:00
|
|
|
rescue ::Exception => e
|
2009-03-28 05:49:33 +00:00
|
|
|
print_status("Error: #{mybatch[0]}-#{mybatch[-1]}: #{e}")
|
2007-04-24 06:27:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Wait for the threads
|
|
|
|
tl.each { |t| t.join }
|
2007-12-29 17:16:22 +00:00
|
|
|
|
|
|
|
# Exit if there are no more pending threads
|
|
|
|
if (tl.length == 0)
|
|
|
|
break
|
|
|
|
end
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
return
|
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
print_status("This module defined no run_host or run_range methods")
|
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
|
2007-04-24 06:27:39 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-11-03 09:17:08 +00:00
|
|
|
end
|