-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaa_suggest.py
executable file
·2841 lines (2413 loc) · 139 KB
/
aa_suggest.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 python3
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-License-Identifier: GPL-3.0-only
# Version: 0.8.14
# Min AppArmor version: 3.0.8 (Debian 12)
# Max AppArmor version: 4.0.1 (Ubuntu 24.04 LTS), stop using if not updated - it will provide ambiguous results
# line, l - single log line in form of dictionary
# normalize - prepare line values for a possible merge; make keys consistent
# adapt - replace line values with suitable for usage in the rule
# merge - make single line from many lines; unequivocally by default, or ambiguously by params
# unequivocally - non-aggressive deduplication; no rule covarage is lost, but paths could be replaced by tunables
# ambiguously - aggressive deduplication; some rule coverage could be broader than needed
# keep/drop - include or exclude the line from deduplication, affects merging (preprocessing); active filtering
# show/hide - include or exclude the line from display, does NOT affect merging (postprocessing)
# MAIN FLOW:
# gather logs ->
# normalization ->
# adaption (& colorization preparation) ->
# merging ->
# sorting ->
# alignment ->
# colorization ->
# display
import sys
import argparse
import re
import shlex
import pathlib
import string
import random
import copy
import os
def adaptFilePath(l, key, ruleStyle):
'''Applied early to fully handle duplicates.
For file paths, not necessarily file lines.
Watch out for bugs: launchpad #1856738
Do only one capture per regex helper, otherwise diffs will be a mess (will match recursively)
'''
# Always surround these helpers with other charactes or new/endlines
# Capturing group must not be optional '?', but always provide at least empty '|' match
# Mix of regex and pcre styles!; 'C' for capture
random6 = '(?![0-9]{6}|[a-z]{6}|[A-Z]{6}|[A-Z][a-z]{5}|[A-Z][a-z]{4}[0-9])(?:[0-9a-zA-Z]{6})' # aBcXy9, AbcXyz, abcxy9; NOT 123789, abcxyz, ABCXYZ, Abcxyz, Abcxy1
random8 = '(?![0-9]{8}|[a-z]{8}|[A-Z]{8}|[A-Z][a-z]{7}|[A-Z][a-z]{6}[0-9])(?:[0-9a-zA-Z]{8})' # aBcDwXy9, AbcdWxyz, abcdwxy9; NOT: 12346789, abcdwxyz, ABCDWXYZ, Abcdwxyz, Abcdwxy1
random10 = '(?![0-9]{10}|[a-z]{10}|[A-Z]{10}|[A-Z][a-z]{9}|[A-Z][a-z]{8}[0-9])(?:[0-9a-zA-Z]{10})' # aBcDeVwXy9, AbcdeVwxyz, abcdevwxy9; NOT: 1234567890, abcdevwxyz, ABCDEVWXYZ, Abcdevwxyz, Abcdevwxy1
random6u = r'(?:\w{6}|\?\?\?\?\?\?)'
random6uC = r'(?:\w{6})'
random8u = r'(?:\w{8}|\?\?\?\?\?\?\?\?)'
random8uC = r'(?:\w{8})'
users = r'(?:[0-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9}|@{uid})'
usersC = r'(?:[0-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9})'
hex2 = r'(?:[0-9a-fA-F]{2}|\[0-9a-f\]\[0-9a-f\]|@{hex2})'
hex2C = r'(?:[0-9a-fA-F]{2})'
hex16 = r'(?:[0-9a-fA-F]{16}|\[0-9a-f\]\*\[0-9a-f\]|@{hex16})'
hex16C = r'(?:[0-9a-fA-F]{16})'
hex32 = r'(?:[0-9a-fA-F]{32}|\[0-9a-f\]\*\[0-9a-f\]|@{hex32})'
hex32C = r'(?:[0-9a-fA-F]{32})'
hex38 = r'(?:[0-9a-fA-F]{38}|\[0-9a-f\]\*\[0-9a-f\]|@{hex38})'
hex38C = r'(?:[0-9a-fA-F]{38})'
hex64 = r'(?:[0-9a-fA-F]{64}|\[0-9a-f\]\*\[0-9a-f\]|@{hex64})'
hex64C = r'(?:[0-9a-fA-F]{64})'
ints = r'(?:\d+|\[0-9\]\*|\[0-9\]{\,\[0-9\]}|@{int})'
ints2 = r'(?:\d{2}|\[0-9\]\[0-9\]|@{int2})'
ints4 = r'(?:\d{4}|\[0-9\]\*\[0-9\]|@{int4})'
ints4C = r'(?:\d{4})'
ints10 = r'(?:\d{10}|\[0-9\]\*\[0-9\]|@{int10})'
ints10C = r'(?:\d{10})'
uuid = r'(?:[0-9a-fA-F]{8}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{12}|\[0-9a-f\]\*\[0-9a-f\]|@{uuid})'
uuidC = r'(?:[0-9a-fA-F]{8}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{12})'
etc_ro = r'(?:/usr/etc|@{etc_ro})'
run = r'(?:/var/run|/run|@{run})'
runC = r'(?:/var/run|/run)'
runJ = r'(?:/var|/run|@{run})'
proc = r'(?:/proc|@{PROC})'
procC = r'(?:/proc)'
sys = r'(?:/sys|@{sys})'
sysC = r'(?:/sys)'
pids = r'(?:[2-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9}|@{pid})' # 3-4999999999
pidsC = r'(?:[2-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9})' # 3-4999999999; capture
tids = r'(?:[1-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9}|@{tid})' # 1-4999999999
tidsC = r'(?:[1-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9})' # 1-4999999999; capture
multiarch = r'(?:[^/]+-linux-(?:gnu|musl)(?:[^/]+)?|@{multiarch})'
multiarchC = r'(?:[^/]+-linux-(?:gnu|musl)(?:[^/]+)?)'
user_cache = r'(?:@?/home/[^/]+/\.cache|@{user_cache_dirs})'
user_cacheC = r'(?:@?/home/[^/]+/\.cache)'
user_config = r'(?:@?/home/[^/]+/\.config|@{user_config_dirs})'
user_configC = r'(?:@?/home/[^/]+/\.config)'
user_share = r'(?:@?/home/[^/]+/\.local/share|@{user_share_dirs})'
user_shareC = r'(?:@?/home/[^/]+/\.local/share)'
homes = r'(?:@?/home/[^/]+|@{HOME})'
homesC = r'(?:@?/home/[^/]+)'
pciId = r'(?:[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.\d|\?\?\?\?:\?\?:\?\?\.\?|@{pci_id})'
pciIdC = r'(?:[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.\d)'
o3 = r'(?:3|{\,3})?' # optional '3'
oWayland = r'(?:-wayland|{\,-wayland})?' # optional '-wayland'
oUsr = r'(?:usr/|{\,usr/})?' # optional '/usr'
oUsrC = r'(?:usr/)?' # optional '/usr'; capture
Any = r'(?!@{.+|{.+|\[0-9.+|\*)[^/]+'
gdm_cache = r'(?:/var/lib/gdm(?:3|{\,3})?/\.cache|@{gdm_cache_dirs})'
gdm_cacheC = r'(?:/var/lib/gdm(?:3|{\,3})?/\.cache)'
gdm_config = r'(?:/var/lib/gdm(?:3|{\,3})?/\.config|@{gdm_config_dirs})'
gdm_configC = r'(?:/var/lib/gdm(?:3|{\,3})?/\.config)'
gdm_local = r'(?:/var/lib/gdm(?:3|{\,3})?/\.local(?!/share)|@{gdm_local_dirs})'
gdm_localC = r'(?:/var/lib/gdm(?:3|{\,3})?/\.local(?!/share))'
gdm_share = r'(?:/var/lib/gdm(?:3|{\,3})?/\.local/share|@{gdm_share_dirs})'
gdm_shareC = r'(?:/var/lib/gdm(?:3|{\,3})?/\.local/share)'
sddm_cache = r'(?:/var/lib/sddm/\.cache|@{sddm_cache_dirs})'
sddm_cacheC = r'(?:/var/lib/sddm/\.cache)'
sddm_config = r'(?:/var/lib/sddm/\.config|@{sddm_config_dirs})'
sddm_configC = r'(?:/var/lib/sddm/\.config)'
sddm_local = r'(?:/var/lib/sddm/\.local(?!/share)|@{sddm_local_dirs})'
sddm_localC = r'(?:/var/lib/sddm/\.local(?!/share))'
sddm_share = r'(?:/var/lib/sddm/\.local/share|@{sddm_share_dirs})'
sddm_shareC = r'(?:/var/lib/sddm/\.local/share)'
lightdm_cache = r'(?:/var/lib/lightdm/\.cache|@{lightdm_cache_dirs})'
lightdm_cacheC = r'(?:/var/lib/lightdm/\.cache)'
lightdm_config = r'(?:/var/lib/lightdm/\.config|@{lightdm_config_dirs})'
lightdm_configC = r'(?:/var/lib/lightdm/\.config)'
lightdm_local = r'(?:/var/lib/lightdm/\.local(?!/share)|@{lightdm_local_dirs})'
lightdm_localC = r'(?:/var/lib/lightdm/\.local(?!/share))'
lightdm_share = r'(?:/var/lib/lightdm/\.local/share|@{lightdm_share_dirs})'
lightdm_shareC = r'(?:/var/lib/lightdm/\.local/share)'
literalBackslash = '\\\\'
# Special cases <3
pciBus = r'(?:(?:pci)?[0-9a-f]{4}:[0-9a-f]{2}|(?:pci)?\?\?\?\?:\?\?|@{pci_bus})'
if ruleStyle == 'AppArmor.d':
Bin = r'(?:/(?:usr/)?(?:s)?bin|@{bin})'
BinC = r'(?:/(?:usr/)?(?:s)?bin)'
pciBusC = r'(?:pci[0-9a-f]{4}:[0-9a-f]{2})'
else:
Bin = r'(?:/(?:usr/)?(?:s)?bin|/{\,usr/}bin)'
BinC = r'(?:/(?:usr/)?bin)'
pciBusC = r'(?:[0-9a-f]{4}:[0-9a-f]{2})'
# Substitute capturing group with t[1] or t[2]; order matters when mentioned
regexpToMacro = [ # non-tunables
# regex # default style # AppArmor.d style # prefix, optional
(rf'^{user_share}/gvfs-metadata/(|{Any})$', None, '{,*}', 'deny'),
(rf'^/var/lib/apt/lists/({Any})\.yml\.gz$', '*', None),
#(f'^{user_share}/yelp/storage/({Any})/', '*', None, 'owner'),
#(f'^{user_share}/yelp/storage/[^/]+/({Any})/', '*', None, 'owner'),
# Capturing *any* goes above
(rf'^{Bin}/(|e|f)grep$', '{,e,f}', None),
(rf'^{Bin}/(|g|m)awk$', '{,g,m}', None),
(rf'^{Bin}/gettext(|\.sh)$', '{,.sh}', None),
(rf'^{Bin}/kreadconfig(|5)$', '{,5}', None),
(rf'^{Bin}/python3\.(\d+)(?:-[a-z]+)?$', '[0-9]{,[0-9]}', '@{int}'),
(rf'^{Bin}/ruby\d+\.(\d+)$', '[0-9]', '@{int}'),
(rf'^{Bin}/which(|\.debianutils)$', '{,.debianutils}', None),
(rf'^{Bin}/ldconfig(|\.real)$', '{,.real}', None),
(rf'^/{oUsr}(?:local/)?lib/python3\.(\d+)/', '[0-9]{,[0-9]}', '@{int}'),
(rf'^/usr/share/gdm({o3})/', '{,3}', None),
(rf'^/usr/share/gtk-([2-4])\.\d+/', '[2-4]', None),
(rf'^/usr/share/icu/(\d+)\.', '[0-9]*', '@{int}'),
(rf'^/usr/share/icu/{ints}\.(\d+)/', '[0-9]*', '@{int}'),
(rf'^/usr/share/qt(|5|6)(?:ct)?/', '{,5,6}', None),
(rf'^/{oUsr}lib/kde(|3|4)/', '{,3,4}', None),
(rf'^/etc/\.chsh\.({random6})$', '??????', '@{rand6}'),
(rf'^/etc/{Any}/(?:{Any}/)?[a-z]+\.(?:d|avail)/({ints2})-', '[0-9][0-9]', '@{int2}'),
(rf'^/etc/apparmor\.d/libvirt/libvirt-({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^/etc/gdm({o3})/', '{,3}', None),
(rf'^/etc/gtk-([2-4])\.0/', '[2-4]', None),
(rf'^/etc/python3\.(\d+)/', '[0-9]{,[0-9]}', '@{int}'),
(rf'^/etc/systemd/network/({ints2})-', '[0-9][0-9]', '@{int2}'),
(rf'^/var/backups/apt\.extended_states\.(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/var/cache/fontconfig/({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^/var/cache/fontconfig/{hex32}-le64\.cache-\d+\.TMP-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/var/cache/fontconfig/({uuidC})-', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^{runJ}/log/journal/({hex32C})/', '[0-9a-f]*[0-9a-f]', '@{hex32}'),
(rf'^{runJ}/log/journal/{hex32}/system@({hex16C})-', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/system@{hex16}-({hex16C})\.', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/system@({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/system@{hex32}-({hex16C})-', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/system@{hex32}-{hex16}-({hex16C})\.', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/user-{users}@({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/user-{users}@{hex32}-({hex16C})-', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/user-{users}@{hex32}-{hex16}-({hex16C})\.', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/user-{users}@({hex16C})-', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^{runJ}/log/journal/{hex32}/user-{users}@{hex16}-({hex16C})\.', '[0-9a-f]*[0-9a-f]', '@{hex16}'), # '@' is a string
(rf'^/var/log/lightdm/seat(\d+)-', '[0-9]*', '@{int}', 'owner'),
(rf'^/var/log/popularity-contest\.(\d+)(?:\.)?$', '[0-9]*', '@{int}', 'owner'),
(rf'^/var/log/Xorg\.(\d+)\.', '[0-9]*', '@{int}', 'owner'),
(rf'^/var/lib/btrfs/scrub\.progress\.({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^/var/lib/btrfs/scrub\.status\.({uuidC})(?:_tmp)?$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^/var/lib/cni/results/cni-loopback-({uuidC})-lo$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^/var/lib/ca-certificates/openssl/({random8})\.', '????????', '@{rand8}'),
(rf'^/var/lib/update-notifier/tmp\.({random10})$', '??????????', '@{rand10}'),
(rf'^({gdm_cacheC})/', None, '@{gdm_cache_dirs}'),
(rf'^({gdm_configC})/', None, '@{gdm_config_dirs}'),
(rf'^({gdm_localC})/', None, '@{gdm_local_dirs}'),
(rf'^({gdm_shareC})/', None, '@{gdm_share_dirs}'),
(rf'^({sddm_cacheC})/', None, '@{sddm_cache_dirs}'),
(rf'^({sddm_configC})/', None, '@{sddm_config_dirs}'),
(rf'^({sddm_localC})/', None, '@{sddm_local_dirs}'),
(rf'^({sddm_shareC})/', None, '@{sddm_share_dirs}'),
(rf'^({lightdm_cacheC})/', None, '@{lightdm_cache_dirs}'),
(rf'^({lightdm_configC})/', None, '@{lightdm_config_dirs}'),
(rf'^({lightdm_localC})/', None, '@{lightdm_local_dirs}'),
(rf'^({lightdm_shareC})/', None, '@{lightdm_share_dirs}'),
(rf'^@?/var/lib/gdm({o3})/', '{,3}', None), # after previous
(rf'^@?{gdm_cache}/ibus/dbus-({random8})$', '????????', '@{rand8}'),
(rf'^{gdm_cache}/gstreamer-(\d+)$', '[0-9]*', '@{int}'),
(rf'^{gdm_cache}/mesa_shader_cache/({hex2C})/', '[0-9a-f][0-9a-f]', '@{hex2}'),
(rf'^{gdm_cache}/mesa_shader_cache/{hex2}/({hex38C})(?:\.tmp)?$', '[0-9a-f]*[0-9a-f]', '@{hex38}'), # temp pair? TODO
(rf'^{gdm_config}/ibus/bus/({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}'),
#(f'^{gdm_config}/ibus/bus/{hex32}-unix({oWayland})-{ints}$', '{,-wayland}', None),
(rf'^{gdm_config}/ibus/bus/{hex32}-unix{oWayland}-(\d+)$', '[0-9]*', '@{int}'),
(rf'^{gdm_share}/xorg/Xorg\.(\d+)\.', '[0-9]*', '@{int}'),
(rf'^/var/lib/docker/tmp/buildkit-mount({ints10C})/', '[0-9]*', '@{int10}'),
(rf'^/var/lib/kubelet/pods/({uuidC})/', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^/var/lib/libvirt/swtpm/({uuidC})/', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{homes}/\.pulse/({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^{homes}/xauth_({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^({user_cacheC})/', None, '@{user_cache_dirs}', 'owner'),
(rf'^({user_configC})/', None, '@{user_config_dirs}', 'owner'),
(rf'^{user_cache}/calibre/ev2/[a-z]/[a-z][a-z]-({random8uC})/', '????????', '@{word8}', 'owner'),
(rf'^{user_cache}/fontconfig/({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^{user_cache}/\.fr-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^{user_cache}/fontconfig/{hex32}-le64\.cache-\d+\.TMP-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{user_cache}/fontconfig/({uuidC})-', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^{user_cache}/gnome-software/icons/({hex38C})-', '[0-9a-f]*[0-9a-f]', '@{hex}', 'owner'),
(rf'^{user_cache}/gstreamer-(\d+)/', '[0-9]*', '@{int}', 'owner'),
(rf'^{user_cache}/event-sound-cache\.tdb\.({hex32C})\.', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^{user_cache}/kcrash-metadata/plasmashell\.({hex32C})\.', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^{user_cache}/kcrash-metadata/plasmashell\.{hex32}\.({ints4C})\.', '[0-9]*', '@{int4}', 'owner'),
(rf'^{user_cache}/keyring-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^{user_cache}/mesa_shader_cache/({hex2C})/', '[0-9a-f][0-9a-f]', '@{hex2}', 'owner'),
(rf'^{user_cache}/mesa_shader_cache/{hex2}/({hex38C})(?:\.tmp)?$', '[0-9a-f]*[0-9a-f]', '@{hex38}', 'owner'), # temp pair? TODO
(rf'^{user_cache}/thumbnails/[^/]+/({hex32C})\.', '*', '@{hex32}', 'owner'),
(rf'^{user_cache}/thumbnails/fail/gnome-thumbnail-factory/({hex32C})\.', '*', '@{hex32}', 'owner'),
(rf'^@?{user_cache}/ibus/dbus-({random8})$', '????????', '@{rand8}', 'owner'),
(rf'^{user_config}/#(\d+)$', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^{user_config}/ibus/bus/({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
#r(f'^{user_config}/ibus/bus/{hex32}-unix({oWayland})-{ints}$', '{,-wayland}', None, 'owner'),
(rf'^{user_config}/ibus/bus/{hex32}-unix{oWayland}-(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^{user_config}/kdeglobals\.({random6})', '??????', '@{rand6}', 'owner'),
(rf'^{user_config}/vlc/vlcrc\.(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^{user_config}/vlc/vlc-qt-interface\.conf(|\.{random6})$', '{,.??????}', '{,.@{rand6}}', 'owner'), # unconventional random tail?
(rf'^{user_config}/qBittorrent/\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^{user_config}/qBittorrent/qBittorrent-data\.conf(|\.{random6})$', '{,.??????}', '{,.@{rand6}}', 'owner'), # unconventional random tail?
(rf'^{user_config}/QtProject\.conf(|\.{random6})$', '{,.??????}', '{,.@{rand6}}', 'owner'), # unconventional random tail?
(rf'^{user_config}/qt(|5|6)(?:ct)?/', '{,5,6}', None, 'owner'),
(rf'^{user_share}/\.org\.chromium\.Chromium\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{user_share}/applications/userapp-falkon-({random6})\.', '??????', '@{rand6}', 'owner'),
(rf'^{user_share}/gvfs-metadata/root-({random8})\.', '????????', '@{rand8}', 'owner'),
(rf'^{user_share}/kcookiejar/cookies\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^@({hex16})/', '????????????????', None),
(rf'^@?/tmp/\.X11-unix/X(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^@?/tmp/\.ICE-unix/(\d+)$', '[0-9]*', '@{int}'),
(rf'^@?/tmp/dbus-({random8})$', '????????', '@{rand8}' , 'owner'),
(rf'^@?/tmp/dbus-({random10})$', '??????????', '@{rand10}' , 'owner'),
(rf'^@?/tmp/xauth_({random6})(?:-c|-l)?$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.dotnet/shm/session(\d+)/', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/\.coreclr\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.gnome_desktop_thumbnail\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.java_pid(\d+)$', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/\.mount_nextcl({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.org\.chromium\.Chromium\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.xfsm-ICE-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.t?X(\d+)-', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/\.wine-(\d+)/', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/adb\.(\d+)\.', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/akregator\.({random6})\.', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/akregator\.{random6}\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/apt-changelog-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/apt-changelog-{random6}/\.apt-acquire-privs-test\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/apt\.data\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/apt-dpkg-install-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/apt-key-gpghome\.({random10})/', '??????????', '@{rand10}', 'owner'),
(rf'^/tmp/apt-key-gpghome\.{random10}/\.#lk0x({hex16C})\.', '[0-9a-f]*[0-9a-f]', '@{hex}', 'owner'),
(rf'^/tmp/apt-key-gpghome\.{random10}/\.#lk0x{hex16}\.debian-stable\.(\d+)x?$', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/apparmor-bugreport-({random6uC})\.txt', '??????', '@{word6}', 'owner'),
(rf'^/tmp/aurules\.({random8})$', '????????', '@{rand8}', 'owner'),
(rf'^/tmp/calibre_\d+\.(\d+)\.', '[0-9]{,[0-9]}', '@{int}', 'owner'),
(rf'^/tmp/calibre_\d+\.{ints}\.(\d+)_', '[0-9]{,[0-9]}', '@{int}', 'owner'),
(rf'^/tmp/calibre_\d+\.{ints}\.{ints}_tmp_({random8uC})/', '????????', '@{word8}', 'owner'),
(rf'^/tmp/calibre_\d+\.{ints}\.{ints}_tmp_{random8u}/({random8uC})(?:\.|/|log)', '????????', '@{word8}', 'owner'),
(rf'^/tmp/calibre_\d+\.{ints}\.{ints}_tmp_{random8u}/ipc_result_\d_\d_({random8uC})\.', '????????', '@{word8}', 'owner'),
(rf'^/tmp/clr-debug-pipe-(\d+)-', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/clr-debug-pipe-{ints}-(\d+)-', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/config-err-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/ContentRuleList({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/crontab\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/dehydrated-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/dotnet-diagnostic-(\d+)-', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/dotnet-diagnostic-{ints}-(\d+)-', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^/tmp/dpkg\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/dissect-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/falkon-({random6})/', '??????', '@{rand6}' , 'owner'),
(rf'^/tmp/fz([0-9])temp', '[0-9]', None, 'owner'),
(rf'^/tmp/fz{ints}temp-(\d+)/', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/gdkpixbuf-xpm-tmp\.({random6})$', '??????', '@{rand6}' , 'owner'),
(rf'^/tmp/grub-{Any}\.({random10})$', '??????????', '@{rand10}' , 'owner'),
(rf'^/tmp/kcminit\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/librnnoise-(\d+)\.', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/lxqt-config-appearance\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/messageviewer_attachment_({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/mkinitcpio\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/runtime-info\.txt\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/mozilla-temp-(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/Mozilla({uuidC})-', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/tmp/Mozilla{literalBackslash}{{({uuidC}){literalBackslash}}}-', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/tmp/ollama(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/okular\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/orcexec\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/pty(\d+)/', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/QNapi\.(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/qtsingleapp-quiter-(\d+)-', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/qtsingleapp-quiter-{ints}-(\d+)(?:-)?$', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/read-file(\d+)/', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/(?:syscheck,sanity)-squashfs-(\d+)$', '[0-9]*', '@{int}'),
(rf'^/tmp/scoped_dir({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/server-(\d+)\.', '[0-9]*', '@{int}'),
(rf'^/tmp/sort({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/systemd-private-({hex32C})-', '[0-9a-f]*[0-9a-f]', '@{hex32}', 'owner'),
(rf'^/tmp/systemd-private-{hex32}-[^/]+\.service-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/sddm-:(\d+)-', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/sddm-:{ints}-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/talpid-openvpn-({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/tmp/Temp-({uuidC})/', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/tmp/tmp\.({random10})/', '??????????', '@{rand10}', 'owner'),
(rf'^/tmp/tmp({random8})/', '????????', '@{rand8}', 'owner'),
(rf'^/tmp/user/{users}/tmp\.({random10})$', '??????????', '@{rand10}', 'owner'),
(rf'^/tmp/({uuidC})(?:\.|$)', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/tmp/vdpau-drivers-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/wireshark_extcap_ciscodump_(\d+)_', '[0-9]*', '@{int}', 'owner'),
(rf'^/tmp/zabbix_server_({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/tmp/\.({random8})$', '????????', '@{rand8}', 'owner'),
(rf'^/tmp/\.({random10})$', '??????????', '@{rand10}', 'owner'),
(rf'^/tmp/({hex64C})\.png$', None, '@{hex64}', 'owner'),
(rf'^{run}/cockpit/({random8})$', '????????', '@{rand8}'),
(rf'^{run}/gdm({o3})/', '{,3}', None),
(rf'^{run}/gdm{o3}/dbus/dbus-({random8})$', '????????', '@{rand8}'),
(rf'^{run}/netns/cni-({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{run}/NetworkManager/nm-openvpn-({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{run}/issue\.({random10})$', '??????????', '@{rand10}'),
(rf'^{run}/initcpio-tmp/mkinitcpio\.({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^{run}/sddm/{literalBackslash}{{({uuidC}){literalBackslash}}}-?$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{run}/systemd/seats/seat(\d+)$', '[0-9]*', '@{int}'),
(rf'^{run}/systemd/netif/links/(\d+)$', '[0-9]*', '@{int}'),
(rf'^{run}/systemd/(?:sessions|inhibit)/(.+)\.ref$', '*', None),
(rf'^{run}/snapper-tools-({random6})/', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/\.dbus-proxy/[a-z]+-bus-proxy-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/glfw-shared-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/iceauth_({random6})(?:-|$)', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/at-spi/bus_(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^{run}/user/{users}/akregatorbWqrit\.(\d+)\.', '[0-9]*', '@{int}', 'owner'),
(rf'^{run}/user/{users}/discover({random6})\.', '??????', '@{rand6}'),
(rf'^{run}/user/{users}/kmozillahelper({random6})\.', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/kmozillahelper{random6}\.(\d+)\.', '[0-9]*[0-9]', '@{int}', 'owner'),
(rf'^{run}/user/{users}/wayland-(\d+)(?:\.lock)?$', '[0-9]*', '@{int}', 'owner'),
(rf'^{run}/user/{users}/webkitgtk/[a-z]+-proxy-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/xauth_({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{run}/user/{users}/pipewire-(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^{run}/user/{users}/snap\.snapd-desktop-integration/wayland-cursor-shared-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^{sys}/block/zram(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/bus/pci/slots/(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/{pciBus}/({pciIdC})/', '????:??:??.?', '@{pci_id}'),
(rf'^{sys}/devices/{pciBus}/{pciId}/({pciIdC})/', '????:??:??.?', '@{pci_id}'),
(rf'^{sys}/devices/{pciBus}/{pciId}/[^/]+/host(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/{pciBus}/{pciId}/(?:{pciId}/)?drm/card(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/{pciBus}/{pciId}/(?:{pciId}/)?drm/card{ints}/metrics/({uuidC})/', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{sys}/devices/pci({pciBusC})/', '????:??', None),
(rf'^{sys}/devices/({pciBusC})/', None, '@{pci_bus}'),
(rf'^{sys}/devices/i2c-(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/platform/serial\d+/tty/ttyS?(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/system/cpu/cpu(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/system/cpu/cpufreq/policy(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/system/memory/memory(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/system/node/node(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/virtual/tty/tty(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/virtual/hwmon/hwmon(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/virtual/block/dm-(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/virtual/vc/[a-z]+(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/devices/virtual/block/dm-(\d+)/', '[0-9]*', '@{int}'),
(rf'^{sys}/fs/cgroup/user\.slice/user-{users}\.slice/user@@{users}\.service/app\.slice/app-gnome-org\.gnome\.Epiphany-(\d+)\.', '[0-9]*', '@{int}'),
(rf'^{sys}/fs/btrfs/({uuidC})/', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{sys}/firmware/efi/efivars/[^/]+-({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}'),
(rf'^{sys}/kernel/iommu_groups/(\d+)/', '[0-9]*', '@{int}'),
(rf'^{proc}/{pids}/fdinfo/(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^{proc}/{pids}/net/(?:tcp|udp)(4|6)$', '{4,6}', None),
(rf'^{proc}/sys/net/ipv(4|6)/', '{4,6}', None),
(rf'^{proc}/irq/(\d+)/', '[0-9]*', '@{int}'),
(rf'^/dev/cpu/(\d+)/', '[0-9]*', '@{int}'),
(rf'^/dev/snd/controlC(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/dri/card(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/dm-(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/input/event(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/loop(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/media(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/parport(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/pts/(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/dev/shm/\.org\.chromium\.Chromium\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/dev/shm/dunst-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/dev/shm/grim-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/dev/shm/mono\.(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/dev/shm/sem\.({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/dev/shm/sem\.mp-({random8uC})$', '????????', '@{word8}', 'owner'),
(rf'^/dev/shm/({uuidC})$', '[0-9a-f]*[0-9a-f]', '@{uuid}', 'owner'),
(rf'^/dev/shm/wlroots-({random6})$', '??????', '@{rand6}', 'owner'),
(rf'^/dev/sr(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/dev/tty(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/dev/ttyS(\d+)$', '[0-9]*', '@{int}', 'owner'),
(rf'^/dev/vfio/(\d+)$', '[0-9]*', '@{int}'),
(rf'^/dev/video(\d+)$', '[0-9]*', '@{int}'),
(rf'/#(\d+)$', '[0-9]*[0-9]', '@{int}'),
(rf'/\.goutputstream-({random6})$', '??????', '@{rand6}'),
(rf'/\.uuid\.TMP-({random6})$', '??????', '@{rand6}'),
(rf'/\.mutter-Xwaylandauth\.({random6})$', '??????', '@{rand6}'),
(rf'/file({random6})$', '??????', '@{rand6}'),
(rf'/gnome-control-center-user-icon-({random6})$', '??????', '@{rand6}'),
(rf'/blkid\.tab-({random6})$', '??????', '@{rand6}'),
(rf'/nvidia-xdriver-({random8})$', '????????', '@{rand8}'),
(rf'/socket-({random8})$', '????????', '@{rand8}'),
(rf'/pulse/({hex32C})-runtime(?:\.tmp)?$', '[0-9a-f]*[0-9a-f]', '@{hex32}'), # temp pair? TODO
(rf'/({random6})\.(?:tmp|TMP)$', '??????', '@{rand6}', 'owner'),
(rf'/({random8})\.(?:tmp|TMP)$', '????????', '@{rand8}', 'owner'),
(rf'/({random10})\.(?:tmp|TMP)$', '??????????', '@{rand10}', 'owner'),
(rf'/tmp\.({random6})(?:/|$)', '??????', '@{rand6}', 'owner'),
(rf'/tmp\.({random8})(?:/|$)', '????????', '@{rand8}', 'owner'),
(rf'/tmp\.({random10})(?:/|$)', '??????????', '@{rand10}', 'owner'),
(rf'^(/home/{Any})/', '@{HOME}', None, 'owner'), # before the last; tunable isn't matching unix lines
(rf'^@/home/({Any})/', '*', None, 'owner'), # last; fallback for unix lines
(rf'^(/{oUsr}lib(?:exec|32|64)?)/', None, '@{lib}'), # last <3
(rf'^({BinC})/', None, '@{bin}'), # last <3
(rf'^/({oUsr}s)bin/', '{,usr/}{,s}', None), # last <3 (to match unhandled)
(rf'^/({oUsr}local/)bin', '{,usr/}{,local/}', None), # last <3
(rf'^/({oUsrC})bin/', '{,usr/}', None), # last <3
(rf'^/({oUsrC})lib/', '{,usr/}', None), # last <3
]
tunables = [ # default tunables only
(rf'^/{oUsr}lib/({multiarchC})/', '@{multiarch}', None),
(rf'^({etc_ro})/', '@{etc_ro}', None),
(rf'^/etc/passwd\.(pidsC)$', '@{pid}', None),
(rf'^/dev/shm/lttng-ust-wait-{ints}-({usersC})$', '@{uid}', None, 'owner'),
(rf'^{runJ}/log/journal/{hex32}/user-({usersC})(?:@|\.)', '@{uid}', None),
(rf'^/var/log/Xorg\.pid-({pidsC})\.', '@{pid}', None, 'owner'),
(rf'^{homes}/(Desktop)/', '@{XDG_DESKTOP_DIR}', None, 'owner'),
(rf'^{homes}/(Downloads)/', '@{XDG_DOWNLOAD_DIR}', None, 'owner'),
(rf'^{homes}/(Templates)/', '@{XDG_TEMPLATES_DIR}', None, 'owner'),
(rf'^{homes}/(Public)/', '@{XDG_PUBLICSHARE_DIR}',None, 'owner'),
(rf'^{homes}/(Documents)/', '@{XDG_DOCUMENTS_DIR}', None, 'owner'),
(rf'^{homes}/(Music)/', '@{XDG_MUSIC_DIR}', None, 'owner'),
(rf'^{homes}/(Pictures)/', '@{XDG_PICTURES_DIR}', None, 'owner'),
(rf'^{homes}/(Videos)/', '@{XDG_VIDEOS_DIR}', None, 'owner'),
(rf'^({user_shareC})/', '@{user_share_dirs}', None, 'owner'),
(rf'^({runC})/', '@{run}', None),
(rf'^{run}/user/({usersC})/', '@{uid}', None, 'owner'),
(rf'^{run}/systemd/users/({usersC})$', '@{uid}', None),
(rf'^({sysC})/', '@{sys}', None),
(rf'^{sys}/fs/cgroup/user\.slice/user-({usersC})\.', '@{uid}', None),
(rf'^{sys}/fs/cgroup/user\.slice/user-{users}\.slice/user@({usersC})\.', '@{uid}', None), # '@' is a string
(rf'^({procC})/', '@{PROC}', None),
(rf'^{proc}/({pidsC})/', '@{pid}', None, 'owner'),
(rf'^{proc}/{pids}/task/({tidsC})/', '@{tid}', None, 'owner'),
(rf'^/tmp/tracker-extract-\d-files.({usersC})/', '@{uid}', None, 'owner'),
(rf'^/tmp/user/({usersC})/', '@{uid}', None, 'owner'),
(rf'^/tmp/user/{users}/Temp-({uuidC})/', '@{uuid}', None, 'owner'),
]
lineType = findLineType(l)
if lineType != 'UNIX':
tunables.extend(regexpToMacro) # tunables come first
regexpToMacro = tunables
path = l.get(key)
hexToString_Out = hexToString(path)
if hexToString_Out != path: # changed
path = hexToString_Out
l[key] = path
# Backslash special characters after decoding and before PCRE replacement
pcreChars = ('\\', '?', '*', '[', ']', '{', '}', '"', '!', "'", '^')
for i in pcreChars:
occurences = range(l.get(key).count(i))
for j in occurences:
regexp = f'(?<!{literalBackslash})()\\{i}' # do not match already escaped
subGroup = substituteGroup(l.get(key), '\\', regexp)
if subGroup[0]:
resultPath = subGroup[0]
subSpan = subGroup[1]
l[key] = resultPath
updatePostcolorizationDiffs(l, subSpan, '', key)
# Attempt to substitute matches in path one by one
for t in regexpToMacro:
regexp = t[0]
d = t[1] # default
a = t[2] # AppArmor.d
if not d and not a:
raise ValueError('No rule style choices. Check your regexes.')
# Assign chosen style, fallback to default if absent
# Always choose default for unix line
# Lastly, skip if default is absent
if (ruleStyle == 'default' or \
lineType == 'UNIX') and \
not d:
continue
elif lineType == 'UNIX':
macro = d
elif ruleStyle == 'AppArmor.d' and a:
macro = a
else:
macro = d
path = l.get(key) # fetch again in case it had changed
subGroup = substituteGroup(path, macro, regexp)
if subGroup[0]:
resultPath = subGroup[0]
subSpan = subGroup[1]
oldDiff = subGroup[2]
l[key] = resultPath
updatePostcolorizationDiffs(l, subSpan, oldDiff, key)
if len(t) >= 4:
l[f'{key}_prefix'] = t[3]
return l
def adaptDbusPaths(lines, ruleStyle):
# First capture but not (second) match; 'C' for capture
Any = r'(?!@{.+|{.+|\[0-9.+|\*)[^/]+'
usersC = r'(?:[0-9]|[1-9][0-9]{1,8}|[1-4][0-9]{9})'
hex32C = r'(?:[0-9a-fA-F]{32})'
uuidC = r'(?:[0-9a-fA-F]{8}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{12})'
regexpToMacro = (
# regex # default # AppArmor.d
(rf'^/org/freedesktop/ColorManager/devices/({Any})$', '*', None),
(rf'^/org/freedesktop/login1/session/({Any})$', '*', None),
(rf'^/org/freedesktop/systemd1/unit/({Any})$', '*', None),
(rf'^/org/freedesktop/UDisks2/drives/({Any})$', '*', None),
(rf'^/org/freedesktop/UDisks2/block_devices/({Any})$', '*', None),
(rf'^/org/freedesktop/UPower/devices/({Any})$', '*', None),
# Capturing *any* goes above
(rf'/User({usersC})(?:/|$)', '@{uid}', None),
(rf'/({uuidC})(?:/|$)', '*', '@{uuid}'),
(rf'/org/bluez/obex/({uuidC})$', '*', '@{uuid}'),
(rf'/icc_({hex32C})$', '*', '@{hex32}'),
(rf'/(\d+)$', '[0-9]*', '@{int}'),
(rf'/(\d+)/', '[0-9]*', '@{int}'), # separate to mitigate overlaping
(rf'/_(\d+)$', '[0-9]*', '@{int}'),
(rf'/Client(\d+)(?:/|$)', '[0-9]*', '@{int}'),
(rf'/ServiceBrowser(\d+)(?:/|$)', '[0-9]*', '@{int}'),
(rf'/seat(\d+)$', '[0-9]*', '@{int}'),
(rf'/Source_(\d+)(?:/|$)', '[0-9]*', '@{int}'),
(rf'/prompt/u(\d+)$', '[0-9]*', '@{int}'),
(rf'/Prompt/p(\d+)$', '[0-9]*', '@{int}'),
(rf'/loop(\d+)$', '[0-9]*', '@{int}'), # unreachable?
)
for profile in lines:
for l in lines[profile]:
if not findLineType(l).startswith('DBUS'):
raise ValueError('Using this function to handle non-DBus log lines could lead to silent errors.')
if not l.get('path'): # skip bind, eavesdrop, etc
continue
for r,d,a in regexpToMacro:
if ruleStyle == 'AppArmor.d' and a:
macro = a
else:
macro = d
path = l.get('path')
subGroup = substituteGroup(path, macro, r)
if subGroup[0]:
subPath = subGroup[0]
subSpan = subGroup[1]
oldDiff = subGroup[2]
l['path'] = subPath
updatePostcolorizationDiffs(l, subSpan, oldDiff, 'path')
return lines
def findTempTailPair(filename, ruleStyle):
'''Intended only for temp pairs'''
# Fallback to default if style have 'None'
tempRegexesToMacro = (
# regex # default style # AppArmor.d style
(r'\.tmp$', '{,.tmp}', None),
(r'~$', '{,~}', None),
(r'\.[A-Z0-9]{6}$', '{,.??????}', '{,.@{rand6}}'),
(r'\.tmp[A-Z0-9]{6}$', '{,.tmp??????}', '{,.tmp@{rand6}}'),
(r'\.tmp[0-9]{4}$', '{,.tmp????}', '{,.tmp@{int}}'),
)
# TODO
# /usr/share/applications/mimeinfo.cache
# /usr/share/applications/.mimeinfo.cache.JLI8D2
for r,d,a in tempRegexesToMacro:
suffixRe = re.search(r, filename)
if suffixRe:
tempTail = suffixRe.group(0)
if ruleStyle == 'AppArmor.d' and a:
macro = a
else:
macro = d
break
else:
tempTail = None
macro = None
return (tempTail, macro)
def highlightWords(string_, isHighlightVolatile=True):
'''Sensitive words should not have false-positives, volatile words are expected to have false-positives
Volatile is for those paths which could not be unequivocally normalized
Repeating, non-positional patterns must be greedy
Capturing group is the highlight'''
ignorePath = '/usr/share'
grn = r'\x1b\[0;32m' # (escaped) regular green
rst = r'\x1b\[0m' # (escaped) reset
proc = '@{PROC}'
pids = '@{pids?}'
sensitivePatterns = ( # with Red; re.I
r'/\.ssh/(id[^.]+)(?!.*\.pub)(?:/|$)',
r'/(ssh_host_[^/]+_key(?:[^.]|))(?!.*\.pub)',
r'(?<!pubring\.orig\.)(?<!pubring\.)(?<!mouse)(?<!turn|whis|flun|dove|alar|rick|apt-|gtk-)(?<!hot|hoc|don|mon|tur|coc|joc|lac|buc|soc|haw|pun|tac|flu|dar|sna|smo|cri|coo|pin|din|dic)(?<!ca|mi)(key)(?!button|stroke|board|punch|less|code|pal|pad|gen|\.pub|word\.cpython|-manager-qt_ykman\.png|-personalization-gui\.png|-personalization-gui_yubikey-personalization-gui\.png)', # only key; NOT: turkey, keyboard, keygen, etc
r'(?<!/ISRG_)(?<!grass|snake|birth|colic|coral|arrow|blood|orris|bread|squaw|fever|itter|inger)(?<!worm|alum|rose|club|pink|beet|poke|musk|fake)(?<!tap|che|red|she)(?<!sc|ch)(root)(?!stalk|worm|stock|less|s)', # only root; NOT: grassroots, rootless, chroot, etc
r'(?<!non)(secret)(?!agogue|ion|ary|ari)', # only secret, secrets; NOT: nonsecret, secretary, etc
r'(?<!non|set)(priv)(?!ate\.CoreLib|atdocent|atdozent|iledge|ates|ation|ateer|atise|arize|atist|ation|er|et|es|ed|ie|al|y|e)', # only priv, private; NOT: nonprivate, privatise, etc
r'(?<!com|sur|out)(pass)(?!word-symbolic\.svg|epied|erine|enger|along|ible|erby|able|less|band|ivi|ive|age|ade|ion|ed|el|er|wd)', # only pass, password; NOT: compass, passage, etc
r'(?<!over|fore)(?<!be)(shadow)(?!coord|graph|iest|like|less|map|ing|ily|ers|box|ier|er|ed|y|s)', # only shadow; NOT: foreshadow, shadows, etc
r'(?<!na|sa|ac)(cred)(?!ulous|ulity|uliti|enza|ence|ibl|ibi|al|it|o)', # only cred, creds, credentials; NOT: sacred, credence, etc
r'(?:/|^)(0)(?:/|$)', # standalone zero: 0, /0, /0/; NOT: a0, 0a, 01, 10, 101, etc
rf'^(?:/proc|{proc}|{grn}{proc}{rst})/(1)/',
rf'^(?:/proc|{proc}|{grn}{proc}{rst})(?:/\d+|/(?:{grn}{pids}{rst}|{pids}))?/(cmdline)$',
r'(cookies)\.sqlite(?:-wal)?$',
r'(cookiejar)',
)
random6 = r'(?![0-9]{6}|[a-z]{6}|[A-Z]{6}|[A-Z][a-z]{5}|[A-Z][a-z]{4}[0-9]|base\d\d|\d{5}x|sha\d{3}|[a-z]{5}\d|UPower)[0-9a-zA-Z]{6}' # aBcXy9, AbcXyz, ABCXY9; NOT 123789, abcxyz, ABCXYZ, Abcxyz, Abcxy1, base35, 12345x, abcxy9
random8 = r'(?<!arphic-)(?![0-9]{8}|[a-z]{8}|[A-Z]{8}|[A-Z][a-z]{7}|[A-Z][a-z]{6}[0-9]|[a-z]{7}\d|GeoClue\d)[0-9a-zA-Z]{8}' # aBcDwXy9, AbcdWxyz, ABCDWXY9; NOT: 12346789, abcdwxyz, ABCDWXYZ, Abcdwxyz, Abcdwxy1, abcdwxy9
random10 = r'(?![0-9]{10}|[a-z]{10}|[A-Z]{10}|[A-Z][a-z]{9}|[A-Z][a-z]{8}[0-9]|PackageKit|PolicyKit\d)[0-9a-zA-Z]{10}' # aBcDeVwXy9, AbcdeVwxyz, abcdevwxy9, ABCDEVWXY9; NOT: 1234567890, abcdevwxyz, ABCDEVWXYZ, Abcdevwxyz, Abcdevwxy1, PackageKit
volatilePatterns = ( # with Yellow
r'/#(\d+)$', # trailing number with leading hash sign
r'[-./@](1000)(?:/|$)', # first user id
rf'[-.]({random6})(?:/|$)',
rf'[-.]({random8})(?:/|$)',
rf'[-.]({random10})(?:/|$)',
rf'/(?:var/)?tmp/({random6})(?:/|$)',
rf'/(?:var/)?tmp/({random8})(?:/|$)',
rf'/(?:var/)?tmp/({random10})(?:/|$)',
r'[-.](?![0-9]{8}|[a-z]{8})([0-9a-z]{8})\.log$',
r'[-.](?![0-9]{8}|[A-Z]{8})([0-9A-Z]{8})\.log$',
r'(?=[^0-9a-fA-F]0x([0-9a-fA-F]{16})(?:[^0-9a-fA-F]|$))', # hex address
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{32})(?:[^0-9a-fA-F]|$))', # standalone MD5
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{38})(?:[^0-9a-fA-F]|$))', # ??
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{56})(?:[^0-9a-fA-F]|$))', # standalone SHA224
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{64})(?:[^0-9a-fA-F]|$))', # standalone SHA256
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{96})(?:[^0-9a-fA-F]|$))', # standalone SHA384
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{128})(?:[^0-9a-fA-F]|$))', # standalone SHA512
r'(?=[^0-9a-fA-F]([0-9a-fA-F]{8}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{4}[-_][0-9a-fA-F]{12})(?:[^0-9a-fA-F]|$))', # standalone UUID
r'^@?/home/([^/]+)/', # previously unmatched homes
r'/python\d(?:\.\d+|\.\[0-9\]\{\,\[0-9\]\}|\.@\{int\})?/dist-packages/([^/]+)/',
r'/\.mozilla/firefox/(\w{8})\.default',
)
if not string_.startswith(ignorePath):
for r in sensitivePatterns:
allSpans = []
sensitiveRe = re.finditer(r, string_, re.I)
for m in sensitiveRe:
allSpans.append(m.span(1))
# Begin from the end to mitigate colorization shifting
allSpans.sort(reverse=True)
for s in allSpans:
string_ = colorizeBySpan(string_, 'Red', s)
if isHighlightVolatile:
for r in volatilePatterns:
allSpans = []
volatileRe = re.finditer(r, string_)
for m in volatileRe:
allSpans.append(m.span(1))
allSpans.sort(reverse=True)
for s in allSpans:
string_ = colorizeBySpan(string_, 'Yellow', s)
return string_
def findExecType(path):
always_ix = { # not for programs with network access or large scope
'awk', 'nice',
'base64', 'nproc',
'basename', 'od',
'bunzip2', 'readlink',
'bzip2', 'realpath',
'cat', 'rm',
'chmod', 'rmdir',
'chown', 'sed',
'cp', 'seq',
'cpio', 'shasum',
'cut', 'sha1sum',
'date', 'sha224sum',
'diff', 'sha256sum',
'dir', 'sha384sum',
'dirname', 'sha512sum',
'echo', 'shred',
'egrep', 'sleep',
'env', 'sort',
'expr', 'strings',
'false', 'sync',
'fgrep', 'tac',
'file', 'tail',
'find', 'tar',
'fold', 'tempfile',
'gawk', 'test',
'getfacl', 'timeout',
'getopt', 'touch',
'gettext', 'tr',
'grep', 'true',
'gzip', 'uname',
'head', 'uniq',
'id', 'unzip',
'ionice', 'wc',
'ln', 'which',
'locale', 'which.debianutils',
'ls', 'xargs',
'lz4cat', 'xz',
'lzop', 'xzcat',
'mawk', 'zgrep',
'md5sum', 'zip',
'mktemp', 'zstd',
'mv', 'setfacl',
'unexpand', 'tsort',
'truncate', 'yes',
'sum', 'install',
'fold', 'factor',
'fmt', 'split',
'b2sum', 'base32',
}
always_Px = {
'ps',
'spice-vdagent',
}
if path in always_ix:
result = 'i'
elif path in always_Px:
result = 'P'
else:
result = None
return result
def adaptProfileAutoTransitions(l):
# Move automatic transition to the parent
if '▶' in l.get('profile'): # only for 'profile'
split = l.get('profile').split('▶')
transitionExecType = findExecType(split[-1])
if transitionExecType == 'i' and \
split[-1] == l.get('comm'): # if present in 'always_ix' and equals to 'comm'
del split[-1] # delete last automatic transition id
l['profile'] = '▶'.join(split) # identify as parent
l['comm'] = colorize(l.get('comm'), 'Blue') # colorize immediately
binExecType = findExecType(getBaseBin(l.get('name')))
if binExecType == 'i':
l['requested_mask'] += 'i' # mark as possible 'ix' candidate
return l
def isBaseAbstractionTransition(l, profile_):
'''Only for file lines. Must be done after normalization and before adaption. Temporary solution?'''
multiarch = r'(?:[^/]+-linux-(?:gnu|musl)(?:[^/]+)?|@{multiarch})'
proc = r'(?:/proc|@{PROC})'
etc_ro = r'(?:/usr/etc|/etc|@{etc_ro})'
run = r'(?:/var/run|/run|@{run})'
sys = r'(?:/sys|@{sys})'
baseAbsRules = { # re.match
rf'^/dev/log$': {'w'},
rf'^/dev/u?random$': {'r'},
rf'^{run}/uuidd/request$': {'r'},
rf'^{etc_ro}/locale/.+': {'r'},
rf'^{etc_ro}/locale\.alias$': {'r'},
rf'^{etc_ro}/localtime$': {'r'},
rf'^/etc/writable/localtime$': {'r'},
rf'^/usr/share/locale-bundle/.+': {'r'},
rf'^/usr/share/locale-langpack/.+': {'r'},
rf'^/usr/share/locale/.+': {'r'},
rf'^/usr/share/.+/locale/.+': {'r'},
rf'^/usr/share/zoneinfo/.*': {'r'},
rf'^/usr/share/X11/locale/.+': {'r'},
rf'^{run}/systemd/journal/dev-log$': {'w'},
rf'^{run}/systemd/journal/socket$': {'w'},
rf'^{run}/systemd/journal/stdout$': {'r', 'w'},
rf'^/(|usr/)lib(|32|64)/locale/.+': {'m', 'r'},
rf'^/(|usr/)lib(|32|64)/gconv/[^/]+\.so$': {'m', 'r'},
rf'^/(|usr/)lib(|32|64)/gconv/gconv-modules(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib/{multiarch}/gconv/[^/]+\.so$': {'m', 'r'},
rf'^/(|usr/)lib/{multiarch}/gconv/gconv-modules(|[^/]+)$': {'m', 'r'},
rf'^{etc_ro}/bindresvport\.blacklist$': {'r'},
rf'^{etc_ro}/ld\.so\.cache$': {'m', 'r'},
rf'^{etc_ro}/ld\.so\.conf$': {'r'},
rf'^{etc_ro}/ld\.so\.conf\.d/(|[^/]+\.conf)$': {'r'},
rf'^{etc_ro}/ld\.so\.preload$': {'r'},
rf'^/(|usr/)lib(|32|64)/ld(|32|64)-[^/]+\.so$': {'m', 'r'},
rf'^/(|usr/)lib/{multiarch}/ld(|32|64)-[^/]+\.so$': {'m', 'r'},
rf'^/(|usr/)lib/tls/i686/(cmov|nosegneg)/ld-[^/]+\.so$': {'m', 'r'},
rf'^/(|usr/)lib/i386-linux-gnu/i686/(cmov|nosegneg)/ld-[^/]+\.so$': {'m', 'r'},
rf'^/opt/[^/]+-linux-uclibc/lib/ld-uClibc(|[^/]+)so(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib(|32|64)/.+': {'r'},
rf'^/(|usr/)lib(|32|64)/.+\.so(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib/{multiarch}/.+': {'r'},
rf'^/(|usr/)lib/{multiarch}/.+\.so(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib/tls/i686/(cmov|nosegneg)/[^/]+\.so(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib/i386-linux-gnu/i686/(cmov|nosegneg)/[^/]+\.so(|[^/]+)$': {'m', 'r'},
rf'^/(|usr/)lib(|32|64)/\.lib[^/]+\.so[^/]+\.hmac$': {'r'},
rf'^/(|usr/)lib/{multiarch}/\.lib[^/]+\.so[^/]+\.hmac$': {'r'},
rf'^/dev/null$': {'r', 'w',},
rf'^/dev/zero$': {'r', 'w',},
rf'^/dev/full$': {'r', 'w',},
rf'^{proc}/sys/kernel/version$': {'r'},
rf'^{proc}/sys/kernel/ngroups_max$': {'r'},
rf'^{proc}/meminfo$': {'r'},
rf'^{proc}/stat$': {'r'},
rf'^{proc}/cpuinfo$': {'r'},
rf'^{sys}/devices/system/cpu/(|online)$': {'r'},
rf'^{proc}/\d+/(maps|auxv|status)$': {'r'},
rf'^{proc}/crypto/[^/]+$': {'r'},
rf'^/usr/share/common-licenses/.+': {'r'},
rf'^{proc}/filesystems$': {'r'},
rf'^{proc}/sys/vm/overcommit_memory$': {'r'},
rf'^{proc}/sys/kernel/cap_last_cap$': {'r'},
# crypto include (oldest)
rf'^{etc_ro}/gcrypt/random\.conf$': {'r'},
rf'^{proc}/sys/crypto/[^/]+$': {'r'},
rf'^/(etc/|usr/share/)crypto-policies/[^/]+/[^/]+\.txt$': {'r'},
}
result = False
if '▶' in profile_ or isTransitionComm(l.get('comm')): # transition features
path = l.get('path')
pathMasks = l.get('mask')
for regex,mask in baseAbsRules.items():
if 'a' in pathMasks and \
'w' in mask: # 'w' consumes 'a'
mask.add('a')
if re.match(regex, path) and \
pathMasks.issubset(mask):
result = True
break
return result
def findBootId(positionalId):
raise NotImplementedError('Handling previous boot IDs is not yet implemented.')
return None
def grabJournal(args):
if not args.keep_status_audit:
statusTypes = '(?:AVC |USER_AVC )?apparmor="?(ALLOWED|DENIED)'
else:
statusTypes = '(?:AVC |USER_AVC )?apparmor="?(ALLOWED|DENIED|AUDIT)'
disableEpochConvertion = {'__REALTIME_TIMESTAMP': int}
j = journal.Reader(converters=disableEpochConvertion)
if args.boot_id:
hexBootId = findBootId(args.boot_id)
j.this_boot(hexBootId)
else:
j.this_boot()
j.this_machine()
j.add_match('SYSLOG_IDENTIFIER=kernel',
'SYSLOG_IDENTIFIER=audit',
'SYSLOG_IDENTIFIER=dbus-daemon') # try to limit spoofing surface
rawLines = []
for entry in j:
if re.search(statusTypes, entry['MESSAGE']):
rawLines.append(entry)
return rawLines
def isDbusJournalLine(entry):
'''"_SELINUX_CONTEXT" is arbitrary, don't use for higher trusts'''
if entry.get('SYSLOG_IDENTIFIER') == 'dbus-daemon':
result = True
elif entry.get('_SELINUX_CONTEXT') == 'dbus-daemon':
result = True
elif entry.get('_SELINUX_CONTEXT') == 'dbus-daemon (complain)':
result = True
elif entry.get('_SELINUX_CONTEXT') == 'dbus-daemon (complain)\n':
result = True
else:
result = False
return result
def findLogLines(rawLines, args):
toDropDbusKeyValues_inLines = {
'hostname': '?',
'addr': '?',
'terminal': '?',
'exe': '/usr/bin/dbus-daemon',
}
lineDicts = []
latestTimestamp = 0
trusts_byLine = {}
timestamps_byLine = {}
for entry in rawLines:
normalizedLine = normalizeJournalLine(entry['MESSAGE'], args)
processedLine = normalizedLine[0]
trust = normalizedLine[1] # dirty or None, expected to be overwritten further
lineType = findLineType(processedLine)
if lineType.startswith('DBUS'): # drop non-informative DBus data
[processedLine.pop(k) for k,v in toDropDbusKeyValues_inLines.items() if processedLine.get(k) == v]
if not lineType.startswith('DBUS') and isDbusJournalLine(entry): # came from DBus, but not a DBus line
trust = 1
elif entry.get('_AUDIT_TYPE_NAME') == 'USER_AVC':
if not lineType.startswith('DBUS'):
trust = 3
else:
trust = 8
elif entry.get('_AUDIT_TYPE_NAME') == 'AVC':
if isDbusJournalLine(entry) and entry.get('AUDIT_FIELD_BUS') != 'system':
trust = 3
elif entry.get('AUDIT_FIELD_BUS') == 'system':
trust = 9
else:
trust = 10 # not necessarily top trust
elif isDbusJournalLine(entry): # not matched previously gets lower trust
trust = 7
elif lineType.startswith('DBUS'): # what line itself tells
trust = 6
else: # Finished without falling under other conditions
trust = 4
lineId = makeHashable(processedLine)
lineTrust = trusts_byLine.get(lineId)
# Only mark to merge if current trust is no less that 4
if trust <= 3:
processedLine['trust'] = trust # assign immediately to prevent merging
lineId = makeHashable(processedLine) # regenerate the ID
trusts_byLine[lineId] = trust
elif lineTrust: # duplicate line
# Only mark to merge if current trust is higher than trust for previously gathered line
if lineTrust <= trust:
trusts_byLine[lineId] = trust
# Skip reassigning trust for duplicate line if nothing is matched
else: # new line
trusts_byLine[lineId] = trust
timestamps_byLine[lineId] = entry['__REALTIME_TIMESTAMP']
if processedLine in lineDicts:
lineDicts.remove(processedLine) # always use most recent line
lineDicts.append(processedLine)
for l in lineDicts:
lineId = makeHashable(l)
l['timestamp'] = timestamps_byLine.get(lineId)
if trusts_byLine.get(lineId):
l['trust'] = trusts_byLine[lineId]
else: # guard
l['trust'] = 2
if l.get('timestamp') > latestTimestamp:
latestTimestamp = l.get('timestamp')
return (lineDicts, latestTimestamp)