打包解包小工具

目的效果:打包程序selfCompress.exe将指定目录下的所有文件以及文件夹打包成一个文件,由于程序逻辑均可逆向,所以仅对文件名以及文件内容进行了xor encode。将解包程序selfDecompress.exe以及打包好后的文件babyCompress.fate放在同一目录下,运行selfDecompress.exe即可解包,不能二次解包同一文件babyCompress.fate,除非修改babyCompress.fate偏移16处的标志位1为0,且需要删除*.fate文件(打包时没有设置.fate文件则无所谓),因为*.fate设置了隐藏以及系统保护属性。

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
//selfCompress.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
#include<stdio.h>
#include"header.h"

//#pragma pack(1) 如果修改该结构体,可使用强制内存1字节对齐
typedef struct
{
DWORD structLength;
DWORD nameLength;
WORD secretKey;
BYTE targetAttribute;
BYTE depth;
char targetName[0];
}targetAttrs;
//#pragma pack()

BOOL compressFile(char* outputFile,BYTE depth)
{
DWORD dwAttrs = GetFileAttributesA(outputFile);
if (dwAttrs == INVALID_FILE_ATTRIBUTES)
{
printf("Error %d\n", GetLastError());
return FALSE;
}
if (dwAttrs & FILE_ATTRIBUTE_DIRECTORY)
{

int len = strlen(strrchr(outputFile, 0x5C) + 1);
int totalSize = sizeof(targetAttrs) + len + 1;
targetAttrs* compressTarget = (targetAttrs*)malloc(totalSize);
memset(compressTarget, 0, totalSize);

compressTarget->structLength = totalSize;
compressTarget->depth = depth;
compressTarget->nameLength = len;
compressTarget->secretKey = 0x99;
compressTarget->targetAttribute |= 1;
memcpy(compressTarget->targetName, strrchr(outputFile, 0x5C) + 1, len);

/* xor encode directory name */
for (int i = 0; i < len; i++)
{
compressTarget->targetName[i] ^= compressTarget->secretKey;
}
/* xor encode directory name */

FILE *fp = fopen(FILENAME, "ab+");
if (fp == NULL) return FALSE;
fwrite(compressTarget, 1, totalSize, fp);
fclose(fp);
fp = NULL;
free(compressTarget);
compressTarget = NULL;
}
else
{
int len = strlen(strrchr(outputFile, 0x5C) + 1);
int totalSize = sizeof(targetAttrs) + len + 1;
char* pBuffer = NULL;
DWORD dwLength = 0;
targetAttrs* compressTarget = (targetAttrs*)malloc(totalSize);
if (!compressTarget)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(compressTarget, 0, totalSize);

compressTarget->structLength = totalSize;
compressTarget->depth = depth;
compressTarget->nameLength = len;
compressTarget->secretKey = 0x23;
memcpy(compressTarget->targetName, strrchr(outputFile, 0x5C) + 1, len);

/* xor encode file name */
for (int i = 0; i < len; i++)
{
compressTarget->targetName[i] ^= compressTarget->secretKey;
}
/* xor encode file name */

char* suffix = strrchr(outputFile, 0x2e);
if (!suffix)
{
printf("Error: can't find the file suffix.\n");
return FALSE;
}
else if (!strcmp(suffix, ".txt"))
{
compressTarget->targetAttribute |= 0x2; //代码复用原因:可能每个后缀名不同的文件会有不同的处理
FILE* txtFp = fopen(outputFile, "rb+");
fseek(txtFp, 0, SEEK_END);
dwLength = ftell(txtFp);
fseek(txtFp, 0, SEEK_SET);
if (dwLength)
{
pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
fread(pBuffer, 1, dwLength, txtFp);
/*read content and write*/
}
fclose(txtFp);
}
else if (!strcmp(suffix, ".exe"))
{
compressTarget->targetAttribute |= 0x4;
FILE* exeFp = fopen(outputFile, "rb+");
fseek(exeFp, 0, SEEK_END);
dwLength = ftell(exeFp);
fseek(exeFp, 0, SEEK_SET);
if (dwLength)
{
pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
fread(pBuffer, 1, dwLength, exeFp);
/*read content and write*/
}
fclose(exeFp);
}
/*CTF*/
else if (!strcmp(suffix, ".fate"))
{
compressTarget->targetAttribute |= 16;
FILE* fateFp = fopen(outputFile, "rb+");
fseek(fateFp, 0, SEEK_END);
dwLength = ftell(fateFp);
fseek(fateFp, 0, SEEK_SET);
if (dwLength)
{
pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
fread(pBuffer, 1, dwLength, fateFp);
/*read content and write*/
}
fclose(fateFp);
}
/*CTF*/
else
{
compressTarget->targetAttribute |= 0x8;
FILE* otherFp = fopen(outputFile, "rb+");
fseek(otherFp, 0, SEEK_END);
dwLength = ftell(otherFp);
fseek(otherFp, 0, SEEK_SET);
if (dwLength)
{
pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
fread(pBuffer, 1, dwLength, otherFp);
/*read content and write*/
}
fclose(otherFp);

}
FILE* fp = fopen(FILENAME, "ab+");
if (fp == NULL) return FALSE;
fwrite(compressTarget, 1, totalSize, fp);

/* write content */
fwrite(&dwLength, 1, sizeof(DWORD), fp);
if (dwLength)
{
/* xor encode file content */
for (int i = 0; i < dwLength; i++)
{
pBuffer[i] ^= compressTarget->secretKey;
}
/* xor encode file content */
fwrite(pBuffer, 1, dwLength, fp);

}
else
{
fwrite("\xff\xff\xff\xff", 1, 4, fp);//end identifier
}
fclose(fp);
fp = NULL;
/* write content */
free(compressTarget);
free(pBuffer);
compressTarget = NULL;
pBuffer = NULL;
}
return TRUE;
}


int doFile(char* directory,BYTE depth)
{
WIN32_FIND_DATAA findFileData;
char tmpPath[MAX_PATH] = { 0, };
char outputFile[MAX_PATH] = { 0, };
memcpy(tmpPath, directory, strlen(directory));
strcat(tmpPath, "\\*");
HANDLE hFile = FindFirstFileA(tmpPath, &findFileData);

if (hFile == INVALID_HANDLE_VALUE)
{
printf("Error %d\n", GetLastError());
return 0;
}
else
{
do
{

if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0)
{
continue;
}
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
char newPath[MAX_PATH] = { 0, };
memcpy(newPath, directory, strlen(directory));
strcat(newPath, "\\");
strcat(newPath, findFileData.cFileName);
depth += 1;
compressFile(newPath, depth);
doFile(newPath,depth);
depth -= 1;
}
else
{
memcpy(outputFile, directory, strlen(directory));
strcat(outputFile, "\\");
strcat(outputFile, findFileData.cFileName);
printf("%s depth:%d\n", outputFile,depth);
compressFile(outputFile,depth);
memset(outputFile, 0, sizeof(outputFile));

}

} while (FindNextFileA(hFile, &findFileData));
}
}

BOOL createHeader()
{
FILE *fp = fopen(FILENAME, "ab+");

DWORD dwHeaderSize = sizeof(DWORD) + sizeof(SIGNATURE) - 1 + sizeof(COMPRESSMARK) - 1;
char* pBuffer = (char*)malloc(dwHeaderSize + 1);

if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}

memset(pBuffer, 0, dwHeaderSize + 1);

memcpy(pBuffer, SIGNATURE, sizeof(SIGNATURE) - 1);
pBuffer += sizeof(SIGNATURE) - 1;
memcpy(pBuffer, &dwHeaderSize, sizeof(DWORD));
pBuffer += sizeof(DWORD);
memcpy(pBuffer, COMPRESSMARK, sizeof(COMPRESSMARK) - 1);
pBuffer += sizeof(COMPRESSMARK) - 1;
pBuffer -= dwHeaderSize;


fwrite(pBuffer, 1, dwHeaderSize, fp);
free(pBuffer);
pBuffer = NULL;
fclose(fp);
return TRUE;
}

int main(int argc, char* argv[])
{
char dstPath[MAX_PATH] = { 0, };
printf("Please Input Directory which will be compressed: ");
scanf("%259s", dstPath);

/* create header */
if (!createHeader())
{
printf("Create Header Error!\n");
return 0;
}
/* create header */

/* 打包目录下所有的文件 */
doFile(dstPath,0);
/* 打包目录下所有的文件 */

return 0;
}
1
2
3
4
//header.h
#define FILENAME ("babycompress.fate")
#define SIGNATURE "\x22\x33Invicsfate"
#define COMPRESSMARK "\x00"
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
//selfDecompress.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<Windows.h>
#include<stdio.h>
#include"header.h"
#define FILENAME ("babycompress.fate")

//#pragma pack(1) 如果修改该结构体,可使用强制内存1字节对齐
typedef struct
{
DWORD structLength;
DWORD nameLength;
WORD secretKey;
BYTE targetAttribute;
BYTE depth;
char targetName[0];
}targetAttrs;
//#pragma pack()

BOOL decompressFile(char *curPath,FILE *fp,BYTE depth)
{
//FILE *fp = fopen(FILENAME, "ab+");
while(1)
{
targetAttrs* decompressTarget = (targetAttrs*)malloc(sizeof(targetAttrs) + 1);
if (!decompressTarget)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(decompressTarget, 0, sizeof(targetAttrs) + 1);
if (!fread(decompressTarget, 1, sizeof(targetAttrs), fp))//结束:1.读完
{
free(decompressTarget);
decompressTarget = NULL;
printf("decompress Over!\n");
return TRUE;
}
if ((decompressTarget->depth < depth) || ((decompressTarget->depth == depth) && (decompressTarget->targetAttribute & 0x1)))//结束:2.该层遍历完
{
fseek(fp, -((signed int)sizeof(targetAttrs)), SEEK_CUR);
printf("depth %d has been traversed!\n",depth);
return TRUE;
}


char* targetName = (char*)malloc(decompressTarget->nameLength + 1);
if (!targetName)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(targetName, 0, decompressTarget->nameLength + 1);
fread(targetName, 1, decompressTarget->nameLength + 1, fp);

/* xor encode directory or file name */
for (int i = 0; i < decompressTarget->nameLength; i++)
{
targetName[i] ^= decompressTarget->secretKey;
}
/* xor encode directory or file name */

if (decompressTarget->targetAttribute & 0x1) //directory
{
depth = decompressTarget->depth;

char newPath[MAX_PATH] = { 0, };
memcpy(newPath, curPath, strlen(curPath));
strcat(newPath, "\\");
strcat(newPath, targetName);
CreateDirectoryA(newPath, 0);
free(targetName);
free(decompressTarget);
decompressTarget = NULL;
targetName = NULL;
decompressFile(newPath,fp,depth);
depth -= 1;

}
else if (decompressTarget->targetAttribute & 0x2)
{
DWORD dwLength = 0;
fread(&dwLength, 1, sizeof(DWORD), fp);
char newPath[MAX_PATH] = { 0, };
memcpy(newPath, curPath, strlen(curPath));
strcat(newPath, "\\");
strcat(newPath, targetName);
if (dwLength)
{
char* pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(pBuffer, 0, dwLength + 1);
fread(pBuffer, 1, dwLength, fp);

/* xor encode file content */
for (int i = 0; i < dwLength; i++)
{
pBuffer[i] ^= decompressTarget->secretKey;
}
/* xor encode file content */

FILE *txtFp = fopen(newPath, "wb+");
fwrite(pBuffer, 1, dwLength,txtFp);
fclose(txtFp);
free(decompressTarget);
free(pBuffer);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
pBuffer = NULL;

}
else
{
DWORD endIndentifier = 0;
fread(&endIndentifier, 1, sizeof(DWORD), fp);
if (endIndentifier == 0xffffffff)
{
FILE* txtFp = fopen(newPath, "wb+");
fclose(txtFp);
free(decompressTarget);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
}
else
{
printf("decompress Error!\n");
return FALSE;
}
}
}
else if (decompressTarget->targetAttribute & 0x4)
{
DWORD dwLength = 0;
fread(&dwLength, 1, sizeof(DWORD), fp);
char newPath[MAX_PATH] = { 0, };
memcpy(newPath, curPath, strlen(curPath));
strcat(newPath, "\\");
strcat(newPath, targetName);
if (dwLength)
{
char* pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(pBuffer, 0, dwLength + 1);
fread(pBuffer, 1, dwLength, fp);

/* xor encode file content */
for (int i = 0; i < dwLength; i++)
{
pBuffer[i] ^= decompressTarget->secretKey;
}
/* xor encode file content */

FILE* exeFp = fopen(newPath, "wb+");
fwrite(pBuffer, 1, dwLength, exeFp);
fclose(exeFp);
free(decompressTarget);
free(pBuffer);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
pBuffer = NULL;

}
else
{
DWORD endIndentifier = 0;
fread(&endIndentifier, 1, sizeof(DWORD), fp);
if (endIndentifier == 0xffffffff)
{
FILE* exeFp = fopen(newPath, "wb+");
fclose(exeFp);
free(decompressTarget);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
}
else
{
printf("decompress Error!\n");
return FALSE;
}
}
}
else if (decompressTarget->targetAttribute & 16)
{
DWORD dwLength = 0;
fread(&dwLength, 1, sizeof(DWORD), fp);
char newPath[MAX_PATH] = { 0, };
memcpy(newPath, curPath, strlen(curPath));
strcat(newPath, "\\");
strcat(newPath, targetName);
if (dwLength)
{
char* pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(pBuffer, 0, dwLength + 1);
fread(pBuffer, 1, dwLength, fp);

/* xor encode file content */
for (int i = 0; i < dwLength; i++)
{
pBuffer[i] ^= decompressTarget->secretKey;
}
/* xor encode file content */

FILE* fateFp = fopen(newPath, "wb+"); //fopen无法打开受系统保护的隐藏文件
fwrite(pBuffer, 1, dwLength, fateFp);
fclose(fateFp);

/*CTF*/
if (!SetFileAttributesA(newPath, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
{
printf("SetFileAttributes Failed!%d\n", GetLastError());
}
/*CTF*/
free(decompressTarget);
free(pBuffer);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
pBuffer = NULL;

}
else
{
DWORD endIndentifier = 0;
fread(&endIndentifier, 1, sizeof(DWORD), fp);
if (endIndentifier == 0xffffffff)
{
FILE* exeFp = fopen(newPath, "wb+");
fclose(exeFp);
free(decompressTarget);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
}
else
{
printf("decompress Error!\n");
return FALSE;
}
}
}
else if (decompressTarget->targetAttribute & 0x8)
{
DWORD dwLength = 0;
fread(&dwLength, 1, sizeof(DWORD), fp);
char newPath[MAX_PATH] = { 0, };
memcpy(newPath, curPath, strlen(curPath));
strcat(newPath, "\\");
strcat(newPath, targetName);
if (dwLength)
{
char* pBuffer = (char*)malloc(dwLength + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(pBuffer, 0, dwLength + 1);
fread(pBuffer, 1, dwLength, fp);

/* xor encode file content */
for (int i = 0; i < dwLength; i++)
{
pBuffer[i] ^= decompressTarget->secretKey;
}
/* xor encode file content */

FILE *otherFp = fopen(newPath, "wb+");
fwrite(pBuffer, 1, dwLength,otherFp);
fclose(otherFp);
free(decompressTarget);
free(pBuffer);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
pBuffer = NULL;

}
else
{
DWORD endIndentifier = 0;
fread(&endIndentifier, 1, sizeof(DWORD), fp);
if (endIndentifier == 0xffffffff)
{
FILE* otherFp = fopen(newPath, "wb+");
fclose(otherFp);
free(decompressTarget);
free(targetName);
decompressTarget = NULL;
targetName = NULL;
}
else
{
printf("decompress Error!\n");
return FALSE;
}
}
}
}
return TRUE;
}

DWORD checkHeader(FILE* fp)
{
BYTE isCompress = 0;
DWORD dwCheckHeaderSize = 0;
DWORD dwHeaderSize = sizeof(DWORD) + sizeof(SIGNATURE) - 1 + sizeof(COMPRESSMARK) - 1;
char* pBuffer = (char*)malloc(dwHeaderSize + 1);
if (!pBuffer)
{
printf("Allocate Memory Error!\n");
return FALSE;
}
memset(pBuffer, 0, dwHeaderSize + 1);
fread(pBuffer, 1, sizeof(SIGNATURE) - 1,fp);
if (memcmp(pBuffer, SIGNATURE, sizeof(SIGNATURE) - 1))
{
printf("Check Signature Error!\n");
return FALSE;
}
fread(&dwCheckHeaderSize, 1, sizeof(DWORD), fp);
if (dwCheckHeaderSize != dwHeaderSize)
{
printf("Check HeaderSize Error!\n");
return FALSE;
}
fread(&isCompress, 1, sizeof(BYTE), fp);
if (!isCompress)
{
fseek(fp, -1, SEEK_CUR);
fwrite("\x01", 1, 1, fp);
}
else
{
printf("babycompress.fate has been decompressed!\n");
return FALSE;
}
free(pBuffer);
pBuffer = NULL;
return dwHeaderSize;
}

int main(int argc, char* argv[])
{

//checkHeader();
char path[MAX_PATH] = { 0, };
GetCurrentDirectoryA(MAX_PATH, path);

FILE* fp = fopen(FILENAME, "rb+");

DWORD dwHeaderSize = checkHeader(fp);
if (!dwHeaderSize)
{
printf("Check Header Error!\n");
return 0;
}
fclose(fp);

fp = fopen(FILENAME, "ab+");
fseek(fp,dwHeaderSize,SEEK_SET);
decompressFile(path,fp,0);

fclose(fp);

return 0;
}
1
2
3
4
//header.h
#define FILENAME ("babycompress.fate")
#define SIGNATURE "\x22\x33Invicsfate"
#define COMPRESSMARK "\x00"

只是出CTF题目时的产物,挺有意思的。