ronin-rb/ronin-exploits

View on GitHub
lib/ronin/exploits/loot.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/loot/file'

require 'fileutils'

module Ronin
  module Exploits
    #
    # Represents an exploit's loot store.
    #
    # @api private
    #
    # @since 1.0.0
    #
    class Loot

      include Enumerable

      #
      # Initializes the loot store.
      #
      def initialize
        @files = {}
      end

      #
      # Adds a captured loot file to the loot store.
      #
      # @param [String] path
      #   The file name or relative path for the loot file.
      #
      # @param [String, Array, Hash, #to_s] contents
      #   The contents of the loot file.
      #
      # @param [Hash{Symbol => Object}] kwargs
      #   Additional keyword arguments for {File#initialize}.
      #
      # @option kwargs [:json, :yaml, :csv, nil] :format
      #   Optional export format to use.
      #
      # @api public
      #
      def add(path,contents,**kwargs)
        @files[path] = File.new(path,contents,**kwargs)
      end

      #
      # Accesses a loot file in the loot store.
      #
      # @param [String] path
      #   The file name or relative path for the loot file.
      #
      # @return [File, nil]
      #   The matching loot file for the given path.
      #
      def [](path)
        @files[path]
      end

      #
      # Enumerates over each loot file.
      #
      # @yield [file]
      #   The given block will be passed each loot file.
      #
      # @yieldparam [File] file
      #   A loot file in the loot store.
      #
      # @return [Enumerator]
      #   If no block is given, an Enumerator will be returned.
      #
      def each(&block)
        @files.each_value(&block)
      end

      #
      # Determines if the loot store is empty or not.
      #
      # @return [Boolean]
      #
      def empty?
        @files.empty?
      end

      #
      # Saves the loot files to the output directory.
      #
      # @param [String] output_dir
      #
      def save(output_dir)
        FileUtils.mkdir_p(output_dir)

        @files.each_value do |file|
          file.save(output_dir)
        end
      end

    end
  end
end