Skip to content

Commit

Permalink
Added the --convert-to option to ronin-payloads build (closes #182).
Browse files Browse the repository at this point in the history
* Added the `Build#convert_to` method.
* Removed unused `Build#built_payload` and `Build#encoded_payload` methods.
  Use `@payload.to_s` instead.
  • Loading branch information
postmodern committed Aug 19, 2024
1 parent b7675f4 commit b87f126
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
49 changes: 35 additions & 14 deletions lib/ronin/payloads/cli/commands/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
#
Expand All @@ -58,6 +61,7 @@ class Build < PayloadCommand
include FormatOption
include EncoderMethods
include Core::CLI::Options::Param
include Printing

option :output, short: '-o',
value: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -174,35 +193,37 @@ def build_payload
end

#
# The built payload.
# Optionally converts the payload into a different payload type.
#
# @return [String]
# @since 0.3.0
#
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
Expand Down
3 changes: 3 additions & 0 deletions man/ronin-payloads-build.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
21 changes: 21 additions & 0 deletions spec/cli/commands/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b87f126

Please sign in to comment.