Add support for IPv6 to machine and cluster chart
[icn.git] / deploy / cluster / resources / harden_os.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 function append {
5     local -r line=$1
6     local -r file=$2
7     if [[ $(grep -c "${line}" ${file}) == 0 ]]; then
8         echo "${line}" >>${file}
9     fi
10 }
11
12 function replace_or_append {
13     local -r pattern=$1
14     local -r line=$2
15     local -r file=$3
16     sed -i -E '/'"${pattern}"'/ s/.*/'"${line}"'/w /tmp/changelog.txt' ${file}
17     if [[ ! -s /tmp/changelog.txt ]]; then
18         echo "${line}" >>${file}
19     fi
20 }
21
22 function replace_or_insert_before {
23     local -r pattern=$1
24     local -r line=$2
25     local -r before=$3
26     local -r file=$4
27     sed -i -E '/'"${pattern}"'/ s/.*/'"${line}"'/w /tmp/changelog.txt' ${file}
28     if [[ ! -s /tmp/changelog.txt ]]; then
29         cp ${file} ${file}.bak
30         awk '/'"${before}"'/ {print "'"${line}"'"}1' ${file}.bak >${file}
31         rm ${file}.bak
32     fi
33 }
34
35 function replace_or_insert_after {
36     local -r pattern=$1
37     local -r line=$2
38     local -r after=$3
39     local -r file=$4
40     sed -i -E '/'"${pattern}"'/ s/.*/'"${line}"'/w /tmp/changelog.txt' ${file}
41     if [[ ! -s /tmp/changelog.txt ]]; then
42         cp ${file} ${file}.bak
43         awk '/'"${after}"'/ {print; print "'"${line}"'"; next}1' ${file}.bak >${file}
44         rm ${file}.bak
45     fi
46 }
47
48 # Check for GRUB boot password
49 # Set user and password in GRUB configuration
50 # Password hash generated with grub-mkpasswd-pbkdf2, password: root
51 # TODO This is currently disabled as it interferes with the reboot in set_kernel_cmdline.sh
52 # cat <<END >>/etc/grub.d/00_header
53 # cat <<EOF
54 # set superusers="root"
55 # password_pbkdf2 root grub.pbkdf2.sha512.10000.E4F52CBE09DFC3C338A314E9EDC8AA682BB2832A35FF2FF9E1D12D30EB3D58E9DDE023F88B8A82CD7BF5FC8138500CD0E67174EBA6EFACF98635A693C5AD4BB9.BB41DC42C8E2C68723B94F14F5F1E43845054A7D443C80F074E9B41C44927FEA2832B0E23C83E6B7C5E1D740B67756FA3093DA9A99B2E461A20F4831BBB289AF
56 # EOF
57 # END
58 # update-grub
59
60 # Check password hashing methods
61 # Check /etc PAM and configure algorithm rounds
62 sed -i -E 's/^(password\s+.*sha512)$/\1 rounds=10000/' /etc/pam.d/common-password
63 echo "Passwords in /etc/shadow must be encrypted with new values"
64
65 # Check group password hashing rounds
66 # Configure minimum encryption algorithm rounds in /etc/login.defs
67 replace_or_insert_after '^\s*SHA_CRYPT_MIN_ROUNDS\s+' 'SHA_CRYPT_MIN_ROUNDS 10000' '^#\s+SHA_CRYPT_MIN_ROUNDS' /etc/login.defs
68 # Configure maximum encryption algorithm rounds in /etc/login.defs
69 replace_or_insert_after '^\s*SHA_CRYPT_MAX_ROUNDS\s+' 'SHA_CRYPT_MAX_ROUNDS 10000' '^#\s+SHA_CRYPT_MAX_ROUNDS' /etc/login.defs
70
71 # Checking user password aging
72 # Set PASS_MAX_DAYS option in /etc/login.defs
73 # PASS_MAX_DAYS of 99999 is considered unconfigued by lynis
74 replace_or_insert_before '^\s*PASS_MAX_DAYS\s+' 'PASS_MAX_DAYS 99000' '^PASS_MIN_DAYS' /etc/login.defs
75
76 # Default umask values
77 # Set default umask in /etc/login.defs to more strict
78 replace_or_append '^\s*UMASK\s+' 'UMASK 027' /etc/login.defs
79
80 # Check for presence of USBGuard
81 # Ensure USBGuard is installed
82 apt-get -y install usbguard
83 # TODO USB hubs and HID device must be enabled for BMC Console Redirection
84 # Authorize USB hubs in USBGuard daemon
85 append 'allow with-interface equals { 09:00:\* }' /etc/usbguard/rules.conf
86 # Authorize multi-function Human Interface Devices
87 append 'allow with-interface equals { 03:\*:\* 03:\*:\* }' /etc/usbguard/rules.conf
88 # Set PresentControllerPolicy to apply-policy in USBGuard daemon
89 sed -i -E 's/^PresentControllerPolicy\s*=\s*keep/PresentControllerPolicy=apply-policy/' /etc/usbguard/usbguard-daemon.conf
90 chmod 0600 /etc/usbguard/rules.conf
91 systemctl restart usbguard
92
93 # Checking for debsums utility
94 # Install debsums utility
95 apt-get -y install debsums
96
97 # Check SSH specific defined options
98 # Disable AllowTcpForwarding
99 replace_or_append '^\s*AllowTcpForwarding\s+' 'AllowTcpForwarding no' /etc/ssh/sshd_config
100 # Set ClientAliveCountMax to 2
101 replace_or_append '^\s*ClientAliveCountMax\s+' 'ClientAliveCountMax 2' /etc/ssh/sshd_config
102 # Set MaxAuthTries to 3
103 replace_or_append '^\s*MaxAuthTries\s+' 'MaxAuthTries 3' /etc/ssh/sshd_config
104 # Set MaxSessions to 2
105 # TODO MaxSessions of 2 prevents lynis from running under bluval
106 replace_or_append '^\s*MaxSessions\s+' 'MaxSessions 10' /etc/ssh/sshd_config
107 # Set server Port to 2222
108 # TODO lynis, etc. robot files need to be updated to handle a different port
109 replace_or_append '^\s*Port\s+' 'Port 22' /etc/ssh/sshd_config
110 # Set client Port to 2222
111 # TODO lynis, etc. robot files need to be updated to handle a different port
112 replace_or_append '^\s*Port\s+' '    Port 22' /etc/ssh/ssh_config
113 # Disable TCPKeepAlive
114 replace_or_append '^\s*TCPKeepAlive\s+' 'TCPKeepAlive no' /etc/ssh/sshd_config
115 # Restrict SSH to administrators
116 replace_or_append '^\s*AllowGroups\s+' 'AllowGroups root sudo' /etc/ssh/sshd_config
117 # Restart SSH
118 systemctl restart ssh
119
120 # Disabling Apport is necessary to prevent it from overriding
121 # fs.suid_dumpable in sysctl conf below
122 replace_or_append '^enabled=' 'enabled=0' /etc/default/apport
123
124 # The fs.protected_fifos setting below in 99-zzz-icn.conf does not
125 # stick on reboot.  The setting in /usr/lib takes precendence, but per
126 # the sysctl.d manpage, a file with the same name in /etc will
127 # override /usr/lib.
128 #
129 # Reference:
130 # https://groups.google.com/g/linux.debian.bugs.dist/c/cYMr7EXCcWY?pli=1
131 sed -e 's/fs.protected_fifos = .*/fs.protected_fifos = 2/' /usr/lib/sysctl.d/protect-links.conf > /etc/sysctl.d/protect-links.conf
132
133 # Check sysctl key pairs in scan profile
134 cat <<EOF >/etc/sysctl.d/99-zzz-icn.conf
135 dev.tty.ldisc_autoload = 0
136 fs.protected_fifos = 2
137 fs.suid_dumpable = 0
138 kernel.core_uses_pid = 1
139 kernel.dmesg_restrict = 1
140 kernel.kptr_restrict = 2
141 # TODO module loading required by accelerator drivers
142 # kernel.modules_disabled = 1
143 kernel.sysrq = 0
144 kernel.unprivileged_bpf_disabled = 1
145 net.core.bpf_jit_harden = 2
146 net.ipv4.conf.all.accept_redirects = 0
147 # TODO forwarding required by k8s
148 # net.ipv4.conf.all.forwarding = 0
149 net.ipv4.conf.all.log_martians = 1
150 net.ipv4.conf.all.rp_filter = 1
151 net.ipv4.conf.all.send_redirects = 0
152 net.ipv4.conf.default.accept_redirects = 0
153 net.ipv4.conf.default.accept_source_route = 0
154 net.ipv4.conf.default.log_martians = 1
155 net.ipv6.conf.all.accept_redirects = 0
156 # TODO forwarding required by k8s
157 net.ipv6.conf.all.forwarding = 1
158 net.ipv6.conf.default.accept_redirects = 0
159 EOF
160 sysctl --system
161
162 # Check compiler permissions
163 # Uninstall compilers
164 apt-get -y remove gcc binutils