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
352
353
354
355
356
357
358
359
|
/* Chrysalide - Outil d'analyse de fichiers binaires
* pe-int.c - structures internes du format Portable Executable
*
* Copyright (C) 2010 Cyrille Bagard
*
* This file is part of Chrysalide.
*
* OpenIDA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* OpenIDA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pe-int.h"
#include <malloc.h>
#include <string.h>
#include "../../common/endianness.h"
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* header = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de programme DOS. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_dos_image_header(const GPeFormat *format, off_t *pos, image_dos_header *header)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
size_t i; /* Boucle de parcours */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u16(&header->e_magic, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_cblp, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_cp, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_crlc, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_cparhdr, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_minalloc, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_maxalloc, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_ss, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_sp, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_csum, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_ip, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_cs, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_lfarlc, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_ovno, content, pos, length, SRE_LITTLE);
for (i = 0; i < 4 && result; i++)
result = read_u16(&header->e_res[i], content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_oemid, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->e_oeminfo, content, pos, length, SRE_LITTLE);
for (i = 0; i < 10 && result; i++)
result = read_u16(&header->e_res2[i], content, pos, length, SRE_LITTLE);
result &= read_u32(&header->e_lfanew, content, pos, length, SRE_LITTLE);
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* header = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de programme PE (1). *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_file_header(const GPeFormat *format, off_t *pos, image_file_header *header)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u16(&header->machine, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->number_of_sections, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->time_date_stamp, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->pointer_to_symbol_table, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->number_of_symbols, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->size_of_optional_header, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->characteristics, content, pos, length, SRE_LITTLE);
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* header = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de programme PE (2). *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_optional_header(const GPeFormat *format, off_t *pos, image_optional_header *header)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
uint32_t i; /* Boucle de parcours */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u16(&header->magic, content, pos, length, SRE_LITTLE);
result &= read_u8(&header->major_linker_version, content, pos, length, SRE_LITTLE);
result &= read_u8(&header->minor_linker_version, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_code, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_initialized_data, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_uninitialized_data, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->address_of_entry_point, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->base_of_code, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->base_of_data, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->image_base, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->section_alignment, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->file_alignment, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->major_operating_system_version, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->minor_operating_system_version, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->major_image_version, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->minor_image_version, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->major_subsystem_version, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->minor_subsystem_version, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->win32_version_value, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_image, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_headers, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->checksum, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->subsystem, content, pos, length, SRE_LITTLE);
result &= read_u16(&header->dll_characteristics, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_stack_reserve, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_stack_commit, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_heap_reserve, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->size_of_heap_commit, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->loader_flags, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->number_of_rva_and_sizes, content, pos, length, SRE_LITTLE);
for (i = 0; i < header->number_of_rva_and_sizes && result; i++)
{
result = read_u32(&header->data_directory[i].virtual_address, content, pos, length, SRE_LITTLE);
result &= read_u32(&header->data_directory[i].size, content, pos, length, SRE_LITTLE);
}
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* header = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de programme PE. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_nt_header(const GPeFormat *format, off_t *pos, image_nt_headers *header)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u32(&header->signature, content, pos, length, SRE_LITTLE);
result &= read_pe_file_header(format, pos, &header->file_header);
result &= read_pe_optional_header(format, pos, &header->optional_header);
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* section = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de section PE. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_image_section_header(const GPeFormat *format, off_t *pos, image_section_header *section)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
size_t i; /* Boucle de parcours */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = true;
for (i = 0; i < IMAGE_SIZEOF_SHORT_NAME && result; i++)
result = read_u8((uint8_t *)§ion->name[i], content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->misc.physical_address, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->virtual_address, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->size_of_raw_data, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->pointer_to_raw_data, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->pointer_to_relocations, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->pointer_to_line_numbers, content, pos, length, SRE_LITTLE);
result &= read_u16(§ion->number_of_relocations, content, pos, length, SRE_LITTLE);
result &= read_u16(§ion->number_of_line_numbers, content, pos, length, SRE_LITTLE);
result &= read_u32(§ion->characteristics, content, pos, length, SRE_LITTLE);
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* desc = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'un répertoire de programme PE. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_image_import_descriptor(const GPeFormat *format, off_t *pos, image_import_descriptor *desc)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u32(&desc->original_first_thunk, content, pos, length, SRE_LITTLE);
result &= read_u32(&desc->time_date_stamp, content, pos, length, SRE_LITTLE);
result &= read_u32(&desc->forwarder_chain, content, pos, length, SRE_LITTLE);
result &= read_u32(&desc->module_name, content, pos, length, SRE_LITTLE);
result &= read_u32(&desc->first_thunk, content, pos, length, SRE_LITTLE);
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* import = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une fonction importée par son nom. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_pe_image_import_by_name(const GPeFormat *format, off_t *pos, image_import_by_name *import)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
uint32_t link; /* Lien vers la prochaine zone */
off_t new_pos; /* Nouvelle tête de lecture */
size_t i; /* Boucle de parcours */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
result = read_u32(&link, content, pos, length, SRE_LITTLE);
if (link == 0)
memset(import, 0, sizeof(image_import_by_name));
else if (link % 2 == 0)
{
new_pos = link;
result = read_u16(&import->hint, content, &new_pos, length, SRE_LITTLE);
import->name = (char *)calloc(1, sizeof(char));
for (i = 0; result; i++)
{
result = read_u8((uint8_t *)&import->name[i], content, &new_pos, length, SRE_LITTLE);
if (import->name[i] == '\0')
break;
import->name = (char *)realloc(import->name, (i + 2) * sizeof(char));
}
}
else /* TODO */;
return result;
}
|