#!/usr/bin/env ruby

################################################################################
#    nikto Me Web?
#  
#    license: GPL 
#    released date: 2016-05-09
#     
#    last updated:  2016-05-09
#
#    (c) Vijay Kumar              
#                                                 
#    This script utilizes the script from the tellmeweb created by 
#    YGN Ethical Hacker Group, Yangon, Myanmar. It modifies it to use
#    nikto on multiple websites
#
#    How it works
#    
#    The niktomeweb takes gnmap outpout (-oG) generated together with -sV option.
#    It takes all hosts with http & https ports open.
#    Then it feeds them into nikto
################################################################################


require 'fileutils'
#load '/opt/niktomeweb/niktomeweb.config'

def print_banner
  puts "\n=============================================================
nikto Me Web? - Automating nikto from NMap Output
=============================================================\n\n"
end


def errmsg(s)
    puts s.to_s
    exit!
end

def check_wwpath()    
    unless File.exist?$niktoweb_location
        puts
        puts '[X]  nikto path not found!'
        puts
        puts '*Configure path to nikto executable in $nikto_location variable in niktoweb.config file'
        exit!
    end     
end
    
def main()

    print_banner()
#    check_wwpath()
      
    errmsg("\nUsage:\nruby #{$0} nmap-out-file-in-gnmap-format\n") if ARGV.length < 1

    nmapout = ARGV[0]
    aggressive = ARGV[1]

    unless File.file?nmapout
       errmsg('[x] File is not found or not valid!')
    end

    ip = ''
    ip_ports = []

    tmp1 = ''
    tmp2 = []
    found_http = 0
    sf = File.new(nmapout,"r")
    furl = []
    while fline = sf.gets
        fu = ''
        
        if fline.length > 1  and fline !~ /^#/ 
        
            if fline =~ /Host: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) \(([^\(^\)].*?)\)\tPorts: ([\S\w\W\s,\/\|\.0-9a-zA-Z\(\)\?\\]+)+/ || fline =~ /Host: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (\(\))\tPorts: ([\S\w\W\s,\/\|\.0-9a-zA-Z\(\)\?\\]+)+/  
                
                ip = $1

                tmp1 = $3            

                tmp2 = []
                tmp2 = fline.scan(/Ports: ([\S\w\W\s,\/\|\.0-9a-zA-Z\(\)\?\\]+)+/)

                tmp3 = ''
                tmp3 = tmp2[0][0].to_s if tmp2.size == 1
                
                tmp4 = tmp3.split(',') if tmp3.length > 1
                tmp4.each do |t|
                    if t =~ /([\d{1,5}]+)\/open\/tcp\/\/http/ 
                        lnk =   ip + ':'  + $1
                        lnk.gsub!("\r\n","")
                        lnk.gsub!("\n","")
                        ip_ports << lnk
                        found_http = 1
                    elsif t =~ /([\d{1,5}]+)\/open\/tcp\/\/https/ 
                        lnk =   ip + ':'  + $1
                        lnk.gsub!("\r\n","")
                        lnk.gsub!("\n","")
                        ip_ports << lnk
                        found_http = 1
                    elsif t =~ /([\d{1,5}]+)\/open\/tcp\/\/ssl\|https/ 
                        lnk =   'https://' + ip + ':'  + $1
                        lnk.gsub!("\r\n","")
                        lnk.gsub!("\n","")
                        ip_ports << lnk
                        found_http = 1
                    end 
                end
                

                
            end
           
        end
        
    end

#Create a niktoweb_logs folder in the current working directory
    wcmd2 = 'mkdir ./niktomeweb_output'
    system(wcmd2)

    if ip_ports.size > 0
        links = []         
        ip_ports.uniq!           
        ip_ports.each do |target|
            if target=~ /:\/\//
                target1   = target
                target1.gsub!("https://","")
                tfn = target1.split(':')
            else
                tfn = target.split(':')
            end
            fn = tfn[0] + '_' + tfn[1] + '.nikto'
            fn.gsub!("http://")
            fn.gsub!("https://")

	    if target.include? ":80"
                   target2 = 'http://' + target
	    end
	    if target.include? ":443"
                   target2 = 'https://' + target
	    end

	    file_path = './niktomeweb_output' + File::SEPARATOR + fn

	    if File.exists?(file_path)
		puts 'exists; skipping'
	    else 

		    wcmd = 'nikto ' + '-Format txt -host ' + target2 + ' -o ./niktomeweb_output' + File::SEPARATOR + fn
		    puts(wcmd)
	            puts '   log file as ' + fn
	            r = system(wcmd) 
	            if r == nil || r == false
	                puts
	                puts '[X]  Error in running nikto saving session command'
	                puts
	                exit!
	            end  
	      end
	            puts
	            puts 
	        end
		    else
	        puts   '[*] No http/https ports found in input file'
	    end
  sleep(3)
puts 
end

if __FILE__ == $0
  main()
end

