Class: Ronin::Payloads::CPayload

Inherits:
BinaryPayload show all
Defined in:
lib/ronin/payloads/c_payload.rb

Overview

A Payload class that represents all C payloads.

Instance Attribute Summary

Attributes inherited from Payload

#encoders, #payload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Payload

#build, #built?, #built_payload, #bytesize, #cleanup, #encode_payload, #encoded_payload, encoder_class, #initialize, #length, #perform_build, #perform_cleanup, #perform_postlaunch, #perform_prelaunch, #perform_validate, #postlaunch, #prelaunch, #rebuild_payload, #reencode_payload, register, #to_s, #validate

Constructor Details

This class inherits a constructor from Ronin::Payloads::Payload

Class Method Details

.ccString

The default C compiler.

Returns:

  • (String)


50
51
52
# File 'lib/ronin/payloads/c_payload.rb', line 50

def self.cc
  ENV['CC'] || 'cc'
end

.payload_typeSymbol

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.

Note:

This is used internally to map an payload class to a printable type.

Returns the type or kind of payload.

Returns:

  • (Symbol)


41
42
43
# File 'lib/ronin/payloads/c_payload.rb', line 41

def self.payload_type
  :c
end

Instance Method Details

#compile(*source_files, output:, defs: nil) ⇒ Object

Compiles one or more source files using cc.

Parameters:

  • source_files (Array<String>)

    The source file(s) to compile.

  • output (String)

    The output file path.

  • defs (Array<String>, Hash{Symbol,String => String}, nil) (defaults to: nil)

    Additional macro definitions to pass to the compiler.

Raises:

  • (ArgumentError)

    defs was not an Array or a Hash.

  • (BuildFailed)

    The cc command failed or is not installed.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ronin/payloads/c_payload.rb', line 76

def compile(*source_files, output: , defs: nil)
  args = [params[:cc], '-o', output]

  if defs
    case defs
    when Array
      defs.each do |value|
        args << "-D#{value}"
      end
    when Hash
      defs.each do |name,value|
        args << "-D#{name}=#{value}"
      end
    else
      raise(ArgumentError,"defs must be either an Array or a Hash: #{defs.inspect}")
    end
  end

  args.concat(source_files)

  case system(*args)
  when false
    raise(BuildFailed,"cc command failed: #{args.join(' ')}")
  when nil
    raise(BuildFailed,"cc command not installed")
  end
end