forked from ANSSI-FR/ORADAD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
302 lines (258 loc) · 7.69 KB
/
Main.cpp
File metadata and controls
302 lines (258 loc) · 7.69 KB
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
#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")
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;
#ifdef TRACELOGGING
TraceLoggingRegister(g_hOradadLoggingProvider);
#endif
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);
}
}
Process(&g_GlobalConfig);
//
// Release
//
End:
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_INFORMATION,
"[.] %sEnd.%s", COLOR_CYAN, 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();
}
_SafeCOMRelease(pXMLDocConfig);
_SafeCOMRelease(pXMLDocSchema);
CoUninitialize();
_SafeHeapRelease(szConfigPath);
_SafeHeapRelease(szSchemaPath);
_SafeHeapRelease(g_GlobalConfig.szOutDirectory);
HeapDestroy(g_hHeap);
#ifdef TRACELOGGING
TraceLoggingUnregister(g_hOradadLoggingProvider);
#endif
return EXIT_SUCCESS;
}