ronin-rb/ronin-exploits

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

Summary

Maintainability
A
0 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/cli/command'
require 'ronin/exploits/cli/exploit_methods'

module Ronin
  module Exploits
    class CLI
      #
      # Base class for all commands which load or run exploits.
      #
      class ExploitCommand < Command

        include ExploitMethods

        usage '[options] {NAME | -f FILE}'

        option :file, short: '-f',
                      value: {
                        type: String,
                        usage: 'FILE'
                      },
                      desc: 'The exploit file to load'

        argument :name, required: false,
                        desc:     'The exploit name to load'

        # The loaded exploit class.
        #
        # @return [Class<Exploit>, nil]
        attr_reader :exploit_class

        # The initialized exploit object.
        #
        # @return [Exploit, nil]
        attr_reader :exploit

        #
        # Loads the exploit.
        #
        # @param [String, nil] name
        #   The optional exploit name to load.
        #
        def run(name=nil)
          if    name           then load_exploit(name)
          elsif options[:file] then load_exploit_from(options[:file])
          else
            print_error "must specify --file or a NAME"
            exit(-1)
          end
        end

        #
        # Loads the exploit and sets {#exploit_class}.
        #
        # @param [String] id
        #   The exploit name to load.
        #
        def load_exploit(id)
          @exploit_class = super(id)
        end

        #
        # Loads the exploit from the given file and sets {#exploit_class}.
        #
        # @param [String] file
        #   The file to load the exploit from.
        #
        def load_exploit_from(file)
          @exploit_class = super(file)
        end

        #
        # Initializes the exploit and sets {#exploit}.
        #
        # @param [Hash{Symbol => Object}] kwargs
        #   Additional keyword arguments for {Exploit#initialize}.
        #
        def initialize_exploit(**kwargs)
          @exploit = super(@exploit_class,**kwargs)
        end

        #
        # Validates the exploit.
        #
        # @raise [Ronin::Core::Params::RequiredParam]
        #   One of the required params was not set.
        #
        # @raise [ValidationError]
        #   Another exploit validation error occurred.
        #
        def validate_exploit
          super(@exploit)
        end

      end
    end
  end
end