-
Notifications
You must be signed in to change notification settings - Fork 0
/
Getters.c
221 lines (182 loc) · 5.3 KB
/
Getters.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
#include "Getters.h"
#include "Analyzers.h"
DWORD RvaToFa(PIMAGE_DOS_HEADER DosHeader, DWORD Rva)
{
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)
((PBYTE)DosHeader + DosHeader->e_lfanew);
PIMAGE_SECTION_HEADER sectionHeader = (PIMAGE_SECTION_HEADER)
((PBYTE)ntHeader + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + ntHeader->FileHeader.SizeOfOptionalHeader);
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; i++)
{
if (sectionHeader[i].VirtualAddress <= Rva &&
Rva < sectionHeader[i].VirtualAddress + sectionHeader[i].Misc.VirtualSize)
{
DWORD offset = Rva - sectionHeader[i].VirtualAddress;
DWORD fileAddress = sectionHeader[i].PointerToRawData + offset;
return fileAddress;
}
}
return 0;
}
PIMAGE_EXPORT_DIRECTORY GetExportDirectory(PIMAGE_DOS_HEADER DosHeader)
{
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)
((PBYTE)DosHeader + DosHeader->e_lfanew);
PIMAGE_OPTIONAL_HEADER optionalHeader = (PIMAGE_OPTIONAL_HEADER)
((PBYTE)ntHeader + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
PIMAGE_OPTIONAL_HEADER64 optionalHeader64 = (PIMAGE_OPTIONAL_HEADER64)optionalHeader;
PIMAGE_DATA_DIRECTORY exportDirective;
if (optionalHeader->Magic == 0x20B) // for 64 bit PE files
{
exportDirective = optionalHeader64->DataDirectory;
}
else
{
exportDirective = optionalHeader->DataDirectory;
}
DWORD fileAddress = RvaToFa(DosHeader, exportDirective->VirtualAddress);
if (fileAddress == 0)
{
return NULL;
}
PIMAGE_EXPORT_DIRECTORY exportDirectory = (PIMAGE_EXPORT_DIRECTORY)
((PBYTE)DosHeader + fileAddress);
return exportDirectory;
}
PIMAGE_IMPORT_DESCRIPTOR GetImportDescriptor(PIMAGE_DOS_HEADER DosHeader)
{
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)
((PBYTE)DosHeader + DosHeader->e_lfanew);
PIMAGE_OPTIONAL_HEADER optionalHeader = (PIMAGE_OPTIONAL_HEADER)
((PBYTE)ntHeader + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
PIMAGE_OPTIONAL_HEADER64 optionalHeader64 = (PIMAGE_OPTIONAL_HEADER64) optionalHeader;
PIMAGE_DATA_DIRECTORY importDirectory;
if (optionalHeader->Magic == 0x20B) // for 64 bit PE files
{
importDirectory = optionalHeader64->DataDirectory + 1;
}
else
{
importDirectory = optionalHeader->DataDirectory + 1;
}
DWORD fileAddress = RvaToFa(DosHeader, importDirectory->VirtualAddress);
if (fileAddress == 0)
{
return NULL;
}
PIMAGE_IMPORT_DESCRIPTOR importDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)
((PBYTE)DosHeader + fileAddress);
return importDescriptor;
}
PBYTE GetDllName(PIMAGE_DOS_HEADER DosHeader, PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor)
{
DWORD fileAddress = RvaToFa(DosHeader, ImportDescriptor->Name);
if (fileAddress == 0)
{
return NULL;
}
PBYTE dllName = (PBYTE)((PBYTE)DosHeader + fileAddress);
return dllName;
}
PBYTE GetFuncName64(PIMAGE_DOS_HEADER DosHeader, PIMAGE_THUNK_DATA64 ThunkData)
{
ULONGLONG funcRva = ThunkData->u1.Function;
if (funcRva >> 63 != 0)
{
return NULL;
}
DWORD funcRva32 = (DWORD) *((PDWORD)&funcRva);
DWORD fileAddress = RvaToFa(DosHeader, funcRva32);
if (fileAddress == 0)
{
return NULL;
}
PIMAGE_IMPORT_BY_NAME importStruct = (PIMAGE_IMPORT_BY_NAME)
((PBYTE)DosHeader + fileAddress);
PBYTE name = (PBYTE)importStruct->Name;
return name;
}
PBYTE GetFuncName(PIMAGE_DOS_HEADER DosHeader, PIMAGE_THUNK_DATA ThunkData)
{
DWORD funcRva = ThunkData->u1.Function;
if (funcRva >> 31 != 0)
{
return NULL;
}
DWORD fileAddress = RvaToFa(DosHeader, funcRva);
if (fileAddress == 0)
{
return NULL;
}
PIMAGE_IMPORT_BY_NAME importStruct = (PIMAGE_IMPORT_BY_NAME)
((PBYTE)DosHeader + fileAddress);
PBYTE name = (PBYTE)importStruct->Name;
return name;
}
DWORD GetOrdinal(PIMAGE_THUNK_DATA ThunkData)
{
if (ThunkData == NULL)
{
return 0;
}
DWORD funcRva = ThunkData->u1.Function;
if (funcRva >> 31 != 0)
{
return funcRva & 0xFFFF;
}
return 0;
}
ULONGLONG GetOrdinal64(PIMAGE_THUNK_DATA64 ThunkData)
{
if (ThunkData == NULL)
{
return 0;
}
ULONGLONG funcRva = ThunkData->u1.Function;
if (funcRva >> 63 != 0)
{
return funcRva & 0xFFFF;
}
return 0;
}
PBYTE GetExportedFuncName(PIMAGE_DOS_HEADER DosHeader, PIMAGE_EXPORT_DIRECTORY ExportDirectory, DWORD FuncRva, PWORD NameOrdinal)
{
DWORD fileAddress = RvaToFa(DosHeader, ExportDirectory->AddressOfFunctions);
if (fileAddress == 0)
{
return (PBYTE)0xFFFFFFFF;
}
PDWORD funcArray = (PDWORD)
((PBYTE)DosHeader + fileAddress);
fileAddress = RvaToFa(DosHeader, ExportDirectory->AddressOfNames);
if (fileAddress == 0)
{
return (PBYTE)0xFFFFFFFF;
}
PDWORD nameArray = (PDWORD)
((PBYTE)DosHeader + fileAddress);
fileAddress = RvaToFa(DosHeader, ExportDirectory->AddressOfNameOrdinals);
if (fileAddress == 0)
{
return (PBYTE)0xFFFFFFFF;
}
PWORD nameOrdinalArray = (PWORD)
((PBYTE)DosHeader + fileAddress);
for (DWORD i = 0; i < ExportDirectory->NumberOfNames; i++)
{
WORD ordinal = nameOrdinalArray[i];
if (FuncRva == funcArray[ordinal])
{
*NameOrdinal = ordinal;
fileAddress = RvaToFa(DosHeader, nameArray[i]);
if (fileAddress == 0)
{
return (PBYTE) 0xFFFFFFFF;
}
PBYTE nameAddress = (PBYTE)
((PBYTE)DosHeader + fileAddress);
return nameAddress;
}
}
return NULL;
}