ronin-rb/ronin-exploits

View on GitHub
lib/ronin/exploits/cli/exploit_methods.rb

Summary

Maintainability
A
45 mins
Test Coverage
# frozen_string_literal: true
#
# ronin-exploits - A Ruby library for ronin-rb that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-exploits is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-exploits is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-exploits.  If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/exploits/registry'
require 'ronin/exploits/exceptions'
require 'ronin/core/params/exceptions'

module Ronin
  module Exploits
    class CLI
      #
      # Mixin which adds methods for loading and running exploit classes.
      #
      module ExploitMethods
        #
        # Loads a exploit class.
        #
        # @param [String] name
        #   The exploit name to load.
        #
        # @return [Class<Exploit>]
        #   The loaded exploit class.
        #
        def load_exploit(name)
          Exploits.load_class(name)
        rescue Exploits::ClassNotFound => error
          print_error(error.message)
          exit(1)
        rescue => error
          print_exception(error)
          print_error("an unhandled exception occurred while loading exploit #{name}")
          exit(-1)
        end

        #
        # Loads the exploit from a given file.
        #
        # @param [String] file
        #   The file to load the exploit class from.
        #
        # @return [Class<Exploit>]
        #   The loaded exploit class.
        #
        def load_exploit_from(file)
          Exploits.load_class_from_file(file)
        rescue Exploits::ClassNotFound => error
          print_error(error.message)
          exit(1)
        rescue => error
          print_exception(error)
          print_error("an unhandled exception occurred while loading exploit from file #{file}")
          exit(-1)
        end

        #
        # Initializes the exploit class.
        #
        # @param [Class<Exploit>] exploit_class
        #   The encoder class.
        #
        # @param [Hash{Symbol => Object}] kwargs
        #   Additional keyword arguments for {Exploit#initialize}.
        #
        # @return [Exploit]
        #   The initialized exploit object.
        #
        def initialize_exploit(exploit_class,**kwargs)
          exploit_class.new(**kwargs)
        rescue Core::Params::ParamError => error
          print_error(error.message)
          exit(1)
        rescue => error
          print_exception(error)
          print_error("an unhandled exception occurred while initializing exploit #{exploit_class.id}")
          exit(-1)
        end

        #
        # Validates the loaded exploit.
        #
        # @param [Exploit] exploit
        #   The exploit to validate.
        #
        # @raise [Ronin::Core::Params::RequiredParam]
        #   One of the required params was not set.
        #
        # @raise [ValidationError]
        #   Another exploit validation error occurred.
        #
        def validate_exploit(exploit)
          exploit.perform_validate
        rescue Core::Params::ParamError, ValidationError => error
          print_error("failed to validate the exploit #{exploit.class_id}: #{error.message}")
          exit(1)
        rescue => error
          print_exception(error)
          print_error("an unhandled exception occurred while validating the exploit #{exploit.class_id}")
          exit(-1)
        end
      end
    end
  end
end