# frozen_string_literal: true
#
# ronin-exploits - A Ruby library for ronin-rb that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2024 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 .
#
require 'ronin/exploits/params/filename'
module Ronin
module Exploits
module Mixins
#
# Adds methods for building exploit files. Also adds a `filename` param
# and a
# {Metadata::DefaultFilename::ClassMethods#default_filename default_filename}
# class method.
#
# ## Examples
#
# include Mixins::FileBuilder
#
# default_filename 'exploit.docx'
#
# def build
# # ...
#
# build_file do |file|
# # ...
# file.write(buffer)
# # ...
# end
#
# # ...
# end
#
# @api public
#
# @since 1.0.0
#
module FileBuilder
#
# Includes {Params::Filename} into the exploit including {FileBuilder}.
#
# @param [Class] exploit
# The exploit including {FileBuilder}.
#
# @api private
#
def self.included(exploit)
exploit.include Params::Filename
end
#
# Opens the file to be built.
#
# @param [String] name
# Optional name of the file to build.
#
# @yield [file]
# If a block is given, it will be passed the newly opened file.
# After the block has returned, the file will be closed.
#
# @yieldparam [Tempfile, File] file
# The newly opened file.
#
# @return [String]
# Path to the built file.
#
# @example
# build_file do |file|
# file << 'some data'
# end
#
# @example Generate a specifically named file.
# build_file('file1.xml') do |file|
# file << ""
# end
#
def build_file(name=filename,&block)
path = File.expand_path(name)
File.open(name,'wb',&block)
return path
end
end
end
end
end