-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ARIAPacketIntelligence.py
2334 lines (1779 loc) · 95 KB
/
ARIAPacketIntelligence.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
import demistomock as demisto
from CommonServerPython import *
import json
import requests
import time
import re
import urllib3
class ParameterError(Exception):
""" Raised when the function parameters do not meet requirements """
pass
"""
Remediation Configuration String (RCS) that use to select SIA.
"""
class RCS:
"""
Define class members
Define class methods
"""
def __init__(self, rcs=None):
self.rcs = rcs
if self.rcs is None:
self.rcs = "PIdevice@all"
"""
Used to indicate which RET types currently supported
"""
self.RET_functions = {"drop": self._parse_RET_drop}
"""
destructor
"""
def __del__(self):
return 0
"""
Parse a drop command and return its representation
for being put into a NRDO action / rule.
"""
def _parse_RET_drop(self, rcs):
if rcs is None:
return None, None
elif rcs == "":
return None, None
rcsp = re.match("^[(][)](.+)$", rcs)
if rcsp is None:
return None, rcs
elif rcsp.group(1) is None:
return "", None
elif rcsp.group(1) == "":
return "", None
RET_drop = ["drop"]
return RET_drop, rcsp.group(1)
"""
Parse a SIA simple name
"""
def _parse_RDL_RD_name(self, rcs):
if rcs is None:
return None, None, "failed: RD name rcs none"
elif rcs == "":
return None, None, "failed: RD name rcs empty"
rcsp = re.match(r"^(\w[\w-]*)(.*)$", rcs)
if rcsp is None:
return None, None, "failed: RD name match none"
elif rcsp.group(1) is None:
return None, None, "failed: RD name none"
elif rcsp.group(1) == "":
return None, None, "failed: RD name empty"
RD_name = ("name", rcsp.group(1))
rcs = rcsp.group(2)
return RD_name, rcs, "success: {0}".format(rcsp.group(1))
"""
Parse a FQN
"""
def _parse_RDL_RD_FQN(self, rcs):
if rcs is None:
return None, None, "failed: RD fqn rcs none"
elif rcs == "":
return None, None, "failed: RD fqn rcs empty"
rcsp = re.match(r"^([<][\w_-<>.]+[>])(.*)$", rcs)
if rcsp is None:
return None, None, "failed: RD fqn match none"
elif rcsp.group(1) is None:
return None, None, "failed: RD fqn none"
elif rcsp.group(1) == "":
return None, None, "failed: RD fqn empty"
RD_fqn = ("FQN", rcsp.group(1))
rcs = rcsp.group(2)
return RD_fqn, rcs, "success: {0}".format(rcsp.group(1))
"""
Parse a security domain name SDN
"""
def _parse_RDL_RD_SDN(self, rcs):
if rcs is None:
return None, None, "failed: RD sd rcs none"
elif rcs == "":
return None, None, "failed: RD sd rcs empty"
rcsp = re.match(r"^\^(\w[\w-]*)(.*)$", rcs)
if rcsp is None:
return None, None, "failed: RD sd match none"
elif rcsp.group(1) is None:
return None, None, "failed: RD sd none"
elif rcsp.group(1) == "":
return None, None, "failed: RD sd empty"
RD_sdn = ("securityDomain", rcsp.group(1))
rcs = rcsp.group(2)
return RD_sdn, rcs, "success: {0}".format(rcsp.group(1))
"""
Parse an RGN label as a name
"""
def _parse_RDL_RD_RGN_name(self, rcs):
if rcs is None:
return None, None, "failed: RD rgn name rcs none"
elif rcs == "":
return None, None, "failed: RD rgn name rcs empty"
rcsp = re.match(r"^(\w[\w-]*)(.*)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn name rcsp none"
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn name rcsp.g1 none"
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn name rcsp.g1 empty"
return rcsp.group(1), rcsp.group(2), "success"
"""
Parse an RGN label as a list of names
"""
def _parse_RDL_RD_RGN_list(self, rcs):
if rcs is None:
return None, None, "failed: RD rgn list rcs none"
elif rcs == "":
return None, None, "failed: RD rgn list rcs empty"
rcsp = re.match(r"^[(](.+)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn list rcsp none"
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn list rcsp.g1 none"
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn list rcsp.g1 empty"
rcs = rcsp.group(1)
names = ""
while True:
rcsp = re.match(r"(\w[\w-]*)(.+)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn list rcsp name none"
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn list rcsp.g1 name none"
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn list rcsp.g1 name empty"
names = "{0}{1}".format(names, rcsp.group(1))
rcs = rcsp.group(2)
if rcs is None:
return None, None, "failed: RD rgn list rcsp.g2 name none"
elif rcs == "":
return None, None, "failed: RD rgn list rcsp.g2 name empty"
rcsp = re.match("^[)](.*)$", rcs)
if rcsp is not None:
rcs = rcsp.group(1)
break
rcsp = re.match("^,(.+)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn list rcsp comma none"
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn list rcsp.g1 comma none"
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn list rcsp.g1 comma empty"
rcs = rcsp.group(1)
names = "{},".format(names)
if names == "":
return None, None, "failed: RD rgn list names empty"
names = "({})".format(names)
return names, rcs, "success: {0}".format(names)
"""
Parse an RGN label as asterik
"""
def _parse_RDL_RD_RGN_asterik(self, rcs):
if rcs is None:
return None, None, "failed: RD rgn asterik rcs none"
elif rcs == "":
return None, None, "failed: RD rgn asterik rcs empty"
rcsp = re.match(r"^\*(.*)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn asterik rcsp none"
return "*", rcsp.group(1), "success"
"""
Parse an RGN
"""
def _parse_RDL_RD_RGN_label(self, rcs):
if rcs is None:
return None, None, "failed: RD label rgn rcs none"
elif rcs == "":
return None, None, "failed: RD label rgn rcs empty"
while True:
rcsp = re.match(r"^[!]([(].*)$", rcs)
if rcsp is not None:
if rcsp.group(1) is None:
return None, None, "failed: RD rgn label exclusive g1 none"
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn label exclusive g1 empty"
rcs = rcsp.group(1)
label, rcs, msg = self._parse_RDL_RD_RGN_list(rcs)
if label is None:
return None, None, "failed: RD rgn label exclusive none {0}".format(msg)
elif label == "":
return None, None, "failed: RD rgn label exclusive empty {0}".format(msg)
label = "!{0}".format(label)
break
rcsp = re.match(r"^[(].*$", rcs)
if rcsp is not None:
label, rcs, msg = self._parse_RDL_RD_RGN_list(rcs)
if label is None:
return None, None, "failed: RD rgn label inclusive none {0}".format(msg)
elif label == "":
return None, None, "failed: RD rgn label inclusive empty {0}".format(msg)
break
rcsp = re.match(r"^\*.*$", rcs)
if rcsp is not None:
label, rcs, msg = self._parse_RDL_RD_RGN_asterik(rcs)
if label is None:
return None, None, "failed: RD rgn label asterik none {0}".format(msg)
elif label == "":
return None, None, "failed: RD rgn label asterik empty {0}".format(msg)
break
rcsp = re.match(r"^[\w].*$", rcs)
if rcsp is not None:
label, rcs, msg = self._parse_RDL_RD_RGN_name(rcs)
if label is None:
return None, None, "failed: RD rgn label name none {0}".format(msg)
elif label == "":
return None, None, "failed: RD rgn label name empty {0}".format(msg)
break
return None, None, "failed: RD rgn label invalid"
return label, rcs, "success"
"""
Parse an RGN
"""
def _parse_RDL_RD_RGN(self, rcs):
if rcs is None:
return None, None, "failed: RD rgn rcs none"
elif rcs == "":
return None, None, "failed: RD rgn rcs empty"
region, rcs, msg = self._parse_RDL_RD_RGN_label(rcs)
if region is None:
return None, None, "failed: RD rgn region none {0}".format(msg)
elif region == "":
return None, None, "failed: RD rgn region empty {0}".format(msg)
elif rcs is None:
return None, None, "failed: RD rgn region rcs none {0}".format(msg)
elif rcs == "":
return None, None, "failed: RD rgn region rcs empty {0}".format(msg)
rcsp = re.match(r"^\.(.+)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn region rcsp none ."
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn region rcsp.g1 none ."
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn region rcsp.g1 empty ."
rcs = rcsp.group(1)
group, rcs, msg = self._parse_RDL_RD_RGN_label(rcs)
if group is None:
return None, None, "failed: RD rgn group none {0}".format(msg)
elif group == "":
return None, None, "failed: RD rgn group empty {0}".format(msg)
elif rcs is None:
return None, None, "failed: RD rgn group rcs none {0}".format(msg)
elif rcs == "":
return None, None, "failed: RD rgn group rcs empty {0}".format(msg)
rcsp = re.match(r"^\.(.+)$", rcs)
if rcsp is None:
return None, None, "failed: RD rgn group rcsp none ."
elif rcsp.group(1) is None:
return None, None, "failed: RD rgn group rcsp.g1 none ."
elif rcsp.group(1) == "":
return None, None, "failed: RD rgn group rcsp.g1 empty ."
rcs = rcsp.group(1)
name, rcs, msg = self._parse_RDL_RD_RGN_label(rcs)
if name is None:
return None, None, "failed: RD rgn name none {0}".format(msg)
elif name == "":
return None, None, "failed: RD rgn name empty {0}".format(msg)
RGN = ("RGN", "{0}.{1}.{2}".format(region, group, name))
return RGN, rcs, "success"
"""
parse the RDL component of the RCS:
RDL :: PIdevice@[<RD><RD_LIST>*]
RD :: name | SDN | RGN | FQN
RD_LIST :: , <RD>
"""
def _parse_RDL(self, rcs):
if rcs is None:
return None, None, "failed: rcs is none"
elif rcs == "":
return None, None, "failed: rcs is empty"
rcsp = re.match("^PIdevice@(.*)$", rcs)
if rcsp is None:
return None, rcs, "failure: invalid keyword"
RDL_all = ("RGN", "all.all.all")
RDL = []
if rcsp.group(1) is None:
return RDL_all, None, "success: all (none)"
elif rcsp.group(1) == "":
return RDL_all, None, "success: all (empty)"
rcs = rcsp.group(1)
while True:
if rcs is None:
break
elif rcs == "":
break
while True:
rcsp = re.match(r"^all\..+$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: RGN (all-none)"
RD, rcs, msg = self._parse_RDL_RD_RGN(rcs)
if RD is None:
return None, None, "failure: RGN (all-obj) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: RGN (all len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^all(.*)$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: all (empty)"
RDL.append(RDL_all)
rcs = rcsp.group(1)
break
rcsp = re.match(r"^\^.*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: SD (none)"
RD, rcs, msg = self._parse_RDL_RD_SDN(rcs)
if RD is None:
return None, None, "failure: SD (empty) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: SD (len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^[<].*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: FQN (empty)"
RD, rcs, msg = self._parse_RDL_RD_FQN(rcs)
if RD is None:
return None, None, "failure: FQN (obj) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: FQN (len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^[!].*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: RGN (exclusive-none)"
RD, rcs, msg = self._parse_RDL_RD_RGN(rcs)
if RD is None:
return None, None, "failure: RGN (exclusive-obj) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: RGN (exclusive len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^[(].*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: RGN (inclusive-none)"
RD, rcs, msg = self._parse_RDL_RD_RGN(rcs)
if RD is None:
return None, None, "failure: RGN (inclusive-none) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: RGN (inclusive len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^\*\..*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: RGN (asterik-none)"
RD, rcs, msg = self._parse_RDL_RD_RGN(rcs)
if RD is None:
return None, None, "failure: RGN (asterik-none) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: RGN (asterik len != 2) {0}".format(msg)
RDL.append(RD)
break
rcsp = re.match(r"^\*(.*)$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: asterik (empty)"
RDL.append(RDL_all)
rcs = rcsp.group(1)
break
rcsp = re.match(r"^[\w].*$", rcs)
if rcsp is None:
return None, None, "failure: name should be there"
rcsp = re.match(r"^\w[\w-]*\..*$", rcs)
if rcsp is not None:
if rcsp.group(0) == "":
return None, None, "failure: name RGN (none)"
RD, rcs, msg = self._parse_RDL_RD_RGN(rcs)
if RD is None:
return None, None, "failure: RGN name (obj) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: RGN name (len != 2) {0}".format(msg)
RDL.append(RD)
break
RD, rcs, msg = self._parse_RDL_RD_name(rcs)
if RD is None:
return None, None, "failure: NAME (obj) {0}".format(msg)
elif len(RD) != 2:
return None, None, "failure: NAME (len != 2) {0}".format(msg)
RDL.append(RD)
break
if rcs is None:
break
elif rcs == "":
break
rcsp = re.match("^,(.*)$", rcs)
if rcsp is None:
return None, None, "failure: RDL , obj (none)"
elif rcsp.group(1) is None:
return None, None, "failure: RDL , (none)"
elif rcsp.group(1) == "":
return None, None, "failure: RDL , (empty)"
rcs = rcsp.group(1)
if rcs is not None:
if rcs != "":
return None, "", "failure: RCS ended-!empty"
if len(RDL) <= 0:
return None, None, "failure: RDL empty"
return RDL, None, "success: {0}".format(len(RDL))
"""
parse the RET component of the RCS:
RET :: Remediation@<ret><ret_list>
ret :: drop() | alert(...) | redirect(...) | serviceChain(...)
SDN_LIST :: , <ret>
"""
def _parse_RET(self, rcs):
if rcs is None:
return None, None, "failure: RET RCS none"
elif rcs == "":
return None, None, "failure: RET RCS empty"
rcsp = re.match("^Remediation@(.+)$", rcs)
if rcsp is None:
rcs = "Remediation@drop()${0}".format(rcs)
rcsp = re.match("^Remediation@(.+)$", rcs)
if rcsp is None:
return None, rcs, "failure: RET failed insert drop()"
if rcsp.group(1) is None:
return None, None, "failuure: RET obj none"
elif rcsp.group(1) == "":
return None, None, "failure: RET obj empty"
rcs = rcsp.group(1)
RET = []
rcsp = re.match(r"^\$(.+)$", rcs)
if rcsp is not None:
rcs = "drop(){0}".format(rcs)
while True:
rcsp = re.match(r"(\w[\w]*)([(].+\$.+)$", rcs)
if rcsp is None:
return None, None, "failure: RET obj type none"
elif rcsp.group(1) is None:
return None, None, "failure: RET type none"
elif rcsp.group(1) == "":
return None, None, "failure: RET type empty"
RET_parse_func = self.RET_functions.get(rcsp.group(1))
if RET_parse_func is None:
return None, None, "failure: RET type not found"
rcs = rcsp.group(2)
if rcs is None:
return None, None, "failure: RET RCS none"
elif rcs == "":
return None, None, "failure: RET RCS empty"
"""
call the RET type parser to create
its PI object.
"""
obj, rcs = RET_parse_func(rcs)
if obj is None:
return None, None, "failure: RET func obj none"
elif len(obj) < 1:
return None, None, "failure: RET func obj 1 or greater"
elif rcs is None:
return None, None, "failure: RET func RCS none"
elif rcs == "":
return None, None, "failure: RET func RCS empty"
RET.append(obj)
rcsp = re.match(r"^\$(.+)$", rcs)
if rcsp is not None:
break
rcsp = re.match(r"^,(\w[\w]*[(].+\$.+)$", rcs)
if rcsp is None:
return None, None, "failure: RET next obj none"
elif rcsp.group(1) is None:
return None, None, "failure: RET next none"
elif rcsp.group(1) == "":
return None, None, "failure: RET next empty"
rcs = rcsp.group(1)
if rcs is None:
return None, None, "failure: RET end RCS none"
elif rcs == "":
return None, None, "failure: RET end RCS empty"
rcsp = re.match(r"^\$(.+)$", rcs)
if rcsp is None:
return None, None, "failure: RET # remove none"
elif rcsp.group(1) is None:
return None, None, "failure: RET # remove group none"
elif rcsp.group(1) == "":
return None, None, "failure: RET # remove group empty"
rcs = rcsp.group(1)
if len(RET) <= 0:
return None, None, "failure: RET list empty"
return RET, rcs, "success: {0}".format(len(RET))
"""
parse the SDL component of the RCS:
SDL :: securityDomain@<SDN><SDN_LIST>*
SDN :: <a-zA-Z0-9_><a-zA-Z0-9_>*
SDN_LIST :: , <SDN>
"""
def _parse_SDL(self, rcs):
if rcs is None:
return None, None, "failure: RCS is none"
elif rcs == "":
return None, None, "failure: RCS is empty"
rcsp = re.match("^securityDomain@(.+)$", rcs)
if rcsp is None:
SDL = ["all"]
return SDL, rcs, "success: 1"
if rcsp.group(1) is None:
return None, None, "failure: none"
elif rcsp.group(1) == "":
return None, None, "failure: empty"
rcs = rcsp.group(1)
SDL = []
while True:
rcsp = re.match(r"(\w[\w-]*)(.*\$.+)$", rcs)
if rcsp is None:
return None, None, "failure: bad SDN"
elif rcsp.group(1) is None:
return None, None, "failure: SDN none"
elif rcsp.group(1) == "":
return None, None, "failure: SDN empty"
SDL.append(rcsp.group(1))
rcs = rcsp.group(2)
if rcs is None:
return None, None, "failure: SDN no more RCS none"
elif rcs == "":
return None, None, "failure: SDN no more RCS empty"
rcsp = re.match(r"^\$(.+)$", rcs)
if rcsp is not None:
break
rcsp = re.match(r"^,(\w[\w-]*.*\$.+)$", rcs)
if rcsp is None:
return None, None, "failure: SDN obj advance none"
elif rcsp.group(1) is None:
return None, None, "failure: SDN advance none"
elif rcsp.group(1) == "":
return None, None, "failure: SDN advance empty"
rcs = rcsp.group(1)
if rcs is None:
return None, None, "failure: SDL RCS none"
elif rcs == "":
return None, None, "failure: SDL RCS empty"
rcsp = re.match(r"^\$(.+)$", rcs)
if rcsp is None:
return None, None, "failure: SDL # remove none"
elif rcsp.group(1) is None:
return None, None, "failure: SDL # remove group none"
elif rcsp.group(1) == "":
return None, None, "failure: SDL # remove group empty"
rcs = rcsp.group(1)
if len(SDL) <= 0:
return None, None, "failure: SDL list empty"
return SDL, rcs, "success: {0}".format(len(SDL))
"""
Parse out the components of the RCS: [SDL] | [RET] | RDL
and return all three. If the optional component is
not found then it returns None but if keyword is
found nothing else then returns empty.
The fourth result returned is if there is remaining characters
in the original RCS then its returned.
Returns info in fifth result
"""
def _parse(self, rcs):
if rcs is None:
return None, None, None, None, "failed: RCS is none"
elif rcs == "":
return None, None, None, None, "failed: RCS is empty"
rcsp = re.search(" ", rcs)
if rcsp is not None:
return None, None, None, None, "failed: space character found in RCS"
SDL, rcs_next, msg = self._parse_SDL(rcs)
if SDL is not None:
if len(SDL) <= 0:
return None, None, None, None, "failed: SDL returned but is empty (msg={0})".format(msg)
if rcs_next is None:
return SDL, None, None, None, "failed: RCS invalid parse after SDL (none) (msg={0})".format(msg)
elif rcs_next == "":
return SDL, None, None, None, "failed: RCS invalid parse after SDL (empty) (msg={0})".format(msg)
RET, rcs_next, msg = self._parse_RET(rcs_next)
if RET is None:
return SDL, None, None, None, "failed: RET is none (msg={0})".format(msg)
elif len(RET) <= 0:
return SDL, None, None, None, "failed: RET is empty (msg={0})".format(msg)
elif rcs_next is None:
return SDL, RET, None, None, "failed: RCS invalid parse after RET (none) (msg={0})".format(msg)
elif rcs_next == "":
return SDL, RET, None, None, "failed: RCS invalid parse after RET (none) (msg={0})".format(msg)
RDL, rcs_next, msg = self._parse_RDL(rcs_next)
if RDL is None:
return SDL, RET, None, None, "failed: RDL is none (msg={0})".format(msg)
elif len(RDL) <= 0:
return SDL, RET, None, None, "failed: RDL is empty (msg={0})".format(msg)
elif rcs_next is not None:
if rcs_next != "":
return SDL, RET, RDL, None, "failed: RCS invalid parse after RDL (not empty) (msg={0})".format(msg)
return SDL, RET, RDL, rcs_next, "success"
"""
Returns true if the RCS provided at object instantiation
time is a valid RCS value, otherwise it returns false.
"""
def _valid(self, rcs):
if rcs is None:
return False
# rcs_save = rcs
SDL, RET, RDL, rcs, rmsg = self._parse(rcs)
if RDL is None:
# print("ARIA: remediation configuraton string (RCS) is invalid -- this will prevent remediation
# to ARIA PI devices from working (rcs={0}:: rmsg={1})".format(rcs_save, rmsg))
return False
# print("ARIA: remediation configuraton string (RCS) is valid (rcs={0})".format(rcs_save))
return True
"""
Returns true if the RCS provided at object instantiation
time is a valid RCS value, otherwise it returns false.
"""
def valid(self):
if not self._valid(self.rcs):
return False
return True
"""
Allows setting the RCS string to act on, this will only
set it if the string is already empty. Otherwise it
should use modify.
"""
def set(self, rcs):
if self.rcs is None:
if not self._valid(rcs):
return False
self.rcs = rcs
else:
return False
return True
"""
Allows changing the RCS string after its been previsouly
set or not.
"""
def modify(self, rcs):
if not self._valid(rcs):
return False
self.rcs = rcs
return True
"""
Assuming the securtiy domain component of the RCS is valid
then it returns the parsed out security domain component if
it exists. It will return it as a list of security domain
object name strings.
If there is an error in parsing the security domain component
null is returned. If it was not provided then "all"
list is returned.
"""
def security_domain(self):
SDL, RET, RDL, rcs, rmsg = self._parse(self.rcs)
if RDL is None:
return None, False
return SDL, True
"""
Assuming the RCS is valid
then return the RDL
If there is an error in parsing the RDL component is
returned as a NULL.
"""
def remediation_device_list(self):
SDL, RET, RDL, rcs, rmsg = self._parse(self.rcs)
if RDL is None:
return None, False
return RDL, True
"""
Assuming the securtiy domain component of the RCS is valid
Assuming the RCS is valid
then return the remediation action instruction.
If there is an error in parsing the RET component is
returned as a NULL.
"""
def remediation_instruction(self):
SDL, RET, RDL, rcs, rmsg = self._parse(self.rcs)
if RDL is None:
return None, False
return RET, True
class ARIA(object):
def __init__(self, sdso_url: str, verify_cert: bool = True):
self.sdso_url = sdso_url
self.time_out = 20
self.verify_cert = verify_cert
"""HELPER FUNCTION"""
@staticmethod
def _build_alert_instruction(transport_type: str, tti_index: int, aio_index: int,
trigger_type: str, trigger_value: int) -> str:
""" Create an alert instruction
Args:
transport_type: The type of notification to generate.
Valid values are 'email', 'SMS', 'syslog' or 'webhook'.
tti_index: The index of the entry in the transport type table.
aio_index: The index of the entry in the alert information object table.
trigger_type: The frequency of the alert. Valid values are 'one-shot', 're-trigger-count',
're-trigger-timed-ms' or 're-trigger-timed-sec'.
trigger_value: The threshold that must be met before the alert is triggered.
Returns: Alert instruction string.
Raises:
ValueError: If parameters are out of range or not in the type list.
"""
transport_type_list = ['email', 'SMS', 'syslog', 'webhook']
if transport_type not in transport_type_list:
raise ValueError(f'Wrong transport_type {transport_type}! Valid values are email, SMS, syslog or webhook')
if tti_index > 7 or tti_index < 0:
# This is an ARIA PI Reaper production requirement
raise ValueError('Transport type info index(tti_index) out of range! '
'Valid value must be in the range [0, 7].')
if aio_index > 15 or aio_index < 0:
# This is an ARIA PI Reaper production requirement
raise ValueError('Alert info object index(aio_index) out of range! '
'Valid value must be in range [0, 15]')
trigger_type_list = ['one-shot', 're-trigger-count', 're-trigger-timed-ms', 're-trigger-timed-sec']
if trigger_type not in trigger_type_list:
# This is an ARIA PI Reaper production requirement
raise ValueError(f'Wrong trigger_type {trigger_type}! Valid values are one-shot, re-trigger-count, '
're-trigger-timed-ms, re-trigger-timed-sec')
if trigger_value < 1 or trigger_value > 8191:
# This is an ARIA PI Reaper production requirement
raise ValueError('Trigger value(trigger_value) out of range! It must be in range [1, 8191]')
instruction = f'ALERT {transport_type} {tti_index} {aio_index} {trigger_type} {trigger_value}'
return instruction
@staticmethod
def _process_port_range(port_range: str = None) -> str:
""" Validation function for range of ports
Args:
port_range: The source or destination port(s). This accepts a
comma-separated list (e.g., “1, 3”), a range (e.g., “1-3”), or a combination (e.g., “1, 3-5”).
Returns: The string of port_range.
Raises:
ValueError: If port_range is out of range 0-65535 or in wrong format.
"""
if not port_range:
port_range = '0-65535' # default port_range value
split_port_range = port_range.replace(' ', '').split(',')
res = ''
for port in split_port_range:
if res:
res = res + ', '
if '-' in port:
beg, end = port.replace(' ', '').split('-')
for j in beg, end:
if int(j) < 0 or int(j) > 65535:
raise ValueError('Port must be in 0-65535!')
if int(beg) > int(end):
raise ValueError('Wrong port range format!')
res += beg + ' - ' + end
else:
if int(port) < 0 or int(port) > 65535:
raise ValueError('Port must be in 0-65535!')
res += port
return res
@staticmethod
def _process_ip_address(ip: str) -> str:
""" Validation function for IP address
Args:
ip: The IP address and mask of the IP address, in the format <IP_address>/<mask>. If the mask is omitted,
a value of 32 is used.
Returns: String of IP address.
Raises:
ValueError: If the netmask is out of range or IP address is not expressed in CIDR notation
"""
netmask = '32'
ip_str = ip.replace(' ', '')
if '/' in ip_str:
ip_addr, netmask = ip_str.split('/')
else:
ip_addr = ip_str
if int(netmask) > 32 or int(netmask) < 1:
raise ValueError('Subnet mask must be in range [1, 32].')
ip_addr_split = ip_addr.split('.')
for syllable in ip_addr_split:
if int(syllable) < 0 or int(syllable) > 255:
raise ValueError('Wrong IP format!')
if len(ip_addr_split) != 4:
raise ValueError('Wrong IP format!')
res = ip_addr + '/' + netmask
return res