-
Notifications
You must be signed in to change notification settings - Fork 162
/
plugin_packer_detection.cpp
300 lines (267 loc) · 14 KB
/
plugin_packer_detection.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
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 <sstream>
#include <algorithm>
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
#include "plugin_framework/plugin_interface.h"
#include "plugin_framework/auto_register.h"
#include "manape/nt_values.h"
namespace plugin {
// Check the section names against a list of known names.
const std::vector<std::string> common_names = boost::assign::list_of(".text")
(".textbss")
(".data")
(".DATA")
("DATA")
("_RDATA")
(".eh_fram")
(".rdata")
(".rsrc")
(".idata")
(".edata")
(".pdata")
(".rodata")
(".reloc")
(".bss")
(".BSS")
("BSS")
(".tls")
(".sxdata") // Apparently related to SafeSEH.
(".gfids")
(".00cfg")
(".code")
("CODE")
(".crt")
(".CRT")
("INIT")
(".oprc")
(".detourc")
(".detourd")
(".didat");
// Also check for known packer section names (i.e. UPX0, etc.)
// This list was updated with information from
// http://www.hexacorn.com/blog/2016/12/15/pe-section-names-re-visited/
const std::map<std::string, std::string> KNOWN_PACKER_SECTIONS =
boost::assign::map_list_of ("\\.ndata", "The PE is an NSIS installer")
("\\.?(upx|UPX)[0-9!]", "The PE is packed with UPX")
("\\.(mpress|MPRESS)[0-9]", "The PE is packed with mpress")
("\\.[Aa][Ss][Pp]ack", "The PE is packed with Aspack")
("\\.adata", "The PE is packed with Aspack or Armadillo")
("\\.boom", "The PE is packed with Boomerang List Builder")
("\\.ccg", "The PE is packed with CCG")
("\\.charmve|\\.pinclie", "The program is instrumented with PIN")
("BitArts", "The PE is packed with Crunch 2.0")
("DAStub", "The PE is packed with Dragon Armor")
("\\.enigma(1|2)", "The PE is packed with Enigma Protector")
("!EPack", "The PE is packed with Epack")
("\\.gentee", "The PE is a gentee installer")
("kkrunchy", "The PE is packed with kkrunchy")
("\\.mackt", "The PE was fixed by ImpREC")
("\\.MaskPE", "The PE is packed with MaskPE")
("MEW", "The PE is packed with MEW")
("\\.neolite?", "The PE is packed with Neolite")
("\\.?nsp[012]", "The PE is packed with NsPack")
("\\.RLPack", "The PE is packed with RLPack")
("(pe|PE)([Bb]undle|[cC][12]([TM]O)?|Compact2)", "The PE is packed with PEBundle")
("PELOCKnt", "This PE is packed with PELock")
("\\.perplex", "This PE is packed with Perplex")
("PESHiELD", "This PE is packed with PEShield")
("\\.petite", "This PE is packed with Petite")
("ProCrypt", "This PE is packed with ProCrypt")
("\\.rmnet", "This PE is packed with Ramnit")
("\\.RPCrypt|RCryptor", "This PE is packed with RPCrypt")
("\\.seau", "This PE is packed with SeauSFX")
("\\.shrink[123]", "This PE is packed with Shrinker")
("\\.spack", "This PE is packed with Simple Pack (by bagie)")
("\\.svkp", "This PE is packed with SVKP")
("\\.taz", "The PE is packed with PESpin")
("(\\.?Themida)|(WinLicen)", "This PE is packed with Themida")
("\\.tsu(arch|stub)", "This PE is packed with TSULoader")
("PEPACK!!", "This PE is packed with PEPack")
("\\.(Upack|ByDwing)", "This PE is packed with Upack")
("\\.vmp[012]", "This PE is packed with VMProtect")
("VProtect", "This PE is packed with VProtect")
("\\.winapi", "This PE was modified with API Override")
("_winzip_", "This PE is a WinZip self-extractor")
("\\.WWPACK", "This PE is packed with WWPACK")
("\\.y(P|0da)", "This PE is packed with Y0da")
("\\.managed", "This PE is an AOT-compiled .NET program");
class PackerDetectionPlugin : public IPlugin
{
public:
int get_api_version() const override { return 1; }
pString get_id() const override {
return boost::make_shared<std::string>("packer");
}
pString get_description() const override {
return boost::make_shared<std::string>("Tries to structurally detect packer presence.");
}
void rich_header_tests(const mana::PE& pe, pResult res) const
{
auto rich = pe.get_rich_header();
if (!rich) {
return;
}
// Validate the checksum in the RICH header
boost::uint32_t checksum = rich->file_offset;
auto bytes = pe.get_raw_bytes(rich->file_offset);
// Checksum of the DOS header
for (unsigned int i = 0; i < rich->file_offset; ++i)
{
// Ignore e_lfanew.
if (0x3c <= i && i < 0x40) {
continue;
}
checksum += utils::rol32(bytes->at(i), i);
}
// Checksum of the @comp.ids.
for (unsigned int i = 0; i < rich->values.size(); ++i)
{
auto v = rich->values.at(i);
checksum += utils::rol32((std::get<0>(v) << 16) | std::get<1>(v), std::get<2>(v));
}
if (checksum != rich->xor_key)
{
res->raise_level(MALICIOUS);
if (res->get_summary() == nullptr) {
res->set_summary("The file headers were tampered with.");
}
res->add_information("The RICH header checksum is invalid.");
}
// Verify that the number of imports is consistent with the one reported in the header
for (auto it = rich->values.begin() ; it != rich->values.end() ; ++it)
{
// Look for the "Total imports" @comp.id.
if (std::get<0>(*it) == 1)
{
auto imports = pe.find_imports(".*");
// For some reason, the number of imports present here seems to be wrong in a lot of goodware.
// It seems however that that number is never smaller than the actual number of imports.
if (std::get<2>(*it) < imports->size())
{
if (res->get_summary() == nullptr) {
res->set_summary("The PE is packed or was manually edited.");
}
res->add_information("The number of imports reported in the RICH header is inconsistent.");
res->raise_level(SUSPICIOUS);
}
}
}
}
pResult analyze(const mana::PE& pe) override
{
pResult res = create_result();
// Analyze sections
mana::shared_sections sections = pe.get_sections();
for (mana::shared_sections::element_type::const_iterator it = sections->begin() ; it != sections->end() ; ++it)
{
if (common_names.end() == std::find(common_names.begin(), common_names.end(), *(*it)->get_name()))
{
// Check section name against known packer section names and set the summary accordingly.
for (auto it2 = KNOWN_PACKER_SECTIONS.begin() ; it2 != KNOWN_PACKER_SECTIONS.end() ; ++it2)
{
boost::regex e(it2->first, boost::regex::icase);
if (boost::regex_match(*(*it)->get_name(), e)) {
res->set_summary(it2->second);
}
}
std::stringstream ss;
ss << "Unusual section name found: " << *(*it)->get_name();
res->add_information(ss.str());
res->raise_level(SUSPICIOUS);
}
// Look for WX sections
unsigned int characteristics = (*it)->get_characteristics();
if (characteristics & nt::SECTION_CHARACTERISTICS.at("IMAGE_SCN_MEM_EXECUTE") &&
characteristics & nt::SECTION_CHARACTERISTICS.at("IMAGE_SCN_MEM_WRITE"))
{
std::stringstream ss;
ss << "Section " << *(*it)->get_name() << " is both writable and executable.";
res->add_information(ss.str());
res->raise_level(SUSPICIOUS);
}
// Check the section's entropy
if ((*it)->get_size_of_raw_data() == 0) { // TODO: Report this in a "structure" plugin?
continue;
}
double entropy = (*it)->get_entropy();
if (entropy > 7.)
{
std::stringstream ss;
ss << "Section " << *(*it)->get_name() << " has an unusually high entropy (" << entropy << ").";
res->raise_level(SUSPICIOUS);
}
}
// A low number of imports indicates that the binary is packed.
mana::const_shared_strings imports = pe.find_imports(".*"); // Get all imports
// A single import could indicate that the file is a .NET executable; don't warn about that.
if (imports->size() == 1)
{
auto mscoree = pe.find_imported_dlls("mscoree.dll");
if (!mscoree->empty())
{
auto corexemain = mscoree->at(0)->get_imports();
if (!corexemain->empty() && corexemain->at(0)->Name == "_CorExeMain") {
return res;
}
}
}
// Read the minimum import number from the configuration
unsigned int min_imports;
if (_config == nullptr || !_config->count("min_imports")) {
min_imports = 10;
}
else
{
try {
min_imports = std::stoi(_config->at("min_imports"));
}
catch (std::invalid_argument&)
{
PRINT_WARNING << "Could not parse packer.min_imports in the configuration file." << std::endl;
min_imports = 10;
}
}
if (imports->size() < min_imports)
{
std::stringstream ss;
ss << "The PE only has " << imports->size() << " import(s).";
res->add_information(ss.str());
res->raise_level(SUSPICIOUS);
}
// Look for resources bigger than the file itself
auto resources = pe.get_resources();
boost::uint64_t sum = 0;
for (auto r = resources->begin() ; r != resources->end() ; ++r) {
sum += (*r)->get_size();
}
if (sum > pe.get_filesize())
{
res->raise_level(SUSPICIOUS);
res->add_information("The PE's resources are bigger than it is.");
}
// Perform tests on the RICH header.
rich_header_tests(pe, res);
// Put a default summary if none was set.
if (res->get_level() != NO_OPINION && res->get_summary() == nullptr) {
res->set_summary("The PE is possibly packed.");
}
return res;
}
};
AutoRegister<PackerDetectionPlugin> auto_register_packer;
} // !namespace plugin