forked from ANSSI-FR/ORADAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.cpp
745 lines (638 loc) · 19.8 KB
/
Buffer.cpp
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <wincrypt.h>
#include <intsafe.h>
#include "ORADAD.h"
extern HANDLE g_hHeap;
extern BOOL g_bSupportsAnsi;
extern GLOBAL_CONFIG g_GlobalConfig;
extern BOOL g_bExtendedTarForAd;
#define READ_BUFFER_SIZE 1024 * 1024
static const LZ4F_preferences_t lz4Prefs =
{
{ LZ4F_max1MB, LZ4F_blockLinked, LZ4F_contentChecksumEnabled, LZ4F_frame, 0, 0, LZ4F_noBlockChecksum },
0, // compressionLevel
1, // autoFlush
0, //favorDecSpeed
{ 0, 0, 0} //Reserved
};
//
// Public functions
//
BOOL
BufferInitialize (
_Out_ PBUFFER_DATA pBuffer,
_In_z_ LPWSTR szFilename,
_In_ BOOL bRawBuffer
)
{
BOOL bResult;
PBUFFER_HEADER pBufferHeader, pBufferHeaderNew;
pBufferHeader = (PBUFFER_HEADER)_HeapAlloc(sizeof(BUFFER_HEADER));
if (pBufferHeader == NULL)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot allocate memory%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
ZeroMemory(pBuffer, sizeof(BUFFER_DATA));
if ((g_GlobalConfig.bCompressionEnabled == TRUE) || (g_GlobalConfig.bEncryptionEnabled == TRUE))
{
// Add suffix for compressed / encrypted files
swprintf_s(pBuffer->szFileName, MAX_PATH, L"%s.oradad", szFilename);
}
else
_tcscpy_s(pBuffer->szFileName, MAX_PATH, szFilename);
pBuffer->hOutputFile = CreateFile(pBuffer->szFileName, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, NULL, 0);
if (pBuffer->hOutputFile == INVALID_HANDLE_VALUE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to open outfile '%S'%s (error %u).", COLOR_RED, pBuffer->szFileName, COLOR_RESET, GetLastError()
);
return FALSE;
}
pBuffer->BufferSize = 1024 * 1024;
pBuffer->pbData = (PBYTE)_HeapAlloc(pBuffer->BufferSize);
pBufferHeader->Magic[0] = 'O';
pBufferHeader->Magic[1] = 'R';
pBufferHeader->Magic[2] = 'A';
pBufferHeader->Magic[3] = 'D';
pBufferHeader->Magic[4] = 'A';
pBufferHeader->Magic[5] = 'D';
pBufferHeader->BufferVersion = BUFFER_VERSION;
//
// Compression
//
if (g_GlobalConfig.bCompressionEnabled)
{
LZ4F_errorCode_t lz4err;
lz4err = LZ4F_createCompressionContext(&(pBuffer->lz4Ctx), LZ4F_VERSION);
if (lz4err != 0)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to create compression context %s (error %llu - %s).", COLOR_RED, COLOR_RESET, lz4err, LZ4F_getErrorName(lz4err)
);
return FALSE;
}
pBuffer->DataCompressSize = LZ4F_compressBound(pBuffer->BufferSize, &lz4Prefs);
// Allocate double to allow compressUpdate and compressEnd to fit in the safe buffer
pBuffer->pbDataCompress = (PBYTE)_HeapAlloc(pBuffer->DataCompressSize * 2);
pBufferHeader->Flags |= BUFFER_COMPRESSED;
}
//
// Encryption
//
if (g_GlobalConfig.bEncryptionEnabled)
{
DWORD dwRsaBufferLen = 0;
PBYTE pbRsaBuffer = NULL;
DWORD dwKeyBlobLen = 0;
PBYTE pbKeyBlob = NULL;
HCRYPTKEY hRsaKey;
DWORD dwKeyExportLen = 0;
PBYTE pbKeyExport = NULL;
DWORD dwSize = sizeof(DWORD);
//
// Make room for encryption
//
bResult = CryptAcquireContext(&(pBuffer->hProv), NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to get Cryptographic Provider%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
//
// Get SHA256 provider
//
bResult = CryptCreateHash(pBuffer->hProv, CALG_SHA_256, 0, 0, &(pBuffer->hHash));
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to get Cryptographic Hash%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
//
// Generate a key
//
bResult = CryptGenKey(pBuffer->hProv, CALG_AES_256, CRYPT_EXPORTABLE, &(pBuffer->hCryptKey));
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to generate Cryptographic Key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
//
// Get Block length
//
bResult = CryptGetKeyParam(pBuffer->hCryptKey, KP_BLOCKLEN, (BYTE *)&(pBuffer->dwEncryptBlockLen), &dwSize, 0);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to get block size%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
//
// Allocate encryption buffer
//
pBuffer->DataEncryptSize = ((pBuffer->BufferSize / pBuffer->dwEncryptBlockLen) + 1) * pBuffer->dwEncryptBlockLen;
pBuffer->pbDataEncrypt = (PBYTE)_HeapAlloc(pBuffer->DataEncryptSize);
//
// Convert then import RSA Public Key
//
bResult = CryptStringToBinary(g_GlobalConfig.szPublicKey, 0, CRYPT_STRING_BASE64HEADER, NULL, &dwRsaBufferLen, NULL, NULL);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to parse RSA public key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
pbRsaBuffer = (PBYTE)_HeapAlloc(dwRsaBufferLen);
bResult = CryptStringToBinary(g_GlobalConfig.szPublicKey, 0, CRYPT_STRING_BASE64HEADER, pbRsaBuffer, &dwRsaBufferLen, NULL, NULL);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to parse RSA public key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
bResult = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, pbRsaBuffer, dwRsaBufferLen, 0, NULL, NULL, &dwKeyBlobLen);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to parse RSA public key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
pbKeyBlob = (PBYTE)_HeapAlloc(dwKeyBlobLen);
bResult = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, pbRsaBuffer, dwRsaBufferLen, 0, NULL, pbKeyBlob, &dwKeyBlobLen);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to parse RSA public key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
bResult = CryptImportKey(pBuffer->hProv, pbKeyBlob, dwKeyBlobLen, NULL, 0, &hRsaKey);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to import RSA public key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
//
// Export AES key
//
bResult = CryptExportKey(pBuffer->hCryptKey, hRsaKey, SIMPLEBLOB, 0, NULL, &dwKeyExportLen);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to export AES key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
pbKeyExport = (PBYTE)_HeapAlloc(dwKeyExportLen);
bResult = CryptExportKey(pBuffer->hCryptKey, hRsaKey, SIMPLEBLOB, 0, pbKeyExport, &dwKeyExportLen);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to export AES key%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
pBufferHeader->Flags |= BUFFER_ENCRYPTED;
pBufferHeader->dwExtraDataLen = dwKeyExportLen;
pBufferHeaderNew = (PBUFFER_HEADER)HeapReAlloc(g_hHeap, HEAP_ZERO_MEMORY, pBufferHeader, sizeof(BUFFER_HEADER) + pBufferHeader->dwExtraDataLen);
if (pBufferHeaderNew == NULL)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot allocate memory%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
pBufferHeader = pBufferHeaderNew;
memcpy_s(pBufferHeader->bExtraData, pBufferHeader->dwExtraDataLen, pbKeyExport, dwKeyExportLen);
CryptDestroyKey(hRsaKey);
_SafeHeapRelease(pbKeyExport);
_SafeHeapRelease(pbKeyBlob);
_SafeHeapRelease(pbRsaBuffer);
}
if (pBufferHeader->Flags != 0)
{
DWORD dwBytesWritten = 0;
bResult = WriteFile(pBuffer->hOutputFile, pBufferHeader, sizeof(BUFFER_HEADER) + pBufferHeader->dwExtraDataLen, &dwBytesWritten, NULL);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot write buffer header%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return FALSE;
}
}
_SafeHeapRelease(pBufferHeader);
// Call initialization functions
if (g_GlobalConfig.bCompressionEnabled)
{
size_t compressSize;
compressSize = LZ4F_compressBegin(pBuffer->lz4Ctx, pBuffer->pbDataCompress, pBuffer->DataCompressSize, &lz4Prefs);
if (compressSize < 0)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to begin compression %s (error %llu).", COLOR_RED, COLOR_RESET, compressSize
);
return FALSE;
}
pBuffer->PositionCompress += compressSize;
}
// FIXME: pertinent?
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
SetFilePointer(pBuffer->hOutputFile, 0, 0, FILE_END);
}
else
{
// Write UTF-16 BOM if buffer is not raw
if (bRawBuffer == FALSE)
{
BYTE pbBomUTF16LE[2] = { 0xFF, 0xFE };
BufferWrite(pBuffer, pbBomUTF16LE, 2);
}
}
return TRUE;
}
BOOL
BufferClose (
_Inout_ PBUFFER_DATA pBuffer
)
{
BOOL bResult;
bResult = BufferSave(pBuffer, TRUE);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to save buffer%s.", COLOR_RED, COLOR_RESET
);
return FALSE;
}
if (pBuffer->ulFileSize > (MAX_OCTAL_SIZE - (1024 * 1024)))
{
g_bExtendedTarForAd = TRUE;
}
_SafeHeapRelease(pBuffer->pbData);
_SafeHeapRelease(pBuffer->pbDataCompress);
_SafeHeapRelease(pBuffer->pbDataEncrypt);
if (pBuffer->lz4Ctx != NULL)
LZ4F_freeCompressionContext(pBuffer->lz4Ctx);
if (pBuffer->hCryptKey != NULL)
CryptDestroyKey(pBuffer->hCryptKey);
if (pBuffer->hHash != NULL)
CryptDestroyHash(pBuffer->hHash);
if (pBuffer->hProv != NULL)
CryptReleaseContext(pBuffer->hProv, 0);
CloseHandle(pBuffer->hOutputFile);
return TRUE;
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_reads_bytes_opt_(dwNumberOfBytesToWrite) LPVOID pvData,
_In_ DWORD dwNumberOfBytesToWrite
)
{
if (pBuffer == NULL)
return 0;
if (pBuffer->pbData == NULL)
return 0;
if (pvData == NULL)
return 0;
if (dwNumberOfBytesToWrite == 0)
return 0;
if (dwNumberOfBytesToWrite >= pBuffer->BufferSize)
{
// Can't write data bigger than buffer size
return 0;
}
else if ((pBuffer->BufferSize - pBuffer->Position) >= dwNumberOfBytesToWrite)
{
// Copy data to buffer
memcpy(pBuffer->pbData + pBuffer->Position, pvData, dwNumberOfBytesToWrite);
pBuffer->Position += dwNumberOfBytesToWrite;
return dwNumberOfBytesToWrite;
}
else
{
BOOL bResult;
// Save buffer
bResult = BufferSave(pBuffer, FALSE);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to save buffer%s.", COLOR_RED, COLOR_RESET
);
return 0;
}
// Copy data to buffer
memcpy(pBuffer->pbData + pBuffer->Position, pvData, dwNumberOfBytesToWrite);
pBuffer->Position += dwNumberOfBytesToWrite;
return dwNumberOfBytesToWrite;
}
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_opt_z_ LPWSTR szString
)
{
size_t StringSize;
if (pBuffer == NULL)
return FALSE;
if (pBuffer->pbData == NULL)
return FALSE;
if (szString == NULL)
return TRUE;
StringSize = wcslen(szString);
RemoveSpecialChars(szString);
if (StringSize == ((size_t)(-1)))
return FALSE;
else if (StringSize == 0)
return TRUE;
else
return BufferWrite(pBuffer, szString, (DWORD)(StringSize * sizeof(WCHAR)));
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_ const FILETIME* fileTime
)
{
SYSTEMTIME st;
WCHAR szDate[20];
FileTimeToSystemTime(fileTime, &st);
swprintf_s(
szDate, 20,
L"%04u-%02u-%02u %02u:%02u:%02u",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond
);
return BufferWrite(pBuffer, szDate);
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_ DWORD dwValue
)
{
wchar_t intTmp[_MAX_ULTOSTR_BASE10_COUNT];
_ultow_s(dwValue, intTmp, _MAX_ULTOSTR_BASE10_COUNT, 10);
return BufferWrite(pBuffer, intTmp);
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_ LONGLONG dwValue
)
{
wchar_t intTmp[_MAX_I64TOSTR_BASE10_COUNT];
_i64tow_s(dwValue, intTmp, _MAX_I64TOSTR_BASE10_COUNT, 10);
return BufferWrite(pBuffer, intTmp);
}
DWORD
BufferWrite (
_In_ PBUFFER_DATA pBuffer,
_In_ unsigned long long dwValue
)
{
wchar_t intTmp[_MAX_U64TOSTR_BASE10_COUNT];
_ui64tow_s(dwValue, intTmp, _MAX_U64TOSTR_BASE10_COUNT, 10);
return BufferWrite(pBuffer, intTmp);
}
DWORD
BufferWriteFromFile (
_In_ PBUFFER_DATA pBuffer,
_In_ HANDLE hFile
)
{
BOOL bResult;
DWORD dwBytesWritten = 0;
LARGE_INTEGER liFileSize;
LARGE_INTEGER liFilePos;
PBYTE pReadBuffer = NULL;
bResult = GetFileSizeEx(hFile, &liFileSize);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot get file size%s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
return 0;
}
pReadBuffer = (PBYTE)_HeapAlloc(READ_BUFFER_SIZE);
if (pReadBuffer == NULL)
return 0;
liFilePos.QuadPart = 0;
while (liFilePos.QuadPart < liFileSize.QuadPart)
{
DWORD dwReadLength = READ_BUFFER_SIZE;
bResult = ReadFile(hFile, pReadBuffer, READ_BUFFER_SIZE, &dwReadLength, NULL);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot read file %s (error %u).", COLOR_RED, COLOR_RESET, GetLastError()
);
break;
}
liFilePos.QuadPart += dwReadLength;
dwBytesWritten = BufferWrite(pBuffer, pReadBuffer, dwReadLength);
}
_SafeHeapRelease(pReadBuffer);
return dwBytesWritten;
}
DWORD
BufferWriteHex (
_Inout_ PBUFFER_DATA pBuffer,
_In_reads_(dwDataSize) PBYTE pbData,
_In_ DWORD dwDataSize
)
{
DWORD dwDataSizeSum = 0;
for (DWORD i = 0; i < dwDataSize; i++)
{
WCHAR szChar[3];
swprintf_s(szChar, 3, L"%02x", pbData[i]);
dwDataSizeSum += BufferWrite(pBuffer, szChar, 4);
}
return dwDataSizeSum;
}
DWORD
BufferWriteLine (
_In_ PBUFFER_DATA pBuffer
)
{
return BufferWrite(pBuffer, (LPVOID)L"\r\n", 2 * sizeof(WCHAR));
}
DWORD
BufferWriteTab (
_In_ PBUFFER_DATA pBuffer
)
{
return BufferWrite(pBuffer, (LPVOID)L"\t", 2);
}
DWORD
BufferWriteSemicolon (
_In_ PBUFFER_DATA pBuffer
)
{
return BufferWrite(pBuffer, (LPVOID)L";", 2);
}
BOOL
BufferSave (
_In_ PBUFFER_DATA pBuffer,
_In_ BOOL bFinal
)
{
BOOL bReturn = FALSE;
BOOL bResult;
DWORD dwBytesWritten;
DWORD dwOutBufferLength;
PBYTE pbOutBuffer;
PSIZE_T pdwOutBufferPosition;
if (pBuffer == NULL)
return FALSE;
if (SIZETToDWord(pBuffer->Position, &dwOutBufferLength) != S_OK)
return FALSE;
pbOutBuffer = pBuffer->pbData;
pdwOutBufferPosition = &(pBuffer->Position);
//
// Compress data
//
if (g_GlobalConfig.bCompressionEnabled)
{
size_t bytesOut;
int iResult;
bytesOut = LZ4F_compressUpdate(
pBuffer->lz4Ctx,
pBuffer->pbDataCompress + pBuffer->PositionCompress, pBuffer->DataCompressSize,
pBuffer->pbData, pBuffer->Position,
NULL
);
iResult = LZ4F_isError(bytesOut);
if (iResult != 0)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot compress data%s (error %llu - %s).", COLOR_RED, COLOR_RESET, bytesOut, LZ4F_getErrorName(bytesOut)
);
return FALSE;
}
pBuffer->PositionCompress += bytesOut;
if (bFinal)
{
bytesOut = LZ4F_compressEnd(
pBuffer->lz4Ctx,
pBuffer->pbDataCompress + pBuffer->PositionCompress, pBuffer->DataCompressSize,
NULL
);
iResult = LZ4F_isError(bytesOut);
if (iResult != 0)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot finalize compression %s (error %llu - %s).", COLOR_RED, COLOR_RESET, bytesOut, LZ4F_getErrorName(bytesOut)
);
return FALSE;
}
pBuffer->PositionCompress += bytesOut;
}
pbOutBuffer = pBuffer->pbDataCompress;
pdwOutBufferPosition = &(pBuffer->PositionCompress);
if (SIZETToDWord(*pdwOutBufferPosition, &dwOutBufferLength) != S_OK)
return FALSE;
// Input buffer has been compressed, reset position
pBuffer->Position = 0;
}
//
// Encrypt data
//
if (g_GlobalConfig.bEncryptionEnabled)
{
DWORD dwBufferSize;
DWORD dwRoundedOutBufferLength;
if (SIZETToDWord(pBuffer->DataEncryptSize, &dwBufferSize) != S_OK)
return FALSE;
dwRoundedOutBufferLength = (dwOutBufferLength / pBuffer->dwEncryptBlockLen) * pBuffer->dwEncryptBlockLen;
// If this is the last block, don't round
if (bFinal == TRUE)
dwRoundedOutBufferLength = dwOutBufferLength;
// Copy required data to encryption buffer
memcpy_s(pBuffer->pbDataEncrypt, dwBufferSize, pbOutBuffer, dwRoundedOutBufferLength);
// Copy remaining data to the beginning of the previous buffer, if not the last block
if (bFinal == FALSE)
{
memcpy_s(pbOutBuffer, pBuffer->BufferSize, pbOutBuffer + dwRoundedOutBufferLength, dwOutBufferLength - dwRoundedOutBufferLength);
*pdwOutBufferPosition = dwOutBufferLength - dwRoundedOutBufferLength;
}
bResult = CryptEncrypt(
pBuffer->hCryptKey,
pBuffer->hHash,
bFinal,
0,
pBuffer->pbDataEncrypt,
&dwRoundedOutBufferLength,
dwBufferSize
);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot encrypt data%s (error %u / %x).", COLOR_RED, COLOR_RESET, GetLastError(), dwOutBufferLength
);
return FALSE;
}
pbOutBuffer = pBuffer->pbDataEncrypt;
dwOutBufferLength = dwRoundedOutBufferLength;
pBuffer->PositionEncrypt = dwOutBufferLength;
pdwOutBufferPosition = &(pBuffer->PositionEncrypt);
}
//
// Write buffer to file
//
bResult = WriteFile(pBuffer->hOutputFile, pbOutBuffer, dwOutBufferLength, &dwBytesWritten, NULL);
if (bResult == TRUE)
{
pBuffer->ulFileSize += dwBytesWritten;
}
bReturn = bResult;
//
// Reset output buffer position
//
if (bResult)
*pdwOutBufferPosition = 0;
return bReturn;
}