Class: Rex::ElfScan::Scanner::JmpRegScanner

Inherits:
Generic
  • Object
show all
Defined in:
lib/rex/elfscan/scanner.rb

Direct Known Subclasses

PopPopRetScanner, RegexScanner

Instance Attribute Summary

Attributes inherited from Generic

#elf, #regex

Instance Method Summary collapse

Methods inherited from Generic

#initialize, #scan

Constructor Details

This class inherits a constructor from Rex::ElfScan::Scanner::Generic

Instance Method Details

#_build_byte_list(base, regnums) ⇒ Object

build a list for regex of the possible bytes, based on a base byte and a list of register numbers..



85
86
87
# File 'lib/rex/elfscan/scanner.rb', line 85

def _build_byte_list(base, regnums)
  regnums.collect { |regnum| Regexp.escape((base | regnum).chr) }.join('')
end

#_parse_ret(data) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rex/elfscan/scanner.rb', line 100

def _parse_ret(data)
  if data.length == 1
    return "ret"
  else
    return "retn 0x%04x" % data[1, 2].unpack('v')[0]
  end
end

#_ret_size(offset) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/rex/elfscan/scanner.rb', line 89

def _ret_size(offset)
  case elf.read(offset, 1)
    when "\xc3"
      return 1
    when "\xc2"
      return 3
  end

  raise "wtf"
end

#config(param) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rex/elfscan/scanner.rb', line 64

def config(param)
  regnums = param['args']

  # build a list of the call bytes
  calls  = _build_byte_list(0xd0, regnums - [4]) # note call esp's don't work..
  jmps   = _build_byte_list(0xe0, regnums)
  pushs1 = _build_byte_list(0x50, regnums)
  pushs2 = _build_byte_list(0xf0, regnums)

  regexstr = '('
  if !calls.empty?
    regexstr += "\xff[#{calls}]|"
  end

  regexstr += "\xff[#{jmps}]|([#{pushs1}]|\xff[#{pushs2}])(\xc3|\xc2..))"

  self.regex = Regexp.new(regexstr, nil, 'n')
end

#scan_segment(program_header, param = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rex/elfscan/scanner.rb', line 109

def scan_segment(program_header, param={})
  offset = program_header.p_offset

  hits = []

  while (offset = elf.index(regex, offset)) != nil

    rva     = elf.offset_to_rva(offset)
    message = ''

    parse_ret = false

    byte1 = elf.read(offset, 1).unpack('C')[0]

    if byte1 == 0xff
      byte2   = elf.read(offset+1, 1).unpack('C')[0]
      regname = Rex::Arch::X86.reg_name32(byte2 & 0x7)

      case byte2 & 0xf8
      when 0xd0
        message = "call #{regname}"
        offset += 2
      when 0xe0
        message = "jmp #{regname}"
        offset += 2
      when 0xf0
        retsize = _ret_size(offset+2)
        message = "push #{regname}; " + _parse_ret(elf.read(offset+2, retsize))
        offset += 2 + retsize
      else
        raise "wtf"
      end
    else
      regname = Rex::Arch::X86.reg_name32(byte1 & 0x7)
      retsize = _ret_size(offset+1)
      message = "push #{regname}; " + _parse_ret(elf.read(offset+1, retsize))
      offset += 1 + retsize
    end

    hits << [ rva, message ]
  end

  return hits
end