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
|
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <imagehlp.h>
#include <cvinfo.h>
#include <cvexefmt.h>
BOOL
StripCv(
PSZ szImage
);
void __cdecl main(int argc, char *argv[]);
void Usage(void);
void
Usage (void)
{
puts("USAGE: StripCV <imagename>\n"
"\tRemove non-public CV info from an image.\n");
}
void __cdecl
main(
int argc,
char *argv[])
{
int i;
if (argc < 2) {
Usage();
exit(1);
}
for (i = 1; i < argc; i++) {
StripCv(argv[i]);
}
}
BOOL
StripCv(
PSZ szImage
)
{
PCHAR CvDebugData;
unsigned int i, NumberOfDebugDirectories;
ULONG DebugDirectorySize, NewCvSize = 0;
PCHAR NewCvData;
HANDLE FileHandle, hMappedFile;
PVOID DebugDirectories, ImageBase;
PIMAGE_NT_HEADERS NtHeaders;
PIMAGE_DEBUG_DIRECTORY DebugDirectory;
BOOL fRemoveCV = FALSE;
BOOL fDbgFile = FALSE;
ULONG *pDebugSize, FileSize;
BOOL RC = TRUE;
FileHandle = CreateFile(
szImage,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL );
if (FileHandle == INVALID_HANDLE_VALUE) {
printf("Error: Unable to open %s - rc: %d\n", szImage, GetLastError());
RC = FALSE;
goto cleanup1;
}
FileSize = GetFileSize(FileHandle, NULL);
hMappedFile = CreateFileMapping( FileHandle, NULL, PAGE_READWRITE, 0, 0, NULL );
if (!hMappedFile) {
printf("Error: Unable to create read/write map on %s - rc: %d\n", szImage, GetLastError());
RC = FALSE;
goto cleanup2;
}
ImageBase = MapViewOfFile( hMappedFile, FILE_MAP_WRITE, 0, 0, 0 );
if (!ImageBase) {
printf("Error: Unable to Map view of file %s - rc: %d\n", szImage, GetLastError);
RC = FALSE;
goto cleanup3;
}
if (*(USHORT *)ImageBase == IMAGE_SEPARATE_DEBUG_SIGNATURE) {
fDbgFile = TRUE;
PIMAGE_SEPARATE_DEBUG_HEADER DbgFile = (PIMAGE_SEPARATE_DEBUG_HEADER) ImageBase;
DebugDirectories = (PIMAGE_DEBUG_DIRECTORY)((PUCHAR)ImageBase +
sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
(DbgFile->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)) +
DbgFile->ExportedNamesSize );
pDebugSize = &(DbgFile->DebugDirectorySize);
DebugDirectorySize = DbgFile->DebugDirectorySize;
} else {
NtHeaders = ImageNtHeader( ImageBase );
if (NtHeaders == NULL) {
printf("Error: %s is not an NT image\n", szImage);
RC = FALSE;
goto cleanup4;
}
DebugDirectories =
ImageDirectoryEntryToData(
ImageBase,
FALSE,
IMAGE_DIRECTORY_ENTRY_DEBUG,
&DebugDirectorySize );
pDebugSize = &(NtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size);
}
if (DebugDirectories == NULL || DebugDirectorySize == 0)
{
printf("Warning: No debug info found on %s\n", szImage);
RC = FALSE;
goto cleanup4;
}
DebugDirectory = (PIMAGE_DEBUG_DIRECTORY) DebugDirectories;
NumberOfDebugDirectories = DebugDirectorySize / sizeof( IMAGE_DEBUG_DIRECTORY );
CvDebugData = NULL;
for (i=0; i < NumberOfDebugDirectories; i++) {
if (DebugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW) {
CvDebugData = (PCHAR)ImageBase + DebugDirectory->PointerToRawData;
break;
}
// Zero out the Fixup data
if (DebugDirectory->Type == IMAGE_DEBUG_TYPE_FIXUP) {
printf("Info: Removing Fixup data from %s\n", szImage);
RtlZeroMemory(((PUCHAR)ImageBase + DebugDirectory->PointerToRawData),
DebugDirectory->SizeOfData);
DebugDirectory = DebugDirectory+1;
*pDebugSize -= sizeof(IMAGE_DEBUG_DIRECTORY);
} else {
DebugDirectory++;
}
}
if (CvDebugData == NULL) {
printf("Info: No CV Debug found on %s\n", szImage);
RC = FALSE;
goto cleanup5;
}
if (RemovePrivateCvSymbolic(CvDebugData, &NewCvData, &NewCvSize)) {
printf("Info: CV types info stripped from %s\n", szImage);
}
RtlCopyMemory(CvDebugData, NewCvData, NewCvSize);
DebugDirectory = (PIMAGE_DEBUG_DIRECTORY) DebugDirectories;
for (i=0; i < NumberOfDebugDirectories; i++) {
if (DebugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW) {
if (i+1 == NumberOfDebugDirectories) {
// This is the simple case. cv is the last entry. Simply truncate the image.
FileSize += NewCvSize - DebugDirectory->SizeOfData;
}
DebugDirectory->SizeOfData = NewCvSize;
break;
}
DebugDirectory++;
}
cleanup5:
// All done.
if (!fDbgFile) {
PIMAGE_NT_HEADERS pHdr = NULL;
DWORD SumHeader;
DWORD SumTotal;
pHdr = CheckSumMappedFile(ImageBase, FileSize, &SumHeader, &SumTotal);
if (pHdr != NULL) {
pHdr->OptionalHeader.CheckSum = SumTotal;
}
}
FlushViewOfFile(ImageBase, NULL);
cleanup4:
UnmapViewOfFile(ImageBase);
cleanup3:
CloseHandle(hMappedFile);
cleanup2:
SetFilePointer(FileHandle, FileSize, NULL, FILE_BEGIN);
SetEndOfFile(FileHandle);
CloseHandle(FileHandle);
cleanup1:
return(RC);
}
|