-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1184 lines (1102 loc) · 42 KB
/
main.py
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
#!/usr/bin/env python
from sys import argv, exit
from dissect.cstruct import cstruct, dumpstruct
import logging
from datetime import datetime as DT
from time import time as TT
from progress.bar import Bar
import os.path
import argparse
# define arg parser
args_parser = argparse.ArgumentParser(prog='OpenBSM Parser',
description='A Python parser for the OpenBSM file format utilising Dissect.Cstruct parsing logic',)
args_parser.add_argument('-i', '--input', action='store', nargs=1, required=True,
metavar='./20211014090822.20211014090900', help='Path to location of OpenBSM audit log')
args_parser.add_argument('-o', '--output', action='store', nargs=1, metavar='./20211014090822.20211014090900.xml',
help='Path to output file for parsed records. If no output is specified, the input file name will be used and extended with ".xml"')
args_parser.add_argument('-p', '--passwd', action='store', nargs=1, metavar='./passwd',
help='Path to the system of origin\'s /etc/passwd equivalent file')
args_parser.add_argument('-g', '--groups', action='store', nargs=1, metavar='./groups',
help='Path to the system of origin\'s /etc/groups equivalent file')
args_parser.add_argument('-l', '--loglevel', action='store', nargs=1,
metavar='ERROR', help='Log level setter, default value is ERROR. Valid options are: DEBUG, INFO, WARN, ERROR, and CRIT')
args_parser.add_argument('-f', '--logfile', action='store', nargs=1, metavar='./output-log.log',
help='File path of where to write the log file to. If no value is specified the input file will be used and extended with ".log"')
active_args = args_parser.parse_args()
# Set logging info
# Log level formatting
custom_level_formats = {
logging.DEBUG: "[+] DEBUG",
logging.INFO: "[i] INFO ",
logging.WARNING: "[!] WARN ",
logging.ERROR: "[E] ERROR",
logging.CRITICAL: "[X] CRIT ",
}
for level, format_str in custom_level_formats.items():
logging.addLevelName(level, format_str)
# Create Logger
logger = logging.getLogger('OpenBSM-Parser')
if active_args.loglevel:
match active_args.loglevel[0]:
case "DEBUG":
logger.setLevel(logging.DEBUG)
case "INFO":
logger.setLevel(logging.INFO)
case "WARN":
logger.setLevel(logging.WARN)
case "ERROR":
logger.setLevel(logging.WARN)
case "CRIT":
logger.setLevel(logging.CRITICAL)
case _:
logger.setLevel(logging.ERROR)
print(
f"[!] invalid logging level: {active_args.loglevel[0]}; setting logging level to ERROR")
else:
logger.setLevel(logging.ERROR)
# Set format of log messages
# TODO: make the time appear here again, it apperas to be missing in actual logging output
logging_format = logging.Formatter(
"%(levelname)s - %(asctime)s - %(name)s - %(message)s")
if active_args.logfile:
logging.basicConfig(filename=active_args.logfile[0], encoding="utf-8")
else:
logging.basicConfig(
filename=f"{active_args.input[0]}.log", encoding='utf-8')
cdef = """
/*
* Structs pulled from https://github.com/openbsm/openbsm/blob/54a0c07cf8bac71554130e8f6760ca68e5f36c7f/bsm/libbsm.h
* Types changed from u_int8_t / u_int16_t / u_int32_t -> uint8_t / uint16_t / uint32_t / etc to match types with Dissect.cstruct
*/
typedef struct au_tid32 {
uint32_t port;
uint32_t addr;
} au_tid32_t;
typedef struct au_tid64 {
uint64_t port;
uint32_t addr;
} au_tid64_t;
typedef struct au_tidaddr32 {
uint32_t port;
uint32_t type;
uint32_t addr[type / 4];
} au_tidaddr32_t;
typedef struct au_tidaddr64 {
uint64_t port;
uint32_t type;
uint32_t addr[4];
} au_tidaddr64_t;
/*
* argument # 1 byte
* argument value 4 bytes/8 bytes (32-bit/64-bit value)
* text length 2 bytes
* text N bytes + 1 terminating NULL byte
*/
typedef struct {
uchar no;
uint32_t val;
uint16_t len;
// changed type char *text to play nice with Dissect parsing
char text[len-1];
char nbt;
} au_arg32_t;
typedef struct {
uchar no;
uint64_t val;
uint16_t len;
// changed type char *text to play nice with Dissect parsing
char text[len-1];
char nbt;
} au_arg64_t;
/*
* token ID 1 byte
* argument # 1 byte
* uuid 16 bytes
* text length 2 bytes
* text N bytes + 1 terminating NULL byte
*/
typedef struct {
uchar no;
uint8_t uuid[16];
uint16_t len;
char *text;
} au_arg_uuid_t;
/*
* how to print 1 byte
* basic unit 1 byte
* unit count 1 byte
* data items (depends on basic unit)
*/
typedef struct {
uchar howtopr;
uchar bu;
uchar uc;
uchar *data;
} au_arb_t;
/*
* file access mode 4 bytes
* owner user ID 4 bytes
* owner group ID 4 bytes
* file system ID 4 bytes
* node ID 8 bytes
* device 4 bytes/8 bytes (32-bit/64-bit)
*/
typedef struct {
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t fsid;
uint64_t nid;
uint32_t dev;
} au_attr32_t;
typedef struct {
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t fsid;
uint64_t nid;
uint64_t dev;
} au_attr64_t;
/*
* count 4 bytes
* text count null-terminated string(s)
*/
typedef struct {
uint32_t count;
// type is changed from char *text[AUDIT_MAX_ARGS]; to play nice with Dissect parsing
char text[count][];
} au_execarg_t;
/*
* count 4 bytes
* text count null-terminated string(s)
*/
typedef struct {
uint32_t count;
// type is changed from char *text[AUDIT_MAX_ENV]; to play nice with Dissect parsing
char text[count][];
} au_execenv_t;
/*
* status 4 bytes
* return value 4 bytes
*/
typedef struct {
uint32_t status;
uint32_t ret;
} au_exit_t;
/*
* seconds of time 4 bytes
* milliseconds of time 4 bytes
* file name length 2 bytes
* file pathname N bytes + 1 terminating NULL byte
*/
typedef struct {
uint32_t s;
uint32_t ms;
uint16_t len;
char *name;
} au_file_t;
/*
* number groups 2 bytes
* group list N * 4 bytes
*/
typedef struct {
uint16_t no;
// type is changed from u_int32_t list[AUDIT_MAX_GROUPS] to play nice with Dissect parsing
uint32_t list[no][];
} au_groups_t;
/*
* record byte count 4 bytes
* version # 1 byte [2]
* event type 2 bytes
* event modifier 2 bytes
* seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
* milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
*/
typedef struct {
uint32_t size;
uchar version;
uint16_t e_type;
uint16_t e_mod;
uint32_t s;
uint32_t ms;
} au_header32_t;
/*
* record byte count 4 bytes
* version # 1 byte [2]
* event type 2 bytes
* event modifier 2 bytes
* address type/length 1 byte (XXX: actually, 4 bytes)
* machine address 4 bytes/16 bytes (IPv4/IPv6 address)
* seconds of time 4 bytes/8 bytes (32/64-bits)
* nanoseconds of time 4 bytes/8 bytes (32/64-bits)
*/
typedef struct {
uint32_t size;
uchar version;
uint16_t e_type;
uint16_t e_mod;
uint32_t ad_type;
uint32_t addr[4];
uint32_t s;
uint32_t ms;
} au_header32_ex_t;
typedef struct {
uint32_t size;
uchar version;
uint16_t e_type;
uint16_t e_mod;
uint64_t s;
uint64_t ms;
} au_header64_t;
typedef struct {
uint32_t size;
uchar version;
uint16_t e_type;
uint16_t e_mod;
uint32_t ad_type;
uint32_t addr[4];
uint64_t s;
uint64_t ms;
} au_header64_ex_t;
/*
* internet address 4 bytes
*/
typedef struct {
uint32_t addr;
} au_inaddr_t;
/*
* type 4 bytes
* internet address 16 bytes
*/
typedef struct {
uint32_t type;
uint32_t addr[4];
} au_inaddr_ex_t;
/*
* version and ihl 1 byte
* type of service 1 byte
* length 2 bytes
* id 2 bytes
* offset 2 bytes
* ttl 1 byte
* protocol 1 byte
* checksum 2 bytes
* source address 4 bytes
* destination address 4 bytes
*/
typedef struct {
uchar version;
uchar tos;
uint16_t len;
uint16_t id;
uint16_t offset;
uchar ttl;
uchar prot;
uint16_t chksm;
uint32_t src;
uint32_t dest;
} auip_t;
/*
* object ID type 1 byte
* object ID 4 bytes
*/
typedef struct {
uchar type;
uint32_t id;
} auipc_t;
/*
* owner user ID 4 bytes
* owner group ID 4 bytes
* creator user ID 4 bytes
* creator group ID 4 bytes
* access mode 4 bytes
* slot sequence # 4 bytes
* key 4 bytes
*/
typedef struct {
uint32_t uid;
uint32_t gid;
uint32_t puid;
uint32_t pgid;
uint32_t mode;
uint32_t seq;
uint32_t key;
} auipcperm_t;
/*
* port IP address 2 bytes
*/
typedef struct {
uint16_t port;
} auiport_t;
/*
* length 2 bytes
* data length bytes
*/
typedef struct {
uint16_t size;
// changed type from char *data to play nice with Dissect parsing
char data[size-1];
char nbt;
} au_opaque_t;
/*
* path length 2 bytes
* path N bytes + 1 terminating NULL byte
*/
typedef struct {
uint16_t len;
// changed type char *path to play nice with Dissect parsing
char path[len-1];
char nbt;
} au_path_t;
/*
* audit ID 4 bytes
* effective user ID 4 bytes
* effective group ID 4 bytes
* real user ID 4 bytes
* real group ID 4 bytes
* process ID 4 bytes
* session ID 4 bytes
* terminal ID
* port ID 4 bytes/8 bytes (32-bit/64-bit value)
* machine address 4 bytes
*/
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid printing struct au_tid32_t tid;
uint32_t tid_port;
uint32_t tid_addr;
} au_proc32_t;
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid printing struct au_tid64_t tid;
uint64_t tid_port;
uint32_t tid_addr;
} au_proc64_t;
/*
* audit ID 4 bytes
* effective user ID 4 bytes
* effective group ID 4 bytes
* real user ID 4 bytes
* real group ID 4 bytes
* process ID 4 bytes
* session ID 4 bytes
* terminal ID
* port ID 4 bytes/8 bytes (32-bit/64-bit value)
* type 4 bytes
* machine address 16 bytes
*/
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
au_tidaddr32_t tid;
} au_proc32ex_t;
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
au_tidaddr64_t tid;
} au_proc64ex_t;
/*
* error status 1 byte
* return value 4 bytes/8 bytes (32-bit/64-bit value)
*/
typedef struct {
uchar status;
uint32_t ret;
} au_ret32_t;
typedef struct {
uchar err;
uint64_t val;
} au_ret64_t;
/*
* token ID 1 byte
* return value # 1 byte
* uuid 16 bytes
* text length 2 bytes
* text N bytes + 1 terminating NULL byte
*/
typedef struct {
uchar no;
uint8_t uuid[16];
uint16_t len;
char *text;
} au_ret_uuid_t;
/*
* sequence number 4 bytes
*/
typedef struct {
uint32_t seqno;
} au_seq_t;
/*
* socket type 2 bytes
* local port 2 bytes
* local Internet address 4 bytes
* remote port 2 bytes
* remote Internet address 4 bytes
*/
typedef struct {
uint16_t type;
uint16_t l_port;
uint32_t l_addr;
uint16_t r_port;
uint32_t r_addr;
} au_socket_t;
// OpenBSM source code lists wrong comment
// struct def taken from: https://github.com/apple/darwin-xnu/blob/8f02f2a044b9bb1ad951987ef5bab20ec9486310/bsd/security/audit/audit_bsm_token.c#L803
/*
* socket domain 2 bytes
* socket type 2 bytes
* address type 2 bytes
* local port 2 bytes
* local address 4 bytes/16 bytes (IPv4/IPv6 address)
* remote port 2 bytes
* remote address 4 bytes/16 bytes (IPv4/IPv6 address)
*/
typedef struct {
uint16_t domain;
uint16_t type;
uint16_t atype;
uint16_t l_port;
uint8_t l_addr[atype];
uint16_t r_port;
uint8_t r_addr[atype];
} au_socket_ex32_t;
/*
* socket family 2 bytes
* local port 2 bytes
* socket address 4 bytes/16 bytes (IPv4/IPv6 address)
*/
typedef struct {
uint16_t family;
uint16_t port;
uint32_t addr[4];
} au_socketinet_ex32_t;
typedef struct {
uint16_t family;
uint16_t port;
uint32_t addr;
} au_socketinet32_t;
/*
* socket family 2 bytes
* path 104 bytes
*/
typedef struct {
uint16_t family;
char path[104];
} au_socketunix_t;
/*
* audit ID 4 bytes
* effective user ID 4 bytes
* effective group ID 4 bytes
* real user ID 4 bytes
* real group ID 4 bytes
* process ID 4 bytes
* session ID 4 bytes
* terminal ID
* port ID 4 bytes/8 bytes (32-bit/64-bit value)
* machine address 4 bytes
*/
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid displaying struct au_tid32_t tid;
uint32_t tid_port;
uint32_t tid_addr;
} au_subject32_t;
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid printing struct au_tid64_t tid;
uint64_t tid_port;
uint32_t tid_addr;
} au_subject64_t;
/*
* audit ID 4 bytes
* effective user ID 4 bytes
* effective group ID 4 bytes
* real user ID 4 bytes
* real group ID 4 bytes
* process ID 4 bytes
* session ID 4 bytes
* terminal ID
* port ID 4 bytes/8 bytes (32-bit/64-bit value)
* type 4 bytes
* machine address 16 bytes
*/
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid printing struct au_tidaddr32_t tid;
uint32_t port;
uint32_t type;
uint32_t addr[type / 4];
} au_subject32ex_t;
typedef struct {
uint32_t auid;
uint32_t euid;
uint32_t egid;
uint32_t ruid;
uint32_t rgid;
uint32_t pid;
uint32_t sid;
// commented out to aid printing struct au_tidaddr64_t tid;
uint64_t port;
uint32_t type;
uint32_t addr[4];
} au_subject64ex_t;
/*
* text length 2 bytes
* text N bytes + 1 terminating NULL byte
*/
typedef struct {
uint16_t len;
// changed type from char *text to play nice with dissect parsing
char text[len-1];
char nbt;
} au_text_t;
/*
* upriv status 1 byte
* privstr len 2 bytes
* privstr N bytes + 1 (\0 byte)
*/
typedef struct {
uint8_t sorf;
uint16_t privstrlen;
// changed type char *priv to play nice with Dissect parsing
char priv[privstrlen-1];
char nbt;
} au_priv_t;
/*
* privset
* privtstrlen 2 bytes
* privtstr N Bytes + 1
* privstrlen 2 bytes
* privstr N Bytes + 1
*/
typedef struct {
uint16_t privtstrlen;
char *privtstr;
uint16_t privstrlen;
char *privstr;
} au_privset_t;
/*
* zonename length 2 bytes
* zonename text N bytes + 1 NULL terminator
*/
typedef struct {
uint16_t len;
// changed type char *zonename to play nice with Dissect parsing
char zonename[len-1];
char nbt;
} au_zonename_t;
typedef struct {
uint32_t ident;
uint16_t filter;
uint16_t flags;
uint32_t fflags;
uint32_t data;
} au_kevent_t;
typedef struct {
uint16_t length;
// changed type char *data to play nice with Dissect parsing
char data[length-1];
char nbt;
} auinvalid_t;
/*
* trailer magic number 2 bytes
* record byte count 4 bytes
*/
typedef struct {
uint16_t magic;
uint32_t count;
} au_trailer_t;
// special struct that is used to parse local Unix sockets
// struct matches AUT_SOCKET // 0x82
typedef struct {
ushort family;
char addr[];
} au_unixsock_t_special;
// macOS specific struct pulled from darwin-xnu source code at:
// https://github.com/apple/darwin-xnu/blob/8f02f2a044b9bb1ad951987ef5bab20ec9486310/bsd/security/audit/audit_private.h#L206
/*
* signer type 4 bytes
* signer id length 2 bytes
* signer id n bytes
* signer id truncated 1 byte
* team id length 2 bytes
* team id n bytes
* team id truncated 1 byte
* cdhash length 2 bytes
* cdhash n bytes
*/
struct au_identity_info {
uint32_t signer_type;
short signer_id_length;
char signing_id[signer_id_length-1];
char nbt;
uchar signing_id_trunc;
short team_id_length;
char team_id[team_id_length-1];
char nbt;
uchar team_id_trunc;
short cdhash_length;
char cdhash[cdhash_length];
};
// Struct def pulled from: https://github.com/apple/darwin-xnu/blob/8f02f2a044b9bb1ad951987ef5bab20ec9486310/bsd/security/audit/audit_bsm_token.c#L921
/*
* socket family 2 bytes
* local port 2 bytes
* socket address 16 bytes
*/
typedef struct {
short socket_family;
ushort l_port;
uint8_t addr[16];
} au_socketinet128_t;
// Struct def pulled from: https://github.com/apple/darwin-xnu/blob/8f02f2a044b9bb1ad951987ef5bab20ec9486310/bsd/security/audit/audit_bsm_token.c#L229
/*
* how to print 1 byte
* basic unit 1 byte
* unit count 1 byte
* data items (depends on basic unit)
*/
typedef struct {
uint8_t htprint;
uint8_t butype;
uint8_t unit_count;
// thx again Yoran
uint8_t data_items[unit_count * 1 << butype];
} au_data_t;
"""
# TODO: add Solaris parsing support
class Bar(Bar):
fill = "*"
suffix = '%(remaining)d Bytes left - %(elapsed)d Seconds elapsed'
def uid_to_name(fh):
passwd_dict = {}
with open(fh, "r+") as f:
for line in f:
if line.startswith('#'):
continue
fields = line.split(':')
name = fields[0]
uid = int(fields[2])
passwd_dict[uid] = name
return passwd_dict
def main():
aurecord = cstruct(endian=">")
aurecord.load(cdef, compiled=True)
# Define output file name
if active_args.output:
output_file = active_args.output[0]
else:
output_file = f"{active_args.input[0]}.xml"
try:
logger.info(f'Attempting to open file: {active_args.input[0]}')
fh = open(active_args.input[0], "rb")
except FileNotFoundError:
logging.error(f"Could not open file: {active_args.input[0]}")
raise FileNotFoundError
print("[-] Valid file path given; starting parser")
not_empty = True
clean = True
record_count = 0
with open(f"{output_file}", "w+") as f:
f.write("<?xml version='1.0'?>\n<audit>\n")
# passwd parsing testing location
if active_args.passwd:
passwd_dict = uid_to_name(active_args.passwd[0])
if active_args.groups:
groups_dict = uid_to_name(active_args.groups[0])
# Progress bar creation
bar = Bar('Bytes read', max=int(os.path.getsize(active_args.input[0])))
# start perf timer HERE
start_time = TT()
while not_empty and clean:
# Check the first byte for record type
logger.info("Reading one byte to determine record type")
header_type = fh.read(1)
bar.goto(fh.tell())
match header_type:
case b"\x00":
token_type = "AUINVALID_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
auinvalid_t = aurecord.auinvalid_t(fh)
case b"\x13":
token_type = "AU_TRAILER_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_trailer_t = aurecord.au_trailer_t(fh)
logger.info(f"Record end reached; returning for next record")
record_count += 1
with open(f"{output_file}", "a+") as f:
f.write("</record>\n")
case b"\x14":
token_type = "AU_HEADER32_T"
logger.info("Record start; parsing record contents")
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_header32_t = aurecord.au_header32_t(fh)
logger.debug(f"Writing record to disk as XML")
with open(f"{output_file}", "a+") as f:
f.write(
f'<record version="{au_header32_t.version}" event="{au_header32_t.e_type}" modifier="{au_header32_t.e_mod}" time="{DT.fromtimestamp(au_header32_t.s).strftime("%c")}" msec= " + {au_header32_t.ms} msec" >\n')
case b"\x15":
token_type = "AU_HEADER32_EX_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_header32_ex_t = aurecord.au_header32_ex_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x21":
token_type = "AU_DATA_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_data_t = aurecord.au_data_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x22":
token_type = "AUIPC_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
auipc_t = aurecord.auipc_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x23":
token_type = "AU_PATH_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_path_t = aurecord.au_path_t(fh)
logger.debug(f"Writing record to disk as XML")
with open(output_file, "a+") as f:
f.write(f'<path>{au_path_t.path.decode("utf-8")}</path>\n')
case b"\x24":
token_type = "AU_SUBJECT32_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_subject32_t = aurecord.au_subject32_t(fh)
if au_subject32_t.auid == 4294967295:
au_subject32_t.auid = 0
logger.debug(f"Writing record to disk as XML")
with open(output_file, "a+") as f:
if active_args.passwd and active_args.groups:
f.write(
f'<subject audit-uid="{passwd_dict.get(au_subject32_t.auid)}" uid="{passwd_dict.get(au_subject32_t.euid)}" gid="{groups_dict.get(au_subject32_t.egid)}" ruid="{passwd_dict.get(au_subject32_t.ruid)}" rgid="{groups_dict.get(au_subject32_t.rgid)}" pid="{au_subject32_t.pid}" sid="{au_subject32_t.sid}" tid="{au_subject32_t.tid_port + au_subject32_t.tid_addr}" />\n')
elif active_args.passwd:
f.write(
f'<subject audit-uid="{passwd_dict.get(au_subject32_t.auid)}" uid="{passwd_dict.get(au_subject32_t.euid)}" gid="{au_subject32_t.egid}" ruid="{passwd_dict.get(au_subject32_t.ruid)}" rgid="{au_subject32_t.rgid}" pid="{au_subject32_t.pid}" sid="{au_subject32_t.sid}" tid="{au_subject32_t.tid_port + au_subject32_t.tid_addr}" />\n')
elif active_args.groups:
f.write(
f'<subject audit-uid="{au_subject32_t.auid}" uid="{au_subject32_t.euid}" gid="{groups_dict.get(au_subject32_t.egid)}" ruid="{au_subject32_t.ruid}" rgid="{groups_dict.get(au_subject32_t.rgid)}" pid="{au_subject32_t.pid}" sid="{au_subject32_t.sid}" tid="{au_subject32_t.tid_port + au_subject32_t.tid_addr}" />\n')
else:
f.write(
f'<subject audit-uid="{au_subject32_t.auid}" uid="{au_subject32_t.euid}" gid="{au_subject32_t.egid}" ruid="{au_subject32_t.ruid}" rgid="{au_subject32_t.rgid}" pid="{au_subject32_t.pid}" sid="{au_subject32_t.sid}" tid="{au_subject32_t.tid_port + au_subject32_t.tid_addr}" />\n')
case b"\x26":
token_type = "AU_PROC32_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_proc32_t = aurecord.au_proc32_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x27":
token_type = "AU_RET32_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_ret32_t = aurecord.au_ret32_t(fh)
logger.debug(f"Writing record to disk as XML")
with open(output_file, "a+") as f:
f.write(f'<return errval="{au_ret32_t.status}" retval="{au_ret32_t.ret}"/>\n')
case b"\x28":
token_type = "AU_TEXT_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_text_t = aurecord.au_text_t(fh)
au_text_text = au_text_t.text.decode("utf-8")
case b"\x29":
token_type = "AU_OPAQUE_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_opaque_t = aurecord.au_opaque_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x2a":
token_type = "AUINADDR_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_inaddr_t = aurecord.au_inaddr_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x2b":
token_type = "AUIP_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
auip_t = aurecord.auip_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x2c":
token_type = "AUIPORT_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
auiport_t = aurecord.auiport_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x2d":
token_type = "AU_ARG32_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_arg32_t = aurecord.au_arg32_t(fh)
logger.debug(f"Writing record to disk as XML")
with open(output_file, "a+") as f:
f.write(f'<argument arg-num="{au_arg32_t.no}" value="{au_arg32_t.val}" desc="{au_arg32_t.text.decode("utf-8")}"/>\n')
case b"\x2e":
token_type = "AU_SOCKET_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_socket_t = aurecord.au_socket_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x2f":
token_type = "AU_SEQ_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_seq_t = aurecord.au_seq_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x31":
token_type = "AU_ATTR_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_attr_t = aurecord.au_attr_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x32":
token_type = "AUIPCPERM_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
auipcperm_t = aurecord.auipcperm_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x34":
token_type = "AU_GROUPS_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_groups_t = aurecord.au_groups_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x38":
token_type = "AU_PRIV_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_priv_t = aurecord.au_priv_t(fh)
logger.warning(f"XML support not (yet) implemented for this type!")
case b"\x3c":
token_type = "AU_EXECARG_T"
logger.info(f"Byte: {'0x' + header_type.hex()} - {token_type}")
logger.debug(f"Parsing memory for type: {token_type}")
au_execarg_t = aurecord.au_execarg_t(fh)
with open(output_file, "a+") as f: