-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_parse_evtx.cpp
1145 lines (961 loc) · 25.6 KB
/
main_parse_evtx.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =====================================================================================
* Filename: parse_evtx.cpp
* Description: Parse EVTX format files
* Created: 09.01.2018 16:59:43
* Author: Igor Soumenkov (igosha), igosha@kaspersky.com
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <utils/win_types.h>
#include "eventlist.h"
// #define PRINT_TAGS
#include <tools/wintime.h>
#pragma pack(push, 1)
#define EVTX_HEADER_MAGIC "ElfFile"
typedef struct
{
char magic[8];
uint64_t numberOfChunksAllocated;
uint64_t numberOfChunksUsed;
uint64_t checksum;
uint32_t flags;
uint32_t version;
uint64_t fileSize;
uint8_t reserved[0x1000 - 0x30];
}
EvtxHeader;
#define EVTX_CHUNK_HEADER_MAGIC "ElfChnk"
typedef struct
{
char magic[8];
uint64_t firstRecordNumber;
uint64_t lastRecordNumber;
uint64_t firstRecordNumber2;
uint64_t lastRecordNumber2;
uint32_t chunkHeaderSize;
uint8_t reserved[0x80 - 0x2C];
uint8_t reserved2[0x200 - 0x80];
}
EvtxChunkHeader;
#define EVTX_CHUNK_SIZE 0x10000
typedef struct
{
uint32_t magic;
uint32_t size;
uint64_t number;
uint64_t timestamp;
}
EvtxRecordHeader;
typedef struct
{
uint32_t d1;
uint16_t w1;
uint16_t w2;
uint8_t b1[8];
}
EvtxGUID;
#pragma pack(pop)
typedef enum
{
StateNormal = 1,
StateInAttribute = 2,
}
XmlParseState;
typedef struct sParseContext
{
sParseContext* chunkContext;
const uint8_t* data;
size_t dataLen;
size_t offset;
size_t offsetFromChunkStart;
XmlParseState state;
unsigned int currentTemplateIdx;
char cachedValue[256];
}
ParseContext;
static bool ParseBinXml(ParseContext* ctx, size_t inFileOffset);
static bool HaveEnoughData(ParseContext* ctx, size_t numBytes)
{
return ( ctx->offset + numBytes <= ctx->dataLen );
}
static void SkipBytes(ParseContext* ctx, size_t numBytes)
{
ctx->offset += numBytes;
}
template<class c>
static bool ReadData(ParseContext* ctx, c* result, size_t count = 1)
{
if ( !HaveEnoughData(ctx, sizeof(*result) * count) )
return false;
for (size_t idx = 0; idx < count; idx++)
{
result[idx] = *(c*)(ctx->data + ctx->offset);
ctx->offset += sizeof(*result);
}
return true;
}
#define MAX_IDS 256
#define MAX_NUM_ARGS 256
#define INVALID_TEMPLATE_IDX ((unsigned int)-1)
typedef struct sTemplateArgPair
{
sTemplateArgPair* next;
char* key;
uint16_t type;
uint16_t argIdx;
}
TemplateArgPair;
typedef struct sTemplateFixedPair
{
sTemplateFixedPair* next;
char* key;
char* value;
}
TemplateFixedPair;
typedef struct
{
uint32_t shortID;
TemplateFixedPair fixedRoot;
TemplateArgPair argsRoot;
}
TemplateDescription;
template <class c>
static void InitRoot(c* root)
{
root->next = NULL;
}
template<class c>
static c* AddPair(c* root)
{
c* item = (c*)malloc(sizeof(c));
if ( item != NULL )
{
item->next = root->next;
root->next = item;
}
return item;
}
static void FreePair(TemplateFixedPair* item)
{
free(item->key);
free(item->value);
free(item);
}
static void FreePair(TemplateArgPair* item)
{
free(item->key);
free(item);
}
template <class c>
static void ResetRoot(c* root)
{
c* nextItem = NULL;
for (c* ptr = root->next; ptr != NULL; ptr = nextItem)
{
nextItem = ptr->next;
FreePair(ptr);
}
root->next = NULL;
}
static void InitTemplateDescription(TemplateDescription* item)
{
InitRoot(&item->fixedRoot);
InitRoot(&item->argsRoot);
item->shortID = 0;
}
static void ResetTemplateDescription(TemplateDescription* item)
{
ResetRoot(&item->fixedRoot);
ResetRoot(&item->argsRoot);
item->shortID = 0;
}
static uint32_t knownIDs[MAX_IDS] = { 0 };
static TemplateDescription templates[MAX_IDS];
static unsigned int numIDs = 0;
#define MAX_NAME_STACK_DEPTH 20
#define INVALID_STACK_DEPTH ((ssize_t)-1)
typedef struct
{
char name[256];
}
NameStackElement;
#define countof(arr) ( sizeof(arr) / sizeof(*arr) )
static void InitTemplates(void)
{
for (size_t idx = 0; idx < countof(templates); idx++)
InitTemplateDescription(&templates[idx]);
}
static ssize_t nameStackPtr = INVALID_STACK_DEPTH;
static NameStackElement nameStack[MAX_NAME_STACK_DEPTH];
const char** eventDescriptionHashTable = NULL;
const char* logonTypes[] = { NULL, NULL, "Interactive", "Network", "Batch", "Service", NULL, "Unlock", "NetworkCleartext", "NewCredentials", "RemoteInteractive", "CachedInteractive"};
static void RegisterFixedPair(unsigned int templateIdx, const char* key, const char* value)
{
TemplateFixedPair* newPair = AddPair(&templates[templateIdx].fixedRoot);
if ( newPair == NULL )
return;
newPair->key = strdup(key);
newPair->value = strdup(value);
}
static void RegisterArgPair(unsigned int templateIdx, const char* key, uint16_t type, uint16_t argIdx)
{
TemplateArgPair* newPair = AddPair(&templates[templateIdx].argsRoot);
if ( newPair == NULL )
return;
// broken record 3420028194 (security.evtx)
newPair->key = strdup(key == NULL ? "" : key);
newPair->type = type;
newPair->argIdx = argIdx;
}
static void PushName(const char* name)
{
if ( nameStackPtr >= MAX_NAME_STACK_DEPTH )
return;
nameStackPtr++;
strncpy(nameStack[nameStackPtr].name, name, sizeof(nameStack[nameStackPtr].name));
nameStack[nameStackPtr].name[ sizeof(nameStack[nameStackPtr].name) - 1 ] = 0;
}
static void PopName(void)
{
if ( nameStackPtr > INVALID_STACK_DEPTH )
nameStackPtr--;
}
static const char* GetName(void)
{
if ( nameStackPtr <= INVALID_STACK_DEPTH )
return NULL;
return nameStack[nameStackPtr].name;
}
static const char* GetUpperName(void)
{
if ( nameStackPtr <= INVALID_STACK_DEPTH )
return NULL;
if ( nameStackPtr < 1 )
return NULL;
return nameStack[nameStackPtr - 1].name;
}
static bool IsKnownID(uint32_t id, unsigned int* templateIdx)
{
for (unsigned int idx = 0; idx < numIDs; idx++)
{
if ( knownIDs[idx] == id )
{
if ( templateIdx != NULL )
*templateIdx = idx;
return true;
}
}
return false;
}
static bool RegisterID(uint32_t id, unsigned int* templateIdx)
{
if ( numIDs >= MAX_IDS )
return false;
knownIDs[numIDs] = id;
templates[numIDs].shortID = id;
*templateIdx = numIDs;
numIDs++;
return true;
}
static void ResetTemplates(void)
{
for (size_t idx = 0; idx < numIDs; idx++)
ResetTemplateDescription(&templates[idx]);
numIDs = 0;
}
static void SetState(ParseContext* ctx, XmlParseState newState)
{
if ( newState == ctx->state )
return;
if ( ctx->state == StateInAttribute )
PopName();
ctx->state = newState;
}
static void UTF16ToUTF8(uint16_t w, char* buffer, size_t* bufferUsed, size_t bufferSize)
{
uint32_t charLength = 1;
uint8_t msb = 0;
uint8_t mask = 0;
if ( w > 0x7F )
{
charLength++;
msb |= 0x80 + 0x40;
mask = 0xFF;
}
if ( w > 0x7FF )
{
charLength++;
msb |= 0x20;
mask = 0x1F;
}
if ( w > 0xFFFF )
{
charLength++;
msb |= 0x10;
mask = 0x0F;
}
if ( *bufferUsed + charLength >= bufferSize )
return; /* no buffer overruns */
if ( charLength == 1 )
{
buffer[*bufferUsed] = w;
(*bufferUsed)++;
return;
}
// printf("\n%04X -> ", (uint16_t)w);
for (uint32_t charIndex = charLength - 1; charIndex > 0; charIndex--)
{
buffer[*bufferUsed + charIndex] = 0x80 | ( w & 0x3F );
// printf(" ... [%X] %02X ", charIndex, buffer[*bufferUsed + charIndex]);
w >>= 6;
}
buffer[*bufferUsed] = msb | ( w & mask );
#if 0
for (uint32_t idx = 0; idx < charLength; idx++)
printf("%02X ", (uint8_t)buffer[*bufferUsed + idx]);
printf("\n");
#endif
*bufferUsed += charLength;
}
static bool ReadPrefixedUnicodeString(ParseContext* ctx, char* nameBuffer, size_t nameBufferSize, bool isNullTerminated)
{
uint16_t nameCharCnt;
size_t nameBufferUsed = 0;
size_t idx = 0;
if ( !ReadData(ctx, &nameCharCnt) )
return false;
// TODO : convert UTF-16 to UTF-8
for (idx = 0; idx < nameCharCnt && idx*2 < ( nameBufferSize - 1 ) ; idx ++)
{
uint16_t w;
if ( !ReadData(ctx, &w) )
return false;
UTF16ToUTF8(w, nameBuffer, &nameBufferUsed, nameBufferSize);
}
if ( nameBufferUsed >= nameBufferSize )
nameBufferUsed = nameBufferSize - 1;
nameBuffer[nameBufferUsed] = 0;
SkipBytes(ctx, (nameCharCnt - idx + ( isNullTerminated ? 1 : 0 ))*2);
return true;
}
static bool ReadName(ParseContext* ctx, char* nameBuffer, size_t nameBufferSize)
{
uint16_t nameHash;
uint32_t chunkOffset;
uint32_t d;
ParseContext temporaryCtx(*ctx->chunkContext);
ParseContext* ctxPtr = ctx;
if ( nameBufferSize < 2 )
return false;
nameBuffer[0] = 0;
if ( !ReadData(ctx, &chunkOffset) )
return false;
if ( ctx->offset + ctx->offsetFromChunkStart != chunkOffset )
{
// printf("!!!!!! %08X %08X\n", chunkOffset, (uint32_t)(ctx->offset + ctx->offsetFromChunkStart));
ctxPtr = &temporaryCtx;
ctxPtr->offset = chunkOffset;
}
if ( !ReadData(ctxPtr, &d) )
return false;
if ( !ReadData(ctxPtr, &nameHash) )
return false;
if ( !ReadPrefixedUnicodeString(ctxPtr, nameBuffer, nameBufferSize, true) )
return false;
return true;
}
static const char* GetProperKeyName(ParseContext* ctx)
{
const char* key;
const char* upperName;
key = GetName();
// printf("Key: %s Upper: %s\n", key, GetUpperName());
upperName = GetUpperName();
if ( ( upperName != NULL ) &&
!strcmp(key, "Data") &&
!strcmp(upperName, "EventData") &&
ctx->cachedValue[0] != 0 )
{
key = ctx->cachedValue;
}
return key;
}
static bool ParseValueText(ParseContext* ctx)
{
uint8_t stringType;
char valueBuffer[256];
const char* upperName;
const char* key;
if ( !ReadData(ctx, &stringType) )
return false;
if ( !ReadPrefixedUnicodeString(ctx, valueBuffer, sizeof(valueBuffer), false) )
return false;
// printf("******* %s=%s", GetName(), valueBuffer);
key = GetProperKeyName(ctx);
upperName = GetUpperName();
if ( ( key != NULL ) &&
( ( upperName == NULL ) ||
strcmp(key, "Name") ||
strcmp(GetUpperName(), "Data") ) )
{
RegisterFixedPair(ctx->currentTemplateIdx, key, valueBuffer);
}
SetState(ctx, StateNormal);
strncpy(ctx->cachedValue, valueBuffer, sizeof(valueBuffer));
ctx->cachedValue[sizeof(ctx->cachedValue)-1] = 0;
return true;
}
static bool ParseAttributes(ParseContext* ctx)
{
char nameBuffer[256];
if ( !ReadName(ctx, nameBuffer, sizeof(nameBuffer)) )
return false;
// printf(" %s", nameBuffer);
PushName(nameBuffer);
SetState(ctx, StateInAttribute);
return true;
}
static bool ParseOpenStartElement(ParseContext* ctx, bool hasAttributes)
{
uint8_t b;
uint16_t w;
uint32_t elementLength;
uint32_t attributeListLength = 0;
char nameBuffer[256];
if ( !ReadData(ctx, &w) )
return false;
if ( !ReadData(ctx, &elementLength) )
return false;
if ( !ReadName(ctx, nameBuffer, sizeof(nameBuffer)) )
return false;
if ( hasAttributes )
{
if ( !ReadData(ctx, &attributeListLength) )
return false;
}
#ifdef PRINT_TAGS
printf("<%s [%08X] ", nameBuffer, attributeListLength);
fflush(stdout);
#endif
PushName(nameBuffer);
return true;
}
static bool ParseCloseStartElement(ParseContext* ctx)
{
SetState(ctx, StateNormal);
#ifdef PRINT_TAGS
printf(">");
fflush(stdout);
#endif
return true;
}
static bool ParseCloseElement(ParseContext* ctx)
{
SetState(ctx, StateNormal);
PopName();
#ifdef PRINT_TAGS
printf("</>");
fflush(stdout);
#endif
return true;
}
static void DumpTemplateContents(ParseContext* ctx, unsigned int templateIdx)
{
return ;
printf("********************* TEMPLATE BEGIN ************************\n");
printf("Short ID: %08X\n", templates[templateIdx].shortID);
for ( TemplateFixedPair* ptr = templates[templateIdx].fixedRoot.next; ptr != NULL; ptr = ptr->next )
{
printf(" %s = %s\n", ptr->key, ptr->value);
}
for ( TemplateArgPair* ptr = templates[templateIdx].argsRoot.next; ptr != NULL; ptr = ptr->next )
{
printf(" %s { arg %04X type %04X } \n", ptr->key, ptr->argIdx, ptr->type);
}
printf("********************* TEMPLATE END ************************\n");
}
static bool ParseTemplateInstance(ParseContext* ctx)
{
uint8_t b;
uint32_t numArguments;
uint32_t shortID;
uint32_t tempResLen;
uint32_t totalArgLen = 0;
if ( !ReadData(ctx, &b) )
return false;
if ( b != 0x01 )
return false;
if ( !ReadData(ctx, &shortID) )
return false;
if ( !ReadData(ctx, &tempResLen) )
return false;
if ( !ReadData(ctx, &numArguments) )
return false;
// printf("OK, template %08X\n", shortID);
if ( !IsKnownID(shortID, &ctx->currentTemplateIdx) )
//if ( numArguments == 0x00000000 )
{
uint8_t longID[16];
uint32_t templateBodyLen;
ParseContext templateCtx;
/* template definition follows */
if ( !ReadData(ctx, &longID[0], sizeof(longID)) )
return false;
if ( !ReadData(ctx, &templateBodyLen) )
return false;
// printf("Template body, len %08X\n", templateBodyLen);
templateCtx.data = ctx->data + ctx->offset;
templateCtx.dataLen = templateBodyLen; /* mm_min ... */
templateCtx.offset = 0;
templateCtx.chunkContext = ctx;
templateCtx.offsetFromChunkStart = ctx->offset + ctx->offsetFromChunkStart;
templateCtx.cachedValue[0] = 0;
RegisterID(shortID, &templateCtx.currentTemplateIdx);
if ( !ParseBinXml(&templateCtx, 0) )
return false;
SkipBytes(ctx, templateBodyLen);
if ( !ReadData(ctx, &numArguments) )
return false;
ctx->currentTemplateIdx = templateCtx.currentTemplateIdx;
DumpTemplateContents(ctx, ctx->currentTemplateIdx);
}
// printf("Number of arguments: %08X\n", numArguments);
for ( TemplateFixedPair* ptr = templates[ctx->currentTemplateIdx].fixedRoot.next; ptr != NULL; ptr = ptr->next )
{
bool alreadyPrinted = false;
if ( !strcmp(ptr->key, "EventID") )
{
uint16_t eventID = strtoul(ptr->value, NULL, 10);
if ( ( eventID != 0 ) && ( eventDescriptionHashTable[eventID] != NULL ) )
{
printf("'%s':%u (%s), ", ptr->key, eventID, eventDescriptionHashTable[eventID]);
alreadyPrinted = true;
}
}
if ( !alreadyPrinted )
printf("'%s':'%s', ", ptr->key, ptr->value);
}
// printf("\n");
size_t argumentMapCount = numArguments * 2;
uint16_t* argumentMap = (uint16_t*)malloc(sizeof(*argumentMap)*argumentMapCount);
if ( !ReadData(ctx, argumentMap, argumentMapCount) )
{
printf("Failed to read the arguments\n");
free(argumentMap);
return false;
}
for (uint64_t argumentIdx = 0; argumentIdx < numArguments; argumentIdx++)
{
uint16_t argLen = argumentMap[argumentIdx*2];
uint16_t argType = argumentMap[argumentIdx*2 + 1];
TemplateArgPair* argPair = NULL;
// printf("\n %08X : [%02X %02X %02X] Arg %" PRIX64" type %08X len %08X\n",
// (uint32_t)ctx->offset, ctx->data[ctx->offset], ctx->data[ctx->offset+1], ctx->data[ctx->offset+2],
// argumentIdx, argType, argLen);
for ( TemplateArgPair* ptr = templates[ctx->currentTemplateIdx].argsRoot.next; ptr != NULL; ptr = ptr->next )
{
if ( ptr->argIdx == argumentIdx )
{
argPair = ptr;
break;
}
}
if ( argPair == NULL )
{
// printf("Argument not found\n");
SkipBytes(ctx, argLen);
}
else
{
uint8_t v_b;
uint16_t v_w;
uint32_t v_d;
uint64_t v_q;
time_t unixTimestamp;
struct tm localtm;
struct tm* t;
uint8_t sid[2+6];
EvtxGUID guid;
char* stringBuffer;
size_t stringNumUsed = 0;
size_t stringSize = 0;
switch(argType)
{
//// case 0x00: /* void */
//break;
case 0x01: /* String */
stringSize = argLen*2+2;
stringBuffer = (char*)malloc(stringSize);
if ( stringBuffer == NULL )
return false;
for (size_t idx = 0; idx < argLen/2; idx++)
{
if ( !ReadData(ctx, &v_w) )
return false;
UTF16ToUTF8(v_w, stringBuffer, &stringNumUsed, stringSize);
}
if ( stringNumUsed >= stringSize )
stringNumUsed = stringSize - 1;
stringBuffer[stringNumUsed] = 0;
printf("'%s':'%s', ", argPair->key, stringBuffer);
free(stringBuffer);
break;
case 0x04: /* uint8_t */
if ( !ReadData(ctx, &v_b) )
return false;
printf("'%s':%02u, ", argPair->key, v_b);
break;
case 0x06: /* uint16_t */
if ( !ReadData(ctx, &v_w) )
return false;
if ( !strcmp(argPair->key, "EventID") && ( eventDescriptionHashTable[v_w] != NULL ))
printf("'%s':%04u (%s), ", argPair->key, v_w, eventDescriptionHashTable[v_w]);
else
printf("'%s':%04u, ", argPair->key, v_w);
break;
case 0x08: /* uint32_t */
if ( !ReadData(ctx, &v_d) )
return false;
if ( !strcmp(argPair->key, "LogonType") && ( v_d <= 11 ) && ( logonTypes[v_d] != NULL ))
printf("'%s':%08u (%s), ", argPair->key, v_d, logonTypes[v_d]);
else
printf("'%s':%08u, ", argPair->key, v_d);
break;
case 0x0A: /* uint64_t */
if ( !ReadData(ctx, &v_q) )
return false;
printf("'%s':%016" PRIu64 ", ", argPair->key, v_q);
break;
case 0x0E: /* binary */
printf("'%s':", argPair->key);
for (size_t idx = 0; idx < argLen; idx++)
{
if ( !ReadData(ctx, &v_b) )
return false;
printf("%02X", v_b);
}
printf(", ");
break;
case 0x0F: /* GUID */
if ( !ReadData(ctx, &guid) )
return false;
printf("'%s':%08X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X, ", argPair->key,
guid.d1, guid.w1, guid.w2,
guid.b1[0], guid.b1[1], guid.b1[2], guid.b1[3],
guid.b1[4], guid.b1[5], guid.b1[6], guid.b1[7]);
break;
case 0x14: /* HexInt32 */
if ( !ReadData(ctx, &v_d) )
return false;
printf("'%s':%08" PRIX32", ", argPair->key, v_d);
break;
case 0x15: /* HexInt64 */
if ( !ReadData(ctx, &v_q) )
return false;
printf("'%s':%016" PRIX64 ", ", argPair->key, v_q);
break;
case 0x11: /* FileTime */
if ( !ReadData(ctx, &v_q) )
return false;
unixTimestamp = UnixTimeFromFileTime(v_q);
t = gmtime_r(&unixTimestamp, &localtm);
if ( t == NULL )
printf("'%s':%016" PRIX64 ", ", argPair->key, v_q);
else
printf("'%s':%04u.%02u.%02u-%02u:%02u:%02u, ",
argPair->key,
t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
break;
case 0x13: /* SID */
if ( argLen < sizeof(sid) )
return false;
if ( !ReadData(ctx, sid, sizeof(sid)) )
return false;
v_q = 0;
for (size_t idx = 0; idx < 6; idx++)
{
v_q <<= 8;
v_q |= sid[2+idx];
}
printf("'%s':S-%u-%" PRIu64 "", argPair->key, sid[0], v_q);
for (size_t idx = sizeof(sid); idx + 4 <= argLen; idx += 4)
{
if ( !ReadData(ctx, &v_d) )
return false;
printf("-%u", v_d);
}
printf(", ");
break;
case 0x21: /* BinXml */
{
ParseContext temporaryCtx(*ctx);
temporaryCtx.dataLen = temporaryCtx.offset + argLen;
if ( !ParseBinXml(&temporaryCtx, 0) )
;//return false;
// printf("=====<<<<< %08X\n", argLen);
SkipBytes(ctx, argLen);
}
break;
default:
if ( argType != 0x00 )
printf("'%s':'...//%04X[%04X]', ", argPair->key, argPair->type, argLen);
SkipBytes(ctx, argLen);
break;
}
}
totalArgLen += argLen;
}
free(argumentMap);
return true;
}
static bool ParseOptionalSubstitution(ParseContext* ctx)
{
uint16_t substitutionID;
uint8_t valueType;
if ( !ReadData(ctx, &substitutionID) )
return false;
if ( !ReadData(ctx, &valueType) )
return false;
if ( valueType == 0x00 )
{
if ( !ReadData(ctx, &valueType) )
return false;
}
// printf("******* %s=<<param %X/type %X>> ", GetName(), substitutionID, valueType);
RegisterArgPair(ctx->currentTemplateIdx, GetProperKeyName(ctx), valueType, substitutionID);
SetState(ctx, StateNormal);
return true;
}
static bool ParseBinXmlPre(const uint8_t* data, size_t dataLen, size_t inFileOffset, size_t inChunkOffset)
{
ParseContext ctx;
ctx.data = data;
ctx.dataLen = dataLen;
ctx.offset = inChunkOffset;
ctx.currentTemplateIdx = INVALID_TEMPLATE_IDX;
ctx.chunkContext = &ctx;
ctx.offsetFromChunkStart = 0;
ctx.cachedValue[0] = 0;
return ParseBinXml(&ctx, inFileOffset);
}
static bool ParseBinXml(ParseContext* ctx, size_t inFileOffset)
{
bool result = true;
ctx->state = StateNormal;
// printf("ParseBinXml(%08X, %08X)\n", (uint32_t)ctx->offset, (uint32_t)ctx->dataLen);
while ( result && ( ctx->offset < ctx->dataLen ) )
{
uint8_t tag = ctx->data[ctx->offset++];
// printf("%08zX: %02X ", inFileOffset + ctx->offset, tag);
// fflush(stdout);
// printf("%08zX: %02X %02X %02X", inFileOffset + ctx->offset, tag, ctx->data[ctx->offset], ctx->data[ctx->offset+1]);
switch(tag)
{
case 0x00: /* EOF */
ctx->offset = ctx->dataLen;
break;
case 0x01: /* OpenStartElementToken */
result = ParseOpenStartElement(ctx, false);
break;
case 0x41:
result = ParseOpenStartElement(ctx, true);
break;
case 0x02: /* CloseStartElementToken */
result = ParseCloseStartElement(ctx);
break;
case 0x03: /* CloseEmptyElementToken */
case 0x04: /* CloseElementToken */
result = ParseCloseElement(ctx);
break;
case 0x05: /* ValueTextToken */
case 0x45:
result = ParseValueText(ctx);
break;
case 0x06: /* AttributeToken */
case 0x46:
result = ParseAttributes(ctx);
break;
case 0x07: /* CDATASectionToken */
case 0x47:
break;
case 0x08: /* CharRefToken */
case 0x48:
break;
case 0x09: /* EntityRefToken */
case 0x49:
break;
case 0x0A: /* PITargetToken */
break;
case 0x0B: /* PIDataToken */
break;
case 0x0C: /* TemplateInstanceToken */
result = ParseTemplateInstance(ctx);
break;
case 0x0D: /* NormalSubstitutionToken */
case 0x0E: /* OptionalSubstitutionToken */
result = ParseOptionalSubstitution(ctx);
break;
case 0x0F: /* FragmentHeaderToken */
SkipBytes(ctx, 3);
break;
default:
result = false;
break;
}
// printf("\n");
}
return result;
}
static bool ParseEVTXInt(int f)
{
EvtxHeader header;
uint64_t off = 0;
uint8_t* chunk;
bool result = true;
if ( read(f, &header, sizeof(header)) != sizeof(header) )
return false;
if ( header.version != 0x00030001)
return false;
#ifdef PRINT_TAGS
printf("Number of chunks: %" PRIu64 " %" PRIu64 " header sz %zu\n", header.numberOfChunksAllocated, header.numberOfChunksUsed, sizeof(header));
#endif
off = sizeof(header);
chunk = (uint8_t*)malloc(EVTX_CHUNK_SIZE);
if ( chunk == NULL )
return false;
while ( result )
{
EvtxChunkHeader* chunkHeader = (EvtxChunkHeader*)chunk;
uint64_t inRecordOff;
ResetTemplates();
if ( lseek(f, off, SEEK_SET) != off )
{
result = false;
break;
}
if ( read(f, chunk, EVTX_CHUNK_SIZE) != EVTX_CHUNK_SIZE )
break;