forked from smx-smx/wcpex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wcpex.c
232 lines (203 loc) · 5.58 KB
/
wcpex.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
/*
* Extraction tool for files handled by WCP - Windows Componentization Platform
* Copyright 2017 Smx <smxdev4@gmail.com>
*
* Program to unpack and (TODO) repack Manifest files, like the ones found in
* %windir%\WinSxS\Manifests
*/
/*
* The manifest files are MSDelta compressed, with a header appended to indicate their type
*/
#include <windows.h>
#include <stdio.h>
#include <stdint.h>
struct __attribute__((packed)) BlobData {
size_t length;
size_t fill;
void *pData;
};
static HMODULE wcp;
static long (__fastcall *InitializeDeltaCompressor)(uintptr_t arg);
static unsigned long (__fastcall *GetCompressedFileType)(struct BlobData *arg);
/*
static long (__fastcall *DeltaCompressFile)(
unsigned long DeltaFlagType,
void *pDictionary,
unsigned long headerSize,
struct BlobData *inData,
struct BlobData *outData
);
*/
static long (__fastcall *DeltaDecompressBuffer)(
unsigned long DeltaFlagType,
void *pDictionary,
unsigned long headerSize,
struct BlobData *inData,
struct BlobData *outData
);
static long (__fastcall *LoadFirstResourceLanguageAgnostic)(
uint32_t unused,
HINSTANCE hModule,
uint16_t lpType,
uint16_t lpName,
void *pOutDict
);
void hexdump(void *pAddressIn, long lSize) {
char szBuf[100];
long lIndent = 1;
long lOutLen, lIndex, lIndex2, lOutLen2;
long lRelPos;
struct {
char *pData;
unsigned long lSize;
} buf;
unsigned char *pTmp, ucTmp;
unsigned char *pAddress = (unsigned char *)pAddressIn;
buf.pData = (char *)pAddress;
buf.lSize = lSize;
while (buf.lSize > 0) {
pTmp = (unsigned char *)buf.pData;
lOutLen = (int)buf.lSize;
if (lOutLen > 16)
lOutLen = 16;
// create a 64-character formatted output line:
sprintf(szBuf, " > %08zX", pTmp - pAddress);
lOutLen2 = lOutLen;
for (lIndex = 1 + lIndent, lIndex2 = 53 - 15 + lIndent, lRelPos = 0; lOutLen2; lOutLen2--, lIndex += 2, lIndex2++) {
ucTmp = *pTmp++;
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
if (!isprint(ucTmp))
ucTmp = '.'; // nonprintable char
szBuf[lIndex2] = ucTmp;
if (!(++lRelPos & 3)) { // extra blank after 4 bytes
lIndex++;
szBuf[lIndex + 2] = ' ';
}
}
if (!(lRelPos & 3))
lIndex--;
szBuf[lIndex] = '<';
szBuf[lIndex + 1] = ' ';
printf("%s\n", szBuf);
buf.pData += lOutLen;
buf.lSize -= lOutLen;
}
}
int init(){
wcp = LoadLibraryA("wcp.dll");
if(!wcp){
fprintf(stderr, "wcp load fail\n");
return -1;
}
GetCompressedFileType = (void *)GetProcAddress(wcp, "?GetCompressedFileType@Rtl@WCP@Windows@@YAKPEBU_LBLOB@@@Z");
InitializeDeltaCompressor = (void *)GetProcAddress(wcp, "?InitializeDeltaCompressor@Rtl@Windows@@YAJPEAX@Z");
//DeltaCompressFile = (void *)GetProcAddress(wcp, "?DeltaCompressFile@Rtl@Windows@@YAJKPEAUIRtlFile@12@PEBU_LBLOB@@00@Z");
DeltaDecompressBuffer = (void *)GetProcAddress(wcp, "?DeltaDecompressBuffer@Rtl@Windows@@YAJKPEAU_LBLOB@@_K0PEAVAutoDeltaBlob@12@@Z");
LoadFirstResourceLanguageAgnostic = (void *)GetProcAddress(wcp, "?LoadFirstResourceLanguageAgnostic@Rtl@Windows@@YAJKPEAUHINSTANCE__@@PEBG1PEAU_LBLOB@@@Z");
if(
GetCompressedFileType == NULL ||
InitializeDeltaCompressor == NULL ||
//DeltaCompressFile == NULL ||
DeltaDecompressBuffer == NULL ||
LoadFirstResourceLanguageAgnostic == NULL
){
return -1;
}
return 0;
}
int main(int argc, char *argv[]){
int eCode = 1; //exit code
size_t dataSize; //size of input data
uint8_t *manifestData = NULL; //copy of input data
long result = -1; //result of calls
if(argc < 2){
fprintf(stderr, "Usage: %s [infile][[outfile]]\n", argv[0]);
goto end;
}
if(init() != 0){
fprintf(stderr, "Cannot load wcp.dll\n");
goto end;
}
FILE *f = fopen(argv[1], "rb");
if(!f){
fprintf(stderr, "Open Fail\n");
return 1;
}
fseek(f, 0, SEEK_END);
dataSize = ftell(f);
rewind(f);
manifestData = calloc(1, dataSize);
fread(manifestData, dataSize, 1, f);
fclose(f);
fprintf(stderr, "Size: %u\n", dataSize);
struct BlobData inData = {
.length = dataSize,
.fill = dataSize,
.pData = (void *)manifestData
};
unsigned long type = GetCompressedFileType(&inData);
fprintf(stderr, "Type is %d\n", type);
if(type != 4){
fprintf(stderr, "WARNING: Untested compression type '%d'\n", type);
}
result = InitializeDeltaCompressor((uintptr_t)NULL);
fprintf(stderr, "InitializeDeltaCompressor: 0x%08X\n", result);
if(result >= 0){
result = 0;
} else {
fprintf(stderr, "InitializeDeltaCompressor failed\n");
goto end;
}
uint64_t DictData[3];
result = LoadFirstResourceLanguageAgnostic(
0, //unused
wcp, //HMODULE
// These seem to have a special meaning
0x266, //lpType (unsure)
1, //unknown
&DictData
);
fprintf(stderr, "LoadFirstResourceLanguageAgnostic: 0x%08X\n", result);
if(result >= 0){
result = 0;
} else {
printf("LoadFirstResourceLanguageAgnostic failed\n");
goto end;
}
//printf("==> Dictionary\n");
//hexdump(&DictData, sizeof(DictData));
struct BlobData outData;
result = DeltaDecompressBuffer(
2, //type?
&DictData,
4, //headerSize
&inData,
&outData
);
fprintf(stderr, "DeltaDecompressBuffer: 0x%08X\n", result);
if(result >= 0){
result = 0;
} else {
printf("DeltaDecompressBuffer failed\n");
goto end;
}
fprintf(stderr, "==> Out Blob\n");
//hexdump(&outData, sizeof(outData));
FILE *outFile;
if(argc < 3)
outFile = stdout;
else
outFile = fopen(argv[2], "w");
if(!outFile){
fprintf(stderr, "Cannot open output file '%s' for writing\n", argv[2]);
goto end;
}
fwrite(outData.pData, outData.length, 1, outFile);
if(argc > 2)
fclose(outFile);
eCode = 0;
end:
if(manifestData != NULL)
free(manifestData);
return eCode;
}