ronin-rb/ronin-exploits

View on GitHub
lib/ronin/exploits/cli/commands/list.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/registry'

module Ronin
  module Exploits
    class CLI
      module Commands
        #
        # Lists the available exploits.
        #
        # ## Usage
        #
        #     ronin-exploits list [options] [NAME]
        #
        # ## Options
        #
        #     -h, --help                       Print help information
        #
        # ## Arguments
        #
        #     [NAME]                           The optional exploit name to list
        #
        class List < Command

          usage '[options] [NAME]'

          argument :name, required: false,
                          desc:     'The optional exploit name to list'

          description 'Lists the available exploits'

          man_page 'ronin-exploits-list.1'

          #
          # Runs the `ronin-exploits list` command.
          #
          # @param [String, nil] dir
          #   The optional exploit directory to list.
          #
          def run(dir=nil)
            files = if dir
                      dir = "#{dir}/" unless name.end_with?('/')

                      Exploits.list_files.select do |file|
                        file.start_with?(dir)
                      end
                    else
                      Exploits.list_files
                    end

            files.each do |file|
              puts "  #{file}"
            end
          end

        end
      end
    end
  end
end