Class: OneGadget::Gadget::Gadget

Inherits:
Object
  • Object
show all
Defined in:
lib/one_gadget/gadget.rb

Overview

Information of a gadget.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset, **options) ⇒ Gadget

Initialize method of OneGadget::Gadget::Gadget instance.

Examples:

OneGadget::Gadget::Gadget.new(0x12345, constraints: ['rax == 0'])

Parameters:

  • offset (Integer)

    The relative address offset of this gadget.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :constraints (Array<String>)

    The constraints need for this gadget. Defaults to [].


23
24
25
26
27
# File 'lib/one_gadget/gadget.rb', line 23

def initialize(offset, **options)
  @offset = offset
  @constraints = options[:constraints] || []
  @effect = options[:effect]
end

Instance Attribute Details

#constraintsArray<String> (readonly)

Returns The constraints need for this gadget.

Returns:

  • (Array<String>)

    The constraints need for this gadget.


13
14
15
# File 'lib/one_gadget/gadget.rb', line 13

def constraints
  @constraints
end

#effectString (readonly)

Returns The final result of this gadget.

Returns:

  • (String)

    The final result of this gadget.


15
16
17
# File 'lib/one_gadget/gadget.rb', line 15

def effect
  @effect
end

#offsetInteger (readonly)

Returns The gadget's address offset.

Returns:

  • (Integer)

    The gadget's address offset.


11
12
13
# File 'lib/one_gadget/gadget.rb', line 11

def offset
  @offset
end

Instance Method Details

#inspectObject

Show gadget in a pretty way.


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/one_gadget/gadget.rb', line 30

def inspect
  str = OneGadget::Helper.hex(offset)
  str += effect ? " #{effect}\n" : "\n"
  unless constraints.empty?
    str += "#{OneGadget::Helper.colorize('constraints')}:\n  "
    str += merge_constraints.join("\n  ")
  end
  str.gsub!(/0x[\da-f]+/) { |s| OneGadget::Helper.colorize(s, sev: :integer) }
  OneGadget::ABI.all.each do |reg|
    str.gsub!(/([^\w])(#{reg})([^\w])/, '\1' + OneGadget::Helper.colorize('\2', sev: :reg) + '\3')
  end
  str + "\n"
end

#scoreFloat

Returns The success probability of the constraints.

Returns:

  • (Float)

    The success probability of the constraints.


46
47
48
# File 'lib/one_gadget/gadget.rb', line 46

def score
  @score ||= constraints.reduce(1.0) { |s, c| s * calculate_score(c) }
end