-
Notifications
You must be signed in to change notification settings - Fork 5
/
readlog.c
464 lines (389 loc) · 10.7 KB
/
readlog.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
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
#include "mrbig.h"
static char **get_args(EVENTLOGRECORD *pBuf)
{
char *cp;
WORD arg_count;
char **args = NULL;
if (pBuf->NumStrings == 0) return NULL;
args = big_malloc("get_args", sizeof(char *) * pBuf->NumStrings);
cp = (char *)pBuf + (pBuf->StringOffset);
for (arg_count=0; arg_count<pBuf->NumStrings; arg_count++) {
args[arg_count] = cp;
cp += strlen(cp) + 1;
}
return args;
}
static BOOL get_module_from_source(char *log,
char *source_name, char *entry_name,
char *expanded_name)
{
DWORD lResult;
DWORD module_name_size;
char module_name[1024];
HKEY hAppKey = NULL;
HKEY hSourceKey = NULL;
BOOL bReturn = FALSE;
char key[1024];
key[0] = '\0';
snprcat(key, sizeof key,
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s", log);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hAppKey);
if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: registry can not open.");
goto Exit;
}
lResult = RegOpenKeyEx(hAppKey, source_name, 0, KEY_READ, &hSourceKey);
if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: can't RegOpenKeyEx");
goto Exit;
}
module_name_size = sizeof module_name - 1;
lResult = RegQueryValueEx(hSourceKey, "EventMessageFile",
NULL, NULL, (BYTE*)module_name, &module_name_size);
if (lResult == ERROR_FILE_NOT_FOUND) {
// Reg key doesn't exist, let's try the other way!
char guid[1024];
DWORD guid_size;
memset(guid, 0, sizeof guid);
if (debug) mrlog("get_module_from_source: ERROR_FILE_NOT_FOUND. Looking for DLL another way.");
if (debug) mrlog("Looking for ProviderGuid");
lResult = RegQueryValueEx(hSourceKey, "ProviderGuid", NULL, NULL, (BYTE*)guid, &guid_size);
if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: Alternate method failed. Giving up.");
goto Exit;
}
if (debug) mrlog("ProviderGuid: %s", guid);
HKEY hPublisherKey = NULL;
char publisherKey[1024];
snprcat(publisherKey, sizeof publisherKey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WINEVT\\Publishers\\%s", guid);
if (debug) mrlog("PublisherKey: %s", publisherKey);
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, publisherKey, 0, KEY_READ, &hPublisherKey);
if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: Guid missing from registry. Giving up.");
goto Exit;
}
lResult = RegQueryValueEx(hPublisherKey, "MessageFilename", NULL, NULL, (BYTE*)module_name, &module_name_size);
if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: RegQueryvValueEx failed on alternate method key.");
goto Exit;
}
}
else if (lResult != ERROR_SUCCESS) {
if (debug) mrlog("get_module_from_source: can't RegQueryValueEx");
goto Exit;
}
/* RegQueryValueEx doesn't necessarily null-terminate */
module_name[sizeof module_name - 1] = '\0';
ExpandEnvironmentStrings(module_name, expanded_name, sizeof module_name - 1);
bReturn = ERROR_SUCCESS;
Exit:
if (hSourceKey != NULL) RegCloseKey(hSourceKey);
if (hAppKey != NULL) RegCloseKey(hAppKey);
return bReturn;
}
static BOOL disp_message(char *log, char *source_name, char *entry_name,
char **args, DWORD MessageId,
char *msgbuf, size_t msgsize)
{
BOOL bResult;
BOOL bReturn = FALSE;
HANDLE hSourceModule = NULL;
char source_module_name[1000];
char *pMessage = NULL;
source_module_name[0] = '\0';
if (debug) {
mrlog("About to call get_module_from_source(%s, %s, %s, %p)",
log, source_name, entry_name, source_module_name);
}
bResult = get_module_from_source(log,
source_name, entry_name, source_module_name);
if (debug) {
mrlog("get_module_from_source returns %d", bResult);
}
if (bResult != ERROR_SUCCESS) {
if (debug) mrlog("disp_message: get_module_from_source failed");
goto Exit;
}
/* Sometimes source_module_name will come back as a list of libraries,
i.e. this.dll;that.dll;someother.dll. That makes LoadLibraryEx
fail and no messages can be formatted. This ugly hack removes all
but the first dll so at least that one is loaded.
*/
if (1) {
char *p = strchr(source_module_name, ';');
if (p) {
if (debug) {
mrlog("source_module_name (before cutting) = '%s'", source_module_name);
}
*p = '\0';
}
}
if (debug) {
mrlog("source_module_name = '%s'", source_module_name);
}
hSourceModule = LoadLibraryEx(source_module_name, NULL,
DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
if (debug) {
mrlog("LoadLibraryEx returns %d", hSourceModule);
}
if (hSourceModule == NULL) goto Exit;
if (debug) {
mrlog("About to call FormatMessage(%d, %d, %d, %d, %p, %d, %p)",
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
hSourceModule,
MessageId,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&pMessage,
0,
(va_list *)args);
}
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
hSourceModule,
MessageId,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&pMessage,
0,
(va_list *)args);
bReturn = TRUE;
Exit:
msgbuf[0] = '\0';
if (pMessage != NULL) snprcat(msgbuf, msgsize, "%s", pMessage);
else snprcat(msgbuf, msgsize, "(%d)\n", (int)MessageId);
if (hSourceModule != NULL) FreeLibrary(hSourceModule);
if (pMessage != NULL) LocalFree(pMessage);
return bReturn;
}
#if 0 /* Doesn't work with W2K3 or XP SP2 */
struct event *read_log(char *log, int maxage)
{
struct event *e = NULL, *p;
DWORD BufSize;
DWORD ReadBytes;
DWORD NextSize;
BOOL bResult;
char *cp;
char *pSourceName;
char *pComputerName;
HANDLE hEventLog = NULL;
EVENTLOGRECORD *pBuf = NULL;
char **args = NULL;
char msgbuf[1024];
hEventLog = OpenEventLog(NULL, log);
if(hEventLog == NULL) {
printf("event log can not open.\n");
goto Exit;
}
for(;;) {
BufSize = 1;
pBuf = big_malloc("read_log (pBuf)", BufSize);
bResult = ReadEventLog(
hEventLog,
EVENTLOG_BACKWARDS_READ |
EVENTLOG_SEQUENTIAL_READ,
0,
pBuf,
BufSize,
&ReadBytes,
&NextSize);
if (!bResult && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
break;
BufSize = NextSize;
pBuf = big_realloc("read_log (pBuf)", pBuf, BufSize);
bResult = ReadEventLog(
hEventLog,
EVENTLOG_BACKWARDS_READ |
EVENTLOG_SEQUENTIAL_READ,
0,
pBuf,
BufSize,
&ReadBytes,
&NextSize);
if(!bResult) break;
if (pBuf->TimeGenerated < maxage) {
//printf("Too old\n");
goto Next;
}
p = big_malloc("read_log (node)", sizeof *p);
p->next = e;
e = p;
e->record = pBuf->RecordNumber;
e->gtime = pBuf->TimeGenerated;
e->wtime = pBuf->TimeWritten;
e->id = pBuf->EventID;
e->type = pBuf->EventType;
cp = (char *)pBuf;
cp += sizeof(EVENTLOGRECORD);
pSourceName = cp;
cp += strlen(cp)+1;
pComputerName = cp;
cp += strlen(cp)+1;
e->source = big_strdup("read_log (source)", pSourceName);
args = get_args(pBuf);
disp_message(log, pSourceName, "EventMessageFile",
args, pBuf->EventID, msgbuf, sizeof msgbuf);
e->message = big_strdup("read_log (message)", msgbuf);
big_free("read_log (args)", args);
args = NULL;
Next:
big_free("read_log (pBuf)", pBuf);
pBuf = NULL;
}
Exit:
big_free("read_log (pBuf)", pBuf);
big_free("read_log (args)", args);
if (hEventLog) CloseEventLog(hEventLog);
return e;
}
#else /* Should work for any version */
struct event *read_log(char *log, int maxage, int fast)
{
struct event *e = NULL, *p;
DWORD BufSize = 64*1024; /* works for NT4 and up */
DWORD ReadBytes;
DWORD NextSize;
char *cp;
char *pSourceName;
char *pComputerName;
HANDLE hEventLog = NULL;
EVENTLOGRECORD *pBuf, *pBuf0 = NULL;
char **args = NULL;
char msgbuf[1024];
hEventLog = OpenEventLog(NULL, log);
if(hEventLog == NULL) {
mrlog("event log can not open.");
goto Exit;
}
pBuf0 = big_malloc("read_log (pBuf)", BufSize);
pBuf = pBuf0;
memset(pBuf, 0, BufSize);
while (ReadEventLog(hEventLog,
EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ,
0,
pBuf,
BufSize,
&ReadBytes,
&NextSize)) {
if (debug > 1) {
mrlog("ReadEventLog returns ReadBytes = %ld",
ReadBytes);
}
while (ReadBytes > 0) {
if (debug > 2) mrlog("ReadBytes = %ld", ReadBytes);
if (pBuf->TimeGenerated < maxage) {
if (debug > 2) mrlog("Too old");
goto Next;
}
p = big_malloc("read_log (node)", sizeof *p);
p->next = e;
e = p;
e->record = pBuf->RecordNumber;
e->gtime = pBuf->TimeGenerated;
e->wtime = pBuf->TimeWritten;
e->id = pBuf->EventID;
e->type = pBuf->EventType;
cp = (char *)pBuf;
cp += sizeof(EVENTLOGRECORD);
pSourceName = cp;
cp += strlen(cp)+1;
pComputerName = cp;
cp += strlen(cp)+1;
e->source = big_strdup("read_log (source)", pSourceName);
args = get_args(pBuf);
memset(msgbuf, 0, sizeof msgbuf);
disp_message(log, pSourceName, "EventMessageFile",
args, pBuf->EventID, msgbuf, sizeof msgbuf);
e->message = big_strdup("read_log (message)", msgbuf);
big_free("read_log (args)", args);
args = NULL;
Next:
ReadBytes -= pBuf->Length;
pBuf = (EVENTLOGRECORD *)
((LPBYTE)pBuf+pBuf->Length);
}
pBuf = pBuf0;
memset(pBuf, 0, BufSize);
if (fast) break;
}
Exit:
big_free("read_log (pBuf)", pBuf0);
big_free("read_log (args)", args);
if (hEventLog) CloseEventLog(hEventLog);
return e;
}
#endif
void free_log(struct event *e)
{
struct event *p;
while (e) {
p = e;
e = p->next;
big_free("free_log(source)", p->source);
big_free("free_log(message)", p->message);
big_free("free_log(node)", p);
}
}
void print_log(struct event *e)
{
if (e == NULL) {
printf("No messages\n");
return;
}
while (e) {
printf("Record Number: %d\n", (int)e->record);
printf("Time Generated: %s", ctime(&e->gtime));
printf("Time Written: %s", ctime(&e->wtime));
printf("Event ID: %lu\n", (DWORD)e->id);
printf("Event Type: ");
switch(e->type) {
case EVENTLOG_SUCCESS:
printf("Success\n");
break;
case EVENTLOG_ERROR_TYPE:
printf("Error\n");
break;
case EVENTLOG_WARNING_TYPE:
printf("Warning\n");
break;
case EVENTLOG_INFORMATION_TYPE:
printf("Information\n");
break;
case EVENTLOG_AUDIT_SUCCESS:
printf("Audit success\n");
break;
case EVENTLOG_AUDIT_FAILURE:
printf("Audit failure\n");
break;
default:
printf("Unknown\n");
break;
}
printf("Source: %s\n", e->source);
printf("Message: %s\n", e->message);
printf("\n");
e = e->next;
}
}
#if 0
int main(int argc, char **argv)
{
int maxage = 360000; /* 100h is really old... */
struct event *app, *sys, *sec;
time_t now = time(NULL);
app = read_log("Application", now-maxage, 0);
sys = read_log("System", now-maxage, 0);
sec = read_log("Security", now-maxage, 0);
printf("Application\n\n");
print_log(app);
printf("\n\nSystem\n\n");
print_log(sys);
printf("\n\nSecurity\n\n");
print_log(sec);
free_log(app);
free_log(sys);
free_log(sec);
return 0;
}
#endif