forked from merces/libpe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpesec.c
497 lines (417 loc) · 13.2 KB
/
pesec.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
pev - the PE file analyzer toolkit
pesec.c - Check for security features in PE files.
Copyright (C) 2012 - 2017 pev authors
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
#include "common.h"
#include <string.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include "compat/strlcat.h"
#include "plugins.h"
#include "utils.h"
#define PROGRAM "pesec"
typedef enum {
CERT_FORMAT_TEXT = 1,
CERT_FORMAT_PEM = 2,
CERT_FORMAT_DER = 3
} cert_format_e;
typedef struct {
cert_format_e certoutform;
BIO *certout;
} options_t;
static void usage(void)
{
static char formats[255];
output_available_formats(formats, sizeof(formats), '|');
printf("Usage: %s [OPTIONS] FILE\n"
"Check for security features in PE files\n"
"\nExample: %s wordpad.exe\n"
"\nOptions:\n"
" -f, --format <%s> change output format (default: text)\n"
" -c, --certoutform <text|pem> specifies the certificate output format (default: text)\n"
" -o, --certout <filename> specifies the output filename to write certificates to (default: stdout)\n"
" -V, --version show version and exit\n"
" --help show this help and exit\n",
PROGRAM, PROGRAM, formats);
}
static cert_format_e parse_certoutform(const char *optarg)
{
cert_format_e result;
if (strcmp(optarg, "text") == 0)
result = CERT_FORMAT_TEXT;
else if (strcmp(optarg, "pem") == 0)
result = CERT_FORMAT_PEM;
else if (strcmp(optarg, "der") == 0)
result = CERT_FORMAT_DER;
else
EXIT_ERROR("invalid cert_format option");
return result;
}
static BIO *parse_certout(const char *optarg)
{
BIO *bio = BIO_new(BIO_s_file());
if (bio == NULL) {
EXIT_ERROR("could not allocate BIO");
}
if (strcmp(optarg, "stdout") == 0) {
BIO_set_fp(bio, stdout, BIO_NOCLOSE);
} else if (strcmp(optarg, "stderr") == 0) {
BIO_set_fp(bio, stderr, BIO_NOCLOSE);
} else {
int ret = BIO_write_filename(bio, (char *)optarg);
if (ret == 0) {
BIO_free(bio);
EXIT_ERROR("failed to open file");
}
}
return bio;
}
static void free_options(options_t *options)
{
if (options == NULL)
return;
if (options->certout != NULL)
BIO_free(options->certout);
free(options);
}
static options_t *parse_options(int argc, char *argv[])
{
options_t *options = malloc_s(sizeof(options_t));
memset(options, 0, sizeof(options_t));
/* Parameters for getopt_long() function */
static const char short_options[] = "f:c:o:V";
static const struct option long_options[] = {
{ "format", required_argument, NULL, 'f' },
{ "certoutform", required_argument, NULL, 'c' },
{ "certout", required_argument, NULL, 'o' },
{ "help", no_argument, NULL, 1 },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
int c, ind;
while ((c = getopt_long(argc, argv, short_options, long_options, &ind)))
{
if (c < 0)
break;
switch (c)
{
case 1: // --help option
usage();
exit(EXIT_SUCCESS);
case 'f':
if (output_set_format_by_name(optarg) < 0)
EXIT_ERROR("invalid format option");
break;
case 'v':
printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY);
exit(EXIT_SUCCESS);
case 'c':
options->certoutform = parse_certoutform(optarg);
break;
case 'o':
options->certout = parse_certout(optarg);
break;
case 'V':
printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "%s: try '--help' for more information\n", PROGRAM);
exit(EXIT_FAILURE);
}
}
return options;
}
/*
find stack cookies, a.k.a canary, buffer security check
option in MVS 2010
*/
static bool stack_cookies(pe_ctx_t *ctx)
{
static const unsigned char mvs2010[] = {
0x55, 0x8b, 0xec, 0x83,
0x33, 0xc5, 0x33, 0xcd,
0xe8, 0xc3
};
if (ctx == NULL)
return false;
size_t found = 0;
const uint8_t *file_bytes = LIBPE_PTR_ADD(ctx->map_addr, 0);
const uint64_t filesize = pe_filesize(ctx);
for (uint64_t ofs=0; ofs < filesize; ofs++) {
for (size_t i=0; i < sizeof(mvs2010); i++) {
if (file_bytes[ofs] == mvs2010[i] && found == i)
found++;
}
}
return found == sizeof(mvs2010);
}
static void print_certificate(BIO *out, cert_format_e format, X509 *cert)
{
if (out == NULL)
return;
switch (format) {
default:
case CERT_FORMAT_TEXT:
X509_print(out, cert);
break;
case CERT_FORMAT_PEM:
PEM_write_bio_X509(out, cert);
break;
case CERT_FORMAT_DER:
EXIT_ERROR("DER format is not yet supported for output");
break;
}
}
static int parse_pkcs7_data(const options_t *options, const CRYPT_DATA_BLOB *blob)
{
int result = 0;
const cert_format_e input_fmt = CERT_FORMAT_DER;
PKCS7 *p7 = NULL;
BIO *in = NULL;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
CRYPTO_malloc_init();
#endif
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
in = BIO_new_mem_buf(blob->pbData, blob->cbData);
if (in == NULL) {
result = -2;
goto error;
}
switch (input_fmt) {
default: EXIT_ERROR("unhandled input format for certificate");
case CERT_FORMAT_DER:
p7 = d2i_PKCS7_bio(in, NULL);
break;
case CERT_FORMAT_PEM:
p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
break;
}
if (p7 == NULL) {
ERR_print_errors_fp(stderr);
result = -3;
goto error;
}
STACK_OF(X509) *certs = NULL;
int type = OBJ_obj2nid(p7->type);
switch (type) {
default: break;
case NID_pkcs7_signed: // PKCS7_type_is_signed(p7)
certs = p7->d.sign->cert;
break;
case NID_pkcs7_signedAndEnveloped: // PKCS7_type_is_signedAndEnveloped(p7)
certs = p7->d.signed_and_enveloped->cert;
break;
}
const int numcerts = certs != NULL ? sk_X509_num(certs) : 0;
for (int i = 0; i < numcerts; i++) {
X509 *cert = sk_X509_value(certs, i);
print_certificate(options->certout, options->certoutform, cert);
// NOTE: Calling X509_free(cert) is unnecessary.
}
// Print whether certificate signature is valid
if (numcerts > 0) {
X509 *subject = sk_X509_value(certs, 0);
X509 *issuer = sk_X509_value(certs, numcerts - 1);
EVP_PKEY *issuer_pubkey = X509_get_pubkey(issuer);
int valid_sig = X509_verify(subject, issuer_pubkey);
EVP_PKEY_free(issuer_pubkey);
output("Signature", valid_sig == 1 ? "valid" : "invalid");
}
// Print signers
if (numcerts > 0) {
output_open_scope("signers", OUTPUT_SCOPE_TYPE_ARRAY);
for (int i = 0; i < numcerts; i++) {
X509 *cert = sk_X509_value(certs, i);
X509_NAME *name = X509_get_subject_name(cert);
int issuer_name_len = X509_NAME_get_text_by_NID(name, NID_commonName, NULL, 0);
if (issuer_name_len > 0) {
output_open_scope("signer", OUTPUT_SCOPE_TYPE_OBJECT);
char issuer_name[issuer_name_len + 1];
X509_NAME_get_text_by_NID(name, NID_commonName, issuer_name, issuer_name_len + 1);
output("Issuer", issuer_name);
output_close_scope(); // signer
}
}
output_close_scope(); // signers
}
error:
if (p7 != NULL)
PKCS7_free(p7);
if (in != NULL)
BIO_free(in);
// Deallocate everything from OpenSSL_add_all_algorithms
EVP_cleanup();
// Deallocate everything from ERR_load_crypto_strings
ERR_free_strings();
return result;
}
static void parse_certificates(const options_t *options, pe_ctx_t *ctx)
{
const IMAGE_DATA_DIRECTORY * const directory = pe_directory_by_entry(ctx, IMAGE_DIRECTORY_ENTRY_SECURITY);
if (directory == NULL)
return;
if (directory->VirtualAddress == 0 || directory->Size == 0)
return;
uint32_t fileOffset = directory->VirtualAddress; // This a file pointer rather than a common RVA.
// TODO(jweyrich): We should count how many certificates the file has, and based on this
// decide whether to proceed and open the certificates scope.
output_open_scope("certificates", OUTPUT_SCOPE_TYPE_ARRAY);
while (fileOffset - directory->VirtualAddress < directory->Size)
{
// Read the size of this WIN_CERTIFICATE
uint32_t *dwLength_ptr = LIBPE_PTR_ADD(ctx->map_addr, fileOffset);
if (!pe_can_read(ctx, dwLength_ptr, sizeof(uint32_t))) {
output_close_scope(); // certificates
// TODO: Should we report something?
return;
}
// Type punning
uint32_t dwLength = *(uint32_t *)dwLength_ptr;
WIN_CERTIFICATE *cert = LIBPE_PTR_ADD(ctx->map_addr, fileOffset);
if (!pe_can_read(ctx, cert, dwLength)) {
output_close_scope(); // certificates
// TODO: Should we report something?
return;
}
output_open_scope("certificate", OUTPUT_SCOPE_TYPE_OBJECT);
static char value[MAX_MSG];
snprintf(value, MAX_MSG, "%u bytes", cert->dwLength);
output("Length", value);
snprintf(value, MAX_MSG, "0x%x (%s)", cert->wRevision,
cert->wRevision == WIN_CERT_REVISION_1_0 ? "1" :
cert->wRevision == WIN_CERT_REVISION_2_0 ? "2" : "unknown");
output("Revision", value);
snprintf(value, MAX_MSG, "0x%x", cert->wCertificateType);
switch (cert->wCertificateType) {
default: bsd_strlcat(value, " (UNKNOWN)", MAX_MSG); break;
case WIN_CERT_TYPE_X509: bsd_strlcat(value, " (X509)", MAX_MSG); break;
case WIN_CERT_TYPE_PKCS_SIGNED_DATA: bsd_strlcat(value, " (PKCS_SIGNED_DATA)", MAX_MSG); break;
case WIN_CERT_TYPE_TS_STACK_SIGNED: bsd_strlcat(value, " (TS_STACK_SIGNED)", MAX_MSG); break;
}
output("Type", value);
fileOffset += utils_round_up(cert->dwLength, 8); // Offset to the next certificate.
if (fileOffset - directory->VirtualAddress > directory->Size)
EXIT_ERROR("either the attribute certificate table or the Size field is corrupted");
switch (cert->wRevision) {
default:
EXIT_ERROR("unknown wRevision");
case WIN_CERT_REVISION_1_0:
EXIT_ERROR("WIN_CERT_REVISION_1_0 is not supported");
case WIN_CERT_REVISION_2_0:
break;
}
switch (cert->wCertificateType) {
default:
EXIT_ERROR("unknown wCertificateType");
case WIN_CERT_TYPE_X509:
EXIT_ERROR("WIN_CERT_TYPE_X509 is not supported");
case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
{
CRYPT_DATA_BLOB p7data;
p7data.cbData = cert->dwLength - offsetof(WIN_CERTIFICATE, bCertificate);
p7data.pbData = cert->bCertificate;
parse_pkcs7_data(options, &p7data);
break;
}
case WIN_CERT_TYPE_TS_STACK_SIGNED:
EXIT_ERROR("WIN_CERT_TYPE_TS_STACK_SIGNED is not supported");
case WIN_CERT_TYPE_EFI_PKCS115:
EXIT_ERROR("WIN_CERT_TYPE_EFI_PKCS115 is not supported");
case WIN_CERT_TYPE_EFI_GUID:
EXIT_ERROR("WIN_CERT_TYPE_EFI_GUID is not supported");
}
output_close_scope(); // certificate
}
output_close_scope(); // certificates
}
int main(int argc, char *argv[])
{
pev_config_t config;
PEV_INITIALIZE(&config);
if (argc < 2) {
usage();
exit(EXIT_FAILURE);
}
output_set_cmdline(argc, argv);
options_t *options = parse_options(argc, argv); // opcoes
const char *path = argv[argc-1];
pe_ctx_t ctx;
pe_err_e err = pe_load_file(&ctx, path);
if (err != LIBPE_E_OK) {
pe_error_print(stderr, err);
return EXIT_FAILURE;
}
err = pe_parse(&ctx);
if (err != LIBPE_E_OK) {
pe_error_print(stderr, err);
return EXIT_FAILURE;
}
if (!pe_is_pe(&ctx))
EXIT_ERROR("not a valid PE file");
IMAGE_OPTIONAL_HEADER *optional = pe_optional(&ctx);
if (optional == NULL)
return EXIT_FAILURE;
uint16_t dllchar = 0;
switch (optional->type) {
default:
return EXIT_FAILURE;
case MAGIC_PE32:
dllchar = optional->_32->DllCharacteristics;
break;
case MAGIC_PE64:
dllchar = optional->_64->DllCharacteristics;
break;
}
output_open_document();
char field[MAX_MSG];
// aslr
snprintf(field, MAX_MSG, "ASLR");
output(field, (dllchar & 0x40) ? "yes" : "no");
// dep/nx
snprintf(field, MAX_MSG, "DEP/NX");
output(field, (dllchar & 0x100) ? "yes" : "no");
// seh
snprintf(field, MAX_MSG, "SEH");
output(field, (dllchar & 0x400) ? "no" : "yes");
// stack cookies
snprintf(field, MAX_MSG, "Stack cookies (EXPERIMENTAL)");
output(field, stack_cookies(&ctx) ? "yes" : "no");
// certificados
parse_certificates(options, &ctx);
output_close_document();
// libera a memoria
free_options(options);
// free
err = pe_unload(&ctx);
if (err != LIBPE_E_OK) {
pe_error_print(stderr, err);
return EXIT_FAILURE;
}
PEV_FINALIZE(&config);
return EXIT_SUCCESS;
}