forked from cisagov/LME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·1202 lines (1023 loc) · 42.7 KB
/
deploy.sh
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
############################
# LME Deploy Script #
############################
# This script configures a host for LME including generating certificates and populating configuration files.
REQUIRED_PACKS=(curl zip net-tools jq)
DATE="$(date '+%Y-%m-%d-%H:%M:%S')"
#TODO: convert all logging messages to the following formats:
ED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
BOLD=`tput bold`
RST=`tput sgr0`
function msg { echo -e "${CYAN} $@ ${RST}"; }
function info { echo -e "\e[32m[X]\e[0m $@"; }
function success { echo -e "${GREEN}[+] $@ ${RST}"; }
function warn { echo -e "${YELLOW}[!] $@ ${RST}"; }
function error { echo -e "${RED}[-] $@ ${RST}"; }
#ready?
ready() {
if [ -z "$1" ]; then
str="Are you sure?"
else
str=$1
fi
echo $str
check=""
while ! ([ "${check}" = "n" ] || [ "${check}" = "y" ] );
do
read -e -p " OK [y/n]?" -i "y" check
if [ "${check}" == "n" ]; then
echo -e "\e[33m[!]\e[0m Selected **NO** EXITING"
exit 1
elif [ "${check}" == "y" ]; then
#ready check passed by user
return
else
echo -e "\e[33m[!]\e[0m ONLY PROVIDE y or n"
fi
done
}
#prompt for y/n
prompt() {
if [ -z "$1" ]; then
str="Are you sure?"
else
str=$1
fi
while true; do
echo -n "$str"
read -r -p " [Y/n] " -i "y" input
case $input in
[yY][eE][sS] | [yY])
return 0 #true
break
;;
[nN][oO] | [nN])
return 1 #false
break
;;
*)
echo "Invalid input..."
;;
esac
done
}
#pull latest version from github or
#SET: FORCE_LATEST_VERSION in environment to force a specific version in testing
function get_latest_version() {
if (: "${FORCE_LATEST_VERSION?}") 2>/dev/null;
then
echo -n "$FORCE_LATEST_VERSION"
else
curl -sL https://api.github.com/repos/cisagov/lme/releases/latest | jq -r ".tag_name" | sed 's/v//g' | tr -d "\n"
fi
return 0
}
function customlogstashconf() {
#add option for custom logstash config
CUSTOM_LOGSTASH_CONF=/opt/lme/Chapter\ 3\ Files/logstash_custom.conf
if test -f "$CUSTOM_LOGSTASH_CONF"; then
echo -e "\e[32m[X]\e[0m Custom logstash config exists, Not creating"
else
echo -e "\e[32m[X]\e[0m Creating custom logstash conf"
echo "#custom logstash configuration file" >>/opt/lme/Chapter\ 3\ Files/logstash_custom.conf
fi
}
function generatepasswords() {
elastic_user_pass=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 32 | head -n 1)
kibana_system_pass=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 32 | head -n 1)
logstash_system_pass=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 32 | head -n 1)
logstash_writer=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 32 | head -n 1)
update_user_pass=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 32 | head -n 1)
kibanakey=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w 42 | head -n 1)
echo -e "\e[32m[X]\e[0m Updating logstash configuration with logstash writer"
cp /opt/lme/Chapter\ 3\ Files/logstash.conf /opt/lme/Chapter\ 3\ Files/logstash.edited.conf
sed -i "s/insertlogstashwriterpasswordhere/$logstash_writer/g" /opt/lme/Chapter\ 3\ Files/logstash.edited.conf
}
function setroles() {
echo -e "\n\e[32m[X]\e[0m Setting logstash writer role"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/role/logstash_writer" -H 'Content-Type: application/json' -d'
{
"cluster": ["manage_index_templates", "monitor", "manage_ilm", "manage_pipeline"],
"indices": [
{
"names": [ "logstash-*, ecs-logstash-*","winlogbeat-*" ],
"privileges": ["write","create","create_index","manage","manage_ilm"]
}
]
}
'
#create role, Only needs kibana perms so the other data is just falsified.
echo -e "\n\e[32m[X]\e[0m Setting dashboard update role"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/role/dashboard_update" -H 'Content-Type: application/json' -d'
{
"cluster":[],
"indices":[],
"applications":[{
"application":"kibana-.kibana",
"privileges":[
"feature_canvas.all",
"feature_savedObjectsManagement.all",
"feature_indexPatterns.all",
"feature_dashboard.all",
"feature_visualize.all"],
"resources":["*"]}],
"run_as":[],
"metadata":{},
"transient_metadata":{"enabled":true}}
'
}
function setpasswords() {
temp="temp"
echo -e "\e[32m[X]\e[0m Waiting for Elasticsearch to be ready"
max_attempts=25
attempt=0
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' --cacert certs/root-ca.crt --user elastic:${temp} https://127.0.0.1:9200)" != "200" ]]; do
printf '.'
sleep 10
((attempt++))
if ((attempt > max_attempts)); then
echo "Elasticsearch is not responding after $max_attempts attempts - exiting."
exit 1
fi
done
echo -e "\n\e[32m[X]\e[0m Elasticsearch is up and running."
echo -e "\e[32m[X]\e[0m Setting elastic user password"
curl --cacert certs/root-ca.crt --user elastic:${temp} -X POST "https://127.0.0.1:9200/_security/user/elastic/_password" -H 'Content-Type: application/json' -d' { "password" : "'"$elastic_user_pass"'"} '
echo -e "\n\e[32m[X]\e[0m Setting kibana system password"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/kibana_system/_password" -H 'Content-Type: application/json' -d' { "password" : "'"$kibana_system_pass"'"} '
echo -e "\n\e[32m[X]\e[0m Setting logstash system password"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/logstash_system/_password" -H 'Content-Type: application/json' -d' { "password" : "'"$logstash_system_pass"'"} '
setroles
echo -e "\n\e[32m[X]\e[0m Creating logstash writer user"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/logstash_writer" -H 'Content-Type: application/json' -d'
{
"password" : "logstash_writer",
"roles" : [ "logstash_writer"],
"full_name" : "Internal Logstash User"
}
'
echo -e "\n\e[32m[X]\e[0m Setting logstash writer password"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/logstash_writer/_password" -H 'Content-Type: application/json' -d' { "password" : "'"$logstash_writer"'"} '
echo -e "\n\e[32m[X]\e[0m Creating dashboard update user"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/dashboard_update" -H 'Content-Type: application/json' -d'
{
"password" : "dashboard_update",
"roles" : [ "dashboard_update"],
"full_name" : "Internal dashboard update User"
}
'
echo -e "\n\e[32m[X]\e[0m Setting dashboard update user password"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X POST "https://127.0.0.1:9200/_security/user/dashboard_update/_password" -H 'Content-Type: application/json' -d' { "password" : "'"$update_user_pass"'"} '
}
function zipfiles() {
#zip the files to allow the user to download them for the WLB install.
#copy them to home to start with
echo -e "\n\e[32m[X]\e[0m Generating files_for_windows zip"
mkdir -p /tmp/lme
cp /opt/lme/Chapter\ 3\ Files/winlogbeat.yml /tmp/lme/
if [ -r /opt/lme/Chapter\ 3\ Files/certs/wlbclient.crt ]; then
cp /opt/lme/Chapter\ 3\ Files/certs/wlbclient.crt /tmp/lme/
fi
if [ -r /opt/lme/Chapter\ 3\ Files/certs/wlbclient.key ]; then
cp /opt/lme/Chapter\ 3\ Files/certs/wlbclient.key /tmp/lme/
fi
cp /opt/lme/Chapter\ 3\ Files/certs/root-ca.crt /tmp/lme/
sed -i "s/logstash_dns_name/$logstashcn/g" /tmp/lme/winlogbeat.yml
zip -rmT /opt/lme/files_for_windows.zip /tmp/lme
# Give global read permissions to new archive for later retrieval
chmod 664 /opt/lme/files_for_windows.zip
}
function generateCA() {
echo -e "\e[33m[!]\e[0m Note: Depending on your OpenSSL configuration you may see an error opening a .rnd file into RNG, this will not block the installation"
#configure certificate authority
mkdir -p certs
#make a new key for the root ca
echo -e "\e[32m[X]\e[0m Making root Certificate Authority"
openssl genrsa -out certs/root-ca.key 4096
#make a cert signing request for this key
openssl req -new -key certs/root-ca.key -out certs/root-ca.csr -sha256 -subj "$CERT_STRING/CN=Swarm"
#Set openssl so that this root can only sign certs and not sign intermediates
{
echo "[root_ca]"
echo "basicConstraints = critical,CA:TRUE,pathlen:1"
echo "keyUsage = critical, nonRepudiation, cRLSign, keyCertSign"
echo "subjectKeyIdentifier=hash"
} >certs/root-ca.cnf
#sign the root ca
echo -e "\e[32m[X]\e[0m Signing root CA"
openssl x509 -req -days 3650 -in certs/root-ca.csr -signkey certs/root-ca.key -sha256 -out certs/root-ca.crt -extfile certs/root-ca.cnf -extensions root_ca
}
function generatelogstashcert() {
##logstash server
#make a new key for logstash
echo -e "\e[32m[X]\e[0m Making Logstash certificate"
openssl genrsa -out certs/logstash.key 4096
#make a cert signing request for logstash
openssl req -new -key certs/logstash.key -out certs/logstash.csr -sha256 -subj "$CERT_STRING/CN=$logstashcn"
#set openssl so that this cert can only perform server auth and cannot sign certs
{
echo "[server]"
echo "authorityKeyIdentifier=keyid,issuer"
echo "basicConstraints = critical,CA:FALSE"
echo "extendedKeyUsage=serverAuth"
echo "keyUsage = critical, digitalSignature, keyEncipherment"
echo "subjectAltName = DNS:$logstashcn, IP: $logstaship"
echo "subjectKeyIdentifier=hash"
} >certs/logstash.cnf
#sign the logstash cert
echo -e "\e[32m[X]\e[0m Signing logstash cert"
openssl x509 -req -days 750 -in certs/logstash.csr -sha256 -CA certs/root-ca.crt -CAkey certs/root-ca.key -CAcreateserial -out certs/logstash.crt -extfile certs/logstash.cnf -extensions server
mv certs/logstash.key certs/logstash.key.pem && openssl pkcs8 -in certs/logstash.key.pem -topk8 -nocrypt -out certs/logstash.key
}
function generateclientcert() {
##winlogbeat client
#make a new key for winlogbeat client
echo -e "\e[32m[X]\e[0m Making Winlogbeat client certificate"
openssl genrsa -out certs/wlbclient.key 4096
#make a cert signing request for wlbclient
openssl req -new -key certs/wlbclient.key -out certs/wlbclient.csr -sha256 -subj "$CERT_STRING/CN=wlbclient"
#set openssl so that this cert can only perform server auth and cannot sign certs
{
echo "[server]"
echo "authorityKeyIdentifier=keyid,issuer"
echo "basicConstraints = critical,CA:FALSE"
echo "extendedKeyUsage=clientAuth"
echo "keyUsage = critical, digitalSignature, keyEncipherment"
#echo "subjectAltName = DNS:localhost, IP:127.0.0.1"
echo "subjectKeyIdentifier=hash"
} >certs/wlbclient.cnf
#sign the wlbclient cert
echo -e "\e[32m[X]\e[0m Signing wlbclient cert"
openssl x509 -req -days 750 -in certs/wlbclient.csr -sha256 -CA certs/root-ca.crt -CAkey certs/root-ca.key -CAcreateserial -out certs/wlbclient.crt -extfile certs/wlbclient.cnf -extensions server
}
function generateelasticcert() {
##elasticsearch server
#make a new key for elasticsearch
echo -e "\e[32m[X]\e[0m Making Elasticsearch certificate"
openssl genrsa -out certs/elasticsearch.key 4096
#make a cert signing request for elasticsearch
openssl req -new -key certs/elasticsearch.key -out certs/elasticsearch.csr -sha256 -subj "$CERT_STRING/CN=elasticsearch"
#set openssl so that this cert can only perform server auth and cannot sign certs
{
echo "[server]"
echo "authorityKeyIdentifier=keyid,issuer"
echo "basicConstraints = critical,CA:FALSE"
echo "extendedKeyUsage=serverAuth,clientAuth"
echo "keyUsage = critical, digitalSignature, keyEncipherment"
#echo "subjectAltName = DNS:elasticsearch, IP:127.0.0.1"
echo "subjectAltName = DNS:elasticsearch, IP:127.0.0.1, DNS:$logstashcn, IP: $logstaship"
echo "subjectKeyIdentifier=hash"
} >certs/elasticsearch.cnf
#sign the elasticsearchcert
echo -e "\e[32m[X]\e[0m Sign elasticsearch cert"
openssl x509 -req -days 750 -in certs/elasticsearch.csr -sha256 -CA certs/root-ca.crt -CAkey certs/root-ca.key -CAcreateserial -out certs/elasticsearch.crt -extfile certs/elasticsearch.cnf -extensions server
mv certs/elasticsearch.key certs/elasticsearch.key.pem && openssl pkcs8 -in certs/elasticsearch.key.pem -topk8 -nocrypt -out certs/elasticsearch.key
}
function generatekibanacert() {
##kibana server
#make a new key for kibana
echo -e "\e[32m[X]\e[0m Making Kibana certificate"
openssl genrsa -out certs/kibana.key 4096
#make a cert signing request for kibana
openssl req -new -key certs/kibana.key -out certs/kibana.csr -sha256 -subj "$CERT_STRING/CN=kibana"
#set openssl so that this cert can only perform server auth and cannot sign certs
{
echo "[server]"
echo "authorityKeyIdentifier=keyid,issuer"
echo "basicConstraints = critical,CA:FALSE"
echo "extendedKeyUsage=serverAuth"
echo "keyUsage = critical, digitalSignature, keyEncipherment"
#echo "subjectAltName = DNS:$logstashcn, IP: $logstaship"
echo "subjectAltName = DNS:kibana, IP:127.0.0.1, DNS:$logstashcn, IP: $logstaship"
echo "subjectKeyIdentifier=hash"
} >certs/kibana.cnf
#sign the kibanacert
echo -e "\e[32m[X]\e[0m Sign kibana cert"
openssl x509 -req -days 750 -in certs/kibana.csr -sha256 -CA certs/root-ca.crt -CAkey certs/root-ca.key -CAcreateserial -out certs/kibana.crt -extfile certs/kibana.cnf -extensions server
mv certs/kibana.key certs/kibana.key.pem && openssl pkcs8 -in certs/kibana.key.pem -topk8 -nocrypt -out certs/kibana.key
}
function populatecerts() {
#add to docker secrets
echo -e "\e[32m[X]\e[0m Adding certificates and keys to Docker"
#ca cert
docker secret create ca.crt certs/root-ca.crt
#logstash
docker secret create logstash.key certs/logstash.key
docker secret create logstash.crt certs/logstash.crt
#elasticsearch server
docker secret create elasticsearch.key certs/elasticsearch.key
docker secret create elasticsearch.crt certs/elasticsearch.crt
#kibana server
docker secret create kibana.key certs/kibana.key
docker secret create kibana.crt certs/kibana.crt
}
function removecerts() {
#add to docker secrets
echo -e "\e[32m[X]\e[0m Removing existing certificates and keys from Docker"
#ca cert
docker secret rm ca.crt
#logstash
docker secret rm logstash.key
docker secret rm logstash.crt
#elasticsearch server
docker secret rm elasticsearch.key
docker secret rm elasticsearch.crt
#kibana server
docker secret rm kibana.key
docker secret rm kibana.crt
}
function populatelogstashconfig() {
#add logstash conf to config
docker config create logstash.conf logstash.edited.conf
#add logstash_custom conf to config
customlogstashconf
docker config create logstash_custom.conf logstash_custom.conf
}
function configuredocker() {
sysctl -w vm.max_map_count=262144
SYSCTL_STATUS=$(grep vm.max_map_count /etc/sysctl.conf)
if [ "$SYSCTL_STATUS" == "vm.max_map_count=262144" ]; then
echo "SYSCTL already configured"
else
echo "vm.max_map_count=262144" >>/etc/sysctl.conf
fi
RAM_COUNT="$(awk '( $1 == "MemAvailable:" ) { print $2/1048576 }' /proc/meminfo | xargs printf "%.*f\n" 0)"
#Table for ES ram
if [ "$RAM_COUNT" -lt 8 ]; then
echo -e "\e[31m[!]\e[0m LME Requires 8GB of RAM Available for use - exiting"
exit 1
elif [ "$RAM_COUNT" -ge 8 ] && [ "$RAM_COUNT" -le 16 ]; then
ES_RAM=$((RAM_COUNT - 4))
elif [ "$RAM_COUNT" -ge 17 ] && [ "$RAM_COUNT" -le 32 ]; then
ES_RAM=$((RAM_COUNT - 6))
elif [ "$RAM_COUNT" -ge 33 ] && [ "$RAM_COUNT" -le 49 ]; then
ES_RAM=$((RAM_COUNT - 8))
elif [ "$RAM_COUNT" -ge 50 ]; then
ES_RAM=31
else
echo -e "\e[31m[!]\e[0m Unable to determine RAM - exiting"
exit 1
fi
sed -i "s/ram-count/$ES_RAM/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
sed -i "s/insertkibanapasswordhere/$kibana_system_pass/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
sed -i "s/kibanakey/$kibanakey/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
sed -i "s/insertpublicurlhere/https:\/\/$logstashcn/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
}
function installdocker() {
echo -e "\e[32m[X]\e[0m Installing Docker"
curl -fsSL https://get.docker.com -o get-docker.sh >/dev/null
sh get-docker.sh >/dev/null
}
function initdockerswarm() {
echo -e "\e[32m[X]\e[0m Configuring Docker swarm"
docker swarm init --advertise-addr "$logstaship"
if [ "$?" == 1 ]; then
echo -e "\e[31m[!]\e[0m Failed to initialize docker swarm (Is $logstaship the correct IP address?) - exiting"
exit 1
fi
}
function pulllme() {
info " Pulling ELK images"
docker compose -f /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml pull
}
function deploylme() {
docker stack deploy lme --compose-file /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
}
get_distribution() {
lsb_dist=""
# Every system that we officially support has /etc/os-release
if [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
# Returning an empty string here should be alright since the
# case statements don't act unless you provide an actual value
echo "$lsb_dist"
}
function indexmappingupdate() {
echo -e "\n\e[32m[X]\e[0m Uploading the LME index template"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/_index_template/lme_template" -H 'Content-Type: application/json' --data "@winlog-index-mapping.json"
}
function pipelineupdate() {
echo -e "\n\e[32m[X]\e[0m Setting Elastic pipelines"
#create beats pipeline
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/_ingest/pipeline/winlogbeat" -H 'Content-Type: application/json' -d'
{
"description": "Add geoip info and ingest timestamp",
"processors": [
{
"geoip": {
"field": "client.ip",
"target_field": "client.geo",
"ignore_missing": true
}
},
{
"geoip": {
"field": "source.ip",
"target_field": "source.geo",
"ignore_missing": true
}
},
{
"geoip": {
"field": "destination.ip",
"target_field": "destination.geo",
"ignore_missing": true
}
},
{
"geoip": {
"field": "server.ip",
"target_field": "server.geo",
"ignore_missing": true
}
},
{
"geoip": {
"field": "host.ip",
"target_field": "host.geo",
"ignore_missing": true
}
},
{
"set": {
"field": "event.ingested",
"value": "{{_ingest.timestamp}}",
"ignore_failure": true
}
}
]
}
'
}
function data_retention() {
# Show ext4 disk
DF_OUTPUT="$(df -BG -l -t ext4 --output=source,size /var/lib/docker)"
# Pull device name
DISK_DEV="$(echo "$DF_OUTPUT" | awk 'NR==2 {print $1}')"
# Pull device size
DISK_SIZE="$(echo "$DF_OUTPUT" | awk 'NR==2 {print $2}' | sed 's/G//')"
# Check if DISK_SIZE is empty or not a number
if ! [[ "$DISK_SIZE" =~ ^[0-9]+$ ]]; then
echo -e "\e[31m[!]\e[0m DISK_SIZE not an integer or is empty - exiting."
exit 1
fi
echo -e "\e[32m[X]\e[0m We think your main disk is $DISK_DEV and its size is $DISK_SIZE gigabytes"
if [ "$DISK_SIZE" -lt 128 ]; then
echo -e "\e[33m[!]\e[0m Warning: Disk size less than 128GB, recommend a larger disk for production environments. Install continuing..."
sleep 3
RETENTION="30"
elif [ "$DISK_SIZE" -ge 128 ] && [ "$DISK_SIZE" -le 179 ]; then
RETENTION="45"
elif [ "$DISK_SIZE" -ge 180 ] && [ "$DISK_SIZE" -le 359 ]; then
RETENTION="90"
elif [ "$DISK_SIZE" -ge 360 ] && [ "$DISK_SIZE" -le 539 ]; then
RETENTION="180"
elif [ "$DISK_SIZE" -ge 540 ] && [ "$DISK_SIZE" -le 719 ]; then
RETENTION="270"
elif [ "$DISK_SIZE" -ge 720 ]; then
RETENTION="365"
else
echo -e "\e[31m[!]\e[0m Unable to determine disk size - exiting."
exit 1
fi
echo -e "\e[32m[X]\e[0m We are assigning $RETENTION days as your retention period for log storage"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/_ilm/policy/lme_ilm_policy" -H 'Content-Type: application/json' -d'
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_age": "30d",
"max_primary_shard_size": "50gb"
}
}
},
"warm": {
"min_age": "2d",
"actions": {
"shrink": {
"number_of_shards": 1
}
}
},
"delete": {
"min_age": "'$RETENTION'd",
"actions": {
"delete": {
"delete_searchable_snapshot": true
}
}
}
},
"_meta": {
"description": "LME ILM policy using the hot and warm phases with a retention of '$RETENTION' days"
}
}
}
'
}
function auto_os_updates() {
lin_ver=$(get_distribution)
echo "This OS was detected as: $lin_ver"
if [ "$lin_ver" == "ubuntu" ]; then
echo -e "\e[32m[X]\e[0m Configuring Auto Updates"
apt install unattended-upgrades -y -q
sed -i 's#//Unattended-Upgrade::Automatic-Reboot "false";#Unattended-Upgrade::Automatic-Reboot "true";#g' /etc/apt/apt.conf.d/50unattended-upgrades
sed -i 's#//Unattended-Upgrade::Automatic-Reboot-Time "02:00";#Unattended-Upgrade::Automatic-Reboot-Time "02:00";#g' /etc/apt/apt.conf.d/50unattended-upgrades
auto_os_updatesfile='/etc/apt/apt.conf.d/20auto-upgrades'
apt_UPL_0='APT::Periodic::Update-Package-Lists "0";'
apt_UPL_1='APT::Periodic::Update-Package-Lists "1";'
apt_UU_0='APT::Periodic::Unattended-Upgrade "0";'
apt_UU_1='APT::Periodic::Unattended-Upgrade "1";'
apt_DUP_0='APT::Periodic::Download-Upgradeable-Packages "0";'
apt_DUP_1='APT::Periodic::Download-Upgradeable-Packages "1";'
# check if package list is set to 1 or 0 and then make sure its 1 if its not set then set it
if grep -q -F -e "$apt_UPL_0" -e "$apt_UPL_1" "$auto_os_updatesfile"; then
sed -i "s#$apt_UPL_0#$apt_UPL_1#g" $auto_os_updatesfile
else
echo "$apt_UPL_1" >>$auto_os_updatesfile
fi
# check unattended upgrade is set to 1 or 0 and then make sure its 1 if its not set then set it
if grep -q -F -e "$apt_UU_0" -e "$apt_UU_1" "$auto_os_updatesfile"; then
sed -i "s#$apt_UU_0#$apt_UU_1#g" $auto_os_updatesfile
else
echo "$apt_UU_1" >>$auto_os_updatesfile
fi
# check download packages is set to 1 or 0 and then make sure its 1 if its not set then set it
if grep -q -F -e "$apt_DUP_0" -e "$apt_DUP_1" "$auto_os_updatesfile"; then
sed -i "s#$apt_DUP_0#$apt_DUP_1#g" $auto_os_updatesfile
else
echo "$apt_DUP_1" >>$auto_os_updatesfile
fi
else
echo -e "\e[33m[!]\e[0m Not configuring automatic updates as this OS is not supported"
fi
}
function configelasticsearch() {
echo -e "\n\e[32m[X]\e[0m Configuring elasticsearch Replica settings"
#set future index to always have no replicas
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/_template/number_of_replicas" -H 'Content-Type: application/json' -d' { "template": "*", "settings": { "number_of_replicas": 0 }}'
#set all current indices to have 0 replicas
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/_all/_settings" -H 'Content-Type: application/json' -d '{"index" : {"number_of_replicas" : 0}}'
}
function writeconfig() {
echo -e "\n\e[32m[X]\e[0m Writing LME Config"
#write LME version
echo "version=$(get_latest_version)" >/opt/lme/lme.conf
if [ -z "$logstashcn" ]; then
# $logstashcn is not set - so this function is not called from an initial install
read -e -p "Enter the Fully Qualified Domain Name (FQDN) of this Linux server: " logstashcn
fi
#write elastic hostname
echo "hostname=$logstashcn" >>/opt/lme/lme.conf
cp dashboard_update.sh /opt/lme/
chmod 700 /opt/lme/dashboard_update.sh
echo -e "\e[32m[X]\e[0m Updating dashboard update configuration with dashboard update user credentials"
sed -i "s/dashboardupdatepassword/$update_user_pass/g" /opt/lme/dashboard_update.sh
cp lme_update.sh /opt/lme/
chmod 700 /opt/lme/lme_update.sh
}
function uploaddashboards() {
echo -e "\e[32m[X]\e[0m Uploading Kibana dashboards"
sleep 30 #sleep to make sure port is responsive, it seems to not immediately be available sometimes
/opt/lme/dashboard_update.sh
echo ""
}
function zipnewcerts() {
echo -e "\n\e[32m[X]\e[0m Generating new_client_certificates.zip"
mkdir -p /tmp/lme
cp /opt/lme/Chapter\ 3\ Files/certs/wlbclient.crt /tmp/lme/
cp /opt/lme/Chapter\ 3\ Files/certs/wlbclient.key /tmp/lme/
cp /opt/lme/Chapter\ 3\ Files/certs/root-ca.crt /tmp/lme/
zip -rmT /opt/lme/new_client_certificates.zip /tmp/lme
}
function bootstrapindex() {
if [[ "$(curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -s -o /dev/null -w ''%{http_code}'' https://127.0.0.1:9200/winlogbeat-000001)" != "200" ]]; then
echo -e "\n\e[32m[X]\e[0m Bootstrapping index alias"
curl --cacert certs/root-ca.crt --user "elastic:$elastic_user_pass" -X PUT "https://127.0.0.1:9200/winlogbeat-000001" -H 'Content-Type: application/json' -d'
{
"aliases": {
"winlogbeat-alias": {
"is_write_index": true
}
}
}
'
else
echo -e "\n\e[33m[!]\e[0m Initial index already exists, no need to bootstrap"
fi
}
function fixreadability() {
cd /opt/lme/
chmod -077 -R .
#some permissions to help with seeing files
chown root:sudo /opt/lme/
chmod 750 /opt/lme/
chmod 644 files_for_windows.zip
#fix backups
chown -R 1000:1000 /opt/lme/backups
chmod -R go-rwx /opt/lme/backups
#fix chapter 3 files: group and owner should have rx permissions
chown 1000:1000 /opt/lme/Chapter\ 3\ Files/
chmod ug+rx /opt/lme/Chapter\ 3\ Files
}
function install() {
export FRESH_INSTALL="true"
echo -e "Will execute the following intrusive actions:\n\t- apt update & upgrade\n\t- install docker (please uninstall before proceeding, or indicate skipping the install)\n\t- initialize docker swarm (execute \`sudo docker swarm leave --force\` before proceeding if you are part of a swarm\n\t- automatic os updates via unattened-upgrades\n\t- checkout lme directory to latest version, and throw away local changes)"
prompt "Proceed?"
status=$?
#user entered no
if ! (exit $status);
then
error "Exiting"
return 1
fi
echo -e "\e[32m[X]\e[0m Updating OS software"
apt-get update
DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get upgrade -yq
echo -e "\e[32m[X]\e[0m Installing prerequisites"
DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get install ${REQUIRED_PACKS[*]} -yq
if [ -f /var/run/reboot-required ]; then
echo -e "\e[31m[!]\e[0m A reboot is required in order to proceed with the install."
echo -e "\e[31m[!]\e[0m Please reboot and re-run this script to finish the install."
exit 1
fi
#enable auto updates if ubuntu
auto_os_updates
#move configs
cp docker-compose-stack.yml docker-compose-stack-live.yml
#find the IP winlogbeat will use to communicate with the logstash box (on elk)
#get interface name of default route
DEFAULT_IF="$(route | grep '^default' | grep -o '[^ ]*$')"
#get ip of the interface
EXT_IP="$(/sbin/ifconfig "$DEFAULT_IF" | awk -F ' *|:' '/inet /{print $3}')"
read -e -p "Enter the IP of this Linux server: " -i "$EXT_IP" logstaship
read -e -p "Enter the Fully Qualified Domain Name (FQDN) of this Linux server. This needs to be resolvable from the Windows Event Collector: " logstashcn
echo -e "\e[32m[X]\e[0m Configuring winlogbeat config and certificates to use $logstaship as the IP and $logstashcn as the DNS"
read -e -p "This script will use self signed certificates for communication and encryption. Do you want to continue with self signed certificates? ([y]es/[n]o): " -i "y" selfsignedyn
read -e -p "Skip Docker Install? ([y]es/[n]o): " -i "n" skipdinstall
if [ "$selfsignedyn" == "y" ]; then
#make certs
generateCA
generatelogstashcert
generateclientcert
generateelasticcert
generatekibanacert
elif [ "$selfsignedyn" == "n" ]; then
echo "Please make sure you have the following certificates named correctly"
echo "./certs/root-ca.crt"
echo "./certs/elasticsearch.key"
echo "./certs/elasticsearch.crt"
echo "./certs/logstash.crt"
echo "./certs/logstash.key"
echo "./certs/kibana.crt"
echo "./certs/kibana.key"
echo -e "\e[32m[X]\e[0m Checking for root-ca.crt"
if [ ! -f ./certs/root-ca.crt ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for elasticsearch.key"
if [ ! -f ./certs/elasticsearch.key ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for elasticsearch.crt"
if [ ! -f ./certs/elasticsearch.crt ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for logstash.crt"
if [ ! -f ./certs/logstash.crt ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for logstash.key"
if [ ! -f ./certs/logstash.key ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for kibana.crt"
if [ ! -f ./certs/kibana.crt ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
echo -e "\e[32m[X]\e[0m Checking for kibana.key"
if [ ! -f ./certs/kibana.key ]; then
echo -e "\e[31m[!]\e[0m File not found!"
exit 1
fi
else
echo "Not a valid option"
fi
if [ "$skipdinstall" == "n" ]; then
installdocker
fi
initdockerswarm
populatecerts
generatepasswords
populatelogstashconfig
configuredocker
pulllme
deploylme
setpasswords
configelasticsearch
zipfiles
#pipelines
pipelineupdate
#ILM
data_retention
#index mapping
indexmappingupdate
#bootstrap
bootstrapindex
#create config file
writeconfig
#dashboard upload
uploaddashboards
#prompt user to enable auto update
#Deprecated
#promptupdate
#fix readability:
fixreadability
displaycredentials
echo -e "If you prefer to set your own elastic user password, then refer to our troubleshooting documentation:"
echo -e "https://github.com/cisagov/LME/blob/main/docs/markdown/reference/troubleshooting.md#changing-elastic-username-password\n\n"
}
function displaycredentials() {
echo ""
echo "##################################################################################"
echo "## Kibana/Elasticsearch Credentials are (these will not be accessible again!)"
echo "##"
echo "## Web Interface login:"
echo "## elastic:$elastic_user_pass"
echo "##"
echo "## System Credentials"
echo "## kibana:$kibana_system_pass"
echo "## logstash_system:$logstash_system_pass"
echo "## logstash_writer:$logstash_writer"
echo "## dashboard_update:$update_user_pass"
echo "##################################################################################"
echo ""
}
function uninstall() {
echo -e "Performs the following:\n\t-kill all container processes\n\t-remove certs from docker"
read -e -p "Proceed ([y]es/[n]o):" -i "n" check
if [ "$check" == "n" ]; then
return
elif [ "$check" == "y" ]; then
echo -e "\e[32m[X]\e[0m Removing Docker stack and configuration"
docker stack rm lme
docker secret rm ca.crt logstash.crt logstash.key elasticsearch.key elasticsearch.crt
docker secret rm kibana.crt kibana.key
docker config rm logstash.conf logstash_custom.conf
echo -e "\e[32m[X]\e[0m Attempting to remove legacy LME files (this will cause expected errors if these no longer exist)"
docker secret rm winlogbeat.crt winlogbeat.key nginx.crt nginx.key
docker config rm osmap.csv
echo -e "\e[32m[X]\e[0m Leaving Docker swarm"
docker swarm leave --force
echo -e "\e[32m[X]\e[0m Removing LME config files and configured auto-updates"
rm -r certs
crontab -l | sed -E '/lme_update.sh|dashboard_update.sh/d' | crontab -
echo -e "\e[33m[!]\e[0m NOTICE!"
echo -e "\e[33m[!]\e[0m No data has been deleted:"
echo -e "\e[33m[!]\e[0m - Run 'sudo docker volume rm lme_esdata' to delete the elasticsearch database"
echo -e "\e[33m[!]\e[0m - Run 'sudo docker volume rm lme_logstashdata' to delete the logstash data directory"
return
else
echo -e "\e[33m[!]\e[0m ONLY PROVIDE y or n"
fi
}
function upgrade() {
#remove auto updates
crontab -l | sed -E '/lme_update.sh|dashboard_update.sh/d' | crontab -
#grab latest version
latest=$(get_latest_version)
#check if the config file we're now creating on new installs exists
if [ -r /opt/lme/lme.conf ]; then
#reference this file as a source
. /opt/lme/lme.conf
#check if the version number is equal to the one we want
#NCSC -> CISA
if [ "$version" == "0.5.1" ]; then
echo -e "\e[32m[X]\e[0m Updating from git repo"
git -C /opt/lme/ pull
echo -e "\e[32m[X]\e[0m Removing existing Docker stack"
docker stack rm lme
docker config rm logstash.conf logstash_custom.conf
echo -e "\e[32m[X]\e[0m Attempting to remove legacy LME files (this will cause expected errors if these no longer exist)"
docker config rm osmap.csv
echo -e "\e[32m[X]\e[0m Sleeping for one minute to allow Docker actions to complete..."
sleep 1m
#Update Logstash Config
echo -e "\e[32m[X]\e[0m Updating current configuration files"
# mv old config to .old
mv /opt/lme/Chapter\ 3\ Files/logstash.edited.conf /opt/lme/Chapter\ 3\ Files/logstash.edited.conf.old
# copy new git version
cp /opt/lme/Chapter\ 3\ Files/logstash.conf /opt/lme/Chapter\ 3\ Files/logstash.edited.conf
# copy pass from old config into var
Logstash_Config_Pass="$(awk '{if(/password/) print $3}' </opt/lme/Chapter\ 3\ Files/logstash.edited.conf.old | head -1 | tr -d \")"
# Insert var into new config
sed -i "s/insertlogstashwriterpasswordhere/$Logstash_Config_Pass/g" /opt/lme/Chapter\ 3\ Files/logstash.edited.conf
# delete old config
rm /opt/lme/Chapter\ 3\ Files/logstash.edited.conf.old
#Update Docker Config
#Move old docker config to .old
mv /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml.old
#copy new git version
cp /opt/lme/Chapter\ 3\ Files/docker-compose-stack.yml /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
# copy ramcount into var
Ram_from_conf="$(grep -P -o "(?<=Xms)\d+" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml.old)"
# update Config file with ramcount
sed -i "s/ram-count/$Ram_from_conf/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
# copy elastic pass into var
Kibanapass_from_conf="$(grep -P -o "(?<=ELASTICSEARCH_PASSWORD: ).*" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml.old)"
#update config with kibana password
sed -i "s/insertkibanapasswordhere/$Kibanapass_from_conf/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
#copy kibana encryption key
kibanakey="$(grep -P -o "(?<=XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY: ).*" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml.old)"
#update config with kibana key
sed -i "s/kibanakey/$kibanakey/g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
# copy publicbaseurl
baseurl_from_conf="$(grep -P -o "(?<=SERVER_PUBLICBASEURL: ).*" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml.old)"
#update config with publicbaseurl
if [ -n "$baseurl_from_conf" ] && [ "$baseurl_from_conf" != "insertpublicurlhere" ]; then
sed -i "s,insertpublicurlhere,$baseurl_from_conf,g" /opt/lme/Chapter\ 3\ Files/docker-compose-stack-live.yml
elif [ -n "$hostname" ]; then