Class: Rex::PeParsey::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/peparsey/section.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(isource, base_rva, section_header = nil) ⇒ Section

Initialize a section.

isource        - The ImageSource class backing the image
base_vma       - The address of this section base
section_header - The section header (struct2) although this is not
  required, which is why there is a base_vma.  This can be nil.


21
22
23
24
25
# File 'lib/rex/peparsey/section.rb', line 21

def initialize(isource, base_rva, section_header = nil)
  self._isource        = isource
  self.base_rva        = base_rva
  self._section_header = section_header
end

Instance Attribute Details

#_isourceObject

Returns the value of attribute _isource.



10
11
12
# File 'lib/rex/peparsey/section.rb', line 10

def _isource
  @_isource
end

#_section_headerObject

Returns the value of attribute _section_header.



10
11
12
# File 'lib/rex/peparsey/section.rb', line 10

def _section_header
  @_section_header
end

#base_rvaObject

Returns the value of attribute base_rva.



11
12
13
# File 'lib/rex/peparsey/section.rb', line 11

def base_rva
  @base_rva
end

Instance Method Details

#_check_offset(offset, len = 1) ⇒ Object



61
62
63
64
65
# File 'lib/rex/peparsey/section.rb', line 61

def _check_offset(offset, len = 1)
  if offset < 0 || offset+len > size
    raise BoundsError, "Offset #{offset} outside of section", caller
  end
end

#contains_file_offset?(foffset) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rex/peparsey/section.rb', line 118

def contains_file_offset?(foffset)
  contains_offset?(foffset - file_offset)
end

#contains_offset?(offset) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rex/peparsey/section.rb', line 114

def contains_offset?(offset)
  offset >= 0 && offset < size
end

#contains_rva?(rva) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rex/peparsey/section.rb', line 122

def contains_rva?(rva)
  contains_offset?(rva - base_rva)
end

#file_offsetObject



27
28
29
# File 'lib/rex/peparsey/section.rb', line 27

def file_offset
  _isource.file_offset
end

#file_offset_to_rva(foffset) ⇒ Object



97
98
99
# File 'lib/rex/peparsey/section.rb', line 97

def file_offset_to_rva(foffset)
  return offset_to_rva(foffset - file_offset)
end

#flagsObject



43
44
45
46
47
# File 'lib/rex/peparsey/section.rb', line 43

def flags
  # a section header is not required
  return nil if !_section_header
  _section_header.v['Characteristics']
end

#index(*args) ⇒ Object



85
86
87
# File 'lib/rex/peparsey/section.rb', line 85

def index(*args)
  _isource.index(*args)
end

#nameObject



35
36
37
38
39
40
41
# File 'lib/rex/peparsey/section.rb', line 35

def name
  # a section header is not required
  return nil if !_section_header

  # FIXME make this better...
  _section_header.v['Name'].gsub(/\x00+$/n, '')
end

#offset_to_rva(offset) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rex/peparsey/section.rb', line 89

def offset_to_rva(offset)
  if !contains_offset?(offset)
    raise BoundsError, "Offset #{offset} outside of section", caller
  end

  return offset + base_rva
end

#raw_sizeObject



55
56
57
58
59
# File 'lib/rex/peparsey/section.rb', line 55

def raw_size
  # a section header is not required
  return nil if !_section_header
  _section_header.v['SizeOfRawData']
end

#read(offset, len) ⇒ Object



67
68
69
70
# File 'lib/rex/peparsey/section.rb', line 67

def read(offset, len)
  _check_offset(offset, len)
  return _isource.read(offset, len)
end

#read_asciiz(offset) ⇒ Object



76
77
78
79
# File 'lib/rex/peparsey/section.rb', line 76

def read_asciiz(offset)
  _check_offset(offset)
  return _isource.read_asciiz(offset)
end

#read_asciiz_rva(rva) ⇒ Object



81
82
83
# File 'lib/rex/peparsey/section.rb', line 81

def read_asciiz_rva(rva)
  return read_asciiz(rva_to_offset(rva))
end

#read_rva(rva, len) ⇒ Object



72
73
74
# File 'lib/rex/peparsey/section.rb', line 72

def read_rva(rva, len)
  return read(rva_to_offset(rva), len)
end

#rva_to_file_offset(rva) ⇒ Object



110
111
112
# File 'lib/rex/peparsey/section.rb', line 110

def rva_to_file_offset(rva)
  return rva_to_offset(rva) + file_offset
end

#rva_to_offset(rva) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/rex/peparsey/section.rb', line 101

def rva_to_offset(rva)
  offset = rva - base_rva
  if !contains_offset?(offset)
    raise BoundsError, "RVA #{rva} outside of section", caller
  end

  return offset
end

#sizeObject



31
32
33
# File 'lib/rex/peparsey/section.rb', line 31

def size
  _isource.size
end

#vmaObject



49
50
51
52
53
# File 'lib/rex/peparsey/section.rb', line 49

def vma
  # a section header is not required
  return nil if !_section_header
  _section_header.v['VirtualAddress']
end