Class: Rex::Post::Meterpreter::Extensions::Bofloader::CnaArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/meterpreter/extensions/bofloader/cna_argument_parser.rb

Overview

Packs positional arguments extracted from a CNA alias.

Defined Under Namespace

Classes: Error

Constant Summary collapse

INTEGER_RANGES =
{
  'int32' => (-2**31)..(2**31 - 1),
  'int16' => (-2**15)..(2**15 - 1)
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(arguments:) ⇒ CnaArgumentParser

Creates an argument parser for a CNA alias.

Parameters:

  • arguments (Array<Hash>) —

    Ordered packed-value definitions.



20
21
22
# File 'lib/rex/post/meterpreter/extensions/bofloader/cna_argument_parser.rb', line 20

def initialize(arguments:)
  @arguments = arguments
end

Instance Method Details

#parse(tokens) ⇒ Hash

Converts CNA positionals into native bof_pack input.

Parameters:

  • tokens (Array<String>) —

    Positional command arguments.

Returns:

  • (Hash) —

    Format string and typed values.

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rex/post/meterpreter/extensions/bofloader/cna_argument_parser.rb', line 28

def parse(tokens)
  positions = @arguments.filter_map { |argument| argument['position'] }.uniq
  required_count = @arguments.filter_map { |argument| argument['position'] + 1 if argument['required'] }.max || 0
  expected_count = positions.empty? ? 0 : positions.max + 1

  raise Error, "Missing positional BOF argument #{required_count}" if tokens.length < required_count
  raise Error, "Too many positional BOF arguments (expected at most #{expected_count})" if tokens.length > expected_count

  values = @arguments.map do |argument|
    if argument.key?('fixed')
      raw_value = argument['fixed']
    else
      raw_value = tokens[argument['position']]
      raw_value = argument['default'] if raw_value.nil? && argument.key?('default')
      raise Error, "Missing positional BOF argument #{argument['position'] + 1}" if raw_value.nil?
    end

    coerce(argument, raw_value)
  end
  { 'format' => @arguments.empty? ? nil : @arguments.map { |argument| argument['format'] }.join, 'values' => values }
end