-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCIS-Amazon_Linux2.sh
2316 lines (2112 loc) · 119 KB
/
CIS-Amazon_Linux2.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
: '
#SYNOPSIS
Quick win script for remediation of Amazon Linux 2 baseline misconfigurations.
.DESCRIPTION
This script will remediation all possible OS baseline misconfigurations for Amazon Linux 2 based Virtual machines.
.NOTES
Copyright (c) Cloudneeti. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Version: 1.0
# PREREQUISITE
.EXAMPLE
Command to execute : bash CIS_Amazon_Linux2_Benchmark_v1_0_0_Remediation.sh
.INPUTS
.OUTPUTS
None
'
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;35m'
NC='\033[0m'
success=0
fail=0
yum update -y && yum install wget -y
###########################################################################################################################
##Category 1.1 Initial Setup - Filesystem Configuration
echo
echo -e "${BLUE}1.1 Initial Setup - Filesystem Configuration${NC}"
#Ensure mounting of cramfs filesystems is disabled
echo
echo -e "${RED}1.1.1.1${NC} Ensure mounting of cramfs filesystems is disabled"
modprobe -n -v cramfs | grep "^install /bin/true$" || echo "install cramfs /bin/true" >> /etc/modprobe.d/CIS.conf
amazon_linux_1_1_1_1=$?
lsmod | egrep "^cramfs\s" && rmmod cramfs
if [[ "$amazon_linux_1_1_1_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure mounting of cramfs filesystems is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure mounting of cramfs filesystems is disabled"
fail=$((fail + 1))
fi
# Ensure mounting of hfs filesystems is disabled
echo
echo -e "${RED}1.1.1.2${NC} Ensure mounting of hfs filesystems is disabled"
amazon_linux_1_1_1_2="$(modprobe -n -v hfs | grep "^install /bin/true$" || echo "install hfs /bin/true" >> /etc/modprobe.d/CIS.conf)"
amazon_linux_1_1_1_2=$?
lsmod | egrep "^hfs\s" && rmmod hfs
if [[ "$amazon_linux_1_1_1_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure mounting of hfs filesystems is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure mounting of hfs filesystems is disabled"
fail=$((fail + 1))
fi
# Ensure mounting of hfsplus filesystems is disabled
echo
echo -e "${RED}1.1.1.3${NC} Ensure mounting of hfsplus filesystems is disabled"
amazon_linux_1_1_1_3="$(modprobe -n -v hfsplus | grep "^install /bin/true$" || echo "install hfsplus /bin/true" >> /etc/modprobe.d/CIS.conf)"
amazon_linux_1_1_1_3=$?
lsmod | egrep "^hfsplus\s" && rmmod hfsplus
if [[ "$amazon_linux_1_1_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure mounting of hfsplus filesystems is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure mounting of hfsplus filesystems is disabled"
fail=$((fail + 1))
fi
# Ensure mounting of squashfs filesystems is disabled
echo
echo -e "${RED}1.1.1.4${NC} Ensure mounting of squashfs filesystems is disabled"
amazon_linux_1_1_1_4="$(modprobe -n -v squashfs | grep "^install /bin/true$" || echo "install squashfs /bin/true" >> /etc/modprobe.d/CIS.conf)"
amazon_linux_1_1_1_4=$?
lsmod | egrep "^squashfs\s" && rmmod squashfs
if [[ "$amazon_linux_1_1_1_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure mounting of squashfs filesystems is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure mounting of squashfs filesystems is disabled"
fail=$((fail + 1))
fi
# Ensure mounting of udf filesystems is disabled
echo
echo -e "${RED}1.1.1.5${NC} Ensure mounting of udf filesystems is disabled"
amazon_linux_1_1_1_5="$(modprobe -n -v udf | grep "^install /bin/true$" || echo "install udf /bin/true" >> /etc/modprobe.d/CIS.conf)"
amazon_linux_1_1_1_5=$?
lsmod | egrep "^udf\s" && rmmod udf
if [[ "$amazon_linux_1_1_1_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure mounting of udf filesystems is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure mounting of udf filesystems is disabled"
fail=$((fail + 1))
fi
# Ensure sticky bit is set on all world-writable directories
echo
echo -e "${RED}1.1.18${NC} Ensure sticky bit is set on all world-writable directories"
amazon_linux_1_1_18="$(df --local -P | awk {'if (NR!=1) print $6'} | xargs -I '{}' find '{}' -xdev -type d -perm -0002 2>/dev/null | xargs chmod a+t)"
amazon_linux_1_1_18=$?
if [[ "$amazon_linux_1_1_18" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure sticky bit is set on all world-writable directories"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure sticky bit is set on all world-writable directories"
fail=$((fail + 1))
fi
# Disable Automounting
echo
echo -e "${RED}1.1.19${NC} Disable Automounting"
amazon_linux_1_1_19="$(systemctl disable autofs.service)"
amazon_linux_1_1_19=$?
if [[ "$amazon_linux_1_1_19" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Disable Automounting"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Disable Automounting"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.2 Initial Setup - Configure Software Updates
echo
echo -e "${BLUE}1.2 Initial Setup - Configure Software Updates${NC}"
# Ensure gpgcheck is globally activated
echo
echo -e "${RED}1.2.3${NC} Ensure gpgcheck is globally activated"
amazon_linux_1_2_3="$(egrep -q "^(\s*)gpgcheck\s*=\s*\S+(\s*#.*)?\s*$" /etc/yum.conf && sed -ri "s/^(\s*)gpgcheck\s*=\s*\S+(\s*#.*)?\s*$/\1gpgcheck=1\2/" /etc/yum.conf || echo "gpgcheck=1" >> /etc/yum.conf)"
amazon_linux_1_2_3=$?
amazon_linux_1_2_3_temp=0
for file in /etc/yum.repos.d/*; do
amazon_linux_1_2_3_temp_2="$(egrep -q "^(\s*)gpgcheck\s*=\s*\S+(\s*#.*)?\s*$" $file && sed -ri "s/^(\s*)gpgcheck\s*=\s*\S+(\s*#.*)?\s*$/\1gpgcheck=1\2/" $file || echo "gpgcheck=1" >> $file)"
amazon_linux_1_2_3_temp_2=$?
if [[ "$amazon_linux_1_2_3_temp_2" -eq 0 ]]; then
((amazon_linux_1_2_3_temp=amazon_linux_1_2_3_temp+1))
fi
done
amazon_linux_1_2_3_temp_2="$( ls -1q /etc/yum.repos.d/* | wc -l)"
if [[ "$amazon_linux_1_2_3" -eq 0 ]] && [[ "$amazon_linux_1_2_3_temp" -eq "amazon_linux_1_2_3_temp_2" ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure gpgcheck is globally activated"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure gpgcheck is globally activated"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.3 Initial Setup - Filesystem Integrity Checking
echo
echo -e "${BLUE}1.3 Initial Setup - Filesystem Integrity Checking${NC}"
# Ensure AIDE is installed
echo
echo -e "${RED}1.3.1${NC} Ensure AIDE is installed"
amazon_linux_1_3_1="$(rpm -q aide || yum -y install aide)"
amazon_linux_1_3_1=$?
if [[ "$amazon_linux_1_3_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure AIDE is installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure AIDE is installed"
fail=$((fail + 1))
fi
# Ensure filesystem integrity is regularly checked
echo
echo -e "${RED}1.3.2${NC} Ensure filesystem integrity is regularly checked"
amazon_linux_1_3_2="$(crontab -u root -l; crontab -u root -l | egrep -q "^0 5 \* \* \* /usr/sbin/aide --check$" || echo "0 5 * * * /usr/sbin/aide --check" ) | crontab -u root -)"
amazon_linux_1_3_2=$?
if [[ "$amazon_linux_1_3_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure filesystem integrity is regularly checked"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure filesystem integrity is regularly checked"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.4 Initial Setup - Secure Boot Settings
echo
echo -e "${BLUE}1.4 Initial Setup - Secure Boot Settings${NC}"
#Ensure permissions on bootloader config are configured
echo
echo -e "${RED}1.4.1${NC} Ensure permissions on bootloader config are configured"
rhel_1_4_1="$(chown root:root /boot/grub2/grub.cfg && chmod og-rwx /boot/grub2/grub.cfg && chown root:root /boot/grub2/user.cfg && chmod og-rwx /boot/grub2/user.cfg)"
rhel_1_4_1=$?
if [[ "$rhel_1_4_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure permissions on bootloader config are configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure permissions on bootloader config are configured"
fail=$((fail + 1))
fi
# Ensure permissions on bootloader config are configured
echo
echo -e "${RED}1.4.1${NC} Ensure permissions on bootloader config are configured\n"
amazon_linux_1_4_1="$(chmod g-r-w-x,o-r-w-x /boot/grub2/grub.cfg)"
amazon_linux_1_4_1=$?
if [[ "$amazon_linux_1_4_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure permissions on bootloader config are configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure permissions on bootloader config are configured"
fail=$((fail + 1))
fi
# Ensure authentication required for single user mode
echo
echo -e "${RED}1.4.2${NC} Ensure authentication required for single user mode"
amazon_linux_1_4_2_rule1="$(egrep -q "^\s*ExecStart" /usr/lib/systemd/system/rescue.service && sed -ri "s/(^[[:space:]]*ExecStart[[:space:]]*=[[:space:]]*).*$/\1-\/bin\/sh -c \"\/sbin\/sulogin; \/usr\/bin\/systemctl --fail --no-block default\"/" /usr/lib/systemd/system/rescue.service || echo "ExecStart=-/bin/sh -c \"/sbin/sulogin; /usr/bin/systemctl --fail --no-block default\"" >> /usr/lib/systemd/system/rescue.service)"
amazon_linux_1_4_2_rule1=$?
amazon_linux_1_4_2_rule2="$(egrep -q "^\s*ExecStart" /usr/lib/systemd/system/emergency.service && sed -ri "s/(^[[:space:]]*ExecStart[[:space:]]*=[[:space:]]*).*$/\1-\/bin\/sh -c \"\/sbin\/sulogin; \/usr\/bin\/systemctl --fail --no-block default\"/" /usr/lib/systemd/system/emergency.service || echo "ExecStart=-/bin/sh -c \"/sbin/sulogin; /usr/bin/systemctl --fail --no-block default\"" >> /usr/lib/systemd/system/emergency.service)"
amazon_linux_1_4_2_rule1=$?
if [[ "$amazon_linux_1_4_2_rule1" -eq 0 ]] && [[ "$amazon_linux_1_4_2_rule2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure authentication required for single user mode"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure authentication required for single user mode"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.5 Initial Setup - Additional Process Hardening
echo
echo -e "${BLUE}1.5 Initial Setup - Additional Process Hardening${NC}"
# Ensure core dumps are restricted
echo
echo -e "${RED}1.5.1${NC} Ensure core dumps are restricted"
amazon_linux_1_5_1_temp_1="$(egrep -q "^(\s*)\*\s+hard\s+core\s+\S+(\s*#.*)?\s*$" /etc/security/limits.conf && sed -ri "s/^(\s*)\*\s+hard\s+core\s+\S+(\s*#.*)?\s*$/\1* hard core 0\2/" /etc/security/limits.conf || echo "* hard core 0" >> /etc/security/limits.conf)"
amazon_linux_1_5_1_temp_1=$?
amazon_linux_1_5_1_temp_2="$(echo "* hard core 0" >> /etc/security/limits.d/*)"
amazon_linux_1_5_1_temp_2=$?
amazon_linux_1_5_1_temp_3="$(egrep -q "^(\s*)fs.suid_dumpable\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)fs.suid_dumpable\s*=\s*\S+(\s*#.*)?\s*$/\1fs.suid_dumpable = 0\2/" /etc/sysctl.conf || echo "fs.suid_dumpable = 0" >> /etc/sysctl.conf)"
amazon_linux_1_5_1_temp_3=$?
amazon_linux_1_5_1_temp_4="$(egrep -q "^(\s*)fs.suid_dumpable\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.d/* || echo "fs.suid_dumpable = 0" >> /etc/sysctl.d/*)"
amazon_linux_1_5_1_temp_4=$?
amazon_linux_1_5_1_temp_5="$(sysctl -w fs.suid_dumpable=0)"
amazon_linux_1_5_1_temp_5=$?
if [[ "$amazon_linux_1_5_1_temp_1" -eq 0 ]] && [[ "$amazon_linux_1_5_1_temp_2" -eq 0 ]] && [[ "$amazon_linux_1_5_1_temp_3" -eq 0 ]] && [[ "$amazon_linux_1_5_1_temp_4" -eq 0 ]] && [[ "$amazon_linux_1_5_1_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure core dumps are restricted"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure core dumps are restricted"
fail=$((fail + 1))
fi
# Ensure address space layout randomization (ASLR) is enabled
echo
echo -e "${RED}1.5.2${NC} Ensure address space layout randomization (ASLR) is enabled"
amazon_linux_1_5_2_temp_1="$(egrep -q "^(\s*)kernel.randomize_va_space\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)kernel.randomize_va_space\s*=\s*\S+(\s*#.*)?\s*$/\1kernel.randomize_va_space = 2\2/" /etc/sysctl.conf || echo "kernel.randomize_va_space = 2" >> /etc/sysctl.conf)"
amazon_linux_1_5_2_temp_1=$?
amazon_linux_1_5_2_temp_3="$(sysctl -w kernel.randomize_va_space=2)"
amazon_linux_1_5_2_temp_3=$?
if [[ "$amazon_linux_1_5_2_temp_1" -eq 0 ]] && [[ "$amazon_linux_1_5_2_temp_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure address space layout randomization (ASLR) is enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure address space layout randomization (ASLR) is enabled"
fail=$((fail + 1))
fi
# Ensure prelink is disabled
echo
echo -e "${RED}1.5.3${NC} Ensure prelink is disabled"
amazon_linux_1_5_3="$(rpm -q prelink && yum -y remove prelink)"
amazon_linux_1_5_3=$?
if [[ "$amazon_linux_1_5_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure prelink is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure prelink is disabled"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.6 Initial Setup - Mandatory Access Control
echo
echo -e "${BLUE}1.6 Initial Setup - Mandatory Access Control${NC}"
# Ensure SELinux is installed
echo
echo -e "${RED}1.6.2${NC} Ensure SELinux is installed"
amazon_linux_1_6_2="$(rpm -q libselinux || yum -y install libselinux)"
amazon_linux_1_6_2=$?
if [[ "$amazon_linux_1_6_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure SELinux is installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure SELinux is installed"
fail=$((fail + 1))
fi
# Ensure the SELinux state is enforcing
echo
echo -e "${RED}1.6.1.2${NC} Ensure the SELinux state is enforcing"
amazon_linux_1_6_1_2="$(grep SELINUX=enforcing /etc/selinux/config || echo "SELINUX=enforcing" >> /etc/selinux/config)"
amazon_linux_1_6_1_2=$?
if [[ "$amazon_linux_1_6_1_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure the SELinux state is enforcing"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure the SELinux state is enforcing"
fail=$((fail + 1))
fi
# Ensure SELinux policy is configured
echo
echo -e "${RED}1.6.1.3${NC} Ensure SELinux policy is configured"
amazon_linux_1_6_1_3="$(grep SELINUXTYPE=targeted /etc/selinux/config || echo "SELINUXTYPE=targeted" >> /etc/selinux/config)"
amazon_linux_1_6_1_3=$?
if [[ "$amazon_linux_1_6_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure SELinux policy is configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure SELinux policy is configured"
fail=$((fail + 1))
fi
# Ensure SETroubleshoot is not installed
echo
echo -e "${RED}1.6.1.4${NC} Ensure SETroubleshoot is not installed"
amazon_linux_1_6_1_4="$(rpm -q setroubleshoot && yum -y remove setroubleshoot)"
amazon_linux_1_6_1_4=$?
if [[ "$amazon_linux_1_6_1_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure SETroubleshoot is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure SETroubleshoot is not installed"
fail=$((fail + 1))
fi
# Ensure the MCS Translation Service (mcstrans) is not installed
echo
echo -e "${RED}1.6.1.5${NC} Ensure the MCS Translation Service (mcstrans) is not installed"
amazon_linux_1_6_1_5="$(rpm -q mcstrans && yum -y remove mcstrans)"
amazon_linux_1_6_1_5=$?
if [[ "$amazon_linux_1_6_1_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure the MCS Translation Service (mcstrans) is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure the MCS Translation Service (mcstrans) is not installed"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 1.7 Initial Setup - Warning Banners
echo
echo -e "${BLUE}1.7 Initial Setup - Warning Banners${NC}"
# Ensure message of the day is configured properly
echo
echo -e "${RED}1.7.1.1${NC} Ensure message of the day is configured properly"
amazon_linux_1_7_1_1="$(sed -ri 's/(\\v|\\r|\\m|\\s)//g' /etc/motd)"
amazon_linux_1_7_1_1=$?
if [[ "$amazon_linux_1_7_1_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure message of the day is configured properly"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure message of the day is configured properly"
fail=$((fail + 1))
fi
# Ensure local login warning banner is configured properly
echo
echo -e "${RED}1.7.1.2${NC} Ensure local login warning banner is configured properly"
amazon_linux_1_7_1_2="$(echo "Authorized uses only. All activity may be monitored and reported." > /etc/issue)"
amazon_linux_1_7_1_2=$?
if [[ "$amazon_linux_1_7_1_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure local login warning banner is configured properly"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure local login warning banner is configured properly"
fail=$((fail + 1))
fi
# Ensure remote login warning banner is configured properly
echo
echo -e "${RED}1.7.1.3${NC} Ensure remote login warning banner is configured properly"
amazon_linux_1_7_1_3="$(echo "Authorized uses only. All activity may be monitored and reported." > /etc/issue.net)"
amazon_linux_1_7_1_3=$?
if [[ "$amazon_linux_1_7_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure remote login warning banner is configured properly"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure remote login warning banner is configured properly"
fail=$((fail + 1))
fi
# Ensure permissions on /etc/motd are configured
echo
echo -e "${RED}1.7.1.4${NC} Ensure permissions on /etc/motd are configured"
amazon_linux_1_7_1_4="$(chmod -t,u+r+w-x-s,g+r-w-x-s,o+r-w-x /etc/motd)"
amazon_linux_1_7_1_4=$?
if [[ "$amazon_linux_1_7_1_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure permissions on /etc/motd are configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure permissions on /etc/motd are configured"
fail=$((fail + 1))
fi
# Ensure permissions on /etc/issue are configured
echo
echo -e "${RED}1.7.1.5${NC} Ensure permissions on /etc/issue are configured"
amazon_linux_1_7_1_5="$(chmod -t,u+r+w-x-s,g+r-w-x-s,o+r-w-x /etc/issue)"
amazon_linux_1_7_1_5=$?
if [[ "$amazon_linux_1_7_1_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure permissions on /etc/issue are configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure permissions on /etc/issue are configured"
fail=$((fail + 1))
fi
# Ensure permissions on /etc/issue.net are configured
echo
echo -e "${RED}1.7.1.6${NC} Ensure permissions on /etc/issue.net are configured"
amazon_linux_1_7_1_6="$(chmod -t,u+r+w-x-s,g+r-w-x-s,o+r-w-x /etc/issue.net)"
amazon_linux_1_7_1_6=$?
if [[ "$amazon_linux_1_7_1_6" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure permissions on /etc/issue.net are configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure permissions on /etc/issue.net are configured"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 2.1 Services - inetd Services
echo
echo -e "${BLUE}2.1 Services - inetd Services${NC}"
# Ensure time synchronization is in use
echo
echo -e "${RED}2.1.1.1${NC} Ensure time synchronization is in use"
amazon_linux_2_1_1_1="$(rpm -q ntp || rpm -q chrony || yum -y install chrony)"
amazon_linux_2_1_1_1=$?
if [[ "$amazon_linux_2_1_1_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure time synchronization is in use"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure time synchronization is in use"
fail=$((fail + 1))
fi
# Ensure ntp is configured
echo
echo -e "${RED}2.1.1.2${NC} Ensure ntp is configured"
if rpm -q ntp >/dev/null; then
amazon_linux_2_1_1_2_temp_1="$(egrep -q "^\s*restrict(\s+-4)?\s+default(\s+\S+)*(\s*#.*)?\s*$" /etc/ntp.conf && sed -ri "s/^(\s*)restrict(\s+-4)?\s+default(\s+[^[:space:]#]+)*(\s+#.*)?\s*$/\1restrict\2 default kod nomodify notrap nopeer noquery\4/" /etc/ntp.conf || echo "restrict -4 default kod nomodify notrap nopeer noquery" >> /etc/ntp.conf)"
amazon_linux_2_1_1_2_temp_1=$?
amazon_linux_2_1_1_2_temp_2="$(egrep -q "^\s*restrict\s+-6\s+default(\s+\S+)*(\s*#.*)?\s*$" /etc/ntp.conf && sed -ri "s/^(\s*)restrict\s+-6\s+default(\s+[^[:space:]#]+)*(\s+#.*)?\s*$/\1restrict -6 default kod nomodify notrap nopeer noquery\3/" /etc/ntp.conf || echo "restrict -6 default kod nomodify notrap nopeer noquery" >> /etc/ntp.conf)"
amazon_linux_2_1_1_2_temp_2=$?
amazon_linux_2_1_1_2_temp_3="$(egrep -q "^(\s*)OPTIONS\s*=\s*\"(([^\"]+)?-u\s[^[:space:]\"]+([^\"]+)?|([^\"]+))\"(\s*#.*)?\s*$" /etc/sysconfig/ntpd && sed -ri '/^(\s*)OPTIONS\s*=\s*\"([^\"]*)\"(\s*#.*)?\s*$/ {/^(\s*)OPTIONS\s*=\s*\"[^\"]*-u\s+\S+[^\"]*\"(\s*#.*)?\s*$/! s/^(\s*)OPTIONS\s*=\s*\"([^\"]*)\"(\s*#.*)?\s*$/\1OPTIONS=\"\2 -u ntp:ntp\"\3/ }' /etc/sysconfig/ntpd && sed -ri "s/^(\s*)OPTIONS\s*=\s*\"([^\"]+\s+)?-u\s[^[:space:]\"]+(\s+[^\"]+)?\"(\s*#.*)?\s*$/\1OPTIONS=\"\2\-u ntp:ntp\3\"\4/" /etc/sysconfig/ntpd || echo OPTIONS=\"-u ntp:ntp\" >> /etc/sysconfig/ntpd)"
amazon_linux_2_1_1_2_temp_3=$?
if [[ "$amazon_linux_2_1_1_2_temp_1" -eq 0 ]] && [[ "$amazon_linux_2_1_1_2_temp_2" -eq 0 ]] && [[ "$amazon_linux_2_1_1_2_temp_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure ntp is configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure ntp is configured"
fail=$((fail + 1))
fi
else
yum install ntp -y && systemctl enable ntpd
amazon_linux_2_1_1_2_temp_1="$(echo "restrict -4 default kod nomodify notrap nopeer noquery" >> /etc/ntp.conf)"
amazon_linux_2_1_1_2_temp_1=$?
amazon_linux_2_1_1_2_temp_2="$(echo "restrict -6 default kod nomodify notrap nopeer noquery" >> /etc/ntp.conf)"
amazon_linux_2_1_1_2_temp_2=$?
amazon_linux_2_1_1_2_temp_3="$(echo OPTIONS=\"-u ntp:ntp\" >> /etc/sysconfig/ntpd)"
amazon_linux_2_1_1_2_temp_3=$?
if [[ "$amazon_linux_2_1_1_2_temp_1" -eq 0 ]] && [[ "$amazon_linux_2_1_1_2_temp_2" -eq 0 ]] && [[ "$amazon_linux_2_1_1_2_temp_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure ntp is configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure ntp is configured"
fail=$((fail + 1))
fi
fi
# Ensure chrony is configured
echo
echo -e "${RED}2.1.1.3${NC} Ensure chrony is configured"
if rpm -q chrony >/dev/null; then
amazon_linux_2_1_1_3="$(egrep -q "^(\s*)OPTIONS\s*=\s*\"(([^\"]+)?-u\s[^[:space:]\"]+([^\"]+)?|([^\"]+))\"(\s*#.*)?\s*$" /etc/sysconfig/chronyd && sed -ri '/^(\s*)OPTIONS\s*=\s*\"([^\"]*)\"(\s*#.*)?\s*$/ {/^(\s*)OPTIONS\s*=\s*\"[^\"]*-u\s+\S+[^\"]*\"(\s*#.*)?\s*$/! s/^(\s*)OPTIONS\s*=\s*\"([^\"]*)\"(\s*#.*)?\s*$/\1OPTIONS=\"\2 -u chrony\"\3/ }' /etc/sysconfig/chronyd && sed -ri "s/^(\s*)OPTIONS\s*=\s*\"([^\"]+\s+)?-u\s[^[:space:]\"]+(\s+[^\"]+)?\"(\s*#.*)?\s*$/\1OPTIONS=\"\2\-u chrony\3\"\4/" /etc/sysconfig/chronyd || echo OPTIONS=\"-u chrony\" >> /etc/sysconfig/chronyd)"
amazon_linux_2_1_1_3=$?
if [[ "$amazon_linux_2_1_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure chrony is configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure chrony is configured"
fail=$((fail + 1))
fi
else
yum install chrony -y && systemctl start chronyd && systemctl enable chronyd
amazon_linux_2_1_1_3="$(echo OPTIONS=\"-u chrony\" >> /etc/sysconfig/chronyd)"
amazon_linux_2_1_1_3=$?
if [[ "$amazon_linux_2_1_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure chrony is configured"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure chrony is configured"
fail=$((fail + 1))
fi
fi
# Ensure X Window System is not installed
echo
echo -e "${RED}2.1.2${NC} Ensure X Window System is not installed"
amazon_linux_2_2_2="$(yum -y remove xorg-x11*)"
amazon_linux_2_2_2=$?
if [[ "$amazon_linux_2_2_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure X Window System is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure X Window System is not installed"
fail=$((fail + 1))
fi
# Ensure Avahi Server is not enabled
echo
echo -e "${RED}2.1.3${NC} Ensure Avahi Server is not enabled"
amazon_linux_2_1_3="$(systemctl disable avahi-daemon.service || yum erase avahi -y)"
amazon_linux_2_1_3=$?
if [[ "$amazon_linux_2_1_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure Avahi Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure Avahi Server is not enabled"
fail=$((fail + 1))
fi
# Ensure CUPS is not enabled
echo
echo -e "${RED}2.1.4${NC} Ensure CUPS is not enabled"
amazon_linux_2_1_4="$(systemctl disable cups.service || yum erase cups -y)"
amazon_linux_2_1_4=$?
if [[ "$amazon_linux_2_1_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure CUPS is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure CUPS is not enabled"
fail=$((fail + 1))
fi
# Ensure DHCP Server is not enabled
echo
echo -e "${RED}2.1.5${NC} Ensure DHCP Server is not enabled"
amazon_linux_2_1_5="$(systemctl disable dhcpd.service || yum erase dhcpd -y)"
amazon_linux_2_1_5=$?
if [[ "$amazon_linux_2_1_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure DHCP Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure DHCP Server is not enabled"
fail=$((fail + 1))
fi
# Ensure LDAP server is not enabled
echo
echo -e "${RED}2.1.6${NC} Ensure LDAP server is not enabled"
amazon_linux_2_1_6="$(systemctl disable slapd.service || yum erase slapd -y)"
amazon_linux_2_1_6=$?
if [[ "$amazon_linux_2_1_6" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure LDAP server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure LDAP server is not enabled"
fail=$((fail + 1))
fi
# Ensure NFS and RPC are not enabled
echo
echo -e "${RED}2.1.7${NC} Ensure NFS and RPC are not enabled"
amazon_linux_2_1_7_temp_1="$(systemctl disable nfs.service || yum erase nfs -y)"
amazon_linux_2_1_7_temp_1=$?
amazon_linux_2_1_7_temp_2="$(systemctl disable rpcbind.service || yum erase rpcbind -y)"
amazon_linux_2_1_7_temp_2=$?
if [[ "$amazon_linux_2_1_7_temp_1" -eq 0 ]] && [[ "$amazon_linux_2_1_7_temp_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure NFS and RPC are not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure NFS and RPC are not enabled"
fail=$((fail + 1))
fi
# Ensure DNS Server is not enabled
echo
echo -e "${RED}2.1.8${NC} Ensure DNS Server is not enabled"
amazon_linux_2_1_8="$(systemctl disable named.service || yum erase named -y)"
amazon_linux_2_1_8=$?
if [[ "$amazon_linux_2_1_8" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure DNS Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure DNS Server is not enabled"
fail=$((fail + 1))
fi
# Ensure FTP Server is not enabled
echo
echo -e "${RED}2.1.9${NC} Ensure FTP Server is not enabled"
amazon_linux_2_1_9="$(systemctl disable vsftpd.service || yum erase vsftpd -y)"
amazon_linux_2_1_9=$?
if [[ "$amazon_linux_2_1_9" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure FTP Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure FTP Server is not enabled"
fail=$((fail + 1))
fi
# Ensure HTTP server is not enabled
echo
echo -e "${RED}2.1.10${NC} Ensure HTTP server is not enabled"
amazon_linux_2_1_10="$(systemctl disable httpd.service || yum erase httpd -y)"
amazon_linux_2_1_10=$?
if [[ "$amazon_linux_2_1_10" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure HTTP server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure HTTP server is not enabled"
fail=$((fail + 1))
fi
# Ensure IMAP and POP3 server is not enabled
echo
echo -e "${RED}2.1.11${NC} Ensure IMAP and POP3 server is not enabled"
amazon_linux_2_1_11="$(systemctl disable dovecot.service || yum erase dovecot -y)"
amazon_linux_2_1_11=$?
if [[ "$amazon_linux_2_1_11" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure IMAP and POP3 server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure IMAP and POP3 server is not enabled"
fail=$((fail + 1))
fi
# Ensure Samba is not enabled
echo
echo -e "${RED}2.1.12${NC} Ensure Samba is not enabled"
amazon_linux_2_1_12="$(systemctl disable smb.service || yum erase smb -y)"
amazon_linux_2_1_12=$?
if [[ "$amazon_linux_2_1_12" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure Samba is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure Samba is not enabled"
fail=$((fail + 1))
fi
# Ensure HTTP Proxy Server is not enabled
echo
echo -e "${RED}2.1.13${NC} Ensure HTTP Proxy Server is not enabled"
amazon_linux_2_1_13="$(systemctl disable squid.service || yum erase squid -y)"
amazon_linux_2_1_13=$?
if [[ "$amazon_linux_2_1_13" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure HTTP Proxy Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure HTTP Proxy Server is not enabled"
fail=$((fail + 1))
fi
# Ensure SNMP Server is not enabled
echo
echo -e "${RED}2.1.14${NC} Ensure SNMP Server is not enabled"
amazon_linux_2_1_14="$(systemctl disable snmpd.service || yum erase snmpd -y)"
amazon_linux_2_1_14=$?
if [[ "$amazon_linux_2_1_14" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure SNMP Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure SNMP Server is not enabled"
fail=$((fail + 1))
fi
# Ensure NIS Server is not enabled
echo
echo -e "${RED}2.1.16${NC} Ensure NIS Server is not enabled"
amazon_linux_2_1_16="$(systemctl disable ypserv.service || yum erase ypserv -y)"
amazon_linux_2_1_16=$?
if [[ "$amazon_linux_2_1_16" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure NIS Server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure NIS Server is not enabled"
fail=$((fail + 1))
fi
# Ensure rsh server is not enabled
echo
echo -e "${RED}2.1.17${NC} Ensure rsh server is not enabled"
amazon_linux_2_1_17="$(systemctl disable rsh.socket.service || yum erase rsh -y)"
amazon_linux_2_1_17=$?
systemctl disable rlogin.socket.service
systemctl disable rexec.socket.service
if [[ "$amazon_linux_2_1_17" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure rsh server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure rsh server is not enabled"
fail=$((fail + 1))
fi
# Ensure telnet server is not enabled
echo
echo -e "${RED}2.1.18${NC} Ensure telnet server is not enabled"
amazon_linux_2_1_18="$(systemctl disable telnet.socket.service || yum erase telnet -y)"
amazon_linux_2_1_18=$?
if [[ "$amazon_linux_2_1_18" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure telnet server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure telnet server is not enabled"
fail=$((fail + 1))
fi
# Ensure tftp server is not enabled
echo
echo -e "${RED}2.1.19${NC} Ensure tftp server is not enabled"
amazon_linux_2_1_19="$(chkconfig tftp off)"
amazon_linux_2_1_19=$?
systemctl disable tftp.socket.service
if [[ "$amazon_linux_2_1_19" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure tftp server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure tftp server is not enabled"
fail=$((fail + 1))
fi
# Ensure rsync service is not enabled
echo
echo -e "${RED}2.1.20${NC} Ensure rsync service is not enabled"
amazon_linux_2_1_20="$(systemctl disable rsyncd.service || yum erase rsyncd -y)"
amazon_linux_2_1_20=$?
if [[ "$amazon_linux_2_1_20" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure rsync service is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure rsync service is not enabled"
fail=$((fail + 1))
fi
# Ensure talk server is not enabled
echo
echo -e "${RED}2.1.21${NC} Ensure talk server is not enabled"
amazon_linux_2_1_21="$(systemctl disable ntalk.service || yum erase ntalk -y)"
amazon_linux_2_1_21=$?
if [[ "$amazon_linux_2_1_21" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure talk server is not enabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure talk server is not enabled"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 2.2 Services - Special Purpose Services
echo
echo -e "${BLUE}2.2 Services - Special Purpose Services${NC}"
# Ensure NIS Client is not installed
echo
echo -e "${RED}2.2.1${NC} Ensure NIS Client is not installed"
amazon_linux_2_2_1="$(rpm -q ypbind && yum -y erase ypbind)"
amazon_linux_2_2_1=$?
if [[ "$amazon_linux_2_2_1" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure NIS Client is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure NIS Client is not installed"
fail=$((fail + 1))
fi
# Ensure rsh client is not installed
echo
echo -e "${RED}2.2.2${NC} Ensure rsh client is not installed"
amazon_linux_2_2_2="$(rpm -q rsh && yum -y erase rsh)"
amazon_linux_2_2_2=$?
if [[ "$amazon_linux_2_2_2" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure rsh client is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure rsh client is not installed"
fail=$((fail + 1))
fi
# Ensure talk client is not installed
echo
echo -e "${RED}2.2.3${NC} Ensure talk client is not installed"
amazon_linux_2_2_3="$(rpm -q talk && yum -y erase talk)"
amazon_linux_2_2_3=$?
if [[ "$amazon_linux_2_2_3" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure talk client is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure talk client is not installed"
fail=$((fail + 1))
fi
# Ensure telnet client is not installed
echo
echo -e "${RED}2.2.4${NC} Ensure telnet client is not installed"
amazon_linux_2_2_4="$(rpm -q telnet && yum -y erase telnet)"
amazon_linux_2_2_4=$?
if [[ "$amazon_linux_2_2_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure telnet client is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure telnet client is not installed"
fail=$((fail + 1))
fi
# Ensure LDAP client is not installed
echo
echo -e "${RED}2.2.5${NC} Ensure LDAP client is not installed"
amazon_linux_2_2_5="$(rpm -q openldap-clients && yum -y erase openldap-clients)"
amazon_linux_2_2_5=$?
if [[ "$amazon_linux_2_2_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure LDAP client is not installed"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure LDAP client is not installed"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 3.1 Network Configuration - Network Parameters (Host Only)
echo
echo -e "${BLUE}3.1 Network Configuration - Network Parameters (Host Only)${NC}"
# Ensure IP forwarding is disabled
echo
echo -e "${RED}3.1.1${NC} Ensure IP forwarding is disabled"
amazon_linux_3_1_1_temp_1="$(egrep -q "^(\s*)net.ipv4.ip_forward\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.ip_forward\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.ip_forward = 0\2/" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 0" >> /etc/sysctl.conf)"
amazon_linux_3_1_1_temp_1=$?
amazon_linux_3_1_1_temp_2="$(echo "net.ipv4.ip_forward = 0" >> /etc/sysctl.d/*)"
amazon_linux_3_1_1_temp_2=$?
amazon_linux_3_1_1_temp_3="$(sysctl -w net.ipv4.ip_forward=0)"
amazon_linux_3_1_1_temp_3=$?
amazon_linux_3_1_1_temp_4="$(sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_1_1_temp_4=$?
if [[ "$amazon_linux_3_1_1_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_1_1_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_1_1_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_1_1_temp_4" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure IP forwarding is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure IP forwarding is disabled"
fail=$((fail + 1))
fi
# Ensure packet redirect sending is disabled
echo
echo -e "${RED}3.1.2${NC} Ensure packet redirect sending is disabled"
amazon_linux_3_1_2_temp_1="$(egrep -q "^(\s*)net.ipv4.conf.all.send_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.send_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.send_redirects = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.all.send_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_1_2_temp_1=$?
amazon_linux_3_1_2_temp_2="$(egrep -q "^(\s*)net.ipv4.conf.default.send_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.default.send_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.default.send_redirects = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.default.send_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_1_2_temp_2=$?
amazon_linux_3_1_2_temp_3="$(sysctl -w net.ipv4.conf.all.send_redirects=0)"
amazon_linux_3_1_2_temp_3=$?
amazon_linux_3_1_2_temp_4="$(sysctl -w net.ipv4.conf.default.send_redirects=0)"
amazon_linux_3_1_2_temp_4=$?
amazon_linux_3_1_2_temp_5="$(sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_1_2_temp_5=$?
if [[ "$amazon_linux_3_1_2_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_1_2_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_1_2_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_1_2_temp_4" -eq 0 ]] && [[ "$amazon_linux_3_1_2_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure packet redirect sending is disabled"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure packet redirect sending is disabled"
fail=$((fail + 1))
fi
############################################################################################################################
##Category 3.2 Network Configuration - Network Parameters (Host and Router)
echo
echo -e "${BLUE}3.2 Network Configuration - Network Parameters (Host and Router)${NC}"
# Ensure source routed packets are not accepted
echo
echo -e "${RED}3.2.1${NC} Ensure source routed packets are not accepted"
amazon_linux_3_2_1_temp_1="$(egrep -q "^(\s*)net.ipv4.conf.all.accept_source_route\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.accept_source_route\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.accept_source_route = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.all.accept_source_route = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_1_temp_1=$?
amazon_linux_3_2_1_temp_2="$(egrep -q "^(\s*)net.ipv4.conf.default.accept_source_route\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.default.accept_source_route\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.default.accept_source_route = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.default.accept_source_route = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_1_temp_2=$?
amazon_linux_3_2_1_temp_3="$(sysctl -w net.ipv4.conf.all.accept_source_route=0)"
amazon_linux_3_2_1_temp_3=$?
amazon_linux_3_2_1_temp_4="$(sysctl -w net.ipv4.conf.default.accept_source_route=0)"
amazon_linux_3_2_1_temp_4=$?
amazon_linux_3_2_1_temp_5="$sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_2_1_temp_5=$?
if [[ "$amazon_linux_3_2_1_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_2_1_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_2_1_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_2_1_temp_4" -eq 0 ]] && [[ "$amazon_linux_3_2_1_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure source routed packets are not accepted"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure source routed packets are not accepted"
fail=$((fail + 1))
fi
# Ensure ICMP redirects are not accepted
echo
echo -e "${RED}3.2.2${NC} Ensure ICMP redirects are not accepted"
amazon_linux_3_2_2_temp_1="$(egrep -q "^(\s*)net.ipv4.conf.all.accept_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.accept_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.accept_redirects = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_2_temp_1=$?
amazon_linux_3_2_2_temp_2="$(egrep -q "^(\s*)net.ipv4.conf.all.accept_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.accept_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.accept_redirects = 0\2/" /etc/sysctl.conf || echo "net.ipv4.conf.default.accept_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_2_temp_2=$?
amazon_linux_3_2_2_temp_3="$(sysctl -w net.ipv4.conf.all.accept_redirects=0)"
amazon_linux_3_2_2_temp_3=$?
amazon_linux_3_2_2_temp_4="$(sysctl -w net.ipv4.conf.default.accept_redirects=0)"
amazon_linux_3_2_2_temp_4=$?
amazon_linux_3_2_2_temp_5="$(sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_2_2_temp_5=$?
if [[ "$amazon_linux_3_2_2_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_2_2_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_2_2_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_2_2_temp_4" -eq 0 ]] && [[ "$amazon_linux_3_2_2_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure ICMP redirects are not accepted"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure ICMP redirects are not accepted"
fail=$((fail + 1))
fi
# Ensure secure ICMP redirects are not accepted
echo
echo -e "${RED}3.2.3${NC} Ensure secure ICMP redirects are not accepted"
amazon_linux_3_2_3_temp_1="$(egrep -q "^(\s*)net.ipv4.conf.all.secure_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.secure_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.secure_redirects = 1\2/" /etc/sysctl.conf || echo "net.ipv4.conf.all.secure_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_3_temp_1=$?
amazon_linux_3_2_3_temp_2="$(egrep -q "^(\s*)net.ipv4.conf.default.secure_redirects\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.default.secure_redirects\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.default.secure_redirects = 1\2/" /etc/sysctl.conf || echo "net.ipv4.conf.default.secure_redirects = 0" >> /etc/sysctl.conf)"
amazon_linux_3_2_3_temp_2=$?
amazon_linux_3_2_3_temp_3="$(sysctl -w net.ipv4.conf.all.secure_redirects=0)"
amazon_linux_3_2_3_temp_3=$?
amazon_linux_3_2_3_temp_4="$(sysctl -w net.ipv4.conf.default.secure_redirects=0)"
amazon_linux_3_2_3_temp_4=$?
amazon_linux_3_2_3_temp_5="$(sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_2_3_temp_5=$?
if [[ "$amazon_linux_3_2_3_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_2_3_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_2_3_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_2_3_temp_4" -eq 0 ]] && [[ "$amazon_linux_3_2_3_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure secure ICMP redirects are not accepted"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure secure ICMP redirects are not accepted"
fail=$((fail + 1))
fi
# Ensure suspicious packets are logged
echo
echo -e "${RED}3.2.4${NC} Ensure suspicious packets are logged"
amazon_linux_3_2_4_temp_1="$(egrep -q "^(\s*)net.ipv4.conf.all.log_martians\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.all.log_martians\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.all.log_martians = 1\2/" /etc/sysctl.conf || echo "net.ipv4.conf.all.log_martians = 1" >> /etc/sysctl.conf)"
amazon_linux_3_2_4_temp_1=$?
amazon_linux_3_2_4_temp_2="$(egrep -q "^(\s*)net.ipv4.conf.default.log_martians\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.conf.default.log_martians\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.conf.default.log_martians = 1\2/" /etc/sysctl.conf || echo "net.ipv4.conf.default.log_martians = 1" >> /etc/sysctl.conf)"
amazon_linux_3_2_4_temp_2=$?
amazon_linux_3_2_4_temp_3="$(sysctl -w net.ipv4.conf.all.log_martians=1)"
amazon_linux_3_2_4_temp_3=$?
amazon_linux_3_2_4_temp_4="$(sysctl -w net.ipv4.conf.default.log_martians=1)"
amazon_linux_3_2_4_temp_4=$?
amazon_linux_3_2_4_temp_5="$(sysctl -w net.ipv4.route.flush=1)"
amazon_linux_3_2_4_temp_5=$?
if [[ "$amazon_linux_3_2_4_temp_1" -eq 0 ]] && [[ "$amazon_linux_3_2_4_temp_2" -eq 0 ]] && [[ "$amazon_linux_3_2_4_temp_3" -eq 0 ]] && [[ "$amazon_linux_3_2_4_temp_4" -eq 0 ]] && [[ "$amazon_linux_3_2_4_temp_5" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure suspicious packets are logged"
success=$((success + 1))
else
echo -e "${RED}UnableToRemediate:${NC} Ensure suspicious packets are logged"
fail=$((fail + 1))
fi
# Ensure broadcast ICMP requests are ignored
echo
echo -e "${RED}3.2.5${NC} Ensure broadcast ICMP requests are ignored"
amazon_linux_3_2_5_temp_1="$(egrep -q "^(\s*)net.ipv4.icmp_echo_ignore_broadcasts\s*=\s*\S+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^(\s*)net.ipv4.icmp_echo_ignore_broadcasts\s*=\s*\S+(\s*#.*)?\s*$/\1net.ipv4.icmp_echo_ignore_broadcasts = 1\2/" /etc/sysctl.conf || echo "net.ipv4.icmp_echo_ignore_broadcasts = 1" >> /etc/sysctl.conf)"
amazon_linux_3_2_5_temp_1=$?