-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathsection.cpp
150 lines (129 loc) · 4.41 KB
/
section.cpp
1
2
3
4
5
6
7
8
9
10
11
12
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
/*
This file is part of Manalyze.
Manalyze is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Manalyze is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Manalyze. If not, see <http://www.gnu.org/licenses/>.
*/
#include "manape/section.h"
namespace mana
{
Section::Section(const image_section_header& header,
pFile handle,
boost::uint64_t file_size,
const std::vector<pString>& coff_string_table)
: _virtual_size(header.VirtualSize),
_virtual_address(header.VirtualAddress),
_size_of_raw_data(header.SizeOfRawData),
_pointer_to_raw_data(header.PointerToRawData),
_pointer_to_relocations(header.PointerToRelocations),
_pointer_to_line_numbers(header.PointerToLineNumbers),
_number_of_relocations(header.NumberOfRelocations),
_number_of_line_numbers(header.NumberOfLineNumbers),
_characteristics(header.Characteristics),
_file_handle(std::move(handle)),
_file_size(file_size)
{
_name = std::string((char*) header.Name, 8);
boost::trim_right_if(_name, [](char c) { return c == '\x00'; }); // Trim the string for \0 characters.
pString escaped = io::escape(_name);
if (escaped != nullptr) {
_name = *escaped;
}
if (!_name.empty() && _name[0] == '/')
{
std::stringstream ss;
unsigned int index;
ss << _name.substr(1); // Skip the trailing "/"
ss >> index;
if (ss.fail()) {
PRINT_WARNING << "Found a non-integer index of the COFF string table (" << _name << "). This PE "
"was almost certainly manually crafted." << std::endl;
}
else if (index >= coff_string_table.size()) {
PRINT_WARNING << "Tried to read outside the COFF string table to get the name of section " << _name << "!" << std::endl;
}
else {
_name = *coff_string_table[index];
}
}
}
// ----------------------------------------------------------------------------
shared_bytes Section::get_raw_data() const
{
auto res = boost::make_shared<std::vector<boost::uint8_t> >();
if (_size_of_raw_data == 0)
{
PRINT_WARNING << "Section " << _name << " has a size of 0!" << DEBUG_INFO << std::endl;
return res;
}
if (_file_handle == nullptr) {
return res;
}
if (_pointer_to_raw_data + _size_of_raw_data > _file_size)
{
PRINT_WARNING << "Section " << _name << " is larger than the executable!" << DEBUG_INFO << std::endl;
return res;
}
if (fseek(_file_handle.get(), _pointer_to_raw_data, SEEK_SET)) {
return res;
}
try {
res->resize(_size_of_raw_data);
}
catch (const std::exception& e)
{
PRINT_ERROR << "Failed to allocate enough space for section " << *get_name() << "! (" << e.what() << ")"
<< DEBUG_INFO << std::endl;
res->resize(0);
return res;
}
if (_size_of_raw_data != fread(&(*res)[0], 1, _size_of_raw_data, _file_handle.get()))
{
PRINT_WARNING << "Raw bytes from section " << _name << " could not be obtained." << std::endl;
res->resize(0);
}
return res;
}
// ----------------------------------------------------------------------------
bool is_address_in_section(boost::uint64_t rva, mana::pSection section, bool check_raw_size)
{
if (!check_raw_size) {
return section->get_virtual_address() <= rva && rva < section->get_virtual_address() + section->get_virtual_size();
}
else {
return section->get_virtual_address() <= rva && rva < section->get_virtual_address() + section->get_size_of_raw_data();
}
}
// ----------------------------------------------------------------------------
mana::pSection find_section(unsigned int rva, const std::vector<mana::pSection>& section_list)
{
mana::pSection res = mana::pSection();
for (const auto& it : section_list)
{
if (is_address_in_section(rva, it))
{
res = it;
break;
}
}
if (!res) // VirtualSize may be erroneous. Check with RawSizeofData.
{
for (const auto& it : section_list)
{
if (is_address_in_section(rva, it, true))
{
res = it;
break;
}
}
}
return res;
}
} // !namespace mana