-
Notifications
You must be signed in to change notification settings - Fork 29
/
MLA.cpp
514 lines (452 loc) · 12.2 KB
/
MLA.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
#include <Windows.h>
#include <intsafe.h>
#include "ORADAD.h"
#include "resource.h"
#ifdef _WIN64
#define MLA_ARCH "x86_64"
#else
#define MLA_ARCH "i686"
#endif
#ifdef _DEBUG
#define MLA_RELEASE "-debug"
#pragma comment(lib, "Ws2_32.lib") // Required by the Rust runtime in debug mode
#pragma comment(lib, "userenv.lib") // Required by the Rust runtime in debug mode
#else
#define MLA_RELEASE ""
#endif
#pragma comment(lib, "mla\\libmla-windows-" MLA_ARCH MLA_RELEASE "\\mla.lib")
#pragma comment(lib, "bcrypt.lib")
// Compression level 5 found to be most efficient for ORADAD's CSV files and execution time constraints
//const uint32_t ulGlobalCompressionLevel = 5;
HANDLE g_hMlaOutputFile;
MLAArchiveHandle g_hMlaArchive = NULL;
extern HANDLE g_hHeap;
extern BOOL g_bSupportsAnsi;
extern GLOBAL_CONFIG g_GlobalConfig;
#define READ_BUFFER_SIZE 1024 * 1024
//
// Mla callback functions
//
static
int32_t
mla_callback_write(
const uint8_t* pBuffer,
uint32_t length,
void* context,
uint32_t* pBytesWritten
);
static
int32_t
mla_callback_flush(
void* context
);
//
// MLA Public functions
//
BOOL
MlaInit (
_In_z_ LPCWSTR szMlaFilePath
)
{
MLAStatus mlaStatus;
MLAConfigHandle hMlaConfig = NULL;
//
// Open MLA file
//
g_hMlaOutputFile = CreateFile(szMlaFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (g_hMlaOutputFile == INVALID_HANDLE_VALUE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to open MLA file%s '%S' (error %u).",
COLOR_RED, COLOR_RESET, szMlaFilePath, GetLastError()
);
return FALSE;
}
//
// Init MLA
//
mlaStatus = mla_config_default_new(&hMlaConfig);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA config creation failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
return FALSE;
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_config_default_new",
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(hMlaConfig, "hMlaConfig")
);
#endif
//
// Add keys (integrated and from XML config file)
//
if (g_GlobalConfig.bBypassIntegratedMLAKey == FALSE)
{
HMODULE hCurrentProcess;
HRSRC hrMLAKey;
hCurrentProcess = GetModuleHandle(NULL);
hrMLAKey = FindResource(hCurrentProcess, MAKEINTRESOURCE(IDR_MLAKEY), TEXT("KEY"));
if (hrMLAKey != NULL)
{
HGLOBAL hResource;
hResource = LoadResource(hCurrentProcess, hrMLAKey);
if (hResource != NULL)
{
PBYTE pMLAKey;
pMLAKey = (PBYTE)LockResource(hResource);
mlaStatus = mla_config_add_public_keys(hMlaConfig, (char *)pMLAKey);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA integrated public key set failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
return FALSE;
}
}
}
}
if (g_GlobalConfig.szAdditionalMlaKeys != NULL)
{
LPSTR szAdditionalMlaKeysA;
szAdditionalMlaKeysA = LPWSTRtoUTF8(g_GlobalConfig.szAdditionalMlaKeys);
if (szAdditionalMlaKeysA == NULL)
{
return FALSE;
}
else
{
mlaStatus = mla_config_add_public_keys(hMlaConfig, szAdditionalMlaKeysA);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA config public key set failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
return FALSE;
}
_SafeHeapRelease(szAdditionalMlaKeysA);
}
}
/*
//
// Compression Level
//
mlaStatus = mla_config_set_compression_level(hMlaConfig, ulGlobalCompressionLevel);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA compression level set failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
return FALSE;
}
*/
//
// Create file
//
mlaStatus = mla_archive_new(&hMlaConfig, &mla_callback_write, &mla_callback_flush, g_hMlaOutputFile, &g_hMlaArchive);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA archive creation failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
return FALSE;
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_archive_new",
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(g_hMlaArchive, "hMlaArchive")
);
#endif
return TRUE;
}
BOOL
MlaAddFile (
_In_z_ LPCWSTR szFilePath,
_Out_ MLAArchiveFileHandle *phMlaFile
)
{
BOOL bReturn = TRUE;
MLAStatus mlaStatus;
#ifdef TRACELOGGING
UINT Level = WINEVENT_LEVEL_INFO;
#endif
LPSTR szFilenameA;
szFilenameA = LPWSTRtoUTF8(szFilePath);
if (szFilenameA == NULL)
{
*phMlaFile = NULL;
return FALSE;
}
mlaStatus = mla_archive_file_new(g_hMlaArchive, szFilenameA, phMlaFile);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA file creation failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
bReturn = FALSE;
#ifdef TRACELOGGING
Level = WINEVENT_LEVEL_ERROR;
#endif
*phMlaFile = NULL;
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_archive_file_new",
TraceLoggingLevel(Level),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(g_hMlaArchive, "hMlaArchive"),
TraceLoggingPointer(phMlaFile, "hMlaFile"),
TraceLoggingString(szFilenameA, "szFilename")
);
#endif
_SafeHeapRelease(szFilenameA);
return bReturn;
}
BOOL
MlaAddFileFromFile (
_In_z_ LPCWSTR szFilePathToAdd,
_In_z_ LPCWSTR szMLAFilePath
)
{
BOOL bResult;
BOOL bReturn = FALSE;
MLAArchiveFileHandle hMlaFile;
HANDLE hFileToAdd;
LARGE_INTEGER liFileSize;
LARGE_INTEGER liFilePos;
PBYTE pReadBuffer = NULL;
bResult = MlaAddFile(szMLAFilePath, &hMlaFile);
if (bResult == FALSE)
return FALSE;
hFileToAdd = CreateFile(szFilePathToAdd, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFileToAdd == INVALID_HANDLE_VALUE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to open file%s '%S' (error %u).",
COLOR_RED, COLOR_RESET, szFilePathToAdd, GetLastError()
);
goto End;
}
bResult = GetFileSizeEx(hFileToAdd, &liFileSize);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sCannot get file size%s (error %u).",
COLOR_RED, COLOR_RESET, GetLastError()
);
goto End;
}
pReadBuffer = (PBYTE)_HeapAlloc(READ_BUFFER_SIZE);
if (pReadBuffer == NULL)
goto End;
liFilePos.QuadPart = 0;
while (liFilePos.QuadPart < liFileSize.QuadPart)
{
DWORD dwReadLength = READ_BUFFER_SIZE;
bResult = ReadFile(hFileToAdd, 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;
bResult = MlaBufferWrite(hMlaFile, pReadBuffer, dwReadLength);
goto End;
}
bReturn = TRUE;
End:
if (hFileToAdd != INVALID_HANDLE_VALUE)
CloseHandle(hFileToAdd);
_SafeHeapRelease(pReadBuffer);
MlaCloseFile(&hMlaFile);
return bReturn;
}
BOOL
MlaCloseFile (
_Inout_ MLAArchiveFileHandle *phMlaFile
)
{
BOOL bReturn = TRUE;
#ifdef TRACELOGGING
UINT Level = WINEVENT_LEVEL_INFO;
#endif
MLAStatus mlaStatus;
mlaStatus = mla_archive_file_close(g_hMlaArchive, phMlaFile);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to close MLA file%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
bReturn = FALSE;
#ifdef TRACELOGGING
Level = WINEVENT_LEVEL_ERROR;
#endif
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_archive_file_close",
TraceLoggingLevel(Level),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(g_hMlaArchive, "hMlaArchive"),
TraceLoggingPointer(phMlaFile, "hMlaFile")
);
#endif
return bReturn;
}
BOOL
MlaBufferWrite (
_In_ MLAArchiveFileHandle hMlaFile,
_In_reads_(BufferSize) PBYTE pbBuffer,
_In_ uint64_t BufferSize
)
{
BOOL bReturn = TRUE;
#ifdef TRACELOGGING
UINT Level = WINEVENT_LEVEL_INFO;
#endif
MLAStatus mlaStatus;
mlaStatus = mla_archive_file_append(g_hMlaArchive, hMlaFile, pbBuffer, BufferSize);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnable to append MLA file%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
bReturn = FALSE;
#ifdef TRACELOGGING
Level = WINEVENT_LEVEL_ERROR;
#endif
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_archive_file_append",
TraceLoggingLevel(Level),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(g_hMlaArchive, "hMlaArchive"),
TraceLoggingPointer(hMlaFile, "hMlaFile"),
TraceLoggingUInt64(BufferSize, "BufferSize")
);
#endif
return bReturn;
}
BOOL
MlaClose (
)
{
BOOL bReturn = TRUE;
MLAStatus mlaStatus;
#ifdef TRACELOGGING
UINT Level = WINEVENT_LEVEL_INFO;
#endif
if (g_hMlaArchive == NULL)
return TRUE;
mlaStatus = mla_archive_close(&g_hMlaArchive);
if (mlaStatus != MLAStatus::MLA_STATUS_SUCCESS)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sMLA archive close failed%s (error %x).",
COLOR_RED, COLOR_RESET, mlaStatus
);
bReturn = FALSE;
#ifdef TRACELOGGING
Level = WINEVENT_LEVEL_ERROR;
#endif
}
#ifdef TRACELOGGING
TraceLoggingWrite(
g_hOradadLoggingProvider,
"mla_archive_close",
TraceLoggingLevel(Level),
TraceLoggingKeyword(ORADAD_PROVIDER_KEYWORD_MLA),
TraceLoggingHexUInt32(mlaStatus, "mlaStatus"),
TraceLoggingPointer(g_hMlaArchive, "hMlaArchive")
);
#endif
CloseHandle(g_hMlaOutputFile);
return bReturn;
}
//
// Mla callback functions
//
static
int32_t
mla_callback_write (
const uint8_t* pBuffer,
uint32_t length,
void* context,
uint32_t* pBytesWritten
)
{
BOOL bResult;
DWORD dwBytesWritten;
bResult = WriteFile((HANDLE)context, pBuffer, length, &dwBytesWritten, NULL);
if (bResult == FALSE)
{
DWORD dwError;
dwError = GetLastError();
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sWriting to MLA archive failed%s (error %u).",
COLOR_RED, COLOR_RESET, dwError
);
return dwError;
}
*pBytesWritten = dwBytesWritten;
return ERROR_SUCCESS;
}
static
int32_t
mla_callback_flush (
void* context
)
{
BOOL bResult;
bResult = FlushFileBuffers((HANDLE)context);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sFlushing MLA archive failed%s (error %u).",
COLOR_RED, COLOR_RESET, GetLastError()
);
return 1;
}
return 0;
}