From 21d86e2f7469928765c2e451d6a6f743e071752e Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 19 Aug 2024 15:58:51 -0700 Subject: [PATCH] Added the `--convert-to` option to `ronin-payloads build` (closes #182). --- lib/ronin/payloads/cli/commands/build.rb | 49 ++++++++++++++++-------- man/ronin-payloads-build.1.md | 3 ++ spec/cli/commands/build_spec.rb | 21 ++++++++++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/ronin/payloads/cli/commands/build.rb b/lib/ronin/payloads/cli/commands/build.rb index a9012272..c8f0b480 100644 --- a/lib/ronin/payloads/cli/commands/build.rb +++ b/lib/ronin/payloads/cli/commands/build.rb @@ -22,6 +22,7 @@ require_relative '../payload_command' require_relative '../format_option' require_relative '../encoder_methods' +require_relative '../printing' require 'ronin/core/cli/options/param' @@ -46,6 +47,8 @@ module Commands # -E, --encoder ENCODER Adds the encoder to the payload # --encoder-param ENCODER.NAME=VALUE # Sets a param for one of the encoders + # -C perl|php|python|ruby|nodejs|html|command, + # --convert-to Optionally converts the payload into another payload type # -D, --debug Enables debugging messages # -h, --help Print help information # @@ -58,6 +61,7 @@ class Build < PayloadCommand include FormatOption include EncoderMethods include Core::CLI::Options::Param + include Printing option :output, short: '-o', value: { @@ -86,6 +90,20 @@ class Build < PayloadCommand @encoder_params[encoder_name][param_name.to_sym] = value end + option :convert_to, short: '-C', + value: { + type: { + 'perl' => :perl, + 'php' => :php, + 'python' => :python, + 'ruby' => :ruby, + 'nodejs' => :node_js, + 'html' => :html, + 'command' => :command + } + }, + desc: 'Optionally converts the payload into another payload type' + option :debug, short: '-D', desc: 'Enables debugging messages' do Support::CLI::Printing.debug = true @@ -131,6 +149,7 @@ def run(name=nil) initialize_payload validate_payload build_payload + convert_payload if options[:convert_to] if options[:output] then write_payload else print_payload @@ -174,35 +193,35 @@ def build_payload end # - # The built payload. - # - # @return [String] + # Optionally converts the payload into a different payload type. # - def built_payload - @payload.built_payload - end + def convert_payload + convert_to = options[:convert_to] + to_method = "to_#{convert_to}" - # - # The built and encoded payload. - # - # @return [String] - # - def encoded_payload - @payload.encoded_payload + unless @payload.respond_to?(to_method) + from_payload_type = payload_type(@payload_class) + to_payload_type = PAYLOAD_TYPES.fetch(convert_to) + + print_error "unable to convert payload #{@payload_class.id} of type #{from_payload_type} into a #{to_payload_type}" + exit(-1) + end + + @payload = @payload.public_send(to_method) end # # Writes the built and optionally encoded payload to the output file. # def write_payload - File.binwrite(options[:output],format_data(encoded_payload)) + File.binwrite(options[:output],format_data(@payload.to_s)) end # # Prints the built and optionally encoded payload. # def print_payload - print_data(@payload.encode_payload) + print_data(@payload.to_s) end end diff --git a/man/ronin-payloads-build.1.md b/man/ronin-payloads-build.1.md index 4917ddd7..cfd0e4b5 100644 --- a/man/ronin-payloads-build.1.md +++ b/man/ronin-payloads-build.1.md @@ -37,6 +37,9 @@ Loads and builds a payload. `--encoder-param` *ENCODER*`.`*NAME*`.`*VALUE* : Sets a param on the given encoder. +`-C`, `--convert-to` `perl`|`php`|`python`|`ruby`|`nodejs`|`html`|`command` +: Optionally converts the payload into another payload type. + `-D`, `--debug` : Enables debugging messages. diff --git a/spec/cli/commands/build_spec.rb b/spec/cli/commands/build_spec.rb index 873f7b62..163848cb 100644 --- a/spec/cli/commands/build_spec.rb +++ b/spec/cli/commands/build_spec.rb @@ -71,5 +71,26 @@ expect(subject.encoder_ids).to eq([encoder1, encoder2]) end end + + { + 'perl' => :perl, + 'php' => :php, + 'python' => :python, + 'ruby' => :ruby, + 'nodejs' => :node_js, + 'html' => :html, + 'command' => :command + }.each do |arg,convert_to| + context "when parsing '--convert-to #{arg}' options" do + let(:argv) { ['--convert-to', arg] } + let(:convert_to) { convert_to } + + before { subject.option_parser.parse(argv) } + + it "must set options[:convert_to] to #{convert_to.inspect}" do + expect(subject.options[:convert_to]).to eq(convert_to) + end + end + end end end