Skip to content

Commit

Permalink
Fixes in threading and for Java Meterpreter on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
darkoperator committed Jul 23, 2012
1 parent e200f43 commit cdee09b
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions modules/post/multi/gather/ping_sweep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(info={})
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
'Version' => '$Revision$',
'Platform' => [ 'windows','linux', 'osx', 'bsd', 'solaris' ],
'SessionTypes' => [ 'meterpreter','shell' ]
'SessionTypes' => [ 'meterpreter', 'shell' ]
))
register_options(
[
Expand All @@ -40,11 +40,10 @@ def initialize(info={})
# Run Method for when run command is issued
def run
iprange = datastore['RHOSTS']
found_hosts = []
print_status("Performing ping sweep for IP range #{iprange}")
iplst = []
begin
i, a = 0, []
a = []
ipadd = Rex::Socket::RangeWalker.new(iprange)
numip = ipadd.num_ips
while (iplst.length < numip)
Expand All @@ -57,52 +56,56 @@ def run
if session.type =~ /shell/
# Only one thread possible when shell
thread_num = 1
# Use the shell platform for selecting the command
platform = session.platform
else
# When in Meterpreter the safest thread number is 10
thread_num = 10
# For Meterpreter use the sysinfo OS since java Meterpreter returns java as platform
platform = session.sys.config.sysinfo['OS']
end

ip_found = []

iplst.each do |ip|
# Set count option for ping command
case session.platform
when /win/i
count = " -n 1 " + ip
cmd = "ping"
when /solaris/i
count = " #{ip} 1"
cmd = "/bin/ping"
else
count = " -c 1 #{ip}"
cmd = "/bin/ping"
end
platform = session.platform

if i <= thread_num
a.push(::Thread.new {
r = cmd_exec(cmd, count)
if r =~ /(TTL|Alive)/i
print_status "\t#{ip.inspect} host found"
ip_found << ip
else
vprint_status("\t#{ip} host not found")
end

})
i += 1
else
sleep(0.5) and a.delete_if {|x| not x.alive?} while not a.empty?
i = 0
end
case platform
when /win/i
count = " -n 1 "
cmd = "ping"
when /solaris/i
cmd = "/usr/sbin/ping"
else
count = " -n -c 1 -W 2 "
cmd = "ping"
end
a.delete_if {|x| not x.alive?} while not a.empty?

ip_found = []

while(not iplst.nil? and not iplst.empty?)
1.upto(thread_num) do
a << framework.threads.spawn("Module(#{self.refname})", false, iplst.shift) do |ip_add|
next if ip_add.nil?
if platform =~ /solaris/i
r = cmd_exec(cmd, "-n #{ip_add} 1")
else
r = cmd_exec(cmd, count + ip_add)
end
if r =~ /(TTL|Alive)/i
print_status "\t#{ip_add} host found"
ip_found << ip_add
else
vprint_status("\t#{ip_add} host not found")
end

end
a.map {|x| x.join }
end
end
rescue ::Exception => e
print_status("The following Error was encountered: #{e.class} #{e}")

end
ip_found.each do |i|
report_host(:host => i)

ip_found.each do |ip|
report_host(:host => ip)
end
end
end

0 comments on commit cdee09b

Please sign in to comment.