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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

typedef uint8_t BYTE;

typedef uint16_t WORD;

typedef uint32_t DWORD;

typedef uint32_t LONG;

#define IMAGE_DOS_SIGNATURE 0x5A4D

#define IMAGE_OS2_SIGNATURE 0x454E

#define IMAGE_OS2_SIGNATURE_LE 0x454C

#define IMAGE_VXD_SIGNATURE 0x454C

#define IMAGE_NT_SIGNATURE 0x00004550



typedef struct _IMAGE_DOS_HEADER {

	WORD e_magic;

	WORD e_cblp;

	WORD e_cp;

	WORD e_crlc;

	WORD e_cparhdr;

	WORD e_minalloc;

	WORD e_maxalloc;

	WORD e_ss;

	WORD e_sp;

	WORD e_csum;

	WORD e_ip;

	WORD e_cs;

	WORD e_lfarlc;

	WORD e_ovno;

	WORD e_res[4];

	WORD e_oemid;

	WORD e_oeminfo;

	WORD e_res2[10];

	LONG e_lfanew;

} IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER;

#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16

#define IMAGE_SIZEOF_SHORT_NAME 8
#define IMAGE_DIRECTORY_ENTRY_IMPORT	1



typedef struct _IMAGE_FILE_HEADER {

	WORD Machine;

	WORD NumberOfSections;

	DWORD TimeDateStamp;

	DWORD PointerToSymbolTable;

	DWORD NumberOfSymbols;

	WORD SizeOfOptionalHeader;

	WORD Characteristics;

} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

typedef struct _IMAGE_DATA_DIRECTORY {

	DWORD VirtualAddress;

	DWORD Size;

} IMAGE_DATA_DIRECTORY,*PIMAGE_DATA_DIRECTORY;

typedef struct _IMAGE_OPTIONAL_HEADER {

	WORD Magic;

	BYTE MajorLinkerVersion;

	BYTE MinorLinkerVersion;

	DWORD SizeOfCode;

	DWORD SizeOfInitializedData;

	DWORD SizeOfUninitializedData;

	DWORD AddressOfEntryPoint;

	DWORD BaseOfCode;

	DWORD BaseOfData;

	DWORD ImageBase;

	DWORD SectionAlignment;

	DWORD FileAlignment;

	WORD MajorOperatingSystemVersion;

	WORD MinorOperatingSystemVersion;

	WORD MajorImageVersion;

	WORD MinorImageVersion;

	WORD MajorSubsystemVersion;

	WORD MinorSubsystemVersion;

	DWORD Win32VersionValue;

	DWORD SizeOfImage;

	DWORD SizeOfHeaders;

	DWORD CheckSum;

	WORD Subsystem;

	WORD DllCharacteristics;

	DWORD SizeOfStackReserve;

	DWORD SizeOfStackCommit;

	DWORD SizeOfHeapReserve;

	DWORD SizeOfHeapCommit;

	DWORD LoaderFlags;

	DWORD NumberOfRvaAndSizes;

	IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];

} IMAGE_OPTIONAL_HEADER,*PIMAGE_OPTIONAL_HEADER;



typedef struct _IMAGE_NT_HEADERS {

	DWORD Signature;

	IMAGE_FILE_HEADER FileHeader;

	IMAGE_OPTIONAL_HEADER OptionalHeader;

} IMAGE_NT_HEADERS,*PIMAGE_NT_HEADERS;



typedef struct _IMAGE_SECTION_HEADER {

	BYTE Name[IMAGE_SIZEOF_SHORT_NAME];

	union {

		DWORD PhysicalAddress;

		DWORD VirtualSize;

	} Misc;

	DWORD VirtualAddress;

	DWORD SizeOfRawData;

	DWORD PointerToRawData;

	DWORD PointerToRelocations;

	DWORD PointerToLinenumbers;

	WORD NumberOfRelocations;

	WORD NumberOfLinenumbers;

	DWORD Characteristics;

} IMAGE_SECTION_HEADER,*PIMAGE_SECTION_HEADER;

typedef struct _IMAGE_IMPORT_DESCRIPTOR {
	union {
		DWORD Characteristics;
		DWORD OriginalFirstThunk;
	};
	DWORD TimeDateStamp;
	DWORD ForwarderChain;
	DWORD Name;
	DWORD FirstThunk;
} IMAGE_IMPORT_DESCRIPTOR,*PIMAGE_IMPORT_DESCRIPTOR;

#define IMAGE_FIRST_SECTION(h) \

	((PIMAGE_SECTION_HEADER) ((ptrdiff_t)h+offsetof(IMAGE_NT_HEADERS,OptionalHeader)+ \

		((PIMAGE_NT_HEADERS)(h))->FileHeader.SizeOfOptionalHeader))

/*
 *
 */

int process_file(uint8_t *file_data,size_t file_size)
{
    IMAGE_DOS_HEADER *dos_hdr;

    IMAGE_NT_HEADERS *nt_hdr;

    IMAGE_SECTION_HEADER *section;
    IMAGE_IMPORT_DESCRIPTOR *imports;
    DWORD va,pa,pa_size;
    size_t i,count;

    dos_hdr=(IMAGE_DOS_HEADER *)file_data;
    if(dos_hdr->e_magic!=IMAGE_DOS_SIGNATURE)
    {
        printf("DOS signature mismatch\n");
        return EXIT_FAILURE;
    }
    nt_hdr=(IMAGE_NT_HEADERS *)((BYTE *)dos_hdr+dos_hdr->e_lfanew);

    if(nt_hdr->Signature!=IMAGE_NT_SIGNATURE)
    {
        printf("NT signature mismatch\n");
        return EXIT_FAILURE;
    }
    va=nt_hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
    section=IMAGE_FIRST_SECTION(nt_hdr);
    pa=0;
    for(i=0;i<nt_hdr->FileHeader.NumberOfSections;i++,section++)
    {
        if(section->VirtualAddress==va)
        {
            pa=section->PointerToRawData;
            pa_size=section->SizeOfRawData;
            break;
        }
    }

    if(pa==0)
    {
        printf("No imports found\n");
        return EXIT_FAILURE;
    }

    imports=(IMAGE_IMPORT_DESCRIPTOR *)(file_data+pa);
    count=pa_size/sizeof(IMAGE_IMPORT_DESCRIPTOR);

    for(i=0;i<count;i++,imports++)
    {
        if(imports->OriginalFirstThunk==0 && imports->FirstThunk==0)
            break;
        if(imports->Name==0)
            continue;
        /* WARNING: only works if the names are in the same section as the imports directory structures */
        char *name=(char *)(file_data+imports->Name-va+pa);
        printf("Dll: %s\n",name);
    }

    return EXIT_SUCCESS;
}

int main(int argc,char **argv)
{
    if(argc!=2)
    {
        printf("usage: %s exe_file\n",argv[0]);
        return EXIT_FAILURE;
    }

    FILE *f=fopen(argv[1],"rb");
    if(f==NULL)
    {
        perror("Cannot open file for reading");
        return EXIT_FAILURE;
    }

    fseek(f,0,SEEK_END);
    size_t size=ftell(f);
    fseek(f,0,SEEK_SET);
    uint8_t *data=malloc(size);
    if(data==NULL)
    {
        perror("Allocation failed");
        fclose(f);
        return EXIT_FAILURE;
    }

    if(fread(data,1,size,f)!=size)
    {
        perror("Read failed");
        fclose(f);
        free(data);
        return EXIT_FAILURE;
    }
    fclose(f);

    int ret=process_file(data,size);
    free(data);

    return ret;
}