#
# search exploit (db-exploit)
#

require 'fileutils'
require 'optparse'

module Msf
class Plugin::DB_Exploit < Msf::Plugin
  class DB_ExploitCommandDispatcher
    include Msf::Ui::Console::CommandDispatcher

    def name
      "DB-Exploit"
    end

    def commands
      {
        'db_exploit_search' => "Search Exploits",
        'db_exploit_import' => "Import exploit to Msf",
        'db_exploit_help' => "Displays help",
      }
    end

    def cmd_openvas_help()
      print_status("db_exploit_help                   Display this help")
      print_status("db_exploit_search                 Search exploit")
      print_status("db_exploit_import                 Import exploit to Msf")
    end

    # Verify the database is connected and usable
    def database?
      if !(framework.db and framework.db.usable)
        return false
      else
        return true
      end
    end

    # Verify there is an active Searchsploit connection
    def searchsploit?
      if @ov
        return true
      else
        print_error("Searchsploit is not installed. Please install searchsploit.")
        return false
      end
    end

    def args?(args, min=1, max=nil)
      if not max then max = min end
      if (args.length < min or args.length > max or args[0] == "-h")
        return false
      end

      return true
    end

    def cmd_db_exploit_search(*args)
      search = args.join(" ")
      output = `searchsploit #{search}`
      puts "#{output}"
    end

    def cmd_db_exploit_import(*args)
      path = "#{Dir.home}s/.msf4/modules/exploits/"  
      path += File.dirname("#{args[0]}")
      FileUtils.mkdir_p(path) unless File.exists?(path)   
      system ("cp /opt/exploit-database/platforms/#{args[0]} #{Dir.home}/.msf4/modules/exploits/#{args[0]}")  
      print_status("Exploit imported, relad Metasploit!")
    end

  end # End SearchSploit class

#------------------------------
# Plugin initialization
#------------------------------

  def initialize(framework, opts)
    super
    add_console_dispatcher(DB_ExploitCommandDispatcher)
    print_status("Welcome to Searchsploit integration to Metasploit.")
    print_status
    print_status("DB_Exploit integration requires a Searchsploit.")
    print_status("For additional commands use db_exploit_help.")
    print_status
    @ov = nil
    @formats = nil
    @debug = nil
  end

  def cleanup
    remove_console_dispatcher('DB-Exploit')
  end

  def name
    "DB-Exploit"
  end

  def desc
    "Integrates with the Searchsploit - open source exploit database"
  end
end
end