This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWhoCalls.c
310 lines (302 loc) · 14.5 KB
/
WhoCalls.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
#include <stdio.h>
#include <Windows.h>
#include <dbghelp.h>
#define IMPORT_TABLE_INDEX 1
CHAR szOutString[MAX_PATH] = { 0 }; // Global Variable
LPCSTR charsToString(CHAR* pcChars) {
SIZE_T nRealLength = 0; // Length not including null chars
/*
We need to figure out the length of the string because of all the \0's
we can't just use a regular strlen call.
*/
for (int i = 0; i <= INFINITE; i++) {
if (pcChars[i] == 0x00) { // Next char...
if (pcChars[i + 0x1] == 0x00) { // After 2 0x00 (\0) we know are at the end of the string
break;
}
nRealLength++;
}
}
nRealLength++;
int j = 0;
for (int i = 0; i <= (nRealLength * 2); i += 2) {
szOutString[j] = pcChars[i];
j++;
}
return(&szOutString);
}
LPVOID ImageRvaToVa64(PULONGLONG Base, ULONGLONG Rva, PIMAGE_SECTION_HEADER pRvaCalcSection) {
/*
This function is needed because the imagebase in a PE32+ file is a QWORD
the normal ImageRvaToVa function doesn't support such variable sizes
*/
ULONGLONG ullOutput = 0;
ullOutput = Rva - pRvaCalcSection->VirtualAddress + pRvaCalcSection->PointerToRawData;
ullOutput = ullOutput + (BYTE*)Base;
return(ullOutput);
}
/*
These functions are designed after the ones from dbghelp.dll
I include them here to limit dependencies
*/
PIMAGE_SECTION_HEADER myImageRvaToSection(PIMAGE_NT_HEADERS NtHeaders, PVOID Base, ULONG Rva) {
if (NtHeaders == NULL) {
return(-1); // Basic error checking
}
unsigned int nNumOfSections = NtHeaders->FileHeader.NumberOfSections;
int nCurrentNumOfSections = 0;
PIMAGE_SECTION_HEADER pISH;
pISH = (PIMAGE_SECTION_HEADER)((BYTE*)&NtHeaders->OptionalHeader + NtHeaders->FileHeader.SizeOfOptionalHeader);
if (pISH == NULL) {
return(-1);
}
ULONG ulTemp = 0;
while (1) {
ulTemp = pISH->VirtualAddress;
if (Rva >= ulTemp && Rva < pISH->SizeOfRawData + ulTemp) {
break;
}
++pISH;
if (++nCurrentNumOfSections >= nNumOfSections) {
return(-1);
}
}
return(pISH);
}
PVOID myImageRvaToVa(PIMAGE_NT_HEADERS NtHeaders, PVOID Base, ULONG Rva) {
PIMAGE_SECTION_HEADER pISH;
if (NtHeaders == NULL) {
return(-1); // Basic error checking
}
pISH = myImageRvaToSection(NtHeaders, Base, Rva);
return((BYTE*)Base + (Rva - pISH->VirtualAddress + pISH->PointerToRawData));
}
void showAPIMatch(LPCSTR lpszAPIName, LPCSTR lpszFileName) {
printf("[! MATCH !] -> %s Is imported by: %s [PRESS RETURN TO CONTINUE]\n", lpszAPIName, lpszFileName);
getchar();
}
void errorExit(LPCSTR lpszErrorMessage) {
// "Fatal" Exit
printf("[!!!] Error: %s\n", lpszErrorMessage);
ExitProcess(-1);
}
int main(unsigned int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: WhoCalls.exe [API Name] [Path To Query]\n");
return(-1);
}
CHAR szDirectory[MAX_PATH + 0x4] = { 0 };
if (strlen(argv[2]) >= MAX_PATH) { // Length check in order to 'prevent' buffer overflow
errorExit("Directory path is too long");
}
memcpy(&szDirectory, argv[2], strlen(argv[2])); // szDirectory now has "Path To Query" argument
strcat(&szDirectory, "\\"); // Add ending slash "\"
strcat(&szDirectory, "*"); // "*" is a wildcard and it will look for all files
LPCSTR lpszNameOfAPI = argv[1];
WIN32_FIND_DATAA winFindData;
printf("[i] Starting... %s\n", szDirectory);
HANDLE hFirstFile = FindFirstFileA(szDirectory, &winFindData);
if (hFirstFile == INVALID_HANDLE_VALUE) {
FindClose(hFirstFile);
errorExit("Can't open directory. Check your permissions and path argument");
}
CHAR lpszRealFileNameAndPath[MAX_PATH + 0x4] = { 0 };
do {
if (!(winFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
LPCSTR lpszRealFileName = charsToString(winFindData.cFileName); // Custom function to turn an array of chars, surrounded by null bytes, to a string
memset(lpszRealFileNameAndPath, 0x00, MAX_PATH + 0x4); // Null array szRealFileNameAndPath
memcpy(&lpszRealFileNameAndPath, &szDirectory, strlen(&szDirectory) - 1); // szRealFileNameAndPath now has "Path To Query" argument (-1 for "*")
strcat(&lpszRealFileNameAndPath, lpszRealFileName); // Add file name to the path
printf("[i] On file: %s @ %s \n", lpszRealFileName, lpszRealFileNameAndPath);
HANDLE hFile = CreateFileA(lpszRealFileNameAndPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
unsigned char acSigBytes[2] = { 0, 0 };
unsigned char acValidPESignature[2] = { 0x4D, 0x5A }; // Valid DOS header signature
DWORD dwBytesRead;
ReadFile(hFile, acSigBytes, 0x2, &dwBytesRead, NULL); // First 2 bytes should be DOS header signature
printf("[i] First bytes: %X, %X", acSigBytes[0], acSigBytes[1]);
int nResult = memcmp(acSigBytes, acValidPESignature, 0x2);
if (nResult == 0) {
printf(" | File is valid!\n");
HANDLE hFileMapObj = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
LPDWORD lpMemory = MapViewOfFile(hFileMapObj, FILE_MAP_READ, 0, 0, 0);
PIMAGE_DOS_HEADER pIDH = lpMemory; // DOS_HEADER starts at the beginning of the file
PIMAGE_NT_HEADERS32 pINH = (BYTE*)lpMemory + pIDH->e_lfanew;
/*
Now we see if the file is PE(32-bit) or PE32+ (64-bit)
IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10B
IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20B
SRC: https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_optional_header
*/
// printf("value of lpMemory: %X\n", lpMemory); //Debug only. Shows the location in memory where the file mapping starts
if (pINH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // 32-Bit (PE32)
printf("[i] File is 32-Bit\n");
IMAGE_DATA_DIRECTORY IDDImports;
/*
First we check if the import table is present in the image data directory array, which is always 16 sections.
If the sections are not filled they are zeroed out, but still present in the file.
SRC: https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_data_directory
*/
IDDImports = pINH->OptionalHeader.DataDirectory[IMPORT_TABLE_INDEX]; // File has Import table
if (IDDImports.VirtualAddress == 0x0) {
printf("[!] File has invalid import table address");
goto skipFile;
}
PIMAGE_SECTION_HEADER pISHImports = myImageRvaToVa(pINH, lpMemory, IDDImports.VirtualAddress);
PIMAGE_SECTION_HEADER pISHImportsForName = myImageRvaToSection(pINH, lpMemory, IDDImports.VirtualAddress);
if (pISHImportsForName->Name == 0x00) {
printf("[!] Invalid or non existing section name");
CloseHandle(hFileMapObj); // Free before moving to next file
UnmapViewOfFile(lpMemory); // Free before moving to next file
goto skipFile;
}
printf("[i] Name of section: ");
for (int i = 0; i <= IMAGE_SIZEOF_SHORT_NAME; i++) {
if (pISHImportsForName->Name[i] == 0x00) {
break; // We reached the end of the "Name" string
}
printf("%c", pISHImportsForName->Name[i]);
}
printf("\n");
printf("[i] Searching for import: %s\n", lpszNameOfAPI);
PIMAGE_IMPORT_DESCRIPTOR pIID = pISHImports;
PIMAGE_THUNK_DATA32 pITD32;
PIMAGE_IMPORT_BY_NAME pIIBN;
for (; pIID->Name != 0x0; pIID++) { // Loop through each Image Import Descriptor in the import section
/*
The name "Characteristics" is used in Winnt.h, but no longer describes this field.
SRC: https://docs.microsoft.com/en-us/windows/desktop/debug/pe-format#import-directory-table
*/
LPDWORD lpDLLName = myImageRvaToVa(pINH, lpMemory, pIID->Name);
printf("[i] Checking DLL: %s\n", lpDLLName);
/*
Use OriginalFirstThunk over FirstThunk, as the file may be using
DLL import binding. Apparently Original First Think is also an optional array.
*/
if (pIID->OriginalFirstThunk == 0x00) { // Optional
pITD32 = myImageRvaToVa(pINH, lpMemory, pIID->FirstThunk);
}
else if (pIID->OriginalFirstThunk != pIID->FirstThunk) { // Better than FirstThunk
pITD32 = myImageRvaToVa(pINH, lpMemory, pIID->OriginalFirstThunk);
}
else { //Best bet is OriginalFirstThunk
pITD32 = myImageRvaToVa(pINH, lpMemory, pIID->OriginalFirstThunk);
}
for (; pITD32->u1.AddressOfData != 0x00; pITD32++) { // Look through each Image Thunk Data and read names of imported APISz
/*
API is possibly imported by ordinal. So we need to check the most most significant bit
and see if it is 0x8
SRC: https://docs.microsoft.com/en-us/windows/desktop/debug/pe-format#import-directory-table
*/
if ((ULONGLONG)pITD32->u1.AddressOfData >= 0x80000000) {
printf("\t-API imported by ordinal: %X\n", pITD32->u1.AddressOfData);
}
else {
pIIBN = myImageRvaToVa(pINH, lpMemory, pITD32->u1.AddressOfData);
printf("\t-API name: %s\n", pIIBN->Name);
if (strcmp(pIIBN->Name, lpszNameOfAPI) == 0) {
showAPIMatch(lpszNameOfAPI, lpszRealFileName);
}
}
}
}
}
else if (pINH->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { // 64-Bit (PE32+)
printf("[i] File is 64-Bit\n");
PIMAGE_NT_HEADERS64 pINH64 = (BYTE*)lpMemory + pIDH->e_lfanew; // Without cast, you would be adding to sizeof(lpMemory)!
IMAGE_DATA_DIRECTORY IDDImports;
/*
First we check if the import table is present in the image data directory array, which is always 16 sections.
If the sections are not filled they are zeroed out, but still present in the file.
SRC: https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_data_directory
*/
IDDImports = pINH64->OptionalHeader.DataDirectory[IMPORT_TABLE_INDEX]; //File has Import table
if (IDDImports.VirtualAddress == 0x0) {
printf("[!] File has invalid import table address");
goto skipFile;
}
PIMAGE_SECTION_HEADER pISHImports = myImageRvaToVa(pINH64, lpMemory, IDDImports.VirtualAddress);
PIMAGE_SECTION_HEADER pISHImportsForName = myImageRvaToSection(pINH64, lpMemory, IDDImports.VirtualAddress);
if (pISHImportsForName->Name == 0x00) {
printf("[!] Invalid or non existing section name");
CloseHandle(hFileMapObj); // Free before moving to next file
UnmapViewOfFile(lpMemory); // Free before moving to next file
goto skipFile;
}
printf("[i] Name of section: ");
for (int i = 0; i <= IMAGE_SIZEOF_SHORT_NAME; i++) {
if (pISHImportsForName->Name[i] == 0x00) {
break; // We reached the end of the "Name" string
}
printf("%c", pISHImportsForName->Name[i]);
}
printf("\n");
printf("[i] Searching for import: %s\n", lpszNameOfAPI);
PIMAGE_IMPORT_DESCRIPTOR pIID = pISHImports;
printf("Checking virtual address: %X\n", &pIID->Name);
PIMAGE_THUNK_DATA64 pITD64;
PIMAGE_IMPORT_BY_NAME pIIBN;
for (; pIID->Name != 0x0; pIID++) { // Loop through each Image Import Descriptor in the import section
/*
The name "Characteristics" is used in Winnt.h, but no longer describes this field.
SRC: https://docs.microsoft.com/en-us/windows/desktop/debug/pe-format#import-directory-table
*/
LPDWORD lpDLLName = myImageRvaToVa(pINH64, lpMemory, pIID->Name);
printf("[i] Checking DLL: %s\n", lpDLLName);
/*
Use OriginalFirstThunk over FirstThunk, as the file may be using
DLL import binding. Apparently Original First Think is also an optional array.
*/
if (pIID->OriginalFirstThunk == 0x00) { // Optional
pITD64 = myImageRvaToVa(pINH64, lpMemory, pIID->FirstThunk);
}
else if (pIID->OriginalFirstThunk != pIID->FirstThunk) { // Better than FirstThunk
pITD64 = myImageRvaToVa(pINH64, lpMemory, pIID->OriginalFirstThunk);
}
else { //Best bet is OriginalFirstThunk
pITD64 = myImageRvaToVa(pINH64, lpMemory, pIID->OriginalFirstThunk);
}
for (; pITD64->u1.AddressOfData != 0x00; pITD64++) { // Look through each Image Thunk Data and read names of imported APISz
/*
API is possibly imported by ordinal. So we need to check the most most significant bit
and see if it is 0x8
SRC: https://docs.microsoft.com/en-us/windows/desktop/debug/pe-format#import-directory-table
*/
if ((ULONGLONG)pITD64->u1.AddressOfData >= 0x8000000000000000) {
printf("\t-API imported by ordinal: %X\n", pITD64->u1.AddressOfData);
}
else {
pIIBN = ImageRvaToVa64(lpMemory, pITD64->u1.AddressOfData, pISHImportsForName); // Custom function
printf("\t-API name: %s\n", pIIBN->Name);
if (strcmp(pIIBN->Name, lpszNameOfAPI) == 0) {
showAPIMatch(lpszNameOfAPI, lpszRealFileName);
}
}
}
}
}
else {
errorExit("Optional Header could not be read!");
}
CloseHandle(hFileMapObj);
UnmapViewOfFile(lpMemory);
}
else {
skipFile:;
printf(" | INVALID PE file\nMoving to next file...\n\n");
CloseHandle(hFile);
}
}
else {
printf("[!] Unable to open file... CreateFile returned: 0x%X\n", hFile);
printf("Last Error Code: %X (Press Enter)\n", GetLastError());
getchar();
goto skipFile;
}
CloseHandle(hFile);
}
} while (FindNextFileW(hFirstFile, &winFindData) != 0);
printf("[!] End of directory\n");
FindClose(hFirstFile); // MSDN says don't use normal close handle
return(0); // Success?
}