-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathchecksec.cpp
342 lines (306 loc) · 14.2 KB
/
checksec.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "checksec.h"
#include <pe-parse/parse.h>
#include <uthenticode.h>
#include <cstring>
#include <ostream>
#include <vector>
#include <optional>
#define REPORT_EXPLAIN(presence, description, explanation) \
{ MitigationPresence::presence, impl::description, explanation }
#define REPORT(presence, description) REPORT_EXPLAIN(presence, description, std::nullopt)
namespace checksec {
Checksec::Checksec(std::string filepath) : filepath_(filepath), loadedImage_(filepath) {
peparse::nt_header_32 nt = loadedImage_.get()->peHeader.nt;
peparse::file_header* imageFileHeader = &(nt.FileHeader);
targetMachine_ = imageFileHeader->Machine;
imageCharacteristics_ = imageFileHeader->Characteristics;
std::vector<std::uint8_t> loadConfigData;
std::vector<std::uint8_t> debugDirectories;
// Check whether we need a 32 or 32+ optional header.
if (nt.OptionalMagic == peparse::NT_OPTIONAL_64_MAGIC) {
peparse::optional_header_64* optionalHeader = &(nt.OptionalHeader64);
dllCharacteristics_ = optionalHeader->DllCharacteristics;
if (optionalHeader->NumberOfRvaAndSizes < peparse::DIR_COM_DESCRIPTOR + 1) {
std::cerr << "Warn: short image data directory vector (no CLR info?)"
<< "\n";
return;
}
clrConfig_ = optionalHeader->DataDirectory[peparse::DIR_COM_DESCRIPTOR];
// Warn and return early if the image data directory vector
// is too short to contain a reference to the DIR_LOAD_CONFIG.
if (optionalHeader->NumberOfRvaAndSizes < peparse::DIR_LOAD_CONFIG + 1) {
std::cerr << "Warn: short image data directory vector (no load config?)"
<< "\n";
return;
}
if (!peparse::GetDataDirectoryEntry(loadedImage_.get(), peparse::DIR_LOAD_CONFIG,
loadConfigData)) {
std::cerr << "Warn: No load config in the PE"
<< "\n";
return;
}
peparse::image_load_config_64 loadConfig{};
if (loadConfigData.size() > sizeof(loadConfig)) {
std::cerr << "Warn: large load config, probably contains undocumented "
"fields"
<< "\n";
} else if (loadConfigData.size() < sizeof(loadConfig)) {
std::cerr << "Warn: undersized load config, probably missing fields"
<< "\n";
}
auto size = std::min(loadConfigData.size(), sizeof(loadConfig));
memcpy(&loadConfig, loadConfigData.data(), size);
loadConfigSize_ = loadConfigData.size();
loadConfigGuardFlags_ = loadConfig.GuardFlags;
loadConfigSecurityCookie_ = loadConfig.SecurityCookie;
loadConfigSEHandlerTable_ = loadConfig.SEHandlerTable;
loadConfigSEHandlerCount_ = loadConfig.SEHandlerCount;
// Iterate over debug directories
if (!peparse::GetDataDirectoryEntry(loadedImage_.get(), peparse::DIR_DEBUG,
debugDirectories)) {
std::cerr << "Warn: No debug directories"
<< "\n";
return;
}
peparse::debug_dir_entry debugDir{};
uint32_t numberOfDebugDirs = debugDirectories.size() / sizeof(debugDir);
auto debugDirSize = std::min(debugDirectories.size(), sizeof(debugDir));
for (int i = 0; i < numberOfDebugDirs; i++) {
// copy the current debug directory into debugDir
memcpy(&debugDir, debugDirectories.data() + (i * debugDirSize), debugDirSize);
// 20 == IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS
// For now we only care about this debug directory since it contains the CETCOMPAT bit
if (debugDir.Type == 20) {
if (debugDir.PointerToRawData != 0) {
auto dataPtr = debugDir.PointerToRawData + loadedImage_.get()->fileBuffer->buf;
if ((dataPtr < loadedImage_.get()->fileBuffer->buf) ||
(dataPtr > loadedImage_.get()->fileBuffer->buf +
loadedImage_.get()->fileBuffer->bufLen)) {
std::cerr << "Warn: dataPtr is out of bounds"
<< "\n";
extendedDllCharacteristics_ = 0;
return;
}
extendedDllCharacteristics_ = *((uint16_t*)dataPtr);
}
}
}
} else {
peparse::optional_header_32* optionalHeader = &(nt.OptionalHeader);
dllCharacteristics_ = optionalHeader->DllCharacteristics;
if (optionalHeader->NumberOfRvaAndSizes < peparse::DIR_COM_DESCRIPTOR + 1) {
std::cerr << "Warn: short image data directory vector (no CLR info?)"
<< "\n";
return;
}
clrConfig_ = optionalHeader->DataDirectory[peparse::DIR_COM_DESCRIPTOR];
// Warn and return early if the image data directory vector
// is too short to contain a reference to the DIR_LOAD_CONFIG.
if (optionalHeader->NumberOfRvaAndSizes < peparse::DIR_LOAD_CONFIG + 1) {
std::cerr << "Warn: short image data directory vector (no load config?)"
<< "\n";
return;
}
if (!peparse::GetDataDirectoryEntry(loadedImage_.get(), peparse::DIR_LOAD_CONFIG,
loadConfigData)) {
std::cerr << "Warn: No load config in the PE"
<< "\n";
return;
}
peparse::image_load_config_32 loadConfig{};
if (loadConfigData.size() > sizeof(loadConfig)) {
std::cerr << "Warn: large load config, probably contains undocumented "
"fields"
<< "\n";
} else if (loadConfigData.size() < sizeof(loadConfig)) {
std::cerr << "Warn: undersized load config, probably missing fields"
<< "\n";
}
auto size = std::min(loadConfigData.size(), sizeof(loadConfig));
memcpy(&loadConfig, loadConfigData.data(), size);
loadConfigSize_ = loadConfigData.size();
loadConfigGuardFlags_ = loadConfig.GuardFlags;
loadConfigSecurityCookie_ = loadConfig.SecurityCookie;
loadConfigSEHandlerTable_ = loadConfig.SEHandlerTable;
loadConfigSEHandlerCount_ = loadConfig.SEHandlerCount;
if (!peparse::GetDataDirectoryEntry(loadedImage_.get(), peparse::DIR_DEBUG,
debugDirectories)) {
std::cerr << "Warn: No debug directories"
<< "\n";
return;
}
peparse::debug_dir_entry debugDir{};
auto numberOfDebugDirs = debugDirectories.size() / sizeof(debugDir);
auto debugDirSize = std::min(debugDirectories.size(), sizeof(debugDir));
for (int i = 0; i < numberOfDebugDirs; i++) {
memcpy(&debugDir, debugDirectories.data() + (i * debugDirSize), debugDirSize);
if (debugDir.Type == 20) {
if (debugDir.PointerToRawData != 0) {
auto dataPtr = debugDir.PointerToRawData + loadedImage_.get()->fileBuffer->buf;
if ((dataPtr < loadedImage_.get()->fileBuffer->buf) ||
(dataPtr > loadedImage_.get()->fileBuffer->buf +
loadedImage_.get()->fileBuffer->bufLen)) {
std::cerr << "Warn: dataPtr is out of bounds"
<< "\n";
extendedDllCharacteristics_ = 0;
return;
}
extendedDllCharacteristics_ = *((uint16_t*)dataPtr);
}
}
}
}
}
const MitigationReport Checksec::isDynamicBase() const {
if (dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) {
return REPORT(Present, kDynamicBaseDescription);
} else {
return REPORT(NotPresent, kDynamicBaseDescription);
}
}
const MitigationReport Checksec::isASLR() const {
// A binary is ASLR'd if:
// * It was linked with /DYNAMICBASE and has *not* had its relocation
// entries stripped, or
// * It's managed by the CLR, which is always ASLR'd.
if (isDynamicBase()) {
if (imageCharacteristics_ & peparse::IMAGE_FILE_RELOCS_STRIPPED) {
return REPORT_EXPLAIN(NotPresent, kASLRDescription,
"Image has stripped relocations, making ASLR impossible.");
}
return REPORT(Present, kASLRDescription);
} else if (isDotNET()) {
return REPORT_EXPLAIN(Present, kASLRDescription,
".NET binaries have ASLR via the .NET runtime.");
} else {
return REPORT(NotPresent, kASLRDescription);
}
}
const MitigationReport Checksec::isHighEntropyVA() const {
// NOTE(ww): Set by /HIGHENTROPYVA, but not exposed anywhere as a constant.
// Only relevant on 64-bit machines with 64-bit images.
// NOTE(ww): Additionally, don't count a binary as high-entropy capable
// if it isn't also ASLR'd.
if ((dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA) && isASLR()) {
return REPORT(Present, kHighEntropyVADescription);
} else {
return REPORT(NotPresent, kHighEntropyVADescription);
}
}
const MitigationReport Checksec::isForceIntegrity() const {
if (dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY) {
return REPORT(Present, kForceIntegrityDescription);
} else {
return REPORT(NotPresent, kForceIntegrityDescription);
}
}
const MitigationReport Checksec::isNX() const {
if ((dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_NX_COMPAT)) {
return REPORT(Present, kNXDescription);
} else if (isDotNET()) {
return REPORT_EXPLAIN(Present, kNXDescription,
".NET binaries have DEP via the .NET runtime.");
} else {
return REPORT(NotPresent, kNXDescription);
}
}
const MitigationReport Checksec::isIsolation() const {
if (!(dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_NO_ISOLATION)) {
return REPORT(Present, kIsolationDescription);
} else {
return REPORT(NotPresent, kIsolationDescription);
}
}
const MitigationReport Checksec::isSEH() const {
if (!(dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_NO_SEH)) {
return REPORT(Present, kSEHDescription);
} else {
return REPORT(NotPresent, kSEHDescription);
}
}
const MitigationReport Checksec::isCFG() const {
// NOTE(ww): See the /GUARD:CF docs: /DYNAMICBASE is required.
// We check for ASLR instead, since just checking for /DYNAMICBASE
// could result in a false-positive (with stripped relocations).
if (!isASLR()) {
return REPORT_EXPLAIN(NotPresent, kCFGDescription,
"Control Flow Guard requires functional ASLR.");
}
if (dllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_GUARD_CF) {
return REPORT(Present, kCFGDescription);
} else {
return REPORT(NotPresent, kCFGDescription);
}
}
const MitigationReport Checksec::isAuthenticode() const {
if (uthenticode::verify(loadedImage_.get())) {
return REPORT(Present, kAuthenticodeDescription);
} else {
return REPORT(NotPresent, kAuthenticodeDescription);
}
}
const MitigationReport Checksec::isRFG() const {
// NOTE(ww): a load config under 92/148 bytes implies the absence of the
// GuardFlags field. See:
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#load-configuration-layout
if (loadConfigSize_ < 148 &&
!(imageCharacteristics_ & peparse::IMAGE_FILE_32BIT_MACHINE && loadConfigSize_ >= 92)) {
return REPORT_EXPLAIN(NotPresent, kRFGDescription,
"Image load config is too short to contain RFG "
"configuration fields.");
}
// https://xlab.tencent.com/en/2016/11/02/return-flow-guard/
if ((loadConfigGuardFlags_ & 0x00020000) &&
(loadConfigGuardFlags_ & 0x00040000 || loadConfigGuardFlags_ & 0x00080000)) {
return REPORT(Present, kRFGDescription);
} else {
return REPORT(NotPresent, kRFGDescription);
}
}
const MitigationReport Checksec::isSafeSEH() const {
if (targetMachine_ != peparse::IMAGE_FILE_MACHINE_I386) {
return REPORT_EXPLAIN(NotApplicable, kSafeSEHDescription,
"The SafeSEH mitigation only applies to x86_32 binaries.");
}
// NOTE(ww): a load config under 72/112 bytes implies the absence of the
// SafeSEH fields.
if (loadConfigSize_ < 112 &&
!(imageCharacteristics_ & peparse::IMAGE_FILE_32BIT_MACHINE && loadConfigSize_ >= 72)) {
return REPORT_EXPLAIN(NotPresent, kSafeSEHDescription,
"Image load config is too short to contain a SE handler table.");
}
if (isSEH() && loadConfigSEHandlerTable_ != 0 && loadConfigSEHandlerCount_ != 0) {
return REPORT(Present, kSafeSEHDescription);
} else {
return REPORT(NotPresent, kSafeSEHDescription);
}
}
const MitigationReport Checksec::isGS() const {
// NOTE(ww): a load config under 64/96 bytes implies the absence of the
// SecurityCookie field.
if (loadConfigSize_ < 96 &&
!(imageCharacteristics_ & peparse::IMAGE_FILE_32BIT_MACHINE && loadConfigSize_ >= 64)) {
return REPORT_EXPLAIN(NotPresent, kGSDescription,
"Image load config is too short to contain a GS security cookie.");
}
if (loadConfigSecurityCookie_ != 0) {
return REPORT(Present, kGSDescription);
} else {
return REPORT(NotPresent, kGSDescription);
}
}
const MitigationReport Checksec::isDotNET() const {
if (clrConfig_.VirtualAddress != 0) {
return REPORT(Present, kDotNETDescription);
} else {
return REPORT(NotPresent, kDotNETDescription);
}
}
const MitigationReport Checksec::isCetCompat() const {
if (extendedDllCharacteristics_ & peparse::IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT) {
return REPORT(Present, kCetDescription);
} else {
return REPORT(NotPresent, kCetDescription);
}
}
} // namespace checksec