@@ -18,6 +18,8 @@ assignRootPW() {
1818 echo ' root:' $HASH | /usr/sbin/chpasswd -e || exit $ERR_CIS_ASSIGN_ROOT_PW
1919 fi
2020 set -x
21+ chage --maxdays 90 root
22+ chage --inactive 30 root
2123}
2224
2325assignFilePermissions () {
@@ -209,6 +211,100 @@ function addFailLockDir() {
209211 fi
210212}
211213
214+ configureGrub () {
215+ if ! grep -q apparmor /etc/default/grub.d/99-aks-cis.cfg; then
216+ # shellcheck disable=SC2016
217+ echo ' GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX apparmor=1 security=apparmor"' >> /etc/default/grub.d/99-aks-cis.cfg
218+ fi
219+ cat << "EOF " >/etc/grub.d/09_unrestricted
220+ #!/bin/sh
221+ exec tail -n +3 $0
222+ # This file provides an easy way to add custom menu entries. Simply type the
223+ # menu entries you want to add after this comment. Be careful not to change
224+ # the 'exec tail' line above.
225+
226+ menuentry_id_option="--unrestricted $menuentry_id_option"
227+ EOF
228+ chmod +x /etc/grub.d/09_unrestricted
229+ if ! grep -q superusers /etc/grub.d/40_custom; then
230+ set +x
231+ password=$( openssl rand -hex 64)
232+ hash=$( echo -e " ${password} \n${password} " | grub-mkpasswd-pbkdf2 | tail -n1 | sed -e ' s/.*grub.pbkdf2/grub.pbkdf2/' )
233+ set -x
234+ cat << EOF >>/etc/grub.d/40_custom
235+ set superusers="root"
236+ password_pbkdf2 root ${hash}
237+ EOF
238+ fi
239+ update-grub2 || exit 1
240+ chmod 0600 /boot/grub/grub.cfg
241+ }
242+
243+ prepareTmp () {
244+ local changed=0
245+ if ! grep -q /tmp /etc/fstab; then
246+ # echo 'tmpfs /tmp tmpfs nodev,nosuid,noexec,size=50%,mode=1777' >>/etc/fstab
247+ # changed=1
248+ :
249+ fi
250+ if ! grep -q /dev/shm /etc/fstab; then
251+ echo ' tmpfs /dev/shm tmpfs nodev,nosuid,noexec' >> /etc/fstab
252+ changed=1
253+ fi
254+
255+ if [ " ${changed} " = 1 ]; then
256+ systemctl daemon-reload
257+ mount -o remount /dev/shm
258+ # A noexec /tmp interferes with packer operations
259+ fi
260+ }
261+
262+ configureSsh () {
263+ mkdir -p /etc/ssh/sshd_config.d
264+ cat << EOF >/etc/ssh/sshd_config.d/99-aks-cis.conf
265+ ClientAliveInterval 120
266+ ClientAliveCountMax 3
267+ EOF
268+ chmod 0600 -R /etc/ssh/sshd_config.d/
269+ chmod 0755 /etc/ssh/sshd_config.d
270+ chmod 0600 /etc/ssh/sshd_config
271+ systemctl restart ssh
272+ }
273+
274+ configureSudo () {
275+ cat << EOF >/etc/sudoers.d/99-cis
276+ Defaults logfile="/var/log/sudo.log"
277+ EOF
278+ chmod 0440 /etc/sudoers.d/99-cis
279+ cat << EOF >/etc/logrotate.d/sudo
280+ /var/log/sudo.log {
281+ rotate 5
282+ daily
283+ maxsize 50M
284+ missingok
285+ notifempty
286+ compress
287+ delaycompress
288+ sharedscripts
289+ }
290+ EOF
291+ }
292+
293+ configureRootPath () {
294+ sed -i -e ' s|:/snap/bin||' /etc/sudoers /etc/environment
295+ }
296+
297+ configureLimits () {
298+ mkdir -p /etc/security/limits.d/
299+ cat << EOF >/etc/security/limits.d/99-aks-cis.conf
300+ * hard core 0
301+ EOF
302+ }
303+
304+ configureAzureAgent () {
305+ sed -i -e ' s/\(Provisioning.DeleteRootPassword\).*/\1=n/' /etc/waagent.conf
306+ }
307+
212308applyCIS () {
213309 setPWExpiration
214310 assignRootPW
@@ -217,6 +313,19 @@ applyCIS() {
217313 fixUmaskSettings
218314 maskNfsServer
219315 addFailLockDir
316+ if isMarinerOrAzureLinux " $OS " || isAzureLinuxOSGuard " $OS " " $OS_VARIANT " || isFlatcar " $OS " ; then
317+ echo " Further functions only work for Ubuntu"
318+ return
319+ fi
320+ configureGrub
321+ prepareTmp
322+ configureSsh
323+ configureSudo
324+ configureRootPath
325+ configureLimits
326+ configureAzureAgent
327+ # Apply system configuration to running system
328+ sysctl --write --system
220329}
221330
222331applyCIS
0 commit comments