Class: Ronin::CLI::Commands::Unpack Private

Inherits:
Ronin::CLI::Command show all
Includes:
BinaryTemplate
Defined in:
lib/ronin/cli/commands/unpack.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.

Unpacks binary data.

Usage

ronin unpack [options] TYPE [...]

Options

-E, --endian little|big|net      Sets the endianness
-A x86|x86_64|ppc|ppc64|mips|mips_le|mips_be|mips64|mips64_le|mips64_be|arm|arm_le|arm_be|arm64|arm64_le|arm64_be,
    --arch                       Sets the architecture
-O linux|macos|windows|android|apple_ios|bsd|freebsd|openbsd|netbsd,
    --os                         Sets the OS
-S, --string $'\xXX\x..'         The raw binary string to unpack
-f, --file FILE                  The binary file to unpack
-h, --help                       Print help information

Arguments

TYPE:VALUE                      A value and it's type.

Types

Native             Little-endian       Big-endian          Network-endian
------             -------------       ----------          --------------

char
uchar
byte
string
int                int_le              int_be              int_net
int8
int16              int16_le            int16_be            int16_net
int32              int32_le            int32_be            int32_net
int64              int64_le            int64_be            int64_net
short              short_le            short_be            short_net
long               long_le             long_be             long_net
long_long          long_long_le        long_long_be        long_long_net
uint               uint_le             uint_be             uint_net
uint8
uint1616           uint16_le           uint16_be           uint16_net
uint3232           uint32_le           uint32_be           uint32_net
uint6464           uint64_le           uint64_be           uint64_net
ushort             ushort_le           ushort_be           ushort_net
ulong              ulong_le            ulong_be            ulong_net
ulong_long         ulong_long_le       ulong_long_be       ulong_long_net
float              float_le            float_be            float_net
float32            float32_le          float32_be          float32_net
float64            float64_le          float64_be          float64_net
double             double_le           double_be           double_net
pointer            pointer_le          pointer_be          pointer_net

Examples

ronin unpack int32 uint32 char string
ronin unpack int32[4] string[3]
ronin unpack uint32_le
ronin unpack uint32_be
ronin unpack --endian big int uint
ronin unpack --arch arm_le int long
ronin unpack --arch x86_64 --os windows uint
ronin unpack --string $'\x44\x33\x22\x11' int32
ronin unpack --file int32.dat int32
ronin pack int32:0x11223344 | ronin unpack int32

Since:

  • 2.1.0

Instance Method Summary collapse

Methods included from BinaryTemplate

#build_template, included, #parse_type

Instance Method Details

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.

Prints an Array of values.

Parameters:

  • value (Array<Integer, Float, String, Ronin::Support::Binary::Array>)

    The array value to print.

Since:

  • 2.1.0



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ronin/cli/commands/unpack.rb', line 163

def print_array_value(value)
  # convert Ronin::Support::Binary::Array objects to plain Arrays
  value = value.to_a

  print '['

  value.each_with_index do |element,index|
    print_value(element)
    print(', ') unless (index == (value.length - 1))
  end

  print ']'
end

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.

Prints an individual value.

Parameters:

  • value (Integer, Float, String, Ronin::Support::Binary::Array)

    The value to print.

Since:

  • 2.1.0



183
184
185
186
187
188
189
190
# File 'lib/ronin/cli/commands/unpack.rb', line 183

def print_value(value)
  case value
  when Array, Support::Binary::Array
    print_array_value(value)
  else
    print(value.inspect)
  end
end

#run(*args) ⇒ 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 unpack command.

Parameters:

  • args (Array<String>)

    The TYPE arguments to parse.

Since:

  • 2.1.0



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ronin/cli/commands/unpack.rb', line 138

def run(*args)
  types    = args.map(&method(:parse_type))
  template = build_template(types)

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

  values = template.unpack(data)

  # remove the outer-most square brackets
  print_array_value(values)
  puts
end