forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
modules_pe.c
4423 lines (3607 loc) · 128 KB
/
modules_pe.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2014. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include "crypto.h"
#include "md5.h"
typedef MD5_CTX yr_md5_ctx;
#define yr_md5_init(ctx) \
MD5Init(ctx)
#define yr_md5_update(ctx,data,len) \
MD5Update(ctx,data,len)
#define yr_md5_final(digest,ctx) \
MD5Final(digest,ctx)
#if defined(HAVE_LIBCRYPTO)
#include <authenticode-parser/authenticode.h>
#include <openssl/evp.h>
#endif
#include <yara_dotnet.h>
#include <yara_endian.h>
#include <yara_mem.h>
#include <yara_modules.h>
#include <yara_pe.h>
#include <yara_pe_utils.h>
#include <yara_strutils.h>
#include <yara_utils.h>
#define MODULE_NAME pe
#define IMPORT_STANDARD 1
#define IMPORT_DELAYED 2
#define IMPORT_ANY (~0)
// http://msdn.microsoft.com/en-us/library/ms648009(v=vs.85).aspx
#define RESOURCE_TYPE_CURSOR 1
#define RESOURCE_TYPE_BITMAP 2
#define RESOURCE_TYPE_ICON 3
#define RESOURCE_TYPE_MENU 4
#define RESOURCE_TYPE_DIALOG 5
#define RESOURCE_TYPE_STRING 6
#define RESOURCE_TYPE_FONTDIR 7
#define RESOURCE_TYPE_FONT 8
#define RESOURCE_TYPE_ACCELERATOR 9
#define RESOURCE_TYPE_RCDATA 10
#define RESOURCE_TYPE_MESSAGETABLE 11
#define RESOURCE_TYPE_GROUP_CURSOR \
12 // MAKEINTRESOURCE((ULONG_PTR)(RT_CURSOR) + 11)
#define RESOURCE_TYPE_GROUP_ICON \
14 // MAKEINTRESOURCE((ULONG_PTR)(RT_ICON) + 11)
#define RESOURCE_TYPE_VERSION 16
#define RESOURCE_TYPE_DLGINCLUDE 17
#define RESOURCE_TYPE_PLUGPLAY 19
#define RESOURCE_TYPE_VXD 20
#define RESOURCE_TYPE_ANICURSOR 21
#define RESOURCE_TYPE_ANIICON 22
#define RESOURCE_TYPE_HTML 23
#define RESOURCE_TYPE_MANIFEST 24
#define RESOURCE_CALLBACK_CONTINUE 0
#define RESOURCE_CALLBACK_ABORT 1
#define RESOURCE_ITERATOR_FINISHED 0
#define RESOURCE_ITERATOR_ABORTED 1
#define MAX_PE_IMPORTS 16384
#define MAX_PE_EXPORTS 16384
#define MAX_EXPORT_NAME_LENGTH 512
#define MAX_IMPORT_DLL_NAME_LENGTH 256
#define MAX_RESOURCES 65536
#define IS_RESOURCE_SUBDIRECTORY(entry) \
(yr_le32toh((entry)->OffsetToData) & 0x80000000)
#define RESOURCE_OFFSET(entry) (yr_le32toh((entry)->OffsetToData) & 0x7FFFFFFF)
typedef int (*RESOURCE_CALLBACK_FUNC)(
PIMAGE_RESOURCE_DATA_ENTRY rsrc_data,
int rsrc_type,
int rsrc_id,
int rsrc_language,
const IMAGE_RESOURCE_DIR_STRING_U* type_string,
const IMAGE_RESOURCE_DIR_STRING_U* name_string,
const IMAGE_RESOURCE_DIR_STRING_U* lang_string,
void* cb_data);
static size_t available_space(PE* pe, void* pointer)
{
if ((uint8_t*) pointer < pe->data)
return 0;
if ((uint8_t*) pointer >= pe->data + pe->data_size)
return 0;
return pe->data + pe->data_size - (uint8_t*) pointer;
}
static int wide_string_fits_in_pe(PE* pe, char* data)
{
size_t i = 0;
size_t space_left = available_space(pe, data);
while (space_left >= 2)
{
if (data[i] == 0 && data[i + 1] == 0)
return 1;
space_left -= 2;
i += 2;
}
return 0;
}
// Parse the rich signature.
// http://www.ntcore.com/files/richsign.htm
static void pe_parse_rich_signature(PE* pe, uint64_t base_address)
{
PIMAGE_DOS_HEADER mz_header;
PRICH_SIGNATURE rich_signature = NULL;
DWORD* rich_ptr = NULL;
BYTE* raw_data = NULL;
BYTE* clear_data = NULL;
BYTE* version_data = NULL;
DWORD* p = NULL;
uint32_t nthdr_offset = 0;
uint32_t key = 0;
size_t rich_len = 0;
int64_t rich_count = 0;
if (pe->data_size < sizeof(IMAGE_DOS_HEADER))
return;
mz_header = (PIMAGE_DOS_HEADER) pe->data;
if (yr_le16toh(mz_header->e_magic) != IMAGE_DOS_SIGNATURE)
return;
// To find the Rich marker we start at the NT header and work backwards, so
// make sure we have at least enough data to get to the NT header.
nthdr_offset = yr_le32toh(mz_header->e_lfanew);
if (nthdr_offset > pe->data_size + sizeof(uint32_t) || nthdr_offset < 4)
return;
// Most files have the Rich header at offset 0x80, but that is not always
// true. 582ce3eea9c97d5e89f7d83953a6d518b16770e635a19a456c0225449c6967a4 is
// one sample which has a Rich header starting at offset 0x200. To properly
// find the Rich header we need to start at the NT header and work backwards.
p = (DWORD*) (pe->data + nthdr_offset - 4);
while (p >= (DWORD*) (pe->data + sizeof(IMAGE_DOS_HEADER)))
{
if (yr_le32toh(*p) == RICH_RICH)
{
// The XOR key is the dword following the Rich value. We use this to find
// DanS header only.
key = *(p + 1);
rich_ptr = p;
--p;
break;
}
// The NT header is 8 byte aligned so we can move back in 4 byte increments.
--p;
}
// If we haven't found a key we can skip processing the rest.
if (key == 0)
return;
// If we have found the key we need to now find the start (DanS).
while (p >= (DWORD*) (pe->data + sizeof(IMAGE_DOS_HEADER)))
{
if (yr_le32toh((*(p) ^ key)) == RICH_DANS)
{
rich_signature = (PRICH_SIGNATURE) p;
break;
}
--p;
}
if (rich_signature == NULL)
return;
// The three key values must all be equal and the first dword
// XORs to "DanS". Then walk the buffer looking for "Rich" which marks the
// end. Technically the XOR key should be right after "Rich" but it's not
// important.
if (yr_le32toh(rich_signature->key1) != yr_le32toh(rich_signature->key2) ||
yr_le32toh(rich_signature->key2) != yr_le32toh(rich_signature->key3) ||
(yr_le32toh(rich_signature->dans) ^ yr_le32toh(rich_signature->key1)) !=
RICH_DANS)
{
return;
}
// Multiply by 4 because we are counting in DWORDs.
rich_len = (rich_ptr - (DWORD*) rich_signature) * 4;
raw_data = (BYTE*) yr_malloc(rich_len);
if (!raw_data)
return;
memcpy(raw_data, rich_signature, rich_len);
yr_set_integer(
base_address + ((uint8_t*) rich_signature - pe->data),
pe->object,
"rich_signature.offset");
yr_set_integer(rich_len, pe->object, "rich_signature.length");
yr_set_integer(
yr_le32toh(rich_signature->key1), pe->object, "rich_signature.key");
clear_data = (BYTE*) yr_malloc(rich_len);
if (!clear_data)
{
yr_free(raw_data);
return;
}
// Copy the entire block here to be XORed.
memcpy(clear_data, raw_data, rich_len);
for (rich_ptr = (DWORD*) clear_data;
rich_ptr < (DWORD*) (clear_data + rich_len);
rich_ptr++)
{
*rich_ptr ^= rich_signature->key1;
}
yr_set_sized_string(
(char*) raw_data, rich_len, pe->object, "rich_signature.raw_data");
yr_free(raw_data);
yr_set_sized_string(
(char*) clear_data, rich_len, pe->object, "rich_signature.clear_data");
// Allocate space for just the version data. This is a series of every other
// dword from the clear data. This is useful to be able to hash alone.
// We need to skip the first 3 DWORDs of the RICH_SIGNATURE, which are DanS
// and XOR keys.
rich_count = (rich_len - sizeof(RICH_SIGNATURE)) / sizeof(RICH_VERSION_INFO);
version_data = (BYTE*) yr_malloc(rich_count * sizeof(DWORD));
if (!version_data)
{
yr_free(clear_data);
return;
}
rich_signature = (PRICH_SIGNATURE) clear_data;
for (int i = 0; i < rich_count; i++)
{
memcpy(
version_data + (i * sizeof(DWORD)),
&rich_signature->versions[i],
sizeof(DWORD));
}
yr_set_sized_string(
(char*) version_data,
rich_count * sizeof(DWORD),
pe->object,
"rich_signature.version_data");
yr_free(clear_data);
yr_free(version_data);
}
static void pe_parse_debug_directory(PE* pe)
{
PIMAGE_DATA_DIRECTORY data_dir;
PIMAGE_DEBUG_DIRECTORY debug_dir;
int64_t debug_dir_offset;
int i, dcount;
size_t pdb_path_len;
char* pdb_path = NULL;
data_dir = pe_get_directory_entry(pe, IMAGE_DIRECTORY_ENTRY_DEBUG);
if (data_dir == NULL)
return;
if (yr_le32toh(data_dir->Size) == 0)
return;
if (yr_le32toh(data_dir->VirtualAddress) == 0)
return;
debug_dir_offset = pe_rva_to_offset(pe, yr_le32toh(data_dir->VirtualAddress));
if (debug_dir_offset < 0)
return;
dcount = yr_le32toh(data_dir->Size) / sizeof(IMAGE_DEBUG_DIRECTORY);
for (i = 0; i < dcount; i++)
{
int64_t pcv_hdr_offset = 0;
debug_dir = (PIMAGE_DEBUG_DIRECTORY) (pe->data + debug_dir_offset +
i * sizeof(IMAGE_DEBUG_DIRECTORY));
if (!struct_fits_in_pe(pe, debug_dir, IMAGE_DEBUG_DIRECTORY))
break;
if (yr_le32toh(debug_dir->Type) != IMAGE_DEBUG_TYPE_CODEVIEW)
continue;
// The debug info offset may be present either as RVA or as raw offset
// Sample: 0249e00b6d46bee5a17096559f18e671cd0ceee36373e8708f614a9a6c7c079e
if (debug_dir->AddressOfRawData != 0)
{
pcv_hdr_offset = pe_rva_to_offset(
pe, yr_le32toh(debug_dir->AddressOfRawData));
}
// Give it chance to read it from the RAW offset
// Sample: 735f72b3fcd72789f01e923c9de2a9ab5b5ffbece23633da81d976ad0ad159e3
if (pcv_hdr_offset <= 0 && debug_dir->PointerToRawData != 0)
{
pcv_hdr_offset = yr_le32toh(debug_dir->PointerToRawData);
}
if (pcv_hdr_offset <= 0)
continue;
PCV_HEADER cv_hdr = (PCV_HEADER) (pe->data + pcv_hdr_offset);
if (!struct_fits_in_pe(pe, cv_hdr, CV_HEADER))
continue;
if (yr_le32toh(cv_hdr->dwSignature) == CVINFO_PDB20_CVSIGNATURE)
{
PCV_INFO_PDB20 pdb20 = (PCV_INFO_PDB20) cv_hdr;
if (struct_fits_in_pe(pe, pdb20, CV_INFO_PDB20))
pdb_path = (char*) (pdb20->PdbFileName);
}
else if (yr_le32toh(cv_hdr->dwSignature) == CVINFO_PDB70_CVSIGNATURE)
{
PCV_INFO_PDB70 pdb70 = (PCV_INFO_PDB70) cv_hdr;
if (struct_fits_in_pe(pe, pdb70, CV_INFO_PDB70))
pdb_path = (char*) (pdb70->PdbFileName);
}
else if (yr_le32toh(cv_hdr->dwSignature) == CODEVIEW_SIGNATURE_MTOC)
{
PMTOC_ENTRY mtoc = (PMTOC_ENTRY) cv_hdr;
if (struct_fits_in_pe(pe, mtoc, MTOC_ENTRY))
pdb_path = (char*) (mtoc->PdbFileName);
}
if (pdb_path != NULL)
{
pdb_path_len = strnlen(
pdb_path, yr_min(available_space(pe, pdb_path), MAX_PATH));
if (pdb_path_len > 0 && pdb_path_len < MAX_PATH)
{
yr_set_sized_string(pdb_path, pdb_path_len, pe->object, "pdb_path");
break;
}
}
}
}
// Return a pointer to the resource directory string or NULL.
// The callback function will parse this and call yr_set_sized_string().
// The pointer is guaranteed to have enough space to contain the entire string.
static const PIMAGE_RESOURCE_DIR_STRING_U parse_resource_name(
PE* pe,
const uint8_t* rsrc_data,
PIMAGE_RESOURCE_DIRECTORY_ENTRY entry)
{
// If high bit is set it is an offset relative to rsrc_data, which contains
// a resource directory string.
if (yr_le32toh(entry->Name) & 0x80000000)
{
const PIMAGE_RESOURCE_DIR_STRING_U pNameString =
(PIMAGE_RESOURCE_DIR_STRING_U) (rsrc_data +
(yr_le32toh(entry->Name) & 0x7FFFFFFF));
// A resource directory string is 2 bytes for the length and then a variable
// length Unicode string. Make sure we have at least 2 bytes.
if (!fits_in_pe(pe, pNameString, 2))
return NULL;
// Move past the length and make sure we have enough bytes for the string.
if (!fits_in_pe(
pe,
pNameString,
sizeof(uint16_t) + yr_le16toh(pNameString->Length) * 2))
return NULL;
return pNameString;
}
return NULL;
}
static int _pe_iterate_resources(
PE* pe,
PIMAGE_RESOURCE_DIRECTORY resource_dir,
const uint8_t* rsrc_data,
int rsrc_tree_level,
int* type,
int* id,
int* language,
const IMAGE_RESOURCE_DIR_STRING_U* type_string,
const IMAGE_RESOURCE_DIR_STRING_U* name_string,
const IMAGE_RESOURCE_DIR_STRING_U* lang_string,
RESOURCE_CALLBACK_FUNC callback,
void* callback_data)
{
int i, result = RESOURCE_ITERATOR_FINISHED;
int total_entries;
PIMAGE_RESOURCE_DIRECTORY_ENTRY entry;
// A few sanity checks to avoid corrupt files
if (yr_le32toh(resource_dir->Characteristics) != 0 ||
yr_le16toh(resource_dir->NumberOfNamedEntries) > 32768 ||
yr_le16toh(resource_dir->NumberOfIdEntries) > 32768)
{
return result;
}
total_entries = yr_le16toh(resource_dir->NumberOfNamedEntries) +
yr_le16toh(resource_dir->NumberOfIdEntries);
// The first directory entry is just after the resource directory,
// by incrementing resource_dir we skip sizeof(resource_dir) bytes
// and get a pointer to the end of the resource directory.
entry = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (resource_dir + 1);
for (i = 0; i < total_entries; i++)
{
if (!struct_fits_in_pe(pe, entry, IMAGE_RESOURCE_DIRECTORY_ENTRY))
{
result = RESOURCE_ITERATOR_ABORTED;
break;
}
switch (rsrc_tree_level)
{
case 0:
*type = yr_le32toh(entry->Name);
type_string = parse_resource_name(pe, rsrc_data, entry);
break;
case 1:
*id = yr_le32toh(entry->Name);
name_string = parse_resource_name(pe, rsrc_data, entry);
break;
case 2:
*language = yr_le32toh(entry->Name);
lang_string = parse_resource_name(pe, rsrc_data, entry);
break;
}
if (IS_RESOURCE_SUBDIRECTORY(entry) && rsrc_tree_level < 2)
{
PIMAGE_RESOURCE_DIRECTORY directory =
(PIMAGE_RESOURCE_DIRECTORY) (rsrc_data + RESOURCE_OFFSET(entry));
if (struct_fits_in_pe(pe, directory, IMAGE_RESOURCE_DIRECTORY))
{
result = _pe_iterate_resources(
pe,
directory,
rsrc_data,
rsrc_tree_level + 1,
type,
id,
language,
type_string,
name_string,
lang_string,
callback,
callback_data);
}
else
{
result = RESOURCE_ITERATOR_ABORTED;
}
}
else
{
PIMAGE_RESOURCE_DATA_ENTRY data_entry =
(PIMAGE_RESOURCE_DATA_ENTRY) (rsrc_data + RESOURCE_OFFSET(entry));
if (struct_fits_in_pe(pe, data_entry, IMAGE_RESOURCE_DATA_ENTRY))
{
if (callback(
data_entry,
*type,
*id,
*language,
type_string,
name_string,
lang_string,
callback_data) == RESOURCE_CALLBACK_ABORT)
{
result = RESOURCE_ITERATOR_ABORTED;
}
}
else
{
result = RESOURCE_ITERATOR_ABORTED;
}
}
if (result == RESOURCE_ITERATOR_ABORTED)
break;
entry++;
}
return result;
}
static int pe_iterate_resources(
PE* pe,
RESOURCE_CALLBACK_FUNC callback,
void* callback_data)
{
int64_t offset;
int type = -1;
int id = -1;
int language = -1;
IMAGE_RESOURCE_DIR_STRING_U* type_string = NULL;
IMAGE_RESOURCE_DIR_STRING_U* name_string = NULL;
IMAGE_RESOURCE_DIR_STRING_U* lang_string = NULL;
PIMAGE_DATA_DIRECTORY directory = pe_get_directory_entry(
pe, IMAGE_DIRECTORY_ENTRY_RESOURCE);
if (directory == NULL)
return 0;
if (yr_le32toh(directory->VirtualAddress) != 0)
{
PIMAGE_RESOURCE_DIRECTORY rsrc_dir;
offset = pe_rva_to_offset(pe, yr_le32toh(directory->VirtualAddress));
if (offset < 0)
return 0;
rsrc_dir = (PIMAGE_RESOURCE_DIRECTORY) (pe->data + offset);
if (struct_fits_in_pe(pe, rsrc_dir, IMAGE_RESOURCE_DIRECTORY))
{
yr_set_integer(
yr_le32toh(rsrc_dir->TimeDateStamp),
pe->object,
"resource_timestamp");
yr_set_integer(
yr_le16toh(rsrc_dir->MajorVersion),
pe->object,
"resource_version.major");
yr_set_integer(
yr_le16toh(rsrc_dir->MinorVersion),
pe->object,
"resource_version.minor");
_pe_iterate_resources(
pe,
rsrc_dir,
pe->data + offset,
0,
&type,
&id,
&language,
type_string,
name_string,
lang_string,
callback,
callback_data);
return 1;
}
}
return 0;
}
// Align offset to a 32-bit boundary and add it to a pointer
#define ADD_OFFSET(ptr, offset) \
(PVERSION_INFO)((uint8_t*) (ptr) + ((offset + 3) & ~3))
static void pe_parse_version_info(PIMAGE_RESOURCE_DATA_ENTRY rsrc_data, PE* pe)
{
PVERSION_INFO version_info;
int64_t version_info_offset = pe_rva_to_offset(
pe, yr_le32toh(rsrc_data->OffsetToData));
if (version_info_offset < 0)
return;
version_info = (PVERSION_INFO) (pe->data + version_info_offset);
if (!struct_fits_in_pe(pe, version_info, VERSION_INFO))
return;
if (!fits_in_pe(pe, version_info->Key, sizeof("VS_VERSION_INFO") * 2))
return;
if (strcmp_w(version_info->Key, "VS_VERSION_INFO") != 0)
return;
version_info = ADD_OFFSET(version_info, sizeof(VERSION_INFO) + 86);
while (fits_in_pe(pe, version_info->Key, sizeof("VarFileInfo") * 2) &&
strcmp_w(version_info->Key, "VarFileInfo") == 0 &&
yr_le16toh(version_info->Length) != 0)
{
version_info = ADD_OFFSET(version_info, yr_le16toh(version_info->Length));
}
while (fits_in_pe(pe, version_info->Key, sizeof("StringFileInfo") * 2) &&
strcmp_w(version_info->Key, "StringFileInfo") == 0 &&
yr_le16toh(version_info->Length) != 0)
{
PVERSION_INFO string_table = ADD_OFFSET(
version_info, sizeof(VERSION_INFO) + 30);
version_info = ADD_OFFSET(version_info, yr_le16toh(version_info->Length));
while (struct_fits_in_pe(pe, string_table, VERSION_INFO) &&
wide_string_fits_in_pe(pe, string_table->Key) &&
yr_le16toh(string_table->Length) != 0 && string_table < version_info)
{
PVERSION_INFO string = ADD_OFFSET(
string_table,
sizeof(VERSION_INFO) + 2 * (strnlen_w(string_table->Key) + 1));
string_table = ADD_OFFSET(string_table, yr_le16toh(string_table->Length));
while (struct_fits_in_pe(pe, string, VERSION_INFO) &&
wide_string_fits_in_pe(pe, string->Key) &&
yr_le16toh(string->Length) != 0 && string < string_table)
{
char* string_value = (char*) ADD_OFFSET(
string, sizeof(VERSION_INFO) + 2 * (strnlen_w(string->Key) + 1));
if (wide_string_fits_in_pe(pe, string_value))
{
char key[64];
char value[256];
strlcpy_w(key, string->Key, sizeof(key));
strlcpy_w(value, string_value, sizeof(value));
// null terminator of string is not included in version value when
// ValueLength is zero
if (yr_le16toh(string->ValueLength) == 0)
value[yr_le16toh(string->ValueLength)] = '\0';
yr_set_string(value, pe->object, "version_info[%s]", key);
yr_set_string(
key, pe->object, "version_info_list[%i].key", pe->version_infos);
yr_set_string(
value,
pe->object,
"version_info_list[%i].value",
pe->version_infos);
pe->version_infos += 1;
}
string = ADD_OFFSET(string, yr_le16toh(string->Length));
}
}
}
}
static void pe_set_resource_string_or_id(
IMAGE_RESOURCE_DIR_STRING_U* rsrc_string,
int rsrc_int,
const char* string_description,
const char* int_description,
PE* pe)
{
if (rsrc_string)
{
// Multiply by 2 because it is a Unicode string.
size_t length = yr_le16toh(rsrc_string->Length) * 2;
// Check if the whole string fits in the PE image.
// If not, the name becomes UNDEFINED by default.
if (fits_in_pe(pe, rsrc_string->NameString, length))
{
yr_set_sized_string(
(char*) rsrc_string->NameString,
length,
pe->object,
string_description,
pe->resources);
}
}
else
{
yr_set_integer(rsrc_int, pe->object, int_description, pe->resources);
}
}
static int pe_collect_resources(
PIMAGE_RESOURCE_DATA_ENTRY rsrc_data,
int rsrc_type,
int rsrc_id,
int rsrc_language,
IMAGE_RESOURCE_DIR_STRING_U* type_string,
IMAGE_RESOURCE_DIR_STRING_U* name_string,
IMAGE_RESOURCE_DIR_STRING_U* lang_string,
PE* pe)
{
// Don't collect too many resources.
if (pe->resources > MAX_RESOURCES)
return RESOURCE_CALLBACK_CONTINUE;
yr_set_integer(
yr_le32toh(rsrc_data->OffsetToData),
pe->object,
"resources[%i].rva",
pe->resources);
int64_t offset = pe_rva_to_offset(pe, yr_le32toh(rsrc_data->OffsetToData));
if (offset < 0)
offset = YR_UNDEFINED;
yr_set_integer(offset, pe->object, "resources[%i].offset", pe->resources);
yr_set_integer(
yr_le32toh(rsrc_data->Size),
pe->object,
"resources[%i].length",
pe->resources);
pe_set_resource_string_or_id(
type_string,
rsrc_type,
"resources[%i].type_string",
"resources[%i].type",
pe);
pe_set_resource_string_or_id(
name_string,
rsrc_id,
"resources[%i].name_string",
"resources[%i].id",
pe);
pe_set_resource_string_or_id(
lang_string,
rsrc_language,
"resources[%i].language_string",
"resources[%i].language",
pe);
// Resources we do extra parsing on
if (rsrc_type == RESOURCE_TYPE_VERSION)
pe_parse_version_info(rsrc_data, pe);
pe->resources += 1;
return RESOURCE_CALLBACK_CONTINUE;
}
// Function names should have only lowercase, uppercase, digits and a small
// subset of special characters. This is to match behavior of pefile. See
// https://github.com/erocarrera/pefile/blob/593d094e35198dad92aaf040bef17eb800c8a373/pefile.py#L2326-L2348
static int valid_function_name(char* name)
{
if (!strcmp(name, ""))
return 0;
size_t i = 0;
for (char c = name[i]; c != '\x00'; c = name[++i])
{
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') &&
!(c >= '0' && c <= '9') && c != '.' && c != '_' && c != '?' &&
c != '@' && c != '$' && c != '(' && c != ')' && c != '<' && c != '>')
return 0;
}
return 1;
}
static IMPORT_FUNCTION* pe_parse_import_descriptor(
PE* pe,
PIMAGE_IMPORT_DESCRIPTOR import_descriptor,
char* dll_name,
int* num_function_imports)
{
IMPORT_FUNCTION* head = NULL;
IMPORT_FUNCTION* tail = NULL;
// This is tracked separately from num_function_imports because that is the
// number of successfully parsed imports, while this is the number of imports
// attempted to be parsed. This allows us to stop parsing on too many imports
// while still accurately recording the number of successfully parsed imports.
int parsed_imports = 0;
int64_t offset = pe_rva_to_offset(
pe, yr_le32toh(import_descriptor->OriginalFirstThunk));
// I've seen binaries where OriginalFirstThunk is zero. In this case
// use FirstThunk.
if (offset <= 0)
offset = pe_rva_to_offset(pe, yr_le32toh(import_descriptor->FirstThunk));
if (offset < 0)
return NULL;
if (IS_64BITS_PE(pe))
{
PIMAGE_THUNK_DATA64 thunks64 = (PIMAGE_THUNK_DATA64) (pe->data + offset);
uint64_t func_idx = 0;
while (struct_fits_in_pe(pe, thunks64, IMAGE_THUNK_DATA64) &&
yr_le64toh(thunks64->u1.Ordinal) != 0 &&
parsed_imports < MAX_PE_IMPORTS)
{
char* name = NULL;
uint16_t ordinal = 0;
uint8_t has_ordinal = 0;
uint64_t rva_address = 0;
parsed_imports++;
if (!(yr_le64toh(thunks64->u1.Ordinal) & IMAGE_ORDINAL_FLAG64))
{
// If imported by name
offset = pe_rva_to_offset(pe, yr_le64toh(thunks64->u1.Function));
if (offset >= 0)
{
PIMAGE_IMPORT_BY_NAME import = (PIMAGE_IMPORT_BY_NAME) (pe->data +
offset);
if (struct_fits_in_pe(pe, import, IMAGE_IMPORT_BY_NAME))
{
name = (char*) yr_strndup(
(char*) import->Name,
yr_min(available_space(pe, import->Name), 512));
}
}
}
else
{
// If imported by ordinal. Lookup the ordinal.
name = ord_lookup(dll_name, yr_le64toh(thunks64->u1.Ordinal) & 0xFFFF);
// Also store the ordinal.
ordinal = yr_le64toh(thunks64->u1.Ordinal) & 0xFFFF;
has_ordinal = 1;
}
rva_address = yr_le32toh(import_descriptor->FirstThunk) +
(sizeof(uint64_t) * func_idx);
if (name != NULL && !valid_function_name(name))
{
yr_free(name);
thunks64++;
func_idx++;
continue;
}
if (name != NULL || has_ordinal == 1)
{
IMPORT_FUNCTION* imported_func = (IMPORT_FUNCTION*) yr_calloc(
1, sizeof(IMPORT_FUNCTION));
if (imported_func == NULL)
{
yr_free(name);
}
else
{
imported_func->name = name;
imported_func->ordinal = ordinal;
imported_func->has_ordinal = has_ordinal;
imported_func->rva = rva_address;
imported_func->next = NULL;
if (head == NULL)
head = imported_func;
if (tail != NULL)
tail->next = imported_func;
tail = imported_func;
(*num_function_imports)++;
}
}
thunks64++;
func_idx++;
}
}
else
{
PIMAGE_THUNK_DATA32 thunks32 = (PIMAGE_THUNK_DATA32) (pe->data + offset);
uint32_t func_idx = 0;
while (struct_fits_in_pe(pe, thunks32, IMAGE_THUNK_DATA32) &&
yr_le32toh(thunks32->u1.Ordinal) != 0 &&
*num_function_imports < MAX_PE_IMPORTS)
{
char* name = NULL;
uint16_t ordinal = 0;
uint8_t has_ordinal = 0;
uint32_t rva_address = 0;
parsed_imports++;
if (!(yr_le32toh(thunks32->u1.Ordinal) & IMAGE_ORDINAL_FLAG32))
{
// If imported by name
offset = pe_rva_to_offset(pe, yr_le32toh(thunks32->u1.Function));
if (offset >= 0)
{
PIMAGE_IMPORT_BY_NAME import = (PIMAGE_IMPORT_BY_NAME) (pe->data +
offset);
if (struct_fits_in_pe(pe, import, IMAGE_IMPORT_BY_NAME))
{
name = (char*) yr_strndup(
(char*) import->Name,
yr_min(available_space(pe, import->Name), 512));
}
}
}
else
{
// If imported by ordinal. Lookup the ordinal.
name = ord_lookup(dll_name, yr_le32toh(thunks32->u1.Ordinal) & 0xFFFF);
// Also store the ordinal.
ordinal = yr_le32toh(thunks32->u1.Ordinal) & 0xFFFF;
has_ordinal = 1;
}
rva_address = yr_le32toh(import_descriptor->FirstThunk) +
(sizeof(uint32_t) * func_idx);
if (name != NULL && !valid_function_name(name))