Class: Ronin::Payloads::CLI::Commands::Encode Private

Inherits:
Ronin::Payloads::CLI::Command show all
Includes:
EncoderMethods, FormatOption
Defined in:
lib/ronin/payloads/cli/commands/encode.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encodes data using the encoder(s).

Usage

ronin-payloads encode [options] {--string STRING | FILE}

Options

-F hex|c|shell|powershell|xml|html|js|ruby,
    --format                     Formats the outputed data
-E, --encoder ENCODER            The encoder name to load
-p, --param ENCODER.NAME=VALUE   Sets a param on an encoder
-s, --string STRING              The string to encode
-h, --help                       Print help information

Arguments

[FILE]                           The optional file to read and encode

Constant Summary

Constants included from FormatOption

FormatOption::FORMATS

Instance Attribute Summary collapse

Attributes included from FormatOption

#format

Instance Method Summary collapse

Methods included from EncoderMethods

#encoder_type, #initialize_encoder, #load_encoder, #load_encoder_from

Methods included from FormatOption

#format_data, included, #print_data

Constructor Details

#initialize(**kwargs) ⇒ Encode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the ronin-payloads encode command.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



114
115
116
117
118
119
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 114

def initialize(**kwargs)
  super(**kwargs)

  @encoders = []
  @params   = Hash.new { |hash,key| hash[key] = {} }
end

Instance Attribute Details

#encodersArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The encoder names to load.

Returns:

  • (Array<String>)


96
97
98
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 96

def encoders
  @encoders
end

#paramsHash{String => Hash{String => String}} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The params for the encoders.

Returns:

  • (Hash{String => Hash{String => String}})


101
102
103
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 101

def params
  @params
end

#pipelineEncoders::Pipeline? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The encoder pipeline.

Returns:



106
107
108
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 106

def pipeline
  @pipeline
end

Instance Method Details

#build_pipelineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds the encoder pipeline.



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 136

def build_pipeline
  @pipeline = Encoders::Pipeline.new

  @encoders.each do |encoder_id|
    encoder_class = load_encoder(encoder_id)
    params        = @params[encoder_id]
    encoder       = initialize_encoder(encoder_class, params: params)

    validate_encoder(encoder)

    @pipeline << encoder
  end
end

#encode_data(data) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encodes the data.

Parameters:

  • data (String)

Returns:

  • (String)


197
198
199
200
201
202
203
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 197

def encode_data(data)
  @pipeline.encode(data)
rescue => error
  print_error "unhandled exception occurred while encoding data"
  print_exception(error)
  exit(1)
end

#load_data(file = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads the data to encode.

Returns:

  • (String)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 175

def load_data(file=nil)
  if file
    unless File.file?(file)
      print_error "No such file or directory: #{file}"
      exit(-1)
    end

    File.binread(file)
  elsif options[:string]
    options[:string]
  else
    stdin.read
  end
end

#run(file = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin-payloads encode command.

Parameters:

  • file (String, nil) (defaults to: nil)

    The optional file to read data from and encode.



127
128
129
130
131
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 127

def run(file=nil)
  build_pipeline

  print_data(encode_data(load_data(file)))
end

#validate_encoder(encoder) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates the loaded encoders.

Raises:

  • (Ronin::Core::Params::RequiredParam)

    One of the required params was not set.

  • (ValidationError)

    Another encoder validation error occurred.



159
160
161
162
163
164
165
166
167
168
# File 'lib/ronin/payloads/cli/commands/encode.rb', line 159

def validate_encoder(encoder)
  encoder.validate
rescue Core::Params::ParamError, ValidationError => error
  print_error "failed to validate the encoder #{encoder.class_id}: #{error.message}"
  exit(1)
rescue => error
  print_error "an unhandled exception occurred while validating the encoder #{encoder.class_id}"
  print_exception(error)
  exit(-1)
end