-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhardening_script.sh
1501 lines (925 loc) · 56.7 KB
/
hardening_script.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
# This script has been written to semi-automate the hardening of stand-alone Fedora machines for a specific use case
# It is based on the 'CIS Red Hat Enterprise Linux 7 Benchmark' Level 1 for Workstations but has been amended for Fedora
# The machines will be used for development so some controls have been removed to ensure it doesn't impact usability
# The removed controls are still in this script but commented-out so can be easily added back if needed
#########################################################################################################################################
# Confirming that the script has been run with sudo
if [[ $EUID -ne 0 ]]; then
echo "You need to run this script as root (with sudo)"
exit
fi
echo "[i] Beginning hardening process"
#########################################################################################################################################
# 1.1.1.1 Ensure mounting of cramfs filesystems is disabled
echo "[i] Disabling the mounting of cramfs filesystems"
if [ -f "/etc/modprobe.d/CIS.conf" ]
echo "install cramfs /bin/true" >> /etc/modprobe.d/CIS.conf
else
echo "install cramfs /bin/true" > /etc/modprobe.d/CIS.conf
rmmod cramfs
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.2 Ensure mounting of freevxfs filesystems is disabled
echo "[i] Disabling the mounting of freevxfs filesystems"
echo "install freevxfs /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod freevxfs
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.3 Ensure mounting of jffs2 filesystems is disabled
echo "[i] Disabling the mounting of jffs2 filesystems"
echo "install jffs2 /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod jffs2
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.4 Ensure mounting of hfs filesystems is disabled
echo "[i] Disabling the mounting of hfs filesystems"
echo "install hfs /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod hfs
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.5 Ensure mounting of hfsplus filesystems is disabled
echo "[i] Disabling the mounting of hfsplus filesystems"
echo "install hfsplus /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod hfsplus
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.6 Ensure mounting of squashfs filesystems is disabled
echo "[i] Disabling the mounting of squashfs filesystems"
echo "install squashfs /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod squashfs
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.7 Ensure mounting of udf filesystems is disabled
echo "[i] Disabling the mounting of udf filesystems"
echo "install udf /bin/true" >> /etc/modprobe.d/CIS.conf
rmmod udf
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.1.8 Ensure mounting of FAT filesystems is disabled
# echo "[i] Disabling the mounting of FAT filesystems"
# echo "install vfat /bin/true" >> /etc/modprobe.d/CIS.conf
# rmmod vfat
# This has been disabled as there is a need to use portable USB drives which are often FAT formatted.
#########################################################################################################################################
# 1.1.3 Ensure nodev option set on /tmp partition
# 1.1.4 Ensure nosuid option set on /tmp partition
# 1.1.5 Ensure noexec option set on /tmp partition
echo "[i] Setting nodev, nosuid and noexec options on the /tmp partition"
systemctl unmask tmp.mount
systemctl enable tmp.mount
sed -i -e 's/\(Options=\).*/\1mode=1777,strictatime,noexec,nodev,nosuid/' /etc/systemd/system/local-fs.target.wants/tmp.mount
mount -o remount,nodev,nosuid,noexec /tmp
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.8 Ensure nodev option set on /var/tmp partition
# 1.1.9 Ensure nosuid option set on /var/tmp partition
# 1.1.10 Ensure noexec option set on /var/tmp partition
echo "[i] Setting nodev, nosuid and noexec options on the /var/tmp partition"
LINEVARTMP="tmpfs /var/tmp tmpfs nosuid,noexec,nodev 0 0"
grep -F "$LINEVARTMP" /etc/fstab || echo "$LINEVARTMP" | tee -a /etc/fstab > /dev/null
mount -o remount,nodev,nosuid,noexec /var/tmp
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.14 Ensure nodev option set on /home partition
echo "[i] Setting nodev option on the /home partition"
echo "[i] If you have a separate home partition, you need to provide it's name"
echo "[i] If a separate home partition doesn't exist, leave this blank."
echo "[i] Home partition example: /dev/xvda1"
read -p "[?] Enter home partition: " HOME_PARTITION
if [ -b $HOME_PARTITION ]
then
LINEHOME="$HOME_PARTITION /home ext4 rw,relatime,nodev,data=ordered 0 0"
grep -F "$LINEHOME" /etc/fstab || echo "$LINEHOME" | tee -a /etc/fstab > /dev/null
fi
mount -o remount,nodev /home
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.15 Ensure nodev option set on /dev/shm partition
# 1.1.16 Ensure nosuid option set on /dev/shm partition
# 1.1.17 Ensure noexec option set on /dev/shm partition
echo "[i] Setting nodev, nosuid and noexec options on the /dev/shm partition"
LINEDEVSHM="tmpfs /dev/shm tmpfs nosuid,noexec,nodev,relatime,rw 0 0"
grep -F "$LINEDEVSHM" /etc/fstab || echo "$LINEDEVSHM" | tee -a /etc/fstab > /dev/null
mount -o remount,nodev,nosuid,noexec /dev/shm
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.1.21 Ensure sticky bit is set on all world-writable directories
echo "[i] Setting the sticky bit on all world-writable directories"
df --local -P | awk {'if (NR!=1) print $6'} | xargs -I '{}' find '{}' -xdev -type d -perm -0002 2>/dev/null | xargs chmod a+t
#########################################################################################################################################
# 1.2.2 Ensure gpgcheck is globally activated
echo "[i] Globally activating gpgcheck"
sed -i 's/gpgcheck=0/gpgcheck=1/' /etc/yum.conf
#########################################################################################################################################
# 1.3.1 Ensure AIDE is installed
echo "[i] Installing, configuring and initialising AIDE (the file integrity checking tool)"
# Installing AIDE:
yum -y install aide
# Generating AIDE config file:
update-aide.conf
cp /var/lib/aide/aide.conf.autogenerated /etc/aide/aide.conf
# Updating AIDE config file:
LINESAIDE=( "!/var/lib/lxcfs" "!/var/lib/private/systemd" "!/var/log/journal" )
AIDECONFFILE=/etc/aide/aide.conf
for current_line in "${LINESAIDE[@]}"
do
grep -F "$current_line" "$AIDECONFFILE" || echo "$current_line" | tee -a "$AIDECONFFILE" > /dev/null
done
# Inistialising AIDE
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.3.2 Ensure filesystem integrity is regularly checked
echo "[i] Creating a cron job to regularly check filesystem integrity using AIDE"
LINEAIDECRON="0 5 * * * /usr/bin/aide.wrapper --config /etc/aide/aide.conf --check"
AIDECRONFILE=/home/tmp.cron
crontab -l -u root 2>/dev/null
if [ $? -eq 0 ]
then
crontab -u root -l > $AIDECRONFILE
else
touch $AIDECRONFILE
fi
grep -qF "$LINEAIDECRON" "$AIDECRONFILE" || echo "$LINEAIDECRON" | tee -a "$AIDECRONFILE" > /dev/null
crontab -u root $AIDECRONFILE
rm $AIDECRONFILE
fi
#########################################################################################################################################
# 1.4.1 Ensure permissions on bootloader config are configured
echo "[i] Setting correct permissions for the bootloader config"
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
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.4.2 Ensure bootloader password is set
echo "[i] Setting bootloader password"
grub2-setpassword
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.4.3 Ensure authentication required for single user mode
echo "[i] Configuring 'single user mode' to require authentication"
if grep -q "ExecStart=" /usr/lib/systemd/system/rescue.service; then
sed -i 's/^ExecStart=.*/ExecStart=-/bin/sh -c "/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"/' /usr/lib/systemd/system/rescue.service
else
echo "ExecStart=-/bin/sh -c "/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"" >> /usr/lib/systemd/system/rescue.service
fi
if grep -q "ExecStart=" /usr/lib/systemd/system/emergency.service; then
sed -i 's/^ExecStart=.*/ExecStart=-/bin/sh -c "/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"/' /usr/lib/systemd/system/emergency.service
else
echo "ExecStart=-/bin/sh -c "/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"" >> /usr/lib/systemd/system/emergency.service
fi
#########################################################################################################################################
# 1.5.1 Ensure core dumps are restricted
echo "[i] Ensuring core dumps are restricted"
DUMPLINE="* hard core 0"
DUMPFILE=/etc/security/limits.conf
grep -qF "$DUMPLINE" "$DUMPFILE" || echo "$DUMPLINE" | tee -a "$DUMPFILE" > /dev/null
DUMPABLELINE="fs.suid_dumpable=0"
DUMPABLEFILE=/etc/sysctl.conf
grep -qF "$DUMPABLELINE" "$DUMPABLEFILE" || echo "$DUMPABLELINE" | tee -a "$DUMPABLEFILE" > /dev/null
sysctl -w fs.suid_dumpable=0
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.5.3 Ensure address space layout randomization (ASLR) is enabled
echo "[i] Enabling address space layout randomization (ASLR)"
if grep -q "^kernel.randomize_va_space" /etc/sysctl.conf; then
sed -i 's/^kernel.randomize_va_space.*/kernel.randomize_va_space = 2/' /etc/sysctl.conf
else
echo "kernel.randomize_va_space = 2" >> /etc/sysctl.conf
fi
sysctl -w kernel.randomize_va_space=2
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.5.4 Ensure prelink is disabled
echo "[i] Restoring the prelink binaries to normal"
prelink -ua
echo "[i] Uninstalling prelink"
yum -y remove prelink
#########################################################################################################################################
# 1.7.1.1 Ensure message of the day is configured properly
echo "[i] Creating the message of the day"
echo "Unauthorised use of this system is an offence under the Computer Misuse Act 1990. All activity may be monitored and reported." > /etc/motd
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.1.2 Ensure local login warning banner is configured properly
echo "[i] Creating the local login warning banner"
echo "Unauthorised use of this system is an offence under the Computer Misuse Act 1990. All activity may be monitored and reported." > /etc/issue
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.1.3 Ensure remote login warning banner is configured properly
echo "[i] Creating the remote login warning banner"
echo "Unauthorised use of this system is an offence under the Computer Misuse Act 1990. All activity may be monitored and reported." > /etc/issue.net
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.1.4 Ensure permissions on /etc/motd are configured
echo "[i] Setting correct permissions on /etc/motd"
chown root:root /etc/motd
chmod 644 /etc/motd
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.1.5 Ensure permissions on /etc/issue are configured
echo "[i] Setting correct permissions on /etc/issue"
chown root:root /etc/issue
chmod 644 /etc/issue
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.1.6 Ensure permissions on /etc/issue.net are configured
echo "[i] Setting correct permissions on /etc/issue.net"
chown root:root /etc/issue.net
chmod 644 /etc/issue.net
#---------------------------------------------------------------------------------------------------------------------------------------#
# 1.7.2 Ensure GDM login banner is configured
echo "[i] Setting the GDM login banner"
echo "user-db:user" > /etc/dconf/profile/gdm
echo "system-db:gdm" >> /etc/dconf/profile/gdm
echo "file-db:/usr/share/gdm/greeter-dconf-defaults" >> /etc/dconf/profile/gdm
echo "[org/gnome/login-screen]" > /etc/dconf/db/gdm.d/01-banner-message
echo "banner-message-enable=true" >> /etc/dconf/db/gdm.d/01-banner-message
echo "banner-message-text='Unauthorised use of this system is an offence under the Computer Misuse Act 1990. All activity may be monitored and reported." >> /etc/dconf/db/gdm.d/01-banner-message
dconf update
#########################################################################################################################################
# 1.8.1 Ensure updates, patches, and additional security software are installed
echo "[i] Patching, updating and applying security fixes"
yum -y update --security
#########################################################################################################################################
# 2.1.1 Ensure chargen services are not enabled
echo "[i] Turning off chargen services"
chkconfig chargen-dgram off
chkconfig chargen-stream off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.2 Ensure daytime services are not enabled
echo "[i] Turning off daytime services"
chkconfig daytime-dgram off
chkconfig daytime-stream off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.3 Ensure discard services are not enabled
echo "[i] Turning off discard services"
chkconfig discard-dgram off
chkconfig discard-stream off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.4 Ensure echo services are not enabled
echo "[i] Turning off echo services"
chkconfig echo-dgram off
chkconfig echo-stream off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.5 Ensure time services are not enabled
echo "[i] Turning off time services"
chkconfig time-dgram off
chkconfig time-stream off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.6 Ensure tftp server is not enabled
echo "[i] Turning off tftp server"
chkconfig tftp off
#---------------------------------------------------------------------------------------------------------------------------------------#
# 2.1.7 Ensure xinetd is not enabled
echo "[i] Turning off xinetd"
systemctl disable xinetd
#########################################################################################################################################
# 2.2.1.2 Ensure ntp is configured
echo "[i] Configuring ntp"
sed -i 's/^restrict -4.*/restrict -4 default kod nomodify notrap nopeer noquery/' /etc/ntp.conf
sed -i 's/^restrict -6.*/restrict -6 default kod nomodify notrap nopeer noquery/' /etc/ntp.conf
# Adding NTP servers for the UK
if grep -q "^server.*" /etc/ntp.conf; then
sed '/^server.*/d' /etc/ntp.conf
echo "server 0.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 1.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 2.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 3.uk.pool.ntp.org" >> /etc/ntp.conf
else
echo "server 0.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 1.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 2.uk.pool.ntp.org" >> /etc/ntp.conf
echo "server 3.uk.pool.ntp.org" >> /etc/ntp.conf
fi
# Adding '-u ntp:ntp' to the /etc/sysconfig/ntpd
if grep -q "^OPTIONS=" /etc/sysconfig/ntpd; then
sed -i 's/^OPTIONS=.*/OPTIONS="-u ntp:ntp"/' /etc/sysconfig/ntpd
else
echo "OPTIONS=-\"-u ntp:ntp\"/" >> /etc/init.d/ntp
fi
#---------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------------------------#
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
#########################################################################################################################################
# 2.2.1.3 Ensure chrony is configured (Scored)
echo "[i] Configuring chrony"
# Adding NTP servers for the UK
if grep -q "^server.*" /etc/chrony/chrony.conf; then
sed '/^server.*/d' /etc/chrony/chrony.conf
echo "server 0.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 1.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 2.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 3.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
else
echo "server 0.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 1.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 2.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
echo "server 3.uk.pool.ntp.org" >> /etc/chrony/chrony.conf
fi
#########################################################################################################################################
# 2.2.3 Ensure Avahi Server is not enabled (Scored)
echo "[i] Disabling Avahi Server"
systemctl disable avahi-daemon
#########################################################################################################################################
# 2.2.5 Ensure DHCP Server is not enabled (Scored)
echo "[i] Disabling DHCP Server"
systemctl disable isc-dhcp-server
systemctl disable isc-dhcp-server6
#########################################################################################################################################
# 2.2.6 Ensure LDAP server is not enabled (Scored)
echo "[i] Disabling LDAP server"
systemctl disable slapd
#########################################################################################################################################
# 2.2.7 Ensure NFS and RPC are not enabled (Scored)
echo "[i] Disabling NFS and RPC"
systemctl disable nfs-server
systemctl disable rpcbind
#########################################################################################################################################
# 2.2.8 Ensure DNS Server is not enabled (Scored)
echo "[i] Disabling DNS Server"
systemctl disable bind9
#########################################################################################################################################
# 2.2.9 Ensure FTP Server is not enabled (Scored)
echo "[i] Disabling FTP Server"
systemctl disable vsftpd
#########################################################################################################################################
# 2.2.10 Ensure HTTP Server is not enabled (Scored)
echo "[i] Disabling HTTP Server"
systemctl disable apache2
#########################################################################################################################################
# 2.2.11 Ensure IMAP and POP3 server is not enabled (Scored)
echo "[i] Disabling IMAP and POP3 Server"
systemctl disable dovecot
#########################################################################################################################################
# 2.2.12 Ensure Samba is not enabled (Scored)
echo "[i] Disabling Samba"
systemctl disable smbd
#########################################################################################################################################
# 2.2.13 Ensure HTTP Proxy Server is not enabled (Scored)
echo "[i] Disabling HTTP Proxy Server"
systemctl disable squid
#########################################################################################################################################
# 2.2.14 Ensure SNMP Server is not enabled (Scored)
echo "[i] Disabling SNMP Server"
systemctl disable snmpd
#########################################################################################################################################
# 2.2.15 Ensure mail transfer agent is configured for local-only mode (Scored)
echo "[i] Configuring mail transfer agent for local-only mode"
if grep -q "^inet_interfaces = " /etc/postfix/main.cf; then
sed -i 's/^inet_interfaces.*/inet_interface = loopback-only/' /etc/postfix/main.cf
else
echo "inet_interfaces = loopback-only" >> /etc/postfix/main.cf
fi
systemctl restart postfix
#########################################################################################################################################
# 2.2.16 Ensure rsync service is not enabled (Scored)
echo "[i] Disabling rsync service"
systemctl disable rsync
#########################################################################################################################################
# 2.2.17 Ensure NIS Server is not enabled (Scored)
echo "[i] Disabling NIS Server"
systemctl disable nis
#########################################################################################################################################
# 2.3.1 Ensure NIS Client is not installed (Scored)
echo "[i] Uninstalling NIS client"
apt remove -y nis
#########################################################################################################################################
# 2.3.2 Ensure rsh client is not installed (Scored)
echo "[i] Uninstalling the rsh client"
apt remove -y rsh-client rsh-redone-client
#########################################################################################################################################
# 2.3.3 Ensure talk client is not installed (Scored)
echo "[i] Uninstalling the talk client"
apt remove -y talk
#########################################################################################################################################
# 2.3.4 Ensure telnet client is not installed (Scored)
echo "[i] Uninstalling the telnet client"
apt remove -y telnet
#########################################################################################################################################
# 2.3.5 Ensure LDAP client is not installed (Scored)
echo "[i] Uninstalling the LDAP client"
apt remove -y ldap-utils
#########################################################################################################################################
# 3.1.1 Ensure IP forwarding is disabled (Scored)
# 3.1.2 Ensure packet redirect sending is disabled (Scored)
# This are only required if the system is to act as a host only. If needed, run the 'workstation_cis_hardening_level1_scored_HOSTONLY.sh' script to apply these controls
#########################################################################################################################################
# 3.2.1 Ensure source routed packets are not accepted (Scored)
echo "[i] Ensuring source routed packets are not accepted"
if grep -q "^net.ipv4.conf.all.accept_source_route" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.all.accept_source_route.*/net.ipv4.conf.all.accept_source_route = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.all.accept_source_route = 0" >> /etc/sysctl.conf
fi
if grep -q "^net.ipv4.conf.default.accept_source_route" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.default.accept_source_route.*/net.ipv4.conf.default.accept_source_route = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.default.accept_source_route = 0" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.conf.all.accept_source_route=0
sysctl -w net.ipv4.conf.default.accept_source_route=0
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.2 Ensure ICMP redirects are not accepted (Scored)
echo "[i] Ensuring ICMP redirects are not accepted"
if grep -q "^net.ipv4.conf.all.accept_redirects" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.all.accept_redirects.*/net.ipv4.conf.all.accept_redirects = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.conf
fi
if grep -q "^net.ipv4.conf.default.accept_redirects" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.default.accept_redirects.*/net.ipv4.conf.default.accept_redirects = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.default.accept_redirects = 0" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.conf.all.accept_redirects=0
sysctl -w net.ipv4.conf.default.accept_redirects=0
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.3 Ensure secure ICMP redirects are not accepted (Scored)
echo "[i] Ensuring secure ICMP redirects are not accepted"
if grep -q "^net.ipv4.conf.all.secure_redirects" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.all.secure_redirects.*/net.ipv4.conf.all.secure_redirects = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.all.secure_redirects = 0" >> /etc/sysctl.conf
fi
if grep -q "^net.ipv4.conf.default.secure_redirects" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.default.secure_redirects.*/net.ipv4.conf.default.secure_redirects = 0/' /etc/sysctl.conf
else
echo "net.ipv4.conf.default.secure_redirects = 0" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.conf.all.secure_redirects=0
sysctl -w net.ipv4.conf.default.secure_redirects=0
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.4 Ensure suspicious packets are logged (Scored)
echo "[i] Ensuring suspicious packets are logged"
if grep -q "^net.ipv4.conf.all.log_martians" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.all.log_martians.*/net.ipv4.conf.all.log_martians = 1/' /etc/sysctl.conf
else
echo "net.ipv4.conf.all.log_martians = 1" >> /etc/sysctl.conf
fi
if grep -q "^net.ipv4.conf.default.log_martians" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.default.log_martians.*/net.ipv4.conf.default.log_martians = 1/' /etc/sysctl.conf
else
echo "net.ipv4.conf.default.log_martians = 1" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.conf.all.log_martians=1
sysctl -w net.ipv4.conf.default.log_martians=1
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.5 Ensure broadcast ICMP requests are ignored (Scored)
echo "[i] Ensuring broadcast ICMP requests are ignored"
if grep -q "^net.ipv4.icmp_echo_ignore_broadcasts" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.icmp_echo_ignore_broadcasts.*/net.ipv4.icmp_echo_ignore_broadcasts = 1/' /etc/sysctl.conf
else
echo "net.ipv4.icmp_echo_ignore_broadcasts = 1" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.6 Ensure bogus ICMP responses are ignored (Scored)
echo "[i] Ensuring bogus ICMP responses are ignored"
if grep -q "^net.ipv4.icmp_ignore_bogus_error_responses" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.icmp_ignore_bogus_error_responses.*/net.ipv4.icmp_ignore_bogus_error_responses = 1/' /etc/sysctl.conf
else
echo "net.ipv4.icmp_ignore_bogus_error_responses = 1" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.icmp_ignore_bogus_error_responses=1
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.7 Ensure Reverse Path Filtering is enabled (Scored)
echo "[i] Ensuring Reverse Path Filtering is enabled"
if grep -q "^net.ipv4.conf.all.rp_filter" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.all.rp_filter.*/net.ipv4.conf.all.rp_filter = 1/' /etc/sysctl.conf
else
echo "net.ipv4.conf.all.rp_filter = 1" >> /etc/sysctl.conf
fi
if grep -q "^net.ipv4.conf.default.rp_filter" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.conf.default.rp_filter.*/net.ipv4.conf.default.rp_filter = 1/' /etc/sysctl.conf
else
echo "net.ipv4.conf.default.rp_filter = 1" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.conf.all.rp_filter=1
sysctl -w net.ipv4.conf.default.rp_filter=1
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.2.8 Ensure TCP SYN Cookies is enabled (Scored)
echo "[i] Ensuring TCP SYN Cookies is enabled"
if grep -q "^net.ipv4.tcp_syncookies" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.tcp_syncookies.*/net.ipv4.tcp_syncookies = 1/' /etc/sysctl.conf
else
echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
fi
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.route.flush=1
#########################################################################################################################################
# 3.4.1 Ensure TCP Wrappers is installed (Scored)
echo "[i] Installing TCP Wrappers"
apt install -y tcpd
#########################################################################################################################################
# 3.4.2 Ensure /etc/hosts.allow is configured (Scored)
# This control is dependent on the individual organisation so will need to be set manually
# By default, nothing is in the hosts.allow so when we generate the hosts.deny in the next section, no IP addresses with be permitted to connect with the host
#########################################################################################################################################
# 3.4.3 Ensure /etc/hosts.deny is configured (Scored)
echo "[i] The hosts.deny file is being created with a default 'deny all' rule"
echo "ALL: ALL" >> /etc/hosts.deny
#########################################################################################################################################
# 3.4.4 Ensure permissions on /etc/hosts.allow are configured (Scored)
echo "[i] Setting the correct permissions for the 'hosts.allow' file"
chown root:root /etc/hosts.allow
chmod 644 /etc/hosts.allow
#########################################################################################################################################
# 3.4.4 Ensure permissions on /etc/hosts.deny are configured (Scored)
echo "[i] Setting the correct permissions for the 'hosts.deny' file"
chown root:root /etc/hosts.deny
chmod 644 /etc/hosts.deny
#########################################################################################################################################
# 3.6.1 Ensure iptables is installed (Scored)
# 3.6.2 Ensure default deny firewall policy (Scored)
# 3.6.3 Ensure loopback traffic is configured (Scored)
# 3.6.5 Ensure firewall rules exist for all open ports (Scored)
echo "[i] Installing iptables"
apt install -y iptables
echo "[i] Flushing iptable rules"
iptables -F
echo "[i] Adding default deny firewall policy"
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
echo "[i] Configuring loopback traffic rules within firewall policy"
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -s 127.0.0.0/8 -j DROP
echo "[i] Opening inbound ssh (tcp port 22) connections within the firewall policy"
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
echo "[i] All additional rules will need to be added manually if required"
#########################################################################################################################################
# 4.2.1.1 Ensure rsyslog Service is enabled (Scored)
# 4.2.3 Ensure rsyslog or syslog-ng is installed
# Although 4.2.3 should come later, rsyslog needs to be confirmed as installed now otherwise the other controls would fail
apt install -y rsyslog
apt install -y syslog-ng
echo "[i] Enabling rsyslog service"
systemctl enable rsyslog
#########################################################################################################################################
# 4.2.1.3 Ensure rsyslog default file permissions configured (Scored)
echo "[i] Configuring rsyslog default file permissions"
if grep -q "^$FileCreateMode" /etc/sysctl.conf; then
sed -i 's/^$FileCreateMode.*/$FileCreateMode 0640/' /etc/sysctl.conf
else
echo "$FileCreateMode 0640" >> /etc/sysctl.conf
fi
#########################################################################################################################################
# 4.2.1.4 Ensure rsyslog is configured to send logs to a remote log host server (Scored)
# This requires the system administrator to manually add the url of a log host
#########################################################################################################################################
# 4.2.2.1 Ensure syslog-ng service is enabled (Scored)
echo "[i] Enabling syslog-ng service"
update-rc.d syslog-ng enable
#########################################################################################################################################
# 4.2.2.3 Ensure syslog-ng default file permissions configured (Scored)
echo "[i] Configuring the syslog-ng default file permissions"
if grep -q "^options {" /etc/syslog-ng/syslog-ng.conf; then
sed -i 's/^options {.*/options { chain_hostnames(off); flush_lines(0); perm(0640); stats_freq(3600); threaded(yes); };/' /etc/syslog-ng/syslog-ng.conf
else
echo "options { chain_hostnames(off); flush_lines(0); perm(0640); stats_freq(3600); threaded(yes); };" >> /etc/syslog-ng/syslog-ng.conf
fi
#########################################################################################################################################
# 4.2.3 Ensure rsyslog or syslog-ng is installed
# The installation was already carried out during the '4.2.1.1' control above
#########################################################################################################################################
# 4.2.4 Ensure permissions on all logfiles are configured (Scored)
echo "[i] Setting correct permissions on all log files"
chmod -R g-wx, o-rwx /var/log/*
#########################################################################################################################################
# 5.1.1 Ensure cron daemon is enabled (Scored)
echo "[i] Enabling the cron daemon"
systemctl enable cron
#########################################################################################################################################
# 5.1.2 Ensure permissions on /etc/crontab are configured (Scored)
echo "[i] Setting the correct permissions on /etc/crontab"
chown root:root /etc/crontab
chmod og-rwx /etc/crontab
#########################################################################################################################################
# 5.1.3 Ensure permissions on /etc/cron.hourly are configured (Scored)
echo "[i] Setting the correct permissions on /etc/cron.hourly"
chown root:root /etc/cron.hourly