-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMain.cpp
362 lines (310 loc) · 9.72 KB
/
Main.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
#include <windows.h>
#include <msxml6.h>
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include "ORADAD.h"
#pragma comment(lib, "msxml6.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "rpcrt4.lib")
#pragma comment(lib, "NetApi32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "Version.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "bcrypt.lib") // Required by the Rust runtime
#pragma comment(lib, "ntdll.lib") // Required by the Rust runtime
HANDLE g_hHeap = NULL;
HANDLE g_hStdOut;
HANDLE g_hLogFile = NULL;
GLOBAL_CONFIG g_GlobalConfig = { 0 };
BOOL g_bSupportsAnsi;
#ifdef TRACELOGGING
//{D29B3EEC-52D4-4AA1-8FCD-24E0EFAEB169}
TRACELOGGING_DEFINE_PROVIDER(
g_hOradadLoggingProvider,
"OradadTraceLoggingProvider",
(0xD29B3EEC, 0x52D4, 0x4AA1, 0x8F, 0xCD, 0x24, 0xE0, 0xEF, 0xAE, 0xB1, 0x69)
);
#endif
//__declspec(dllexport)
int
wmain (
int argc,
wchar_t *argv[]
)
{
LPWSTR szResult;
BOOL bResult;
HRESULT hr;
SYSTEMTIME st;
IXMLDOMDocument2 *pXMLDocConfig = NULL;
IXMLDOMDocument2 *pXMLDocSchema = NULL;
WCHAR szVersion[MAX_PATH];
LPWSTR szConfigPath = NULL;
LPWSTR szSchemaPath = NULL;
StartStatus DateStatus;
DWORD dwSize;
HANDLE hProcessToken;
#ifdef TRACELOGGING
TraceLoggingRegister(g_hOradadLoggingProvider);
#endif
//
// Check OS type and token type
//
bResult = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken);
bResult = GetTokenInformation(hProcessToken, TokenElevationType, &g_GlobalConfig.TokenType, sizeof(g_GlobalConfig.TokenType), &dwSize);
if (bResult == FALSE)
{
fwprintf_s(stderr, L"\r\n* Error *\r\n");
fwprintf_s(stderr, L"Unable to get current process token information.\r\n\r\n");
return EXIT_FAILURE;
}
g_GlobalConfig.osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(suppress: 4996) // GetVersionEx is deprecated, we know.
bResult = GetVersionEx((OSVERSIONINFO*)&g_GlobalConfig.osvi);
if ((g_GlobalConfig.TokenType == TokenElevationTypeLimited) && (g_GlobalConfig.osvi.wProductType == VER_NT_DOMAIN_CONTROLLER))
{
fwprintf_s(stderr, L"\r\n* Error *\r\n");
fwprintf_s(stderr, L"You are running ORADAD on a domain controller in a shell with a retricted token (UAC).\r\n");
fwprintf_s(stderr, L"Please open an elevated command prompt and run ORADAD from it.\r\n\r\n");
return EXIT_FAILURE;
}
//
// Check current date and build date
//
DateStatus = GetBuildDateStatus();
if (DateStatus == StartStatus::Unkwnon)
return EXIT_FAILURE;
if (DateStatus == StartStatus::Warning)
{
fwprintf_s(stderr, L"\r\n* Warning *\r\n");
fwprintf_s(stderr, L"Your ORADAD version seems old.\r\n");
fwprintf_s(stderr, L"Check on https://github.com/ANSSI-FR/ORADAD/releases that you have the latest version.\r\n\r\n");
}
else if (DateStatus == StartStatus::Expired)
{
if (cmdOptionExists(argv, argc, L"--force") == FALSE)
{
fwprintf_s(stderr, L"\r\n* Error *\r\n");
fwprintf_s(stderr, L"Your ORADAD version seems very old.\r\n");
fwprintf_s(stderr, L"Check on https://github.com/ANSSI-FR/ORADAD/releases that you have the latest version.\r\n");
fwprintf_s(stderr, L"Specify option '--force' on the commande line to avoid this message.\r\n\r\n");
return EXIT_FAILURE;
}
}
//
// Initialization
//
g_hHeap = HeapCreate(0, 0, 0);
if (g_hHeap == NULL)
{
return EXIT_FAILURE;
}
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
//
// Start logging
//
g_hStdOut = CreateFile(L"CONOUT$", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (g_hStdOut == INVALID_HANDLE_VALUE)
{
return EXIT_FAILURE;
}
// Set console output to 'ISO 8859-1 Latin 1; Western European (ISO)'
SetConsoleOutputCP(28591);
g_bSupportsAnsi = SetConsoleMode(g_hStdOut, ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
SetConsoleTitle(L"ORADAD");
// Get version
bResult = GetFileVersion(szVersion, MAX_PATH);
if (bResult == FALSE)
return EXIT_FAILURE;
if (cmdOptionExists(argv, argc, L"--version"))
{
wprintf_s(L"%s\n", szVersion);
return EXIT_SUCCESS;
}
//
// Get output directory
//
g_GlobalConfig.szOutDirectory = (LPWSTR)_HeapAlloc(MAX_PATH * sizeof(WCHAR));
if (argc < 2)
{
szResult = _wfullpath(g_GlobalConfig.szOutDirectory, L".", MAX_PATH);
}
else
{
LPWSTR szLastArg = argv[argc - 1];
if ((szLastArg[0] == '-') && (szLastArg[1] == '-'))
{
// Last argument is an option
szResult = _wfullpath(g_GlobalConfig.szOutDirectory, L".", MAX_PATH);
}
else
{
szResult = _wfullpath(g_GlobalConfig.szOutDirectory, szLastArg, MAX_PATH);
}
}
if (szResult == NULL)
{
fwprintf_s(stderr, L"[!] Unable to get absolute path. Exit.\n");
return EXIT_FAILURE;
}
//
// Create output folder
//
bResult = CreateDirectory(g_GlobalConfig.szOutDirectory, NULL);
if ((bResult == FALSE) && (GetLastError() != ERROR_ALREADY_EXISTS))
{
fwprintf_s(stderr, L"[!] Unable to open output folder '%s'. Exit.\n", g_GlobalConfig.szLogfilePath);
return EXIT_FAILURE;
}
//
// Open log file
//
_stprintf_s(g_GlobalConfig.szLogfilePath, MAX_PATH, TEXT("%s\\oradad.log"), g_GlobalConfig.szOutDirectory);
g_hLogFile = CreateFile(g_GlobalConfig.szLogfilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
if (g_hLogFile == INVALID_HANDLE_VALUE)
{
fwprintf_s(stderr, L"[!] Unable to open log file '%s'. Exit.\n", g_GlobalConfig.szLogfilePath);
return EXIT_FAILURE;
}
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_INFORMATION,
"[.] %sStarting%s.", COLOR_CYAN, COLOR_RESET
);
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_VERBOSE,
"[.] Output directory is '%S'.", g_GlobalConfig.szOutDirectory
);
//
// Init and read configuration
//
g_GlobalConfig.bDisplayProgress = TRUE;
if (cmdOptionExists(argv, argc, L"-c"))
{
if (GetCmdOption(argv, argc, L"-c", ConfigTypeString, &szConfigPath) == FALSE)
{
szConfigPath = NULL;
}
}
if (szConfigPath == NULL)
{
szConfigPath = (LPWSTR)_HeapAlloc(MAX_PATH * sizeof(WCHAR));
swprintf_s(szConfigPath, MAX_PATH, L"config-oradad.xml");
}
pXMLDocConfig = (IXMLDOMDocument2 *)XmlReadConfigFile(szConfigPath, &g_GlobalConfig);
if (pXMLDocConfig == NULL)
goto End;
//
// Read schema
//
if (cmdOptionExists(argv, argc, L"-s"))
{
if (GetCmdOption(argv, argc, L"-s", ConfigTypeString, &szSchemaPath) == FALSE)
{
szSchemaPath = NULL;
}
}
if (szSchemaPath == NULL)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_INFORMATION,
"[.] Using Resource Schema."
);
}
else
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_INFORMATION,
"[.] Using Alternative Schema '%S'.", szSchemaPath
);
}
pXMLDocSchema = (IXMLDOMDocument2 *)XmlReadSchemaFile(szSchemaPath, &g_GlobalConfig, (PVOID)pXMLDocConfig); // Use NULL for resource
if (pXMLDocSchema == NULL)
goto End;
//
// Main process
//
GetSystemTime(&st);
swprintf_s(
g_GlobalConfig.szSystemTime, 17,
L"%04u%02u%02u-%02u%02u%02u",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond
);
if (cmdOptionExists(argv, argc, L"-name"))
{
LPWSTR szInstanceName = NULL;
if (GetCmdOption(argv, argc, L"-name", ConfigTypeString, &szInstanceName) != FALSE)
{
wcsncpy_s(g_GlobalConfig.szSystemTime, 17, szInstanceName, 16);
}
}
bResult = Process(&g_GlobalConfig);
if (bResult == FALSE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sAn error has occurred during capture.%s Try to solve problem and try again.",
COLOR_RED, COLOR_RESET
);
}
//
// Release
//
End:
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_INFORMATION,
"[.] %sEnd.%s", COLOR_CYAN, COLOR_RESET
);
if (g_GlobalConfig.bProcessHasError == TRUE)
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sAn error has occurred during capture.%s The dump may be partial. Check log and try again.",
COLOR_YELLOW, COLOR_RESET
);
}
CloseHandle(g_hLogFile);
//
// Move log file to outputs
//
if ((g_GlobalConfig.bOutputMLA == TRUE) && (g_GlobalConfig.szMlaOutDirectory[0]))
{
TCHAR szFinalPath[MAX_PATH];
_stprintf_s(szFinalPath, MAX_PATH, TEXT("%s\\oradad.log"), g_GlobalConfig.szMlaOutDirectory);
MlaAddFileFromFile(g_GlobalConfig.szLogfilePath, szFinalPath);
}
if ((g_GlobalConfig.bOutputFiles == TRUE) && (g_GlobalConfig.szFileOutDirectory[0]))
{
TCHAR szFinalPath[MAX_PATH];
_stprintf_s(szFinalPath, MAX_PATH, TEXT("%s\\oradad.log"), g_GlobalConfig.szFileOutDirectory);
MoveFile(g_GlobalConfig.szLogfilePath, szFinalPath);
}
//
// Close MLA
//
if (g_GlobalConfig.bOutputMLA == TRUE)
{
MlaClose();
}
// If dump has error, delete MLA file
if (bResult == FALSE)
{
DeleteFile(g_GlobalConfig.szMlaFilePath);
}
_SafeCOMRelease(pXMLDocConfig);
_SafeCOMRelease(pXMLDocSchema);
CoUninitialize();
_SafeHeapRelease(szConfigPath);
_SafeHeapRelease(szSchemaPath);
_SafeHeapRelease(g_GlobalConfig.szOutDirectory);
HeapDestroy(g_hHeap);
#ifdef TRACELOGGING
TraceLoggingUnregister(g_hOradadLoggingProvider);
#endif
if (bResult == FALSE)
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
}