-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
99 lines (77 loc) · 2.57 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
#include <stdio.h>
#include <windows.h>
#include "FileMapping.h"
#include "Analyzers.h"
#include "Getters.h"
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("Unexpected error: invalid argument count! Type \"%s file_name\"\n", argv[0]);
return -1;
}
PBYTE fileMap;
int retVal = MapFile(argv[1], &fileMap, &gFileSize);
if (retVal != 0)
{
printf("Unexpected error: MapFile failed with err-code %08X\n", retVal);
return retVal;
}
gFileBeginning = fileMap;
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)fileMap;
retVal = AnalyzeDosHeader(dosHeader);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeDosHeader failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)(fileMap + dosHeader->e_lfanew);
retVal = AnalyzeNtHeader(ntHeader);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeNtHeader failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_FILE_HEADER fileHeader = (PIMAGE_FILE_HEADER)
((PBYTE)ntHeader + sizeof(DWORD));
retVal = AnalyzeFileHeader(fileHeader);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeFileHeader failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_OPTIONAL_HEADER optionalHeader = (PIMAGE_OPTIONAL_HEADER)
((PBYTE)ntHeader + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
retVal = AnalyzeOptionalHeader(optionalHeader);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeOptionalHeader failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_SECTION_HEADER sectionHeader = (PIMAGE_SECTION_HEADER)
((PBYTE)fileHeader + sizeof(IMAGE_FILE_HEADER) + fileHeader->SizeOfOptionalHeader);
DWORD numberOfSections = fileHeader->NumberOfSections;
retVal = AnalyzeSectionHeaders(sectionHeader, numberOfSections);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeSectionHeaders failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_EXPORT_DIRECTORY exportDirectory = GetExportDirectory(dosHeader);
retVal = AnalyzeExportDirectory(dosHeader, exportDirectory);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeExportDirectory failed with err-code %08X\n", retVal);
goto cleanup;
}
PIMAGE_IMPORT_DESCRIPTOR importDescriptor = GetImportDescriptor(dosHeader);
retVal = AnalyzeImportDescriptor(dosHeader, importDescriptor);
if (retVal != 0)
{
printf("Unexpected error: AnalyzeImportDescriptor failed with err-code %08X\n", retVal);
goto cleanup;
}
cleanup:
UnmapFile(fileMap, gFileMapping, gFile);
return 0;
}