-
Notifications
You must be signed in to change notification settings - Fork 1
/
so-elastalert-create-whiptail
executable file
·1242 lines (1030 loc) · 43.3 KB
/
so-elastalert-create-whiptail
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
#! /bin/bash -i
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################
# Dialog box color scheme #
################################
export NEWT_COLORS='
window=,white
border=white,red
textbox=white
button=white
'
#############################
# Rule Name #
#############################
rule_name_prompt()
{
rulename=$(whiptail --inputbox "\nThe rulename will be used to filter alerts in kibana.\n\nWhat do you want to name the rule?" 10 60 --title "Rule Name" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
# Replace spaces in user input with underscore
rulename=$(echo "$rulename" | sed 's/ /_/g')
cat << EOF >> $rulename.yaml
# Elasticsearch Host
es_host: elasticsearch
es_port: 9200
# (Required)
# Rule name, must be unique
name: $rulename
EOF
else
exit
fi
}
#############################
# Index Name #
#############################
index_name_prompt()
{
indexname=$(whiptail --inputbox "\nWhat elasticsearch index do you want to use? Below are the default Index Patterns used in Security Onion: \n *:logstash-* \n *:logstash-beats-* \n *:elastalert_status*" 12 78 --title "Index Name" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Index to search, wildcard supported
index: "$indexname"
EOF
else
exit
fi
}
#############################
# Alert Options #
#############################
alert_option_prompt()
{
alertoption=$(whiptail --title "Alert Options" --radiolist \
"\nBy default, all matches will be written back to the elastalert index. \nPlease choose from the below options:" 15 100 4 \
"debug" "Will write alerts to the *:elastalert_status* index" ON \
"slack" "Send a notification to a predefined Slack channel" OFF \
"email" "Connect to an smtp server located at smtp_host, or localhost by default." OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
if [ ${alertoption,,} = "slack" ] ; then
slackoption=$(whiptail --inputbox "\nThe webhook URL that includes your auth data and the ID of the channel (room) you want to post to.\n\nGo to the Incoming Webhooks section in your Slack account https://XXXXX.slack.com/services/new/incoming-webhook,\nchoose the channel, click ‘Add Incoming Webhooks Integration’ and copy the resulting URL.\n\nPlease enter the webhook URL below:" 20 85 --title "Slack Alerter Option" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# The alert is use when a match is found
alert:
- slack
# (required,Slack specific)
# Enter the webhook URL below
slack:
- $slackoption
EOF
else
exit
fi
elif [ ${alertoption,,} = "email" ] ; then
emailoption=$(whiptail --inputbox "\nPlease enter the email address you want to send the alerts to.\nNote: Ensure the Master Server is configured for SMTP." 15 85 --title "Slack Alerter Option" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# The alert is use when a match is found
alert:
- email
# (required, email specific)
# a list of email addresses to send alerts to
email:
- $emailoption
EOF
else
exit
fi
elif [ ${alertoption,,} = "debug" ] ; then
whiptail --title "debug Alerter Options" --msgbox "\nUsing the default alert type of debug.\nAlerts will only be written to the *:elastalert_status* index." 10 90
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# The alert is use when a match is found
alert:
- debug
EOF
else
exit
fi
fi
else
exit
fi
}
#############################
# Filter Options #
#############################
filter_options_prompt()
{
filteroption=$(whiptail --title "Filter Options" --radiolist \
"\nThis script will allow you to generate a BASIC filter for your rule. To reduce the number of false positives you may need to modify the filter." 15 100 4 \
"default" "Uses a wildcard search that will include all logs for the $indexname index." ON \
"term" "Allows you to match a value in a field." OFF \
"wildcard" "Allows you to use the wildcard * in the field_value." OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
if [ ${filteroption,,} = "default" ] ; then
whiptail --title "debug Alerter Options" --msgbox "\nUsing default filter options that will include all logs for the $indexname index." 10 90
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
#(Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- wildcard:
event_type: "*"
EOF
else
exit
fi
elif [ ${filteroption,,} = "term" ] ; then
field_name=$(whiptail --inputbox "\nThis option allows you to match a specific value in a field.\nFor example field name: source_ip field value: 192.168.1.1 or choose a specific log type you want to rule to apply ie. field name: event_type field value: bro_http \n\nWhat field do you want to filter on?" 15 90 --title "Term Filter Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 1 ] ; then
exit
else
field_value=$(whiptail --inputbox "\nWhat is the value for the $field_name field." 8 65 --title "Term Filter Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
#(Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query$
filter:
- term:
$field_name: "$field_value"
EOF
else
exit
fi
fi
elif [ ${filteroption,,} = "wildcard" ] ; then
field_name=$(whiptail --inputbox "\nWildcard filters allow you to use the wildcard * in the field_value to match unknown characters or to include the remaining values of a field. For example field_name: useragent and field_value: *Mozilla* or field_name: source_ip and field_value: 192.168.*\n\nWhat field do you want to search on?" 15 90 --title "Wildcard Filter Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 1 ] ; then
exit
else
field_value=$(whiptail --inputbox "\nWhat is the value for the $field_name field." 10 65 --title "Wildcard Filter Options" 3>&1 1>&2 2>&3)
cat << EOF >> $rulename.yaml
#(Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- wildcard:
$field_name: "$field_value"
EOF
fi
fi
else
exit
fi
}
############################
# Re-alert Options #
############################
realert_prompt()
{
if (whiptail --yesno "\nThe realert option allows you to ignore repeating alerts for a given period of time.\nWould you like to set a realert timeframe?" 15 90 --title "ReAlert Option") then
realert_unit_of_measure=$(whiptail --title "Realert Options" --radiolist \
"\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of weeks. " ON \
"days" "Number of days. " OFF \
"hours" "Number of hours. " OFF \
"minutes" "Number of minutes. " OFF \
"seconds" "Number of seconds. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
realert_timeframe=$(whiptail --inputbox "\nPlease enter the number of $realert_unit_of_measure you want to supress alerts." 10 65 --title "ReAlert Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# This option allows you to ignore repeating alerts for a period of time.
realert:
$realert_unit_of_measure: $realert_timeframe
EOF
else
exit
fi
else
exit
fi
fi
}
#######################
# Final prompt #
#######################
final_prompt()
{
current_directory=$(pwd)
whiptail --title "Final Prompt" --msgbox "\nWriting rule to the following location:\n\n $current_directory/$rulename.yaml" 10 50
}
###################################
# Functions for Cardinality Rules #
###################################
cardinality_rule_prompt()
{
cardinality_field=$(whiptail --inputbox "\nThe Cardinality rule will alert when the maximum or minimum number of unique values for a given field reaches a threshold.\nThe Cardinality field is the field to count the cardianality for.\n\nWhat field do you want to be the Cardinality Field?" 15 90 --title "Cardinality Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# The Cardinality rule matches when the total number of unique values for a certain field , within a given timeframe is higher or lower than a threshold.
type: cardinality
# (Required, cardinality specific)
# Count the number of unique value for this field
cardinality_field: $cardinality_field
EOF
else
exit
fi
cardinality_max_min=$(whiptail --title "Cardinality Rule Options" --radiolist \
"\nThis rule requires one of the two following options.\n\nPlease choose from the options below:\n\n" 20 115 4 \
"maximum_cardinality" "Alert on values MORE than X unique values in the cardinality field." ON \
"minimum_cardinality" "Alert on values LESS than X unique values in the cardinality field." OFF \
"finish" "Continue building the rule." OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
if [ $cardinality_max_min = "maximum_cardinality" ] ; then
cardinality_max=$(whiptail --inputbox "\nPlease enter the maximum cardinality value." 10 50 --title "Cardinality Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required, frequency specific)
# Alert when there is more than X unique values
max_cardinality: $cardinality_max
EOF
else
exit
fi
elif [ $cardinality_max_min = "minimum_cardinality" ] ; then
cardinality_min=$(whiptail --inputbox "\nPlease enter the minimum cardinality value." 10 50 --title "Cardinality Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required, frequency specific)
# Alert when there is less than X unique values
min_cardinality: $cardinality_min
EOF
else
exit
fi
fi
else
exit
fi
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe Cardinality timeframe is defined as the number of unique values in the most recent X timeframe.\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of events per week. " ON \
"days" "Number of events per day. " OFF \
"hours" "Number of events per hour. " OFF \
"minutes" "Number of events per minute. " OFF \
"seconds" "Number of events per second. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required, frequency specific)
# The cardinality is defined as the number of unique values for the most recent 4 hours
timeframe:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
if (whiptail --yesno "\nThe query_key groups counts by this field. For each unique value of the query_key field, cardinality will be counted separately.\n\nWould you like to set the query_key parameter?" 8 78 --title "Query Key Option") then
query_key=$(whiptail --inputbox "\nWhat field do you want the query_key to be?" 15 65 --title "Query Key Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Optional, frequency specific)
# query_key: Group cardinality counts by this field. For each unique value of the query_key field, cardinality will be counted separately.
query_key: $query_key
EOF
else
exit
fi
fi
}
#################################
# Functions for Blacklist Rules #
#################################
blacklist_rule_prompt()
{
compare_key=$(whiptail --inputbox "\nThe blacklist rule will compare the values contained in a text file against the compare_key and alert if there is a match.\n\nWhat field do you want to compare to the blacklist?" 15 65 --title "Blacklist rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# The Blacklist rule will check a certain field against a blacklist and match if it is in the blacklist
type: blacklist
# (Required, blacklist)
# The name of the field to use to compare to the blacklist. If the field is null, those events will be ignored.
compare_key: $compare_key
EOF
else
exit
fi
if (whiptail --yesno "\nThe blacklist file should be a text file with a single value per line. Note: The file needs to be accessible by the so-elastalert container.\n\nWould you like to create the file now?" 10 78 --title "Blacklist Option") then
file_location=$(whiptail --inputbox "\nPlease enter the full path AND filename of the blacklist.\n Note: The filepath will be defined in the $rulename.yaml file." 10 65 --title "Blacklist File Location" 3>&1 1>&2 2>&3)
counter=0
while [ $counter -eq 0 ] ; do
file_value=$(whiptail --inputbox "\nPlease enter the values for the $compare_key one at a time.\n\nWhen finished type quit." 15 65 --title "Blacklist values" 3>&1 1>&2 2>&3)
if [ ${file_value,,} = "quit" ] ; then
counter=1
else
echo $file_value >> $file_location
counter=0
fi
done
cat << EOF >> $rulename.yaml
# (Required, blacklist)
# A list of blacklisted values, and/or a list of paths to flat files which contain the blacklisted values
blacklist:
- "!file $file_location"
EOF
else
file_location=$(whiptail --inputbox "\nPlease enter the full path AND filename of the blacklist." 15 65 --title "Blacklist File Location" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required, blacklist)
# A list of blacklisted values, and/or a list of paths to flat files which contain the blacklisted values
blacklist:
- "!file $file_location"
EOF
else
exit
fi
fi
}
#################################
# Functions for Whitelist Rules #
#################################
whitelist_rule_prompt()
{
compare_key=$(whiptail --inputbox "\nThe whitelist rule will compare the values contained in a text file against the compare_key and alert if there is NOT a match.\n\nWhat field do you want to compare to the whitelist?" 15 65 --title "Whitelist rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# The Whitelist rule will check a certain field against a whitelist and match if it is not in the whitelist
type: whitelist
# (Required, wlacklist)
# The name of the field to use to compare to the wlacklist. If the field is null, those events will be ignored.
compare_key: $compare_key
EOF
else
exit
fi
if (whiptail --yesno "\nThe whitelist file should be a text file with a single value per line. Note: The file needs to be accessible by the so-elastalert container.\n\nWould you like to create the file now?" 10 78 --title "Whitelist Rule Option") then
file_location=$(whiptail --inputbox "\nPlease enter the full path AND filename of the whitelist.\nNote: The file path will be placed in the $rulename.yaml file." 10 65 --title "Whitelist File Location" 3>&1 1>&2 2>&3)
counter=0
while [ $counter -eq 0 ] ; do
file_value=$(whiptail --inputbox "\nPlease enter the values for the $compare_key one at a time.\n\nWhen finished type quit." 15 65 --title "Whitelist values" 3>&1 1>&2 2>&3)
if [ ${file_value,,} = "quit" ] ; then
counter=1
else
echo $file_value >> $file_location
counter=0
fi
done
cat << EOF >> $rulename.yaml
# (Required, whitelist)
# A list of whitelisted values, and/or a list of paths to flat files which contain the whitelisted values
whitelist:
- "!file $file_location"
EOF
else
file_location=$(whiptail --inputbox "\nPlease enter the full path AND filename of the whitelist." 15 65 --title "Whitelist File Location" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required, whitelist)
# A list of whitelisted values, and/or a list of paths to flat files which contain the whitelisted values
whitelist:
- "!file $file_location"
EOF
else
exit
fi
fi
if (whiptail --yesno "\nWould you like to ignore null field values?" 10 50 --title "Whitelist Additional Options") then
cat << EOF >> $rulename.yaml
# (Required, whitelist)
# ignore_null: If true, events without a compare_key field will not match.
ignore_null: true
EOF
else
cat <<EOF>> $rulename.yaml
# (Required, whitelist)
# ignore_null: If true, events without a compare_key field will not match.
ignore_null: false
EOF
fi
}
###################################
# Functions for Frequency Rules #
###################################
frequency_rule_prompt()
{
num_events=$(whiptail --inputbox "\nThe Frequency rule matches when there are at least a certain number of events in a given timeframe.\n\nEnter the number of events you want to alert on:" 10 85 --title "Frequency Rule" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe Frequency timeframe is defined as the number of matches in the most recent X timeframe.\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of events per week. " ON \
"days" "Number of events per day. " OFF \
"hours" "Number of events per hour. " OFF \
"minutes" "Number of events per minute. " OFF \
"seconds" "Number of events per second. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# the frequency rule type alerts when num_events events occur with timeframe time
type: frequency
# (Required, frequency specific)
# Alert when this many documents matching the query occur within a timeframe
num_events: $num_events
# (Required, frequency specific)
# num_events must occur within this amount of time to trigger an alert
timeframe:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
else
exit
fi
if (whiptail --yesno "\nThe frequency rule has the following additional options:\n - use_count_query: \n - use_terms_query: \n\n Would you like to set the optional settings?" 10 55 --title "Frequency Rule Additional Options") then
frequency_query_type=$(whiptail --title "Frequency Rule Options" --radiolist \
"Please choose from the options below:" 15 100 4 \
"use_count_query" "If true, ElastALert will poll Elasticsearch using the count api." ON \
"use_terms_query" "If true, ElastAlert will make an aggregation query against Elasticsearch." OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
if [ $frequency_query_type = "use_count_query" ] ; then
cat << EOF >> $rulename.yaml
# Only count number of records, instead of bringing all data back
use_count_query: true
doc_type: 'doc'
EOF
elif [ $frequency_query_type = "use_terms_query" ] ; then
query_key=$(whiptail --inputbox "\nPlease enter the query_key." 15 65 --title "Frequency Rule Additional Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
term_size=$(whiptail --inputbox "\nThe term_size is the maximum number of unique terms that will be returned.\n\nPlease enter the term_size, default is 50." 15 65 --title "Frequency Rule Additional Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# Only count number of records, instead of bringing all data back
use_terms_query: true
doc_type: 'doc'
# Query_key count of documents will be stored independently for each value of query_key
query_key: $query_key
# Term_size is the maximum number of terms returned per query. Default is 50.
terms_size: $term_size
EOF
else
exit
fi
else
exit
fi
fi
else
exit
fi
else
exit
fi
}
################################
# Functions for Change Rules #
################################
change_rule_prompt()
{
compare_key=$(whiptail --inputbox "\nThe change rule will monitor a certain field and match if a value in that field changes.\nThe field must change with respect to the last event with the same query_key.\n\nWhat field do you want to monitor for changes?" 15 75 --title "Change Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
query_key=$(whiptail --inputbox "\nThe query_key parameter names the field that must be present in all of the events that are checked.\n\nPlease enter the query_key." 15 75 --title "Change Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe change rule timeframe is the maximum time between changes. After this time period, ElastAlert will forget the old value of the compare_key field.\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of weeks between changes. " ON \
"days" "Number of days between changes. " OFF \
"hours" "Number of hours between changes. " OFF \
"minutes" "Number of minutes between changes. " OFF \
"seconds" "Number of seconds between changes. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# This rule will monitor a certain field and match if that field changes.
type: change
# (Required, change specific)
# The field to look for changes in
compare_key: $compare_key
# (Required, change specific)
# Ignore documents without the compare_key (source_ip) field
ignore_null: true
# (Required, change specific)
# The change must occur in two documents with the same query_key
query_key: $query_key
# (Required, change specific)
# The value of compare_key must change in two events that are less than timeframe apart to t$
timeframe:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
else
exit
fi
else
exit
fi
}
################################
# Functions for Spike Rules #
################################
spike_rule_prompt()
{
spike_height=$(whiptail --inputbox "\nThe spike rule matches when the volume of events during a given time period is spike_height times larger or smaller than during the previous time period. Note: This value is a multiple!! 2 = 2x as many; 5 = 5x as many\n\nWhat do you want the spike_type parameter to be?" 15 90 --title "Spike Rule Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
spike_type=$(whiptail --title "Spike Rule Options" --radiolist \
"\nPlease choose from the options below: " 15 65 5 \
"up" "more than previous timeframe. " ON \
"down" "less than previous timeframe. " OFF \
"both" "up and down. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe spike rule will average out the rate of events over this time period.\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of weeks to average. " ON \
"days" "Number of days to average. " OFF \
"hours" "Number of hours to average. " OFF \
"minutes" "Number of minutes to average. " OFF \
"seconds" "Number of seconds to average. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# This rule matches when the volume of events during a given time period is spike_height times larger or smaller than during the previous time period.
type: spike
# (Required, spike specific)
# The ratio of number of events in the last timeframe to the previous timeframe.
spike_height: $spike_height
# (Required, spike specific)
# The spike being up, down or both
spike_type: $spike_type
# (Required, spike specific)
# The value of average out the rate of events over this time period.
timeframe:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
else
exit
fi
else
exit
fi
if (whiptail --yesno "The spike rule has the following optional parameters:\n
- field_value: When set, uses the value of the field in the document and not the number of matching documents.\n
- threshold_ref: The minimum number of events that must exist in the reference window for an alert to trigger.\n
- threshold_cur: The minimum number of events that must exist in the current window for an alert to trigger.\n\n
Would you like to choose these options?" 25 78 --title "Spike Rule Additional Options") then
counter=0
while [ $counter -eq 0 ] ; do
counter=$(( $counter + 1 ))
spike_additional_options=$(whiptail --title "Spike Rule Additional Options" --radiolist \
"Please choose from the options below:" 15 130 6 \
"field_value" "The value of the field in the document and not the number of matching documents." ON \
"threshold_ref" "The min number of events that must exist in the reference window for an alert to trigger." OFF \
"threshold_cur" "The minimum number of events that must exist in the current window for an alert to trigger." OFF \
"finish" "Continue building the rule" OFF 3>&1 1>&2 2>&3)
if [ $spike_additional_options = "field_value" ] ; then
field_value_field=$(whiptail --inputbox "What field would you like to use?" 15 65 --title "Spike Rule Additional Options" 3>&1 1>&2 2>&3)
cat << EOF >> $rulename.yaml
#(Optional, spike specific)
# field_value: When set, uses the value of the field in the document and not the number of matching documents.
field_value: $spike_options_select
EOF
# reset the counter for the while loop
counter=0
elif [ $spike_additional_options = "threshold_ref" ] ; then
threshold_ref_field=$(whiptail --inputbox "What would you like the threshold_ref to be?" 15 65 --title "Spike Rule Additional Options" 3>&1 1>&2 2>&3)
cat << EOF >> $rulename.yaml
#(Optional, spike specific)
# The minimum number of events that must exist in the reference window for an alert to trigger.
threshold_ref: $threshold_ref_field
EOF
#Reset the counter for the while loop
counter=0
elif [ $spike_additional_options = "threshold_ref" ] ; then
threshold_ref_field=$(whiptail --inputbox "What would you like the threshold_ref to be?" 15 65 --title "Spike Rule Additional Options" 3>&1 1>&2 2>&3)
cat << EOF >> $rulename.yaml
#(Optional, spike specific)
# The minimum number of events that must exist in the reference window for an alert to trigger.
threshold_ref: $threshold_ref_field
EOF
#Reset the counter for the while loop
counter=0
elif [ $spike_additional_options = "threshold_cur" ] ; then
threshold_cur_field=$(whiptail --inputbox "What would you like the threshold_cur to be?" 15 65 --title "Spike Rule Additional Options" 3>&1 1>&2 2>&3)
#(Optional, spike specific
# The minimum number of events that must exist in the current window for an alert to trigger.
threshold_cur: $threshold_cur_field
EOF
#Reset the counter for the while loop
counter=0
else
counter=1
fi
done
fi
}
###################################
# Functions for new term Rules #
###################################
new_term_rule_prompt()
{
new_term_field=$(whiptail --inputbox "\nThis rule matches when a new value appears in a field that has never been seen before. When ElastAlert starts, it will use an aggregation query to gather all known terms for a field.\n\nWhat field do you want to monitor for new terms?" 15 90 --title "New Term Rule" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Required)
# Type of alert.
# This rule matches when a new value appears in a field that has never been seen before.
type: new_term
# (Required, new_term specific)
# Monitor the field ip_address
fields:
- "$new_term_field"
EOF
else
exit
fi
if (whiptail --yesno "\nThe new term rule has the following optional parameters:\n
- terms_window_size: The amount of time used for the initial query to find existing terms. No term that has occurred within this time frame will trigger an alert. The default is 30 days.\n
- window_step_size: When querying for existing terms, split up the time range into steps of this size. This is usefull when covering large timeframes\n
- alert_on_missing_field: Whether or not to alert when a field is missing from a document. The default is false.\n\n
Would you like to set any of these options?" 20 90 --title "New Term Rule Additional Options") then
counter=0
while [ $counter -eq 0 ] ; do
counter=$(( $counter + 1 ))
new_term_additional_options=$(whiptail --title "Spike Rule Additional Options" --radiolist \
"\nPlease choose from the options below:" 15 130 6 \
"terms_window_size" "The time used for the initial query to find existing terms." ON \
"window_step_size" "Split up the time range into steps of this size." OFF \
"alert_on_missing_field" "Whether or not to alert when a field is missing from a document" OFF \
"finish" "Continue building the rule" OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
if [ $new_term_additional_options = "terms_window_size" ] ; then
exitstatus=$?
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe new term timeframe is the amount of time used for the initial query to find existing terms. Default is 30 Days\n\nPlease choose from the options below:\n\n" 20 80 6 \
"weeks" "Number of weeks for initial query. " ON \
"days" "Number of days for intial query. " OFF \
"hours" "Number of hours for initial query. " OFF \
"minutes" "Number of minutes for initial query. " OFF \
"seconds" "Number of seconds for initial query. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Optional, new_term specific)
# This means that we will query 90 days worth of data when ElastAlert starts to find which values of ip_address already exist
# If they existed in the last 90 days, no alerts will be triggered for them when they appear
terms_window_size:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
#reset counter for while loop
counter=0
elif [ $new_term_additional_options = "window_step_size" ] ; then
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe_units=$(whiptail --title "Timefame Options" --radiolist \
"\nThe new term the window_step_size split up the time range into steps of this size.\n\nPlease choose from the options below:\n\n" 20 90 6 \
"weeks" "Number of weeks. " ON \
"days" "Number of days. " OFF \
"hours" "Number of hours. " OFF \
"minutes" "Number of minutes. " OFF \
"seconds" "Number of seconds. " OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
timeframe=$(whiptail --inputbox "\nPlease enter the number of $timeframe_units you want to use." 15 65 --title "Timeframe Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Optional, new_term specific)
# This means that we will query 90 days worth of data when ElastAlert starts to find which values of ip_address alr$
# If they existed in the last 90 days, no alerts will be triggered for them when they appear
window_step_size:
$timeframe_units: $timeframe
EOF
else
exit
fi
else
exit
fi
else
exit
fi
#reset counter for while loop
counter=0
elif [ $new_term_additional_options = "alert_on_missing_field" ] ; then
alert_on_missing_field_option=$(whiptail --inputbox "\nPlease enter either true or false for the alert_on_missing_field." 15 65 --title "Missing Field Options" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ] ; then
cat << EOF >> $rulename.yaml
# (Optional, new_term specific)
# Whether or not to alert when a field is missing from a document. The default is false.
alert_on_missing_field: $alert_on_missing_field_option
EOF
else
exit
fi
else
counter=1
fi
else
exit
fi
done
fi
}
###################################
# Functions for Flat line Rules #