-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
1632 lines (1361 loc) · 48 KB
/
main.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <tchar.h>
#include <stdint.h>
#include <strsafe.h>
#include <time.h>
#include "TasksQueue.h"
#include "list.h"
#include "scanuser.h"
#include <fltUser.h>
#pragma comment (lib, "fltlib.lib")
#define CRC_INFECTED_FILE 612700139
#define DEFAULT_SPACING 50
#define MAX_THREADS 64
#define SCANNER_DEFAULT_REQUEST_COUNT 5
#define SCANNER_DEFAULT_THREAD_COUNT 2
typedef signed long SIGNED_DWORD;
typedef unsigned long long QWORD;
INT gSpacingSize = DEFAULT_SPACING;
INT gGoodFiles;
INT gBadFiles;
INT gPe64File;
INT gStartingArgument;
UINT gMaxThreads;
UINT gThreadsArray[MAX_THREADS];
CRITICAL_SECTION gCritSection;
GLOBALHANDLE gEvents[2];
LIST_ENTRY gHeadList;
typedef struct _SCANNER_THREAD_CONTEXT {
HANDLE Port;
HANDLE Completion;
} SCANNER_THREAD_CONTEXT, *PSCANNER_THREAD_CONTEXT;
DWORD WINAPI
MultithreadScanner(
__in LPVOID lpParam
);
VOID
ErrorHandler(
__in LPTSTR lpszFunction
);
BOOL
FileExists(
__in_z LPCTSTR FilePath
);
VOID CheckIfFileExists(
__in LPCTSTR FilePath
);
VOID
FileCreate(
__out PHANDLE hFile,
__in LPCSTR FilePath
);
VOID
FileRead(
__in HANDLE hFile,
__out PDWORD FileSize,
__out LPVOID* FileBuffer,
__out PDWORD BytesRead,
__out PINT Error
);
VOID
LogDosHeader(
__in FILE* Out,
__in PIMAGE_DOS_HEADER Pdh
);
VOID
GetComputerArchitecture(
__in WORD Machine,
__in FILE* Out
);
VOID
LogNtHeader(
__in FILE* Out,
__in PIMAGE_NT_HEADERS Pnh,
__in IMAGE_FILE_HEADER Ifh
);
VOID
LogTextInFile(
__in FILE* Out,
__in PCHAR String
);
VOID
LogOptionalHeader(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PINT Error
);
VOID
LogDirectories(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PINT Error
);
VOID
LogDirectories(
__in FILE* out,
__in IMAGE_OPTIONAL_HEADER ioh,
__out PINT error
);
VOID
LogSectionHeader(
__in FILE* Out,
__in IMAGE_FILE_HEADER Ifh,
__in PIMAGE_SECTION_HEADER Psh,
__in DWORD GlobalSize,
__out PINT Error
);
VOID
LogImports(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PIMAGE_IMPORT_DESCRIPTOR Pid,
__in PIMAGE_DOS_HEADER Pdh,
__in IMAGE_FILE_HEADER Ifh,
__in LPCBYTE FileBuffer,
__in DWORD GlobalSize,
__out PINT Error
);
VOID
LogExports(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PIMAGE_EXPORT_DIRECTORY Ped,
__in PIMAGE_DOS_HEADER Pdh,
__in IMAGE_FILE_HEADER Ifh,
__in LPCBYTE FileBuffer,
__in DWORD GlobalSize,
__out PINT Error
);
SIGNED_DWORD
RVA2FA(
__in CONST PIMAGE_DOS_HEADER DosHeader,
__in CONST WORD NumberOfSections,
__in CONST DWORD RVA,
__in LPCBYTE FileBuffer,
__in DWORD GlobalSize
);
VOID
CreatePath(
__inout_z PTCHAR FilePath,
__in_z LPCTSTR Argv,
__in_z LPCTSTR Name
);
VOID
SearchForAdson(
__in FILE* Out,
__in CONST PIMAGE_DOS_HEADER DosHeader,
__in CONST IMAGE_FILE_HEADER Ifh,
__in LPCBYTE FileBuffer,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out BOOL* Virused
);
VOID
LogDword(
__in FILE* Out,
__in DWORD Number32Bits
);
BOOL
CheckIfNumber(
__in PTCHAR WorkerThreads
);
VOID
GetWorkerThreads(
__in PTCHAR FirstArg
);
VOID
CreategEventsWithManualReset(
VOID
);
VOID
InterlockedIncrementInt(
__out PINT number
);
VOID
InterlockedInsertElementInTailList(
__inout PTCHAR FilePath,
__in PTCHAR newARG,
__in WIN32_FIND_DATA FindFileData,
__inout PPROGRAM_ITEM pProgramItem
);
VOID
LogThreadsUsageOnConsole(
VOID
);
INT
_tmain(
__in INT argc,
__in PTCHAR argv[]
)
{
clock_t begin = clock();
if (argc < 2)
{
printf("No argument given! Please give arguments!\n");
return;
}
TCHAR newARG[200];
WIN32_FIND_DATA FindFileData;
HANDLE hFile;
TCHAR FilePath[100];
PPROGRAM_ITEM pProgramItem;
PDWORD dwThreadIdArray;
SCANNER_THREAD_CONTEXT context;
PHANDLE hTreadArray;
PSCANNER_MESSAGE msg;
HRESULT hr;
//PSCANNER_MESSAGE msg = NULL;
// DWORD requestCount = SCANNER_DEFAULT_REQUEST_COUNT;
HANDLE port, completion;
GetWorkerThreads(argv[1]);
gMaxThreads = min(gMaxThreads, 64);
_tcscpy(newARG, argv[gStartingArgument]);
for (INT i = gStartingArgument + 1; i < argc; i++)
{
_tcscat(newARG, " ");
_tcscat(newARG, argv[i]);
}
//
// Open a commuication channel to the filter
//
printf("Scanner: Connecting to the filter ...\n");
hr = FilterConnectCommunicationPort(ScannerPortName,
0,
NULL,
0,
NULL,
&port);
if (IS_ERROR(hr)) {
printf("ERROR: Connecting to filter port: 0x%08x\n", hr);
return 2;
}
completion = CreateIoCompletionPort(port,
NULL,
0,
gMaxThreads);
if (completion == NULL) {
printf("ERROR: Creating completion port: %d\n", GetLastError());
CloseHandle(port);
return;
}
printf("Scanner: Port = 0x%p Completion = 0x%p\n", port, completion);
context.Port = port;
context.Completion = completion;
InitializeListHead(&gHeadList);
InitializeCriticalSection(&gCritSection);
CreategEventsWithManualReset();
dwThreadIdArray = (PDWORD)malloc(sizeof(DWORD) * gMaxThreads);
hTreadArray = (PHANDLE)malloc(sizeof(HANDLE) * gMaxThreads);
if (dwThreadIdArray == NULL || hTreadArray == NULL)
{
printf("Allocation failed!\n");
return;
}
for (int i = 0; (UINT)i < gMaxThreads; i++)
{
hTreadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
MultithreadScanner, // thread function name
// (int*)i, // argument to thread function ( current thread id )
&context,
0, // use default creation flags
&dwThreadIdArray[i]); // returns the thread identifier
// Check the return value for success.
// If CreateThread fails, terminate execution.
// This will automatically clean up threads and memory
if (hTreadArray[i] == NULL)
{
ErrorHandler(TEXT("CreateThread"));
hr = GetLastError();
ExitProcess(2);
}
msg = malloc(sizeof(SCANNER_MESSAGE));
if (msg == NULL) {
hr = ERROR_NOT_ENOUGH_MEMORY;
goto main_cleanup;
}
memset(&msg->Ovlp, 0, sizeof(OVERLAPPED));
//
// Request messages from the filter driver.
//
hr = FilterGetMessage(port,
&msg->MessageHeader,
FIELD_OFFSET(SCANNER_MESSAGE, Ovlp),
&msg->Ovlp);
if (hr != HRESULT_FROM_WIN32(ERROR_IO_PENDING))
{
free(msg);
goto main_cleanup;
}
}
hFile = FindFirstFile(newARG, &FindFileData);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Invalid file handle. Error is %u.\n", GetLastError());
return;
}
pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM), MEMORY_ALLOCATION_ALIGNMENT);
if (pProgramItem == NULL)
{
printf("Memory allocation failed.\n");
return;
}
InterlockedInsertElementInTailList(FilePath, newARG, FindFileData, pProgramItem);
while (FindNextFile(hFile, &FindFileData))
{
pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM), MEMORY_ALLOCATION_ALIGNMENT);
if (pProgramItem == NULL)
{
printf("Memory allocation failed.\n");
return;
}
InterlockedInsertElementInTailList(FilePath, newARG, FindFileData, pProgramItem);
}
// Set the event for no more files
SetEvent(gEvents[1]);
WaitForMultipleObjects(gMaxThreads, hTreadArray, TRUE, INFINITE);
// Close all threads handles and free memory allocations.
DWORD threadExitCode;
for (UINT i = 0; i < gMaxThreads; i++)
{
// GetExitCodeThread returns nonzero if succeeds
BOOL errorCode = GetExitCodeThread(hTreadArray[i], &threadExitCode);
if (errorCode)
{
CloseHandle(hTreadArray[i]);
}
else
{
printf("ERROR! Thread %d i couldn't be closed!", i);
}
}
printf("Found %d good files.\n", gGoodFiles);
printf("Found %d bad files, from which %d are PE64 files.\n", gBadFiles, gPe64File);
free(dwThreadIdArray);
free(hTreadArray);
// LogThreadsUsageOnConsole();
clock_t end = clock();
DOUBLE time_spent = (DOUBLE)(end - begin) / CLOCKS_PER_SEC;
printf("TIME SPENT: %f", time_spent);
main_cleanup:
printf("Scanner processed the file. Result = 0x%08x\n", hr);
CloseHandle(port);
CloseHandle(completion);
return hr;
}
BOOL
FileExists(
__in_z LPCTSTR FilePath
)
{
// If the user didn't given any path return NULL
if (FilePath == NULL)
return false;
WIN32_FILE_ATTRIBUTE_DATA fileAttribute;
// LPVOID - VOID* ( VOID pointer )
// lpFileInformation ( in our case fileAttribute ) must be a WIN32_FILE_ATTRIBUTE_DATA!
if (GetFileAttributesEx(FilePath, GetFileExInfoStandard, &fileAttribute) == 0)
return FALSE;
return TRUE;
}
VOID CheckIfFileExists(
__in LPCTSTR FilePath
)
{
if (!FileExists(FilePath)) {
//printf("File %s doesn't exist.\n", FilePath);
//exit(1);
}
//printf("Step 1 : File %s exists\n", fileName);
}
VOID
FileCreate(
__out PHANDLE hFile,
__in LPCSTR FilePath
)
{
*hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (*hFile == INVALID_HANDLE_VALUE)
{
//printf("CreateFile error. Extended Error Information %d.\n", GetLastError());
return;
}
}
// ReadFile cannot crash ( the handle was already verified before this function is appealed )
VOID
FileRead(
__in HANDLE hFile,
__out PDWORD FileSize,
__out LPVOID *FileBuffer,
__out PDWORD BytesRead,
__out PINT Error
)
{
*FileSize = GetFileSize(hFile, NULL);
*FileBuffer = VirtualAlloc(NULL, *FileSize, MEM_COMMIT, PAGE_READWRITE);
if (ReadFile(hFile, *FileBuffer, *FileSize, &*BytesRead, NULL) == 0)
{
//printf("Couldn't read the file %s. Extended Error Information %d.\n", fileName, GetLastError());
// if we managed to allocate memory, we need to freed it because the read was unsuccessfull
if (*FileBuffer)
VirtualFree(*FileBuffer, *FileSize, MEM_DECOMMIT);
*Error = 1;
}
}
VOID
GetComputerArchitecture(
__in WORD Machine,
__in FILE* Out
)
{
switch (Machine)
{
case IMAGE_FILE_MACHINE_I386:
fprintf(Out, "x86\n");
break;
case IMAGE_FILE_MACHINE_IA64:
fprintf(Out, "Intel Itanium\n");
break;
case IMAGE_FILE_MACHINE_AMD64:
fprintf(Out, "x64\n");
break;
}
}
VOID
LogDosHeader(
__in FILE* Out,
__in PIMAGE_DOS_HEADER Pdh
)
{
//printf("DOS HEADER IN!\n");
fprintf(Out, "***************************** Dos Header *****************************\n");
LogTextInFile(Out, "Magic number:");
fprintf(Out, "%#x (%s)\n", Pdh->e_magic, "MZ");
LogTextInFile(Out, "Bytes on last page of file:");
fprintf(Out, "%d\n", Pdh->e_cblp);
LogTextInFile(Out, "Pages in file:");
fprintf(Out, "%#x\n", Pdh->e_cp);
LogTextInFile(Out, "Relocations:");
fprintf(Out, "%#x\n", Pdh->e_crlc);
LogTextInFile(Out, "Size of header in paragraphs:");
fprintf(Out, "%#x\n", Pdh->e_cparhdr);
LogTextInFile(Out, "Minimum extra paragraphs needed:");
fprintf(Out, "%#x\n", Pdh->e_minalloc);
LogTextInFile(Out, "Maximum extra paragraphs needed:");
fprintf(Out, "%#x\n", Pdh->e_maxalloc);
LogTextInFile(Out, "Initial (relative) SS value:");
fprintf(Out, "%#x\n", Pdh->e_ss);
LogTextInFile(Out, "Initial SP value:");
fprintf(Out, "%#x\n", Pdh->e_sp);
LogTextInFile(Out, "Checksum:");
fprintf(Out, "%#x\n", Pdh->e_csum);
LogTextInFile(Out, "Initial IP value:");
fprintf(Out, "%#x\n", Pdh->e_ip);
LogTextInFile(Out, "Initial (relative) CS value:");
fprintf(Out, "%#x\n", Pdh->e_cs);
LogTextInFile(Out, "File address of relocation table:");
fprintf(Out, "%#x\n", Pdh->e_lfarlc);
LogTextInFile(Out, "Overlay number:");
fprintf(Out, "%#x\n", Pdh->e_ovno);
LogTextInFile(Out, "OEM identifier:");
fprintf(Out, "%#x\n", Pdh->e_oemid);
LogTextInFile(Out, "OEM information:");
fprintf(Out, "%#x\n", Pdh->e_oeminfo);
LogTextInFile(Out, "File address of new exe header:");
fprintf(Out, "%#lx\n\n", Pdh->e_lfanew);
//printf("DOS HEADER OUT!\n");
}
VOID
LogNtHeader(
__in FILE* Out,
__in PIMAGE_NT_HEADERS Pnh,
__in IMAGE_FILE_HEADER Ifh
)
{
//printf("NT IN!\n");
fprintf(Out, "***************************** NT Header *****************************\n");
LogTextInFile(Out, "Signature:");
fprintf(Out, "%#lx (%s)\n", Pnh->Signature, "(Portable Executable)");
LogTextInFile(Out, "Machine:");
GetComputerArchitecture(Ifh.Machine, Out);
LogTextInFile(Out, "Number of sections:");
fprintf(Out, "%#x\n", Ifh.NumberOfSections);
LogTextInFile(Out, "Timestamp:");
fprintf(Out, "%lu\n", Ifh.TimeDateStamp);
LogTextInFile(Out, "Pointer to symbol table:");
fprintf(Out, "%#lx\n", Ifh.PointerToSymbolTable);
LogTextInFile(Out, "Number of symbols");
fprintf(Out, "%#lx\n", Ifh.NumberOfSymbols);
LogTextInFile(Out, "Size of optional header:");
fprintf(Out, "%#x | %lu \n", Ifh.SizeOfOptionalHeader, Ifh.SizeOfOptionalHeader);
LogTextInFile(Out, "Characteristics:");
fprintf(Out, "%#lx\n\n", Ifh.Characteristics);
//printf("NT OUT!\n");
}
VOID
LogOptionalHeader(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PINT Error
)
{
//printf("Optional header IN!\n");
fprintf(Out, "***************************** Optional Header *****************************\n");
LogTextInFile(Out, "Magic:");
fprintf(Out, "%#x (%s)\n", Ioh.Magic, Ioh.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC ? "PE64" : "PE32");
if (Ioh.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
gPe64File++;
*Error = 1;
return;
}
LogTextInFile(Out, "Linker Version:");
fprintf(Out, "%d.%d\n", Ioh.MajorLinkerVersion, Ioh.MinorLinkerVersion);
LogTextInFile(Out, "Size of code:");
fprintf(Out, "%#lu\n", Ioh.SizeOfCode);
LogTextInFile(Out, "Size of initialized data:");
fprintf(Out, "%lu\n", Ioh.SizeOfInitializedData);
LogTextInFile(Out, "Size of uninitialized:");
fprintf(Out, "%lu\n", Ioh.SizeOfUninitializedData);
LogTextInFile(Out, "Adress of entry point :");
fprintf(Out, "%#x\n", Ioh.AddressOfEntryPoint);
LogTextInFile(Out, "Base of code:");
fprintf(Out, "%#x\n", Ioh.BaseOfCode);
LogTextInFile(Out, "Base of data :");
fprintf(Out, "%#x\n", Ioh.BaseOfData);
LogTextInFile(Out, "Image base:");
fprintf(Out, "%#x\n", Ioh.ImageBase);
LogTextInFile(Out, "Section aligment:");
fprintf(Out, "%lu\n", Ioh.SectionAlignment);
LogTextInFile(Out, "File aligment:");
fprintf(Out, "%lu\n", Ioh.FileAlignment);
LogTextInFile(Out, "Operation system version:");
fprintf(Out, "%d.%d\n", Ioh.MajorOperatingSystemVersion, Ioh.MinorOperatingSystemVersion);
LogTextInFile(Out, "Image version:");
fprintf(Out, "%d.%d\n", Ioh.MajorImageVersion, Ioh.MinorImageVersion);
LogTextInFile(Out, "Subsystem version:");
fprintf(Out, "%d.%d\n", Ioh.MajorSubsystemVersion, Ioh.MinorImageVersion);
LogTextInFile(Out, "Win32 version value:");
fprintf(Out, "%d\n", Ioh.Win32VersionValue);
LogTextInFile(Out, "Size of image:");
fprintf(Out, "%lu B \n", Ioh.SizeOfImage);
LogTextInFile(Out, "Size of headers:");
fprintf(Out, "%d\n", Ioh.SizeOfHeaders);
LogTextInFile(Out, "CheckSum:");
fprintf(Out, "%d\n", Ioh.CheckSum);
LogTextInFile(Out, "Subsystem:");
switch (Ioh.Subsystem)
{
case IMAGE_SUBSYSTEM_UNKNOWN:
{
fprintf(Out, "Unknown subsystem.\n");
break;
}
case IMAGE_SUBSYSTEM_WINDOWS_GUI:
{
fprintf(Out, "WINDOWS GUI.\n");
break;
}
}
LogTextInFile(Out, "DLL characteristics:");
fprintf(Out, "%#x\n", Ioh.DllCharacteristics);
LogTextInFile(Out, "Size of stack reserve:");
fprintf(Out, "%lu\n", Ioh.SizeOfStackReserve);
LogTextInFile(Out, "Size of stack commit:");
fprintf(Out, "%lu\n", Ioh.SizeOfStackCommit);
LogTextInFile(Out, "Size of heap reverse:");
fprintf(Out, "%lu\n", Ioh.SizeOfHeapReserve);
LogTextInFile(Out, "Size of heap commit:");
fprintf(Out, "%d\n", Ioh.SizeOfHeapCommit);
LogTextInFile(Out, "Loader flags:");
fprintf(Out, "%#x\n", Ioh.LoaderFlags);
LogTextInFile(Out, "Number of RVA:");
fprintf(Out, "%d\n\n", Ioh.NumberOfRvaAndSizes);
//printf("Optional header OUT!\n");
}
VOID
LogOneDirectory(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__in UINT CurrentDirIndex
)
{
PCHAR buffer;
PCHAR buf;
CHAR dir[30] = "Directory";
buffer = malloc(16);
_itoa(CurrentDirIndex, buffer, 10);
strcat(dir, buffer);
strcat(dir, " : ");
LogTextInFile(Out, dir);
size_t sz;
sz = snprintf(NULL, 0, "%#lx", Ioh.DataDirectory[CurrentDirIndex].VirtualAddress);
buf = (PCHAR)malloc(sz + 1);
snprintf(buf, sz + 1, "%#lx", Ioh.DataDirectory[CurrentDirIndex].VirtualAddress);
gSpacingSize = 10;
LogTextInFile(Out, buf);
gSpacingSize = DEFAULT_SPACING;
fprintf(Out, "%lu bytes\n", Ioh.DataDirectory[CurrentDirIndex].Size);
free(buffer);
free(buf);
}
VOID
LogTextInFile(
__in FILE* Out,
__in PCHAR String
)
{
PCHAR newChar;
newChar = malloc(100);
int i;
for (i = 0; i < gSpacingSize; i++)
{
if ((UINT)i < strlen(String))
newChar[i] = String[i];
else
newChar[i] = ' ';
}
newChar[i] = '\0';
fprintf(Out, newChar);
free(newChar);
}
VOID
LogDirectories(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PINT Error
)
{
//printf("Directories IN!\n");
fprintf(Out, "***************************** Data Dirctories Information *****************************\n");
if (Ioh.NumberOfRvaAndSizes > 16)
{
*Error = 1;
return;
}
for (UINT i = 0; i < Ioh.NumberOfRvaAndSizes; i++)
LogOneDirectory(Out, Ioh, i);
fprintf(Out, "\n");
//printf("Directories OUT!\n");
}
VOID
LogSectionHeader(
__in FILE* Out,
__in IMAGE_FILE_HEADER Ifh,
__in PIMAGE_SECTION_HEADER Psh,
__in DWORD GlobalSize,
__out PINT Error
)
{
//printf("Section Header IN\n");
fprintf(Out, "********************************* Section Header *********************************\n");
for (int i = 0; i < Ifh.NumberOfSections; i++)
{
if (((DWORD)Psh) + sizeof(IMAGE_SECTION_HEADER) >= GlobalSize)
{
*Error = 1;
return;
}
LogTextInFile(Out, "Section name:");
fprintf(Out, "%.8s\n", Psh->Name);
LogTextInFile(Out, "Virtual Address:");
fprintf(Out, "%#lx\n", Psh->VirtualAddress);
LogTextInFile(Out, "Virtual Size:");
fprintf(Out, "%lu bytes\n", Psh->Misc.VirtualSize);
LogTextInFile(Out, "Size Raw Data:");
fprintf(Out, "%lu bytes\n", Psh->SizeOfRawData);
LogTextInFile(Out, "Pointer to raw data:");
fprintf(Out, "%#lx\n", Psh->PointerToRawData);
fprintf(Out, "___________________________________________________________________________________\n\n");
Psh++;
}
//printf("Section header OUT!\n");
}
VOID
LogImports(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PIMAGE_IMPORT_DESCRIPTOR Pid,
__in PIMAGE_DOS_HEADER Pdh,
__in IMAGE_FILE_HEADER Ifh,
__in LPCBYTE FileBuffer,
__in DWORD GlobalSize,
__out PINT Error
)
{
//printf("IMPORTS IN!\n");
if (IMAGE_DIRECTORY_ENTRY_IMPORT >= Ioh.NumberOfRvaAndSizes ||
Ioh.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size == 0)
{
printf("No import table\n");
fprintf(Out, "No import table.\n");
return;
}
else
{
Pid = (PIMAGE_IMPORT_DESCRIPTOR)RVA2FA(Pdh, Ifh.NumberOfSections, Ioh.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress, FileBuffer, GlobalSize);
if ((INT)Pid == -1)
{
*Error = 1;
return;
}
if (Pid)
{
fprintf(Out, "***************************************IMPORTS***************************************\n\n");
while (Pid->Name)
{
if (RVA2FA(Pdh, Ifh.NumberOfSections, Pid->Name, FileBuffer, GlobalSize) == -1)
{
*Error = 1;
return;
}
char* DLLname = (char*)RVA2FA(Pdh, Ifh.NumberOfSections, Pid->Name, FileBuffer, GlobalSize);
LogTextInFile(Out, "DLL Name:");
fprintf(Out, "%s\n", DLLname);
LogTextInFile(Out, "Characteristics:");
fprintf(Out, "%#lx\n", Pid->Characteristics);
LogTextInFile(Out, "First Thunk:");
fprintf(Out, "%#lx\n", Pid->FirstThunk);
LogTextInFile(Out, "Forwarder Chain:");
fprintf(Out, "%#lx\n", Pid->ForwarderChain);
LogTextInFile(Out, "Original First Thunk:");
fprintf(Out, "%#lx\n", Pid->OriginalFirstThunk);
LogTextInFile(Out, "TimeDateStamp:");;
fprintf(Out, "%lu\n\n", Pid->TimeDateStamp);
// Get the adress table:
PIMAGE_THUNK_DATA ptd = (PIMAGE_THUNK_DATA)RVA2FA(Pdh, Ifh.NumberOfSections, Pid->OriginalFirstThunk, FileBuffer, GlobalSize);
if ((int)ptd == -1)
{
*Error = 1;
return;
}
fprintf(Out, "####### Imported functions #######\n");
while ((int)ptd < (int)GlobalSize && ptd->u1.AddressOfData)
{
if (IMAGE_SNAP_BY_ORDINAL(ptd->u1.Ordinal))
{
LogTextInFile(Out, "Ordinal:");
fprintf(Out, "%#lx\n", IMAGE_ORDINAL(ptd->u1.Ordinal));
}
else
{
gSpacingSize = 20;
PIMAGE_IMPORT_BY_NAME pibn = (PIMAGE_IMPORT_BY_NAME)RVA2FA(Pdh, Ifh.NumberOfSections, ptd->u1.AddressOfData, FileBuffer, GlobalSize);
if ((int)pibn == -1)
{
*Error = 1;
return;
}
LogTextInFile(Out, "Function Name:");
fprintf(Out, "%s\n", pibn->Name);
LogTextInFile(Out, "Function Hint:");
fprintf(Out, "#%x\n", pibn->Hint);
fprintf(Out, "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n");
gSpacingSize = DEFAULT_SPACING;
}
ptd++;
}
Pid++;
fprintf(Out, "_____________________________________________________________________\n");
}
}
}
//printf("IMPORTS OUT\n");
}
VOID
LogExports(
__in FILE* Out,
__in IMAGE_OPTIONAL_HEADER Ioh,
__out PIMAGE_EXPORT_DIRECTORY Ped,
__in PIMAGE_DOS_HEADER Pdh,
__in IMAGE_FILE_HEADER Ifh,
__in LPCBYTE FileBuffer,
__in DWORD GlobalSize,
__out PINT Error
)
{
//printf("Exports IN!\n");
DWORD j;
DWORD k;
PDWORD addressOfFunctions;
PWORD addressOfNameOrdinals;
PDWORD addressOfNames;
if (IMAGE_DIRECTORY_ENTRY_EXPORT >= Ioh.NumberOfRvaAndSizes ||
Ioh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size == 0)
{
//printf("No exp table\n");
fprintf(Out, "No export table.\n");
return;
}
Ped = (PIMAGE_EXPORT_DIRECTORY)RVA2FA(Pdh, Ifh.NumberOfSections, Ioh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress, FileBuffer, GlobalSize);
if ((INT)Ped == -1)
{
*Error = 1;
return;
}
if (Ped)
{
fprintf(Out, "***************************************EXPORTS***************************************\n\n");
if (RVA2FA(Pdh, Ifh.NumberOfSections, Ped->Name, FileBuffer, GlobalSize) == -1)
{
*Error = 1;
return;
}
char* Dllname = (char*)RVA2FA(Pdh, Ifh.NumberOfSections, Ped->Name, FileBuffer, GlobalSize);
LogTextInFile(Out, "DLL Name:\n");
fprintf(Out, "%s\n", Dllname);
LogTextInFile(Out, "Characteristics:");
fprintf(Out, "%#lx\n", Ped->Characteristics);
LogTextInFile(Out, "Ordinal Base:");
fprintf(Out, "%#lx\n", Ped->Base);
LogTextInFile(Out, "Major Version:");
fprintf(Out, "%d\n", Ped->MajorVersion);
LogTextInFile(Out, "Minor Version:");
fprintf(Out, "%d\n", Ped->MinorVersion);
LogTextInFile(Out, "Exported functions:");
fprintf(Out, "%lu\n", Ped->NumberOfFunctions);
LogTextInFile(Out, "Functions exported by name:");
fprintf(Out, "%lu\n", Ped->NumberOfNames);