-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_DLL_Dependencies.c
352 lines (318 loc) · 10.1 KB
/
Find_DLL_Dependencies.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
/*
* Find_DLL_Dependencies
* Copyright (C) 2023 Benjamin VERNOUX
* MIT License (MIT)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define APP_NAME "Find_DLL_Dependencies"
#define VERSION "v0.1.1 03/06/2023 B.VERNOUX"
#define BANNER1 APP_NAME " " VERSION "\n"
#define USAGE "usage: " APP_NAME " <win32_64_exe_or_dll>\n"
#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
/* Support only Windows x86 & x64 EXE/DLL */
#define IMAGE_FILE_MACHINE_I386 (0x014c) // x86
#define IMAGE_FILE_MACHINE_AMD64 (0x8664) // x64
#define DLL_NAME_MAX_SIZE (256)
/*
References:
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
https://0xrick.github.io/win-internals/pe1/
https://github.com/0xRick/PE-Parser/tree/main
https://stofu.io/blog/view_post.php?id=10 PE Import Table Parser
*/
typedef struct _IMAGE_DOS_HEADER {
uint16_t e_magic;
uint16_t e_cblp;
uint16_t e_cp;
uint16_t e_crlc;
uint16_t e_cparhdr;
uint16_t e_minalloc;
uint16_t e_maxalloc;
uint16_t e_ss;
uint16_t e_sp;
uint16_t e_csum;
uint16_t e_ip;
uint16_t e_cs;
uint16_t e_lfarlc;
uint16_t e_ovno;
uint16_t e_res[4];
uint16_t e_oemid;
uint16_t e_oeminfo;
uint16_t e_res2[10];
uint32_t e_lfanew;
} IMAGE_DOS_HEADER;
typedef struct _IMAGE_FILE_HEADER {
uint16_t Machine;
uint16_t NumberOfSections;
uint32_t TimeDateStamp;
uint32_t PointerToSymbolTable;
uint32_t NumberOfSymbols;
uint16_t SizeOfOptionalHeader;
uint16_t Characteristics;
} IMAGE_FILE_HEADER;
typedef struct _IMAGE_DATA_DIRECTORY {
uint32_t VirtualAddress;
uint32_t Size;
} IMAGE_DATA_DIRECTORY;
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
typedef struct _IMAGE_OPTIONAL_HEADER32 {
uint16_t Magic;
uint8_t MajorLinkerVersion;
uint8_t MinorLinkerVersion;
uint32_t SizeOfCode;
uint32_t SizeOfInitializedData;
uint32_t SizeOfUninitializedData;
uint32_t AddressOfEntryPoint;
uint32_t BaseOfCode;
uint32_t BaseOfData;
uint32_t ImageBase;
uint32_t SectionAlignment;
uint32_t FileAlignment;
uint16_t MajorOperatingSystemVersion;
uint16_t MinorOperatingSystemVersion;
uint16_t MajorImageVersion;
uint16_t MinorImageVersion;
uint16_t MajorSubsystemVersion;
uint16_t MinorSubsystemVersion;
uint32_t Reserved1;
uint32_t SizeOfImage;
uint32_t SizeOfHeaders;
uint32_t CheckSum;
uint16_t Subsystem;
uint16_t DllCharacteristics;
uint32_t SizeOfStackReserve;
uint32_t SizeOfStackCommit;
uint32_t SizeOfHeapReserve;
uint32_t SizeOfHeapCommit;
uint32_t LoaderFlags;
uint32_t NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER32;
typedef struct _IMAGE_OPTIONAL_HEADER64 {
uint16_t Magic;
uint8_t MajorLinkerVersion;
uint8_t MinorLinkerVersion;
uint32_t SizeOfCode;
uint32_t SizeOfInitializedData;
uint32_t SizeOfUninitializedData;
uint32_t AddressOfEntryPoint;
uint32_t BaseOfCode;
uint64_t ImageBase;
uint32_t SectionAlignment;
uint32_t FileAlignment;
uint16_t MajorOperatingSystemVersion;
uint16_t MinorOperatingSystemVersion;
uint16_t MajorImageVersion;
uint16_t MinorImageVersion;
uint16_t MajorSubsystemVersion;
uint16_t MinorSubsystemVersion;
uint32_t Reserved1;
uint32_t SizeOfImage;
uint32_t SizeOfHeaders;
uint32_t CheckSum;
uint16_t Subsystem;
uint16_t DllCharacteristics;
uint64_t SizeOfStackReserve;
uint64_t SizeOfStackCommit;
uint64_t SizeOfHeapReserve;
uint64_t SizeOfHeapCommit;
uint32_t LoaderFlags;
uint32_t NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER64;
typedef struct _IMAGE_NT_HEADERS32 {
uint32_t Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
} IMAGE_NT_HEADERS32;
typedef struct _IMAGE_NT_HEADERS64 {
uint32_t Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
} IMAGE_NT_HEADERS64;
typedef struct _IMAGE_SECTION_HEADER {
char Name[8];
uint32_t VirtualSize;
uint32_t VirtualAddress;
uint32_t SizeOfRawData;
uint32_t PointerToRawData;
uint32_t PointerToRelocations;
uint32_t PointerToLinenumbers;
uint16_t NumberOfRelocations;
uint16_t NumberOfLinenumbers;
uint32_t Characteristics;
} IMAGE_SECTION_HEADER;
typedef struct _IMAGE_IMPORT_DESCRIPTOR {
union {
uint32_t Characteristics;
uint32_t OriginalFirstThunk;
} DUMMYUNIONNAME;
uint32_t TimeDateStamp;
uint32_t ForwarderChain;
uint32_t Name;
uint32_t FirstThunk;
} IMAGE_IMPORT_DESCRIPTOR;
#define Find_DLL_Dependencies_file_error_exit(file_handle) \
do { \
fclose(file_handle); \
printf("File error exit:\nFile: %s\nLine: %d\nFunction: %s\n", __FILE__, __LINE__, __func__); \
exit(-1); \
} while (0)
/* Return number of DLL dependencies found >0 or error code */
int Find_DLL_Dependencies(const char* filename, char* dependencies[], size_t num_dependencies) {
IMAGE_DOS_HEADER dos_header;
IMAGE_NT_HEADERS32 nt_headers32;
IMAGE_NT_HEADERS64 nt_headers64;
IMAGE_SECTION_HEADER section_header;
int nb_dependencies = 0;
size_t fread_nb;
if (dependencies == NULL)
return -1; // Error dependencies is NULL
if (num_dependencies < 1)
return -2; // Error num_dependencies invalid size
FILE* file = fopen(filename, "rb");
if (!file) {
return -3; // Error to open file
}
fread_nb = fread(&dos_header, 1, sizeof(IMAGE_DOS_HEADER), file);
if(fread_nb != sizeof(IMAGE_DOS_HEADER)) {
Find_DLL_Dependencies_file_error_exit(file);
}
if(fseek(file, dos_header.e_lfanew, SEEK_SET) != 0) {
Find_DLL_Dependencies_file_error_exit(file);
}
fread_nb = fread(&nt_headers32.Signature, 1, sizeof(uint32_t), file);
if(fread_nb != sizeof(uint32_t)) {
Find_DLL_Dependencies_file_error_exit(file);
}
memcpy(&nt_headers64.Signature, &nt_headers32.Signature, sizeof(uint32_t));
// Signature "PE\0\0"
if (nt_headers32.Signature != 0x00004550) {
fclose(file);
return -4; // Error nt_headers32.Signature != "PE\0\0"
}
fread_nb = fread(&nt_headers32.FileHeader, 1, sizeof(IMAGE_FILE_HEADER), file);
if(fread_nb != sizeof(IMAGE_FILE_HEADER)) {
Find_DLL_Dependencies_file_error_exit(file);
}
memcpy(&nt_headers64.FileHeader, &nt_headers32.FileHeader, sizeof(IMAGE_FILE_HEADER));
IMAGE_DATA_DIRECTORY import_directory;
uint16_t FileHeader_NumberOfSections = 0;
switch(nt_headers32.FileHeader.Machine)
{
case IMAGE_FILE_MACHINE_I386: // 32-bit executable / DLL
{
fread_nb = fread(&nt_headers32.OptionalHeader, 1, sizeof(IMAGE_OPTIONAL_HEADER32), file);
if(fread_nb != sizeof(IMAGE_OPTIONAL_HEADER32)) {
Find_DLL_Dependencies_file_error_exit(file);
}
import_directory = nt_headers32.OptionalHeader.DataDirectory[1]; // Set to Import directory
FileHeader_NumberOfSections = nt_headers32.FileHeader.NumberOfSections;
break;
}
case IMAGE_FILE_MACHINE_AMD64: // 64-bit executable / DLL
{
fread_nb = fread(&nt_headers64.OptionalHeader, 1, sizeof(IMAGE_OPTIONAL_HEADER64), file);
if(fread_nb != sizeof(IMAGE_OPTIONAL_HEADER64)) {
Find_DLL_Dependencies_file_error_exit(file);
}
import_directory = nt_headers64.OptionalHeader.DataDirectory[1]; // Set to Import directory
FileHeader_NumberOfSections = nt_headers64.FileHeader.NumberOfSections;
break;
}
default:
{
fclose(file);
return -5; // Image file machine not supported
}
}
// Find the section containing the import directory
int i;
for (i = 0; i < FileHeader_NumberOfSections; i++) {
fread_nb = fread(§ion_header, 1, sizeof(IMAGE_SECTION_HEADER), file);
if(fread_nb != sizeof(IMAGE_SECTION_HEADER)) {
Find_DLL_Dependencies_file_error_exit(file);
}
if (section_header.VirtualAddress <= import_directory.VirtualAddress &&
import_directory.VirtualAddress < (section_header.VirtualAddress + section_header.VirtualSize)) {
break;
}
}
if (i == FileHeader_NumberOfSections) {
fclose(file);
return 0; /* No dependencies found / nt_headers32 or nt_headers64 => FileHeader.NumberOfSections */
}
if (import_directory.VirtualAddress == 0 || import_directory.Size == 0) {
fclose(file);
return 0; /* No dependencies found / Error import_directory invalid data */
}
IMAGE_IMPORT_DESCRIPTOR import_descriptor;
long fileoffset_import_descriptor = section_header.PointerToRawData + (import_directory.VirtualAddress - section_header.VirtualAddress);
while (1) {
if(fseek(file, fileoffset_import_descriptor, SEEK_SET) != 0) {
Find_DLL_Dependencies_file_error_exit(file);
}
fread_nb = fread(&import_descriptor, 1, sizeof(IMAGE_IMPORT_DESCRIPTOR), file);
if(fread_nb != sizeof(IMAGE_IMPORT_DESCRIPTOR)) {
Find_DLL_Dependencies_file_error_exit(file);
}
fileoffset_import_descriptor += sizeof(IMAGE_IMPORT_DESCRIPTOR);
if (import_descriptor.Name == 0 && import_descriptor.FirstThunk == 0)
break;
if (nb_dependencies < num_dependencies) {
if(fseek(file, section_header.PointerToRawData + (import_descriptor.Name - section_header.VirtualAddress), SEEK_SET) != 0) {
Find_DLL_Dependencies_file_error_exit(file);
}
char dll_name[DLL_NAME_MAX_SIZE + 1] = { 0 };
fread_nb = fread(dll_name, 1, DLL_NAME_MAX_SIZE, file);
if(fread_nb != DLL_NAME_MAX_SIZE) {
Find_DLL_Dependencies_file_error_exit(file);
}
dependencies[nb_dependencies++] = strdup(dll_name);
} else {
fclose(file);
return nb_dependencies;
}
}
fclose(file);
return nb_dependencies;
}
void Free_DLL_Dependencies(char* dependencies[], size_t num_dependencies)
{
for (size_t i = 0; i < num_dependencies; i++)
{
free(dependencies[i]);
}
}
/*
int main(int argc, char* argv[]) {
#define MAX_DEPS 10000
printf(BANNER1);
if (argc < 2) {
printf(USAGE);
return -1;
}
const char* filename = argv[1];
char* dependencies[MAX_DEPS] = { 0 }; // Adjust the array size as per your requirements
size_t num_dependencies = ARRAY_LENGTH(dependencies);
int num_deps = Find_DLL_Dependencies(filename, dependencies, num_dependencies);
if (num_deps == 0)
{
printf("No dependencies found.\n");
}else if (num_deps > 0) {
printf("Dependencies(%d):\n", num_deps);
for (size_t i = 0; i < num_deps; i++) {
if (dependencies[i] != NULL) {
printf("%s\n", dependencies[i]);
}
}
Free_DLL_Dependencies(dependencies, num_deps);
} else {
printf("Find_DLL_Dependencies error %d.\n", num_deps);
}
return 0;
}
*/