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.


23
24
25
26
27
# File 'lib/rex/peparsey/section.rb', line 23

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.



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

def _isource
  @_isource
end

#_section_headerObject

Returns the value of attribute _section_header.



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

def _section_header
  @_section_header
end

#base_rvaObject

Returns the value of attribute base_rva.



13
14
15
# File 'lib/rex/peparsey/section.rb', line 13

def base_rva
  @base_rva
end

Instance Method Details

#_check_offset(offset, len = 1) ⇒ Object



63
64
65
66
67
# File 'lib/rex/peparsey/section.rb', line 63

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)


126
127
128
# File 'lib/rex/peparsey/section.rb', line 126

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

#contains_offset?(offset) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#contains_rva?(rva) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rex/peparsey/section.rb', line 130

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

#file_offsetObject



29
30
31
# File 'lib/rex/peparsey/section.rb', line 29

def file_offset
	_isource.file_offset
end

#file_offset_to_rva(foffset) ⇒ Object



99
100
101
# File 'lib/rex/peparsey/section.rb', line 99

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

#flagsObject



45
46
47
48
49
# File 'lib/rex/peparsey/section.rb', line 45

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

#index(*args) ⇒ Object



87
88
89
# File 'lib/rex/peparsey/section.rb', line 87

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

#nameObject



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

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

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

#offset_to_rva(offset) ⇒ Object



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

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



57
58
59
60
61
# File 'lib/rex/peparsey/section.rb', line 57

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

#read(offset, len) ⇒ Object



69
70
71
72
# File 'lib/rex/peparsey/section.rb', line 69

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

#read_asciiz(offset) ⇒ Object



78
79
80
81
# File 'lib/rex/peparsey/section.rb', line 78

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

#read_asciiz_rva(rva) ⇒ Object



83
84
85
# File 'lib/rex/peparsey/section.rb', line 83

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

#read_rva(rva, len) ⇒ Object



74
75
76
# File 'lib/rex/peparsey/section.rb', line 74

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

#rva_to_file_offset(rva) ⇒ Object



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

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

#rva_to_offset(rva) ⇒ Object

end



109
110
111
112
113
114
115
116
# File 'lib/rex/peparsey/section.rb', line 109

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



33
34
35
# File 'lib/rex/peparsey/section.rb', line 33

def size
	_isource.size
end

#vmaObject



51
52
53
54
55
# File 'lib/rex/peparsey/section.rb', line 51

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