ronin-rb/ronin-exploits

View on GitHub
lib/ronin/exploits/loot/file.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 'json'
require 'yaml'
require 'csv'
require 'fileutils'

module Ronin
  module Exploits
    class Loot
      #
      # Represents a loot file.
      #
      # @api private
      #
      # @since 1.0.0
      #
      class File

        # The file name or relative path of the loot file.
        #
        # @return [String]
        attr_reader :path

        # The contents of the loot file.
        #
        # @return [String, Array, Hash, #to_s]
        attr_reader :contents

        # The desired output format for the loot file.
        #
        # @return [:json, :yaml, :csv, nil]
        attr_reader :format

        #
        # Initializes the loot file.
        #
        # @param [Stirng] path
        #   The file name or relative path of the loot file.
        #
        # @param [String, Array, Hash, #to_s] contents
        #   The contents of the loot file.
        #
        # @param [:json, :yaml, :csv, nil] format
        #   The optional output format to serialize the loot file data as.
        #
        def initialize(path,contents, format: nil)
          @path     = path
          @contents = contents
          @format   = format
        end

        #
        # Converts the loot file into a String.
        #
        # @return [String]
        #   The contents of the loot file.
        #
        # @raise [NotImplementedError]
        #   The {#format} value is not supported.
        #
        def to_s
          case @format
          when :json then JSON.pretty_generate(@contents)
          when :yaml then YAML.dump(@contents)
          when :csv
            CSV.generate do |csv|
              @contents.each do |row|
                csv << row
              end
            end
          when nil
            @contents.to_s
          else
            raise(NotImplementedError,"unsupported loot format: #{@format.inspect}")
          end
        end

        #
        # Saves the loot file to the output directory.
        #
        # @param [String] output_dir
        #   The output directory to save the loot file into.
        #
        def save(output_dir)
          absolute_path = ::File.join(output_dir,@path)

          FileUtils.mkdir_p(::File.dirname(absolute_path))
          ::File.binwrite(absolute_path,self.to_s)
        end

      end
    end
  end
end