Class: Ronin::CLI::Commands::Pack Private

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

Packs values into binary data.

Usage

ronin pack [options] TYPE:VALUE [...]

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
-x, --hexdump                    Hexdumps the packed data, instead of writing it out
    --output PATH                Optional output file to write to
-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 pack int32:-1 uint32:0x12345678 char:A string:hello
ronin pack int32[4]:1,2,3,4 string[3]:hello,world
ronin pack uint32_le:0x12345678
ronin pack uint32_be:0x12345678
ronin pack --endian big int:4096 uint:0x12345678
ronin pack --arch arm_le int:4096 long:0x12345678
ronin pack --arch x86_64 --os windows uint:0x12345678

Since:

  • 2.1.0

Instance Method Summary collapse

Methods included from BinaryTemplate

#build_template, included, #parse_type

Instance Method Details

#help_argumentsObject

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 the help information for the arguments and lists TYPEs.

Since:

  • 2.1.0



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ronin/cli/commands/pack.rb', line 153

def help_arguments
  super

  puts
  puts <<HELP
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
HELP
end

#parse_array_value(ctype, string) ⇒ Array<Integer>, ...

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.

Parses an array.

Parameters:

  • ctype (Ronin::Support::Binary::CTypes::Type)

    The C type that represents the array type.

  • string (String)

    The raw string to parse.

Returns:

  • (Array<Integer>, Array<Float>, Array<String>)

    The parsed array.

Since:

  • 2.1.0



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ronin/cli/commands/pack.rb', line 275

def parse_array_value(ctype,string)
  # create array of the desired size
  array = if ctype.length.finite?
            Array.new(ctype.length,ctype.type.uninitialized_value)
          else
            []
          end

  string.split(/(?!\\),/).each_with_index do |element,index|
    array[index] = parse_value(ctype.type,element)
  end

  return array
end

#parse_float(string) ⇒ Float

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.

Parses an float value.

Parameters:

  • string (String)

    The raw string to parse.

Returns:

  • (Float)

    The parsed Float.

Since:

  • 2.1.0



315
316
317
318
319
320
# File 'lib/ronin/cli/commands/pack.rb', line 315

def parse_float(string)
  Float(string)
rescue ArgumentError
  print_error "cannot parse float: #{string}"
  exit(-1)
end

#parse_int(string) ⇒ Integer

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.

Parses an integer value.

Parameters:

  • string (String)

    The raw string to parse.

Returns:

  • (Integer)

    The parsed Integer.

Since:

  • 2.1.0



299
300
301
302
303
304
# File 'lib/ronin/cli/commands/pack.rb', line 299

def parse_int(string)
  Integer(string)
rescue ArgumentError
  print_error "cannot parse integer: #{string}"
  exit(-1)
end

#parse_types_and_values(args) ⇒ (Array<Symbol, (Symbol, Integer)>, Array<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.

Performs an initial parsing of the TYPE:VALUE arguments into two separate lists of TYPEs and VALUEs.

Parameters:

  • args (Array<String>)

    The TYPE:VALUE argumens to parse.

Returns:

  • ((Array<Symbol, (Symbol, Integer)>, Array<String>))

    The parsed types and values.

Since:

  • 2.1.0



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ronin/cli/commands/pack.rb', line 201

def parse_types_and_values(args)
  types  = []
  values = []

  args.each do |string|
    type, value = string.split(':',2)

    types  << parse_type(type)
    values << value
  end

  return types, values
end

#parse_value(ctype, string) ⇒ Array<Integer>, ...

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.

Parses the value based on it's C type.

Parameters:

  • ctype (Ronin::Support::Binary::CType::Type)

    The C type of the value.

  • string (String)

    The raw unparsed string.

Returns:

  • (Array<Integer>, Array<Float>, Array<String>, Integer, Float, String)

    The parsed value.

Since:

  • 2.1.0



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ronin/cli/commands/pack.rb', line 244

def parse_value(ctype,string)
  case ctype
  when Support::Binary::CTypes::ArrayType,
       Support::Binary::CTypes::ArrayObjectType,
       Support::Binary::CTypes::UnboundedArrayType
    parse_array_value(ctype,string)
  when Support::Binary::CTypes::IntType,
       Support::Binary::CTypes::UIntType
    parse_int(string)
  when Support::Binary::CTypes::FloatType
    parse_float(string)
  when Support::Binary::CTypes::CharType,
       Support::Binary::CTypes::StringType
    string
  else
    raise(NotImplementedError,"unable to parse value for CType #{ctype.class}")
  end
end

#parse_values(types, values) ⇒ 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.

Performs a second parsing of the values based on their desired C types.

Parameters:

  • types (Array<Ronin::Support::Binary::CTypes::Type>)

    The desired types of the values.

  • values (Array<String, Array<String>>)

    The values to further parse.

Since:

  • 2.1.0



225
226
227
228
229
230
# File 'lib/ronin/cli/commands/pack.rb', line 225

def parse_values(types,values)
  # now parse the values based on their resolved CType types
  values.map.with_index do |value,index|
    parse_value(types[index],value)
  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 pack command.

Parameters:

  • args (Array<String>)

    The TYPE:VALUE strings to parse and pack.

Since:

  • 2.1.0



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ronin/cli/commands/pack.rb', line 130

def run(*args)
  # perform an initial parsing of the arguments to extract types/values
  types, values = parse_types_and_values(args)

  # build the template using the parsed `TYPE`s
  template = build_template(types)

  # parse the values, but using the resolved C types.
  values = parse_values(template.types,values)

  # finally pack the parsed values using the binary template
  data = template.pack(*values)

  if options[:hexdump]
    data.hexdump
  else
    write_output(data)
  end
end

#write_output(data) ⇒ 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.

Writes the packed data to the --output file or stdout.

Parameters:

  • data (String)

    The packed data to output.

Since:

  • 2.1.0



328
329
330
331
332
333
334
# File 'lib/ronin/cli/commands/pack.rb', line 328

def write_output(data)
  if options[:output]
    File.binwrite(options[:output],data)
  else
    stdout.write(data)
  end
end