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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*++
Copyright (c) 2004-2006 Intel Corporation. All rights reserved
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
ZeroDebugData.c
Abstract:
Zero the Debug Data Fields of Portable Executable (PE) format file.
--*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void
PrintUsage (
void
)
/*++
Routine Description:
print usage of ZeroDebugData command
Arguments:
None
Returns:
None
--*/
// GC_TODO: void - add argument and description to function comment
{
//
// print usage of command
//
printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
}
int
ReadFromFile (
FILE *fp,
long offset,
void *buffer,
int size
)
/*++
Routine Description:
read data from a specified location of file
Arguments:
fp - file pointer
offset - number of bytes from beginning of file
buffer - buffer used to store data
size - size of buffer
Returns:
= 0 - Success
= -1 - Failed
--*/
{
//
// set file pointer to the specified location of file
//
if (fseek (fp, offset, SEEK_SET) != 0) {
printf ("Error: Cannot move the current location of the file.\n");
return -1;
}
//
// read data from the file
//
if (fread (buffer, size, 1, fp) != 1) {
printf ("Error: Cannot read data from the file.\n");
return -1;
}
return 0;
}
int
ZeroDebugData (
FILE *fp,
FILE *fpData
)
/*++
Routine Description:
Zero the debug data fields of the file
Arguments:
fp - file pointer
fpData - pointer to output file that ZeroDebugData progress is written to
Returns:
= 0 - Success
= -1 - Failed
--*/
{
unsigned char header[4];
unsigned long offset;
unsigned long NumberOfRvaAndSizes;
unsigned int nvalue;
unsigned long lvalue;
unsigned long Size;
unsigned long Pointer;
unsigned char *Buffer;
unsigned long Index;
//
// read the header of file
//
if (ReadFromFile (fp, 0, header, 2) != 0) {
printf ("Error: open image file\n");
return -1;
}
//
// "MZ" -- the header of image file (PE)
//
if (strncmp ((char *) header, "MZ", 2) != 0) {
printf ("Error: Invalid Image file.\n");
return -1;
}
//
// At location 0x3C, the stub has the file offset to the
// PE signature.
//
if (ReadFromFile (fp, 0x3C, &offset, 4) != 0) {
return -1;
}
//
// read the header of optional
//
if (ReadFromFile (fp, offset, header, 4) != 0) {
return -1;
}
//
// "PE\0\0" -- the signature of optional header
//
if (strncmp ((char *) header, "PE\0\0", 4) != 0) {
printf ("Error: Invalid PE format file.\n");
return -1;
}
//
// Add 16 to skip COFF file header, and get to optional header.
//
offset += 24;
//
// Check the magic field, 0x10B for PE32 and 0x20B for PE32+
//
if (ReadFromFile (fp, offset, &nvalue, 2) != 0) {
return -1;
}
//
// If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
// Else it is 108.
//
switch (nvalue & 0xFFFF) {
case 0x10B:
offset += 92;
printf ("Info: Image is PE32. ");
break;
case 0x20B:
offset += 108;
printf ("Info: Image is PE32+. ");
break;
default:
printf ("Error: Magic value is unknown.\n");
return -1;
}
//
// get the value of NumberOfRvaAndSizes
//
if (ReadFromFile (fp, offset, &NumberOfRvaAndSizes, 4) != 0) {
printf ("Error: read NumberOfRvaAndSizes error.\n");
return -1;
}
//
// printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
//
//
// Finding Debug Table, offset of Debug Table
// is 4 + 6 * 8 = 52.
//
if (NumberOfRvaAndSizes >= 7) {
if (ReadFromFile (fp, offset + 52, &lvalue, 4) != 0) {
return -1;
}
//
// Read the SizeOfData(16) and PointerToRawData(24)
//
if (ReadFromFile (fp, lvalue + 16, &Size, 4) != 0) {
printf ("error: Size = %d\n", Size);
return -1;
}
printf ("Debug data: size = %xh, ", Size);
fprintf (fpData, "Debug data: size = %xh, ", Size);
if (ReadFromFile (fp, lvalue + 20, &Pointer, 4) != 0) {
printf ("error: LoadOffset = %xh\n", Pointer);
return -1;
}
//
// printf ("LoadOffset = %xh, ", Pointer);
//
fprintf (fpData, "LoadOffset = %xh, ", Pointer);
if (ReadFromFile (fp, lvalue + 24, &Pointer, 4) != 0) {
printf ("error: FileOffset = %xh\n", Pointer);
return -1;
}
printf ("FileOffset = %xh, ", Pointer);
fprintf (fpData, "FileOffset = %xh, \n", Pointer);
if ((lvalue != 0) && (Pointer != 0)) {
//
// prepare buffer
//
Buffer = malloc (Size + 1);
if (Buffer == NULL) {
printf ("Error: Cannot allocate memory.\n");
return -1;
}
//
// set file pointer to the specified location of file
//
if (fseek (fp, Pointer, SEEK_SET) != 0) {
printf ("Error: Cannot move the current location of the file.\n");
free (Buffer);
return -1;
}
//
// read data from PE file
//
if (fread (Buffer, Size, 1, fp) != 1) {
printf ("Error: Cannot read data from the file.\n");
free (Buffer);
return -1;
}
//
// write to data file
//
for (Index = 0; Index < Size;) {
fprintf (fpData, "%02x ", Buffer[Index]);
Index++;
if (Index % 8 == 0) {
fprintf (fpData, "\n");
}
}
fprintf (fpData, "\n");
//
// zero buffer and write back to PE file
//
if (fseek (fp, Pointer, SEEK_SET) != 0) {
printf ("Error: Cannot move the current location of the file.\n");
free (Buffer);
return -1;
}
memset (Buffer, 0, Size);
if (fwrite (Buffer, Size, 1, fp) != 1) {
perror ("Error: Cannot write zero to the file.\n");
free (Buffer);
return -1;
}
//
// set file pointer to the specified location of file
//
if (fseek (fp, lvalue + 4, SEEK_SET) != 0) {
printf ("Error: Cannot move the current location of the file.\n");
free (Buffer);
return -1;
}
if (fwrite (Buffer, 4, 1, fp) != 1) {
perror ("Error: Cannot write zero to the file.\n");
free (Buffer);
return -1;
}
free (Buffer);
}
}
return 0;
}
int
main (
int argc,
char *argv[]
)
/*++
Routine Description:
Prints the zero debug data of the PE file to the DebugData file.
Executes the ZeroDebugData function.
Arguments:
argc - Standard C argument, number of command line arguments.
argv[] - Standard C argument, array of pointers to the input files,
such as the PE and DebugData files.
Returns:
zero - success
nonzero - failure
--*/
{
FILE *fp;
FILE *fpData;
char DataFile[1024] = "";
//
// check the number of parameters
//
if (argc < 2) {
printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
return -1;
}
//
// open the DebugData file, if not exists, return
//
if (argc >= 3) {
strcpy (DataFile, argv[2]);
} else {
strcpy (DataFile, "DebugData.dat");
}
fpData = fopen (DataFile, "a+");
if (fpData == NULL) {
fpData = fopen (DataFile, "w");
if (fpData == NULL) {
printf ("Error: Cannot open the data file!\n");
return -1;
}
}
//
// open the PE file
//
fp = fopen (argv[1], "r+b");
if (fp == NULL) {
printf ("Error: Cannot open the PE file!\n");
return -1;
}
//
// Zero the Debug Data to the PE file
//
printf ("Zero Debug Data to file %s:\n", argv[1]);
fprintf (fpData, "\nZero Debug Data to file %s:\n", argv[1]);
if ((int *) ZeroDebugData (fp, fpData) != 0) {
printf ("Error: Zero Debug Data PE file\n");
fclose (fp);
return -1;
}
printf (" success\n");
//
// close the PE file
//
fflush (fpData);
fflush (fp);
fclose (fpData);
fclose (fp);
return 0;
}
|