Module: PWN::Plugins::ROP

Defined in:
lib/pwn/plugins/rop.rb

Overview

Read-only gadget enumeration; clobbers are conservative syntactic estimates.

Class Method Summary collapse

Class Method Details

.authorsObject



73
74
75
# File 'lib/pwn/plugins/rop.rb', line 73

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\n"
end

.filter(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pwn/plugins/rop.rb', line 49

public_class_method def self.filter(opts = {})
  constraints = opts[:constraints] || {}
  constraints = constraints.transform_keys(&:to_sym)
  allowed = %i[contains max_instructions preserve min_address max_address]
  raise ArgumentError, "unsupported constraints: #{constraints.keys - allowed}" unless (constraints.keys - allowed).empty?

  Array(opts[:gadgets]).select do |row|
    preserved = Array(constraints[:preserve]).map { |register| canonical_register(register: register) }
    clobbered = Array(row[:regs_clobbered]).map { |register| canonical_register(register: register) }
    (!constraints[:contains] || row[:gadget].include?(constraints[:contains].to_s)) &&
      (!constraints[:max_instructions] || row[:gadget].split(';').length <= Integer(constraints[:max_instructions])) &&
      (preserved.empty? || (row[:clobbers_complete] && !preserved.intersect?(clobbered))) &&
      (!constraints[:min_address] || row[:address] >= Integer(constraints[:min_address])) &&
      (!constraints[:max_address] || row[:address] <= Integer(constraints[:max_address]))
  end
end

.gadgets(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pwn/plugins/rop.rb', line 13

public_class_method def self.gadgets(opts = {})
  path = File.realpath(File.expand_path(opts[:path].to_s))
  backend = opts[:backend] || %w[ROPgadget ropper].find { |name| BinaryAnalysis.available?(name: name) } || 'objdump'
  command = case backend
            when 'ROPgadget' then ['ROPgadget', '--binary', path, '--depth', '6']
            when 'ropper' then ['ropper', '--file', path, '--nocolor', '--inst-count', '6']
            when 'objdump' then ['objdump', '-d', '-M', 'intel', path]
            else raise ArgumentError, 'unsupported gadget backend'
            end
  text = BinaryAnalysis.run(argv: command, timeout: opts.fetch(:timeout, 60))
  rows = if backend == 'objdump'
           text.lines.filter_map do |line|
             match = line.match(/^\s*([0-9a-f]+):\s+(?:[0-9a-f]{2}\s+)+\s*(ret\w*\b.*)$/)
             { address: match[1].to_i(16), gadget: match[2].strip } if match
           end
         else
           text.lines.filter_map do |line|
             match = line.match(/^\s*(0x[0-9a-fA-F]+)\s*:\s*(.+)$/)
             { address: match[1].to_i(16), gadget: match[2].strip.sub(/;\s*$/, '') } if match
           end
         end
  rows.each do |row|
    # Include ALL mentioned registers, not only destinations. Unknown implicit
    # effects fail closed under preserve constraints; this is not symbolic proof.
    regs = row[:gadget].downcase.scan(/\b(?:r(?:1[0-5]|[0-9])(?:d|w|b)?|[re]?(?:ax|bx|cx|dx|si|di|sp|bp)|[abcd][lh]|[xyz]mm\d+)\b/)
    regs += %w[rsp rip] if row[:gadget].match?(/\b(?:ret|pop|push|call)\b/)
    row[:regs_clobbered] = regs.uniq
    row[:clobbers_complete] = row[:gadget].split(';').all? { |ins| ins.strip.match?(/\A(?:pop\s+[a-z0-9]+|ret(?:\s+0x[0-9a-f]+)?|nop)\z/i) }
  end
  { backend: backend, status: backend == 'objdump' ? 'degraded' : 'ok', risk_level: 'low', path: path, sha256: Digest::SHA256.file(path).hexdigest, gadgets: filter(gadgets: rows, constraints: opts[:constraints] || {}), warnings: backend == 'objdump' ? ['fallback enumerates aligned returns only, not a complete gadget search'] : ['register clobbers are conservative estimates, not symbolic verification'] }
rescue StandardError => e
  raise if backend == 'objdump' || e.is_a?(ArgumentError)

  gadgets(opts.merge(backend: 'objdump')).tap { |result| result[:warnings] << "#{backend}: #{e.message}" }
end

.helpObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pwn/plugins/rop.rb', line 77

public_class_method def self.help
  puts "USAGE:
    # List expected analysis backend executables.
    #{self}.required_bins

    # Enumerate read-only gadgets and conservatively filter register constraints.
    #{self}.gadgets(
      backend: 'optional - analysis backend name; binutils forces lightweight fallback',
      constraints: 'optional - hash with contains, preserve register array, instruction and address bounds',
      path: 'required - filesystem path to the local artifact or binary',
      timeout: 'optional - positive subprocess or HTTP deadline in seconds'
    )

    # Filter already enumerated gadget records with an explicit constraint set.
    #{self}.filter(
      constraints: 'optional - hash with contains, preserve register array, instruction and address bounds',
      gadgets: 'required - array of normalized gadget records returned by enumeration'
    )

    # Display module authors.
    #{self}.authors
  "
  constants.sort
end

.required_binsObject



9
10
11
# File 'lib/pwn/plugins/rop.rb', line 9

public_class_method def self.required_bins
  %w[ROPgadget ropper objdump]
end