-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathpe_header.cpp
1772 lines (1544 loc) · 71 KB
/
pe_header.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
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
#include "StdAfx.h"
#include "pe_header.h"
pe_header::pe_header( char* filename, PD_OPTIONS* options )
{
this->_options = options;
this->_image_size = 0;
this->_raw_header_size = 0;
this->_disk_image_size = 0;
this->_stream = (stream_wrapper*) new file_stream( filename );
_original_base = 0;
_unique_hash = 0;
_name_filepath_long_size = 0;
_name_filepath_long = NULL;
_name_filepath_short_size = 0;
_name_filepath_short = NULL;
_name_original_exports_size = 0;
_name_original_exports = NULL;
_name_original_manifest_size = 0;
_name_original_manifest = NULL;
_name_symbols_path_size = 0;
_name_symbols_path = NULL;
_export_list = NULL;
this->_parsed_dos = false;
this->_parsed_pe_32 = false;
this->_parsed_pe_64 = false;
this->_parsed_sections = false;
this->_image_size = 0;
this->_disk_image_size = 0;
this->_unique_hash = 0;
if( _stream != NULL )
{
// Assign the disk filename for this file
_name_filepath_long = new char[FILEPATH_SIZE];
_name_filepath_long_size = _stream->get_long_name( _name_filepath_long, FILEPATH_SIZE );
_name_filepath_short = new char[FILEPATH_SIZE];
_name_filepath_short_size = _stream->get_short_name( _name_filepath_short, FILEPATH_SIZE );
}
if( _options->Verbose )
fprintf( stdout, "INFO: Initialized header for module name %s.\r\n", this->get_name() );
}
export_list* pe_header::get_exports()
{
if( (_parsed_pe_32 || _parsed_pe_64) && _export_list != NULL )
{
return this->_export_list;
}
return NULL;
}
pe_header::pe_header( DWORD pid, void* base, module_list* modules, PD_OPTIONS* options )
{
this->_options = options;
this->_image_size = 0;
this->_raw_header_size = 0;
this->_disk_image_size = 0;
_unique_hash = 0;
_header_export_directory = NULL;
_header_import_descriptors = NULL;
_name_filepath_long_size = 0;
_name_filepath_long = NULL;
_name_filepath_short_size = 0;
_name_filepath_short = NULL;
_name_original_exports_size = 0;
_name_original_exports = NULL;
_name_original_manifest_size = 0;
_name_original_manifest = NULL;
_name_symbols_path_size = 0;
_name_symbols_path = NULL;
_export_list = NULL;
this->_parsed_dos = false;
this->_parsed_pe_32 = false;
this->_parsed_pe_64 = false;
this->_parsed_sections = false;
this->_image_size = 0;
this->_disk_image_size = 0;
this->_unique_hash = 0;
this->_stream = (stream_wrapper*) new process_stream( pid, base, modules );
_original_base = base;
if( _stream != NULL )
{
// Assign the disk filename for this file
_name_filepath_long = new char[FILEPATH_SIZE];
_name_filepath_long_size = _stream->get_long_name( _name_filepath_long, FILEPATH_SIZE );
_name_filepath_short = new char[FILEPATH_SIZE];
_name_filepath_short_size = _stream->get_short_name( _name_filepath_short, FILEPATH_SIZE );
}
if( _options->Verbose )
fprintf( stdout, "INFO: Initialized header for module name %s.\r\n", this->get_name() );
}
pe_header::pe_header( DWORD pid, module_list* modules, PD_OPTIONS* options )
{
this->_options = options;
this->_image_size = 0;
this->_raw_header_size = 0;
this->_disk_image_size = 0;
_unique_hash = 0;
_header_export_directory = NULL;
_header_import_descriptors = NULL;
_name_filepath_long_size = 0;
_name_filepath_long = NULL;
_name_filepath_short_size = 0;
_name_filepath_short = NULL;
_name_original_exports_size = 0;
_name_original_exports = NULL;
_name_original_manifest_size = 0;
_name_original_manifest = NULL;
_name_symbols_path_size = 0;
_name_symbols_path = NULL;
_export_list = NULL;
this->_parsed_dos = false;
this->_parsed_pe_32 = false;
this->_parsed_pe_64 = false;
this->_parsed_sections = false;
this->_image_size = 0;
this->_disk_image_size = 0;
this->_unique_hash = 0;
this->_stream = (stream_wrapper*) new process_stream( pid, modules );
_original_base = ((process_stream*) _stream)->base;
if( _options->Verbose )
fprintf( stdout, "INFO: Initialized header for module name %s.\r\n", this->get_name() );
}
pe_header::pe_header( HANDLE ph, void* base, module_list* modules, PD_OPTIONS* options )
{
this->_options = options;
this->_image_size = 0;
this->_raw_header_size = 0;
this->_disk_image_size = 0;
_unique_hash = 0;
_name_filepath_long_size = 0;
_name_filepath_long = NULL;
_name_filepath_short_size = 0;
_name_filepath_short = NULL;
_name_original_exports_size = 0;
_name_original_exports = NULL;
_name_original_manifest_size = 0;
_name_original_manifest = NULL;
_name_symbols_path_size = 0;
_name_symbols_path = NULL;
_export_list = NULL;
this->_parsed_dos = false;
this->_parsed_pe_32 = false;
this->_parsed_pe_64 = false;
this->_parsed_sections = false;
this->_image_size = 0;
this->_disk_image_size = 0;
this->_unique_hash = 0;
this->_stream = (stream_wrapper*) new process_stream( ph, base );
_original_base = base;
if( _options->Verbose )
fprintf( stdout, "INFO: Initialized header for module name %s.\r\n", this->get_name() );
}
void pe_header::print_report(FILE* stream)
{
// Print the on-disk filepath if there is an associated file
// Print the original filename specified by the exports table if it has one
// Print the original filename specified by the manifest file if it has one
// Print the symbols .pdb file path and name if found
// Print the basic information
}
bool pe_header::somewhat_parsed()
{
return _parsed_pe_32 || _parsed_pe_64;
}
bool pe_header::is_dll()
{
if( this->_parsed_pe_32 )
return (this->_header_pe32->FileHeader.Characteristics & IMAGE_FILE_DLL);
if( this->_parsed_pe_64 )
return (this->_header_pe64->FileHeader.Characteristics & IMAGE_FILE_DLL);
return false;
}
bool pe_header::is_exe()
{
if( this->_parsed_pe_32 )
return !(this->_header_pe32->FileHeader.Characteristics & IMAGE_FILE_DLL) && !(this->_header_pe32->FileHeader.Characteristics & IMAGE_FILE_SYSTEM);
if( this->_parsed_pe_64 )
return !(this->_header_pe64->FileHeader.Characteristics & IMAGE_FILE_DLL) && !(this->_header_pe64->FileHeader.Characteristics & IMAGE_FILE_SYSTEM);
return false;
}
bool pe_header::is_sys()
{
if( this->_parsed_pe_32 )
return this->_header_pe32->FileHeader.Characteristics & IMAGE_FILE_SYSTEM;
if( this->_parsed_pe_64 )
return this->_header_pe64->FileHeader.Characteristics & IMAGE_FILE_SYSTEM;
return false;
}
bool pe_header::is_64()
{
return this->_parsed_pe_64;
}
void pe_header::set_name(char* new_name)
{
// Set name to sue for this module
if( _name_filepath_short != NULL )
delete _name_filepath_short;
// Localize
_name_filepath_short = new char[strlen(new_name) + 1];
strcpy_s(_name_filepath_short, strlen(new_name) + 1, new_name);
_name_filepath_short_size = strlen(_name_filepath_short);
}
char* pe_header::get_name()
{
// Return the name of this module if available.
if( this->_name_filepath_short_size > 0 && _name_filepath_short != NULL )
return _name_filepath_short;
return "hiddenmodule";
}
unsigned __int64 pe_header::get_virtual_size()
{
if( this->_parsed_pe_32 || this->_parsed_pe_64 )
{
return _image_size;
}
return 0;
}
bool pe_header::process_hash( )
{
// Build the hash of this library if has been loaded
this->_unique_hash = 0;
if( this->_parsed_pe_32 || this->_parsed_pe_64 )
{
// Hash the PE directory up until the end of the section definition, and hashes
// this with the length of the import table, and number of modules in the import
// table
// First calculate begin hashing from the import table entries
SIZE_T offset = 0;
SIZE_T read_size = 0;
if( _parsed_pe_32 )
{
offset = _header_pe32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress;
read_size = 4;
}
else
{
offset = _header_pe64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress;
read_size = 8;
}
unsigned __int64 last_dw = -1;
// Hash the IAT overview (not the exact values, just the structure).
bool more;
do
{
more = false;
if( _test_read( _image, _image_size, _image + offset, read_size ) )
{
unsigned __int64 new_dw;
if( read_size == 4 )
new_dw = *((DWORD*) (_image + (long) offset));
if( read_size == 8 )
new_dw = *((unsigned __int64*) (_image + (long) offset));
if( new_dw == 0 && last_dw == 0 )
break;
if( new_dw == 0 )
{
// New module hash
_unique_hash = _unique_hash ^ 0x8ADFA91F8ADFA91F;
_unique_hash = _rotl64(_unique_hash, 0x13);
}
else
{
// New import in module hash
_unique_hash = _unique_hash ^ 0x18F31A228FA9B17A;
_unique_hash = _rotl64(_unique_hash, 0x17);
}
offset += read_size;
last_dw = new_dw;
more = true;
}
}while(more);
/*
// Hash this with the DOS header
unsigned char* start = 0;
SIZE_T length = 0;
start = (unsigned char*) &_header_dos;
length = sizeof(IMAGE_DOS_HEADER);
for( unsigned char* i = start; i + 8 < start + length; i+= 4 )
{
// Hash with this segment
_unique_hash = _unique_hash ^ *((unsigned __int32*)(i));
_unique_hash = _rotl64(_unique_hash, 0x19);
}
*/
// Hash this with some of the section information
if( this->_parsed_sections )
{
for( int i = 0; i < this->_num_sections; i++ )
{
// Hash with this section description
_unique_hash = _unique_hash ^ *((unsigned __int64*)(&_header_sections[i].Name));
_unique_hash = _rotl64(_unique_hash, 0x21);
_unique_hash = _unique_hash ^ _header_sections[i].SizeOfRawData;
_unique_hash = _rotl64(_unique_hash, 0x13);
_unique_hash = _unique_hash ^ _header_sections[i].Characteristics;
_unique_hash = _rotl64(_unique_hash, 0x17);
}
}
return true;
}
return false;
return true;
}
bool pe_header::write_image( char* filename )
{
// Writes the loaded and reconstructed memory image to a file
if( _disk_image_size > 0 )
{
FILE* fh = fopen( filename, "wb" );
if( fh != NULL )
{
// Write the image
fwrite( _disk_image, 1, _disk_image_size, fh );
fclose(fh);
}
}
return false;
}
IMPORT_SUMMARY pe_header::get_imports_information( export_list* exports )
{
return get_imports_information( exports, _image_size );
}
IMPORT_SUMMARY pe_header::get_imports_information( export_list* exports, __int64 size_limit )
{
// Builds a structure of information about the imports declared by this PE object. This includes:
// # of different import addresses
// # of code locations that imported
// Generic import hash
// Specific import hash
// Gets the number of distinct import addresses that are imported.
unordered_set<unsigned __int64> import_addresses;
if( _options->Verbose )
printf( "INFO: Building import information.\r\n" );
IMPORT_SUMMARY result;
result.COUNT_UNIQUE_IMPORT_ADDRESSES = 0;
result.COUNT_UNIQUE_IMPORT_LIBRARIES = 0;
result.HASH_GENERIC = 0;
result.HASH_SPECIFIC = 0;
size_t hash_generic = 0x1a78ac10;
size_t hash_specific = 0x1a78ac10;
hash<string> hasher;
if( this->_parsed_sections )
{
// Add matches to exports in this process
unsigned __int32 cand32_last = 0;
unsigned __int64 cand64_last = 0;
for(__int64 offset = 0; offset < _image_size - 8 && offset < size_limit - 8; offset+=4 )
{
// Check if this 4-gram or 8-gram points to an export
unsigned __int32 cand32 = *((__int32*)(_image + offset));
if (cand32 != cand32_last)
{
if (exports->contains(cand32))
{
export_entry entry = exports->find(cand32);
// Found an import reference
unordered_set<unsigned __int64>::const_iterator gotImportAddress = import_addresses.find(cand32);
if (gotImportAddress == import_addresses.end())
{
// Add this new import
import_addresses.insert(cand32);
result.COUNT_UNIQUE_IMPORT_ADDRESSES++;
// Add this imported function hash
if (entry.name != NULL)
{
hash_generic = hash_generic ^ hasher(string(entry.name));
hash_specific = hash_specific ^ hasher(string(entry.name));
}
if (entry.library_name != NULL)
{
hash_generic = hash_generic ^ (hasher(string(entry.library_name)) << 1);
hash_specific = hash_specific ^ (hasher(string(entry.library_name)) << 1);
}
hash_generic = _rotl(hash_generic, 0x05);
hash_specific = hash_specific ^ offset;
hash_specific = _rotl(hash_specific, 0x05);
}
}
}
cand32_last = cand32;
unsigned __int64 cand64 = *((unsigned __int64*)(_image + offset));
if (cand64 != cand64_last && cand64 > 0xffffffff)
{
if (exports->contains(cand64))
{
export_entry entry = exports->find(cand64);
// Found an import reference
unordered_set<unsigned __int64>::const_iterator gotImportAddress = import_addresses.find(cand64);
if (gotImportAddress == import_addresses.end())
{
// Add this new import
import_addresses.insert(cand64);
result.COUNT_UNIQUE_IMPORT_ADDRESSES++;
// Add this imported function hash
if (entry.name != NULL)
{
hash_generic = hash_generic ^ hasher(string(entry.name));
hash_specific = hash_specific ^ hasher(string(entry.name));
}
if (entry.library_name != NULL)
{
hash_generic = hash_generic ^ (hasher(string(entry.library_name)) << 1);
hash_specific = hash_specific ^ (hasher(string(entry.library_name)) << 1);
}
hash_generic = _rotl(hash_generic, 0x05);
hash_specific = hash_specific ^ offset;
hash_specific = _rotl(hash_specific, 0x05);
}
}
}
cand64_last = cand64;
}
}
result.HASH_GENERIC = hash_generic;
result.HASH_SPECIFIC = hash_specific;
if( _options->Verbose )
{
printf( "INFO: Finished building import information:\r\n" );
printf( "INFO: Count Unique Import Addresses = %i\r\n", result.COUNT_UNIQUE_IMPORT_ADDRESSES );
printf( "INFO: Count Unique Import Libraries = %i\r\n", result.COUNT_UNIQUE_IMPORT_LIBRARIES );
printf( "INFO: Generic Hash = 0x%llX\r\n", result.HASH_GENERIC );
printf( "INFO: Specific Hash = 0x%llX\r\n", result.HASH_SPECIFIC );
}
return result;
}
unsigned __int64 pe_header::get_hash()
{
//return rand() + (rand() << 32);
if( _unique_hash == 0 )
process_hash();
return _unique_hash;
}
bool pe_header::build_pe_header( __int64 size, bool amd64 )
{
return build_pe_header( size, amd64, 99 ); // Build it with as many sections as we can.
}
bool pe_header::build_pe_header( __int64 size, bool amd64, int num_sections_limit )
{
if( _stream != NULL )
{
_raw_header_size = 0x2000;
_raw_header = new unsigned char[_raw_header_size];
memset( _raw_header, 0, _raw_header_size );
_original_base = (void*) ((__int64) _original_base - (__int64) _raw_header_size);
_stream->update_base(-(__int64) _raw_header_size);
// Build the old dos header
_header_dos = (IMAGE_DOS_HEADER*) _raw_header;
_header_dos->e_magic=0x5a4d;
_header_dos->e_cblp=0x0090;
_header_dos->e_cp=0x0003;
_header_dos->e_crlc=0x0000;
_header_dos->e_cparhdr=0x0004;
_header_dos->e_minalloc=0x0000;
_header_dos->e_maxalloc=0xffff;
_header_dos->e_ss=0x0000;
_header_dos->e_sp=0x00b8;
_header_dos->e_csum=0x0000;
_header_dos->e_ip=0x0000;
_header_dos->e_cs=0x0000;
_header_dos->e_lfarlc=0x0040;
_header_dos->e_ovno=0x0000;
memset( &_header_dos->e_res, 0, sizeof(WORD)*4 );
_header_dos->e_oemid=0x0000;
_header_dos->e_oeminfo=0x0000;
memset( &_header_dos->e_res2, 0, sizeof(WORD)*10 );
_header_dos->e_lfanew=0x000000e0;
this->_parsed_dos = true;
unsigned char* base_pe = _header_dos->e_lfanew + _raw_header;
if( !amd64 )
{
// Build intel 32 bit PE header
_header_pe32 = (IMAGE_NT_HEADERS32*) base_pe;
_header_pe32->Signature = 0x00004550;
_header_pe32->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
_header_pe32->FileHeader.NumberOfSections = 1;
_header_pe32->FileHeader.NumberOfSymbols = 0;
_header_pe32->FileHeader.PointerToSymbolTable = 0;
_header_pe32->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER32);
if( _options->ReconstructHeaderAsDll )
_header_pe32->FileHeader.Characteristics = 0x0002; // Exe: 0x0002
else
_header_pe32->FileHeader.Characteristics = 0x2000; // Dll: 0x2000
_header_pe32->OptionalHeader.Magic=0x10b;
_header_pe32->OptionalHeader.MajorLinkerVersion=0x08;
_header_pe32->OptionalHeader.MinorLinkerVersion=0x00;
_header_pe32->OptionalHeader.SizeOfCode=0x00000000;
_header_pe32->OptionalHeader.SizeOfInitializedData=0x00000000;
_header_pe32->OptionalHeader.SizeOfUninitializedData=0x00000000;
_header_pe32->OptionalHeader.AddressOfEntryPoint=0x2000; // Made up, start of first section
_header_pe32->OptionalHeader.BaseOfCode=0x00002000;
_header_pe32->OptionalHeader.ImageBase= (DWORD)_original_base; // Set to current address
_header_pe32->OptionalHeader.SectionAlignment=0x00001000;
_header_pe32->OptionalHeader.FileAlignment=0x000001000;
_header_pe32->OptionalHeader.MajorOperatingSystemVersion=0x0004;
_header_pe32->OptionalHeader.MinorOperatingSystemVersion=0x0000;
_header_pe32->OptionalHeader.MajorImageVersion=0x0000;
_header_pe32->OptionalHeader.MinorImageVersion=0x0000;
_header_pe32->OptionalHeader.MajorSubsystemVersion=0x0005;
_header_pe32->OptionalHeader.MinorSubsystemVersion=0x0002;
_header_pe32->OptionalHeader.Win32VersionValue=0x00000000;
_header_pe32->OptionalHeader.SizeOfImage=0x00006000;
_header_pe32->OptionalHeader.SizeOfHeaders=0x00002000;
_header_pe32->OptionalHeader.CheckSum=0x00000000;
_header_pe32->OptionalHeader.Subsystem=0x0003;
_header_pe32->OptionalHeader.DllCharacteristics=0x0000; // 0x2000
_header_pe32->OptionalHeader.SizeOfStackReserve=0x0000000000100000;
_header_pe32->OptionalHeader.SizeOfStackCommit=0x0000000000001000;
_header_pe32->OptionalHeader.SizeOfHeapReserve=0x0000000000100000;
_header_pe32->OptionalHeader.SizeOfHeapCommit=0x0000000000001000;
_header_pe32->OptionalHeader.LoaderFlags=0x00000000;
_header_pe32->OptionalHeader.NumberOfRvaAndSizes=0x00000010;
memset( &_header_pe32->OptionalHeader.DataDirectory, 0, sizeof(IMAGE_DATA_DIRECTORY)*IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
_header_sections = (IMAGE_SECTION_HEADER*) (base_pe + sizeof(IMAGE_NT_HEADERS32));
this->_parsed_pe_32 = true;
}
else
{
// Build intel 64 bit PE header
_header_pe64 = (IMAGE_NT_HEADERS64*) base_pe;
_header_pe64->Signature = 0x00004550;
_header_pe64->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
_header_pe64->FileHeader.NumberOfSections = 1;
_header_pe64->FileHeader.NumberOfSymbols = 0;
_header_pe64->FileHeader.PointerToSymbolTable = 0;
_header_pe64->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64);
if( _options->ReconstructHeaderAsDll )
_header_pe64->FileHeader.Characteristics = 0x0002; // Exe: 0x0002
else
_header_pe64->FileHeader.Characteristics = 0x2000; // Dll: 0x2000
_header_pe64->OptionalHeader.Magic=0x020b;
_header_pe64->OptionalHeader.MajorLinkerVersion=0x08;
_header_pe64->OptionalHeader.MinorLinkerVersion=0x00;
_header_pe64->OptionalHeader.SizeOfCode=0x00000000;
_header_pe64->OptionalHeader.SizeOfInitializedData=0x00000000;
_header_pe64->OptionalHeader.SizeOfUninitializedData=0x00000000;
// Select the entry point
_header_pe64->OptionalHeader.AddressOfEntryPoint=0x2000; // Made up, start of first section
_header_pe64->OptionalHeader.BaseOfCode=0x00002000;
_header_pe64->OptionalHeader.ImageBase= (__int64)_original_base; // Set to current address
_header_pe64->OptionalHeader.SectionAlignment=0x00001000;
_header_pe64->OptionalHeader.FileAlignment=0x000001000;
_header_pe64->OptionalHeader.MajorOperatingSystemVersion=0x0004;
_header_pe64->OptionalHeader.MinorOperatingSystemVersion=0x0000;
_header_pe64->OptionalHeader.MajorImageVersion=0x0000;
_header_pe64->OptionalHeader.MinorImageVersion=0x0000;
_header_pe64->OptionalHeader.MajorSubsystemVersion=0x0005;
_header_pe64->OptionalHeader.MinorSubsystemVersion=0x0002;
_header_pe64->OptionalHeader.Win32VersionValue=0x00000000;
_header_pe64->OptionalHeader.SizeOfImage=0x00006000;
_header_pe64->OptionalHeader.SizeOfHeaders=0x00002000;
_header_pe64->OptionalHeader.CheckSum=0x00000000;
_header_pe64->OptionalHeader.Subsystem=0x0003;
_header_pe64->OptionalHeader.DllCharacteristics=0x0000;
_header_pe64->OptionalHeader.SizeOfStackReserve=0x0000000000100000;
_header_pe64->OptionalHeader.SizeOfStackCommit=0x0000000000001000;
_header_pe64->OptionalHeader.SizeOfHeapReserve=0x0000000000100000;
_header_pe64->OptionalHeader.SizeOfHeapCommit=0x0000000000001000;
_header_pe64->OptionalHeader.LoaderFlags=0x00000000;
_header_pe64->OptionalHeader.NumberOfRvaAndSizes=0x00000010;
memset( &_header_pe64->OptionalHeader.DataDirectory, 0, sizeof(IMAGE_DATA_DIRECTORY)*IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
_header_sections = (IMAGE_SECTION_HEADER*) (base_pe + sizeof(IMAGE_NT_HEADERS64));
this->_parsed_pe_64 = true;
}
// Create the sections
_num_sections = 0;
__int64 image_size = _raw_header_size;
while( _stream->estimate_section_size(image_size) != 0 && image_size >= size && _num_sections < 99 && _num_sections < num_sections_limit )
{
__int64 est_size = _stream->estimate_section_size(image_size);
_header_sections[_num_sections].PointerToRawData = image_size;
_header_sections[_num_sections].SizeOfRawData = est_size;
_header_sections[_num_sections].VirtualAddress = image_size;
_header_sections[_num_sections].Misc.PhysicalAddress = image_size;
_header_sections[_num_sections].Misc.VirtualSize = est_size;
_header_sections[_num_sections].Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE; //_stream->get_region_characteristics(offset);
char name[9];
sprintf_s( name, 9, "pd_rec%i", _num_sections);
memcpy( &_header_sections[_num_sections].Name, name, 8 );
_header_sections[_num_sections].NumberOfLinenumbers = 0;
_header_sections[_num_sections].NumberOfRelocations = 0;
_header_sections[_num_sections].PointerToLinenumbers = 0;
if( _options->Verbose )
printf("%s: size %x\r\n", name, image_size);
_num_sections++;
image_size += est_size;
}
// Update the number of sections and image size
if( !amd64 )
{
_header_pe32->FileHeader.NumberOfSections = _num_sections;
_header_pe32->OptionalHeader.SizeOfImage = image_size;
}
else
{
_header_pe64->FileHeader.NumberOfSections = _num_sections;
_header_pe64->OptionalHeader.SizeOfImage = image_size;
}
return true;
}
return false;
}
bool pe_header::process_pe_header( )
{
if( _options->Verbose )
fprintf( stdout, "INFO: Loading PE header for %s.\r\n", this->get_name() );
if( _stream != NULL )
{
// Request the block size of the first region
_raw_header_size = _stream->block_size(0);
_raw_header = new unsigned char[_raw_header_size];
if( _raw_header_size >= 0x500 )
{
// Read in the PE header
if( _stream->read(0, _raw_header_size, _raw_header, &_raw_header_size) && _raw_header_size >= 0x500 )
{
// Parse the PE header
if( _raw_header_size > sizeof(IMAGE_DOS_HEADER) )
{
this->_header_dos = (IMAGE_DOS_HEADER*) _raw_header;
if( _header_dos->e_magic == 0x5A4D )
{
// Successfully parsed dos header
this->_parsed_dos = true;
// Parse the PE header
unsigned char* base_pe = _header_dos->e_lfanew + _raw_header;
if( _test_read( _raw_header, _raw_header_size, base_pe, sizeof(IMAGE_NT_HEADERS64) ) )
{
// We are unsure if we need to process this as a 32bit or 64bit PE header, lets figure it out.
// The first part is independent of the 32 or 64 bit definition.
if ( ((IMAGE_NT_HEADERS32*)base_pe)->Signature == 0x4550 && ((IMAGE_NT_HEADERS32*)base_pe)->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC )
{
// 32bit module
this->_header_pe32 = ((IMAGE_NT_HEADERS32*) base_pe);
this->_parsed_pe_32 = true;
if( _options->Verbose )
fprintf( stdout, "INFO: Loaded PE header for %s. Somewhat parsed: %d\r\n", this->get_name(), this->somewhat_parsed() );
return true;
}
else if( ((IMAGE_NT_HEADERS64*)base_pe)->Signature == 0x4550 && ((IMAGE_NT_HEADERS64*)base_pe)->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC )
{
// 64bit module
this->_header_pe64 = ((IMAGE_NT_HEADERS64*) base_pe);
this->_parsed_pe_64 = true;
if( _options->Verbose )
fprintf( stdout, "INFO: Loaded PE header for %s. Somewhat parsed: %d\r\n", this->get_name(), this->somewhat_parsed() );
return true;
}
else
{
// error
if (_options->Verbose)
fprintf(stdout, "INFO: Invalid PE header for %s. Somewhat parsed: %d\r\n", this->get_name(), this->somewhat_parsed());
}
}
}
}
}
}
}
else
{
if( _options->Verbose )
fprintf( stderr, "INFO: Invalid stream.\r\n" );
}
if( _options->Verbose )
fprintf( stdout, "INFO: Loaded PE header for %s. Somewhat parsed: %d\r\n", this->get_name(), this->somewhat_parsed() );
return false;
}
bool pe_header::process_sections( )
{
if( _options->Verbose )
fprintf( stdout, "INFO: Loading sections for %s.\r\n", this->get_name() );
if( this->_parsed_pe_32 )
{
// Attempt to parse the sections
unsigned char* base_pe = _header_dos->e_lfanew + _raw_header;
unsigned char* base_sections = base_pe + sizeof(*_header_pe32);
if( _header_pe32->FileHeader.NumberOfSections > 0x100 )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Extremely large number of sections of 0x%x changed to 0x100 as part of sanity check.\r\n",
this->get_name(), location, _header_pe32->FileHeader.NumberOfSections );
_header_pe32->FileHeader.NumberOfSections = 0x100;
delete[] location;
}
if( _test_read( _raw_header, _raw_header_size, base_sections, sizeof(IMAGE_SECTION_HEADER) ) )
{
// Has room for at least 1 section.
if( !_test_read( _raw_header, _raw_header_size, base_sections, _header_pe32->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER) ) )
{
// Parse the maximum number of sections possible
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Number of sections being changed from 0x%x to 0x%x such that it will fit within the PE header buffer.\r\n",
this->get_name(), location,
_header_pe32->FileHeader.NumberOfSections,
( (_raw_header + _raw_header_size - base_sections - 1) / sizeof(IMAGE_SECTION_HEADER) )
);
delete[] location;
_header_pe32->FileHeader.NumberOfSections = ( (_raw_header + _raw_header_size - base_sections - 1) / sizeof(IMAGE_SECTION_HEADER) );
}
this->_parsed_sections = true;
this->_num_sections = _header_pe32->FileHeader.NumberOfSections;
this->_header_sections = (IMAGE_SECTION_HEADER*) base_sections;
if( _options->Verbose )
{
for( int i = 0; i < this->_num_sections; i++ )
{
if( _test_read( _raw_header, _raw_header_size, this->_header_sections[i].Name, 0x40 ) )
fprintf( stdout, "INFO: %s\t#%i\t%s\t0x%x\t0x%x\r\n", this->get_name(), i, this->_header_sections[i].Name, this->_header_sections[i].VirtualAddress, this->_header_sections[i].SizeOfRawData );
else
fprintf( stdout, "INFO: %s\t#%i\tINVALID ADDRESS\t0x%x\t0x%x\r\n", this->get_name(), i, this->_header_sections[i].VirtualAddress, this->_header_sections[i].SizeOfRawData );
}
}
// Calculate the total size of the virtual image by inspecting the last section
DWORD image_size = 0;
if( this->_num_sections > 0 )
{
if( _header_sections[_num_sections - 1].Misc.VirtualSize > MAX_SECTION_SIZE )
{
// Smartly choose _header_pe32->OptionalHeader.SizeOfImage or last section plus max section size
if( _header_pe32->OptionalHeader.SizeOfImage > _header_sections[_num_sections - 1].VirtualAddress &&
_header_pe32->OptionalHeader.SizeOfImage < _header_sections[_num_sections - 1].VirtualAddress + MAX_SECTION_SIZE )
{
// Use the _header_pe32->OptionalHeader.SizeOfImage, since it seems valid
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Image size of last section appears incorrect, using image size specified by optional header instead since it appears valid. This could be as a result of a custom code to load a library by means other than LoadLibrary().\r\n",
this->get_name(), location);
delete[] location;
image_size = _header_pe32->OptionalHeader.SizeOfImage;
}
else
{
// Assume a really large last section since _header_pe32->OptionalHeader.SizeOfImage appears invalid.
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Image size of last section appears incorrect, using built-in max section size of 0x%x instead. This could be as a result of a custom code to load a library by means other than LoadLibrary().\r\n",
this->get_name(), location,
MAX_SECTION_SIZE * (_num_sections+1));
delete[] location;
image_size = _header_sections[_num_sections - 1].VirtualAddress + MAX_SECTION_SIZE;
}
}
else
image_size = _header_sections[_num_sections - 1].VirtualAddress +
_header_sections[_num_sections - 1].Misc.VirtualSize;
}
if( _header_pe32->OptionalHeader.SizeOfImage > image_size )
image_size = _header_pe32->OptionalHeader.SizeOfImage;
// Perform a sanity check on the resulting image size
if( image_size > MAX_SECTION_SIZE * (_num_sections+1) )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Large image size of 0x%x changed to 0x%x as part of sanity check. This could be as a result of a custom code to load a library by means other than LoadLibrary().\r\n",
this->get_name(), location,
image_size, MAX_SECTION_SIZE * (_num_sections+1) );
delete[] location;
image_size = MAX_SECTION_SIZE * (_num_sections+1);
}
// Now lets build a proper image of this file with virtual alignment
_image_size = image_size;
_image = new unsigned char[_image_size];
memset(_image, 0, _image_size);
// Read in this full image
if( _stream->file_alignment )
{
// Read in the full image from disk alignment
// Read in the header
SIZE_T num_read = 0;
if( _test_read( _image, _image_size, _image, _header_pe32->OptionalHeader.SizeOfHeaders ) )
{
if( !_stream->read(0, _header_pe32->OptionalHeader.SizeOfHeaders, _image, &num_read ) && _options->Verbose )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Failed to read in header of size 0x%x. Was only able to read 0x%x bytes from this region.\r\n",this->get_name(), location, _header_pe32->OptionalHeader.SizeOfHeaders, num_read);
delete[] location;
}
}
else
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Failed to read in header.", this->get_name(), location);
delete[] location;
}
// Loop through reading the sections into their respective virtual sections
if( this->_parsed_sections )
{
for( int i = 0; i < this->_num_sections; i++ )
{
// Test the destination is valid
if( _test_read( _image, _image_size,
_image + (SIZE_T) this->_header_sections[i].VirtualAddress, this->_header_sections[i].SizeOfRawData ) )
{
// Read in this section
if( !_stream->read( this->_header_sections[i].PointerToRawData, this->_header_sections[i].SizeOfRawData,
_image + (SIZE_T) this->_header_sections[i].VirtualAddress, &num_read ) && _options->Verbose )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Failed to read in section %i of size 0x%x. Was only able to read 0x%x bytes from this region.\r\n", this->get_name(), location, i, this->_header_sections[i].SizeOfRawData, num_read);
delete[] location;
}
}
}
}
}
else
{
// Read in the full image from virtual alignment
SIZE_T num_read = 0;
if( !_stream->read( 0, _image_size, _image, &num_read ) && _options->Verbose )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Failed to read in image at 0x%llX of size 0x%x. Was only able to read 0x%x bytes from this region.\r\n",this->get_name(), location, this->_stream->get_address(), _image_size, num_read);
delete[] location;
}
}
if( _options->Verbose )
fprintf( stdout, "INFO: Loaded sections for %s with result: %d. %i sections found.\r\n", this->get_name(), this->_parsed_sections, ( this->_parsed_sections ? this->_num_sections : 0 ) );
return true;
}
}
else if( this->_parsed_pe_64 )
{
// Attempt to parse the sections
unsigned char* base_pe = _header_dos->e_lfanew + _raw_header;
unsigned char* base_sections = base_pe + sizeof(*_header_pe64);
if( _header_pe64->FileHeader.NumberOfSections > 0x100 )
{
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Extremely large number of sections of 0x%x changed to 0x100 as part of sanity check.\r\n",
this->get_name(), location, _header_pe64->FileHeader.NumberOfSections );
_header_pe64->FileHeader.NumberOfSections = 0x100;
delete[] location;
}
if( _test_read( _raw_header, _raw_header_size, base_sections, sizeof(IMAGE_SECTION_HEADER) ) )
{
// Has room for at least 1 section.
if( !_test_read( _raw_header, _raw_header_size, base_sections, _header_pe64->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER) ) )
{
// Parse the maximum number of sections possible
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Number of sections being changed from 0x%x to 0x%x such that it will fit within the PE header buffer.\r\n",
this->get_name(), location,
_header_pe64->FileHeader.NumberOfSections,
( (_raw_header + _raw_header_size - base_sections - 1) / sizeof(IMAGE_SECTION_HEADER) )
);
delete[] location;
_header_pe64->FileHeader.NumberOfSections = ( (_raw_header + _raw_header_size - base_sections - 1) / sizeof(IMAGE_SECTION_HEADER) );
}
this->_parsed_sections = true;
this->_num_sections = _header_pe64->FileHeader.NumberOfSections;
this->_header_sections = (IMAGE_SECTION_HEADER*) base_sections;
if( _options->Verbose )
{
for( int i = 0; i < this->_num_sections; i++ )
{
fprintf( stdout, "INFO: %s\t#%i\t%s\t0x%x\t0x%x\r\n", this->get_name(), i, this->_header_sections[i].Name, this->_header_sections[i].VirtualAddress, this->_header_sections[i].SizeOfRawData );
}
}
// Calculate the total size of the virtual image by inspecting the last section
DWORD image_size = 0;
if( this->_num_sections > 0 )
{
if( _header_sections[_num_sections - 1].Misc.VirtualSize > MAX_SECTION_SIZE )
{
// Smartly choose _header_pe64->OptionalHeader.SizeOfImage or last section plus max section size
if( _header_pe64->OptionalHeader.SizeOfImage > _header_sections[_num_sections - 1].VirtualAddress &&
_header_pe64->OptionalHeader.SizeOfImage < _header_sections[_num_sections - 1].VirtualAddress + MAX_SECTION_SIZE )
{
// Use the _header_pe64->OptionalHeader.SizeOfImage, since it seems valid
char* location = new char[FILEPATH_SIZE + 1];
_stream->get_location(location, FILEPATH_SIZE + 1);
fprintf( stderr, "WARNING: module '%s' at %s. Image size of last section appears incorrect, using image size specified by optional header instead since it appears valid. This could be as a result of a custom code to load a library by means other than LoadLibrary().\r\n",