-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompressor.c
233 lines (178 loc) · 5.56 KB
/
Compressor.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
233
#include <Windows.h>
#include <stdio.h>
#include "ntddk.h"
/*
Native file compressor using only Native Apis @ NTDLL.dll
*/
/*//////////////////////////////////////
/
/ Function: CompressBuffer
/
/ Purpose:
/ Compress buffer given some data
/
*////////////////////////////////////////
#define STATUS_UNSUPPORTED_COMPRESSION ((NTSTATUS)0xC000025FUL)
#define STATUS_SUCCESS ((NTSTATUS)0x00000000UL)
#define NtCurrentProcess() ( (HANDLE)(LONG_PTR) -1 )
#define SIZE_NUMB 16
unsigned char *CompressBuffer(unsigned char *Buf, size_t sizeofbuffer, unsigned long *outsize)
{
NTSTATUS st = 0;
unsigned char *container = NULL;
unsigned char *mem = NULL;
unsigned long size1 = 0, size2 = 0;
size_t Size = sizeofbuffer * SIZE_NUMB;
// Get Compression Size
st = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, &size1, &size2);
if (st == STATUS_UNSUPPORTED_COMPRESSION || st == STATUS_INVALID_PARAMETER)
return NULL;
// Allocate both buffers
container = (unsigned char*)RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, (unsigned long)Size);
mem = (unsigned char*)RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, (unsigned long)size1);
if (container == 0 || mem == 0)
return NULL;
// Compress Buffer
st = RtlCompressBuffer(
COMPRESSION_FORMAT_LZNT1 |
COMPRESSION_ENGINE_MAXIMUM,
Buf,
sizeofbuffer,
container,
Size,
0x1000,
outsize,
mem);
if (NT_SUCCESS(st))
{
free(mem);
}
return container; //return de compressed data
}
int wmain(int argc, wchar_t *argv[])
{
OBJECT_ATTRIBUTES Obja1, Obja2;
UNICODE_STRING sourcefile, destfile;
HANDLE openhandle, writehandle;
IO_STATUS_BLOCK io, io2;
LARGE_INTEGER large, large2;
NTSTATUS st;
FILE_STANDARD_INFORMATION fileinfo = { 0 };
unsigned long outsize = 0;
unsigned char *kmalloc = 0;
RtlSecureZeroMemory(&sourcefile, sizeof(sourcefile));
RtlSecureZeroMemory(&destfile, sizeof(destfile));
RtlInitUnicodeString(&sourcefile, (wchar_t*)argv[1]);
if (RtlDosPathNameToNtPathName_U(argv[1], &sourcefile, NULL, 0) == NULL)
{
wprintf(L"[!] Error converting string from Dos to NT: %d", RtlGetLastWin32Error());
return -1;
}
InitializeObjectAttributes(&Obja1, &sourcefile, OBJ_CASE_INSENSITIVE, 0, 0);
large.QuadPart = 2048;
// Open File with read parameters
st = NtCreateFile(&openhandle, FILE_GENERIC_READ, &Obja1, &io, &large, FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ, FILE_OPEN,
FILE_NON_DIRECTORY_FILE |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (NT_SUCCESS(st))
{
wprintf(L"[+] File -> %ws Opened successfully\r\n", sourcefile.Buffer);
wprintf(L"[+] Getting file size...\r\n");
st = NtQueryInformationFile(openhandle, &io, &fileinfo,
sizeof(FILE_STANDARD_INFORMATION),
FileStandardInformation); // GetFileSize @ win32 api
if (NT_SUCCESS(st))
{
wprintf(L"[+] File size: %ld bytes\r\n", fileinfo.EndOfFile.QuadPart);
// Allocate memory
st = NtAllocateVirtualMemory(NtCurrentProcess(),
(void**)&kmalloc,
0,
(unsigned long*)&fileinfo.EndOfFile.QuadPart,
MEM_COMMIT |
MEM_RESERVE,
PAGE_READWRITE);
if (NT_SUCCESS(st))
{
wprintf(L"[+] Memory allocated !\r\n");
wprintf(L"[+] Reading file...\r\n");
// Read file
st = NtReadFile(openhandle,
NULL,
NULL,
NULL,
&io,
kmalloc,
fileinfo.EndOfFile.QuadPart,
NULL,
0);
if (NT_SUCCESS(st))
{
wprintf(L"[+] File successfully read !\r\n");
NtClose(openhandle);
wprintf(L"[+] Compressing data...\r\n");
// Compress data function
unsigned char *GetBuffer = CompressBuffer(kmalloc, (size_t)fileinfo.EndOfFile.QuadPart, &outsize);
if (GetBuffer != NULL)
{
wprintf(L"[+] Data successfully compressed !\r\n");
RtlInitUnicodeString(&destfile, (wchar_t*)argv[2]);
// Again convert the second parameter to NT Format \\??\\
if (RtlDosPathNameToNtPathName_U(argv[2], &destfile, NULL, 0) == NULL)
{
wprintf(L"[!] Error converting string from Dos to NT: %d", RtlGetLastWin32Error());
return -1;
}
InitializeObjectAttributes(&Obja2, &destfile, OBJ_CASE_INSENSITIVE, NULL, NULL);
large2.QuadPart = 2048;
// Create file with right parameters
st = NtCreateFile(&writehandle,
FILE_GENERIC_WRITE,
&Obja2,
&io2,
&large2,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_WRITE,
FILE_CREATE,
FILE_NON_DIRECTORY_FILE |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (NT_SUCCESS(st))
{
wprintf(L"[+] File -> %ws Created successfully\r\n", destfile.Buffer);
wprintf(L"[+] Writing data to file...\r\n");
// Write compressed data to new file
st = NtWriteFile(
writehandle,
NULL,
NULL,
NULL,
&io2,
GetBuffer,
outsize,
NULL,
0);
if (NT_SUCCESS(st))
{
wprintf(L"[+] File filled with data !\r\n");
unsigned long prebytes = (unsigned long)fileinfo.EndOfFile.QuadPart / 1024;
unsigned long finbytes = outsize / 1024;
wprintf(L"[+] Previous file size: %ld KiloBytes\r\n", prebytes);
wprintf(L"[+] Final size: %ld KiloBytes\r\n", finbytes);
unsigned long dif = prebytes - finbytes;
wprintf(L"[+] Total bytes compressed: %ld KiloBytes\r\n", dif);
NtClose(writehandle);
}
}
}
}
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}