Skip to content

Commit 0025705

Browse files
author
Jeremi Piotrowski
authored
AKS CIS compliance for Ubuntu 22.04 and 24.04 (#7061)
1 parent ad96213 commit 0025705

106 files changed

Lines changed: 932 additions & 97 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pipelines/templates/.builder-release-template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ steps:
222222
TargetFolder: '$(Build.ArtifactStagingDirectory)'
223223

224224
- task: CopyFiles@2
225-
condition: and(eq(variables.OS_SKU, 'Ubuntu'), in(variables.OS_VERSION, '22.04', '24.04'), eq(variables.FEATURE_FLAGS, 'None'))
225+
condition: and(eq(variables.OS_SKU, 'Ubuntu'), in(variables.OS_VERSION, '22.04', '24.04'), in(variables.FEATURE_FLAGS, 'None', 'cvm'))
226226
displayName: Copy CIS Reports
227227
inputs:
228228
SourceFolder: '$(System.DefaultWorkingDirectory)'

parts/linux/cloud-init/artifacts/cis.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2325
assignFilePermissions() {
@@ -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+
212308
applyCIS() {
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

222331
applyCIS
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
deny=5
2+
unlock_time=900
3+
even_deny_root

parts/linux/cloud-init/artifacts/modprobe-CIS.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ install cramfs /bin/true
1212
blacklist cramfs
1313
# 1.1.1.2 Ensure mounting of freevxfs filesystems is disabled
1414
install freevxfs /bin/true
15+
blacklist freevxfs
1516
# 1.1.1.3 Ensure mounting of jffs2 filesystems is disabled
1617
install jffs2 /bin/true
18+
blacklist jffs2
1719
# 1.1.1.4 Ensure mounting of hfs filesystems is disabled
1820
install hfs /bin/true
21+
blacklist hfs
1922
# 1.1.1.5 Ensure mounting of hfsplus filesystems is disabled
2023
install hfsplus /bin/true
24+
blacklist hfsplus
25+
# 1.1.1.9 Ensure usb-storage kernel module is not available
26+
install usb-storage /bin/true
27+
blacklist usb-storage
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# /etc/pam.d/common-account - authorization settings common to all services
3+
#
4+
# This file is included from other service-specific PAM config files,
5+
# and should contain a list of the authorization modules that define
6+
# the central access policy for use on the system. The default is to
7+
# only deny service to users whose accounts are expired in /etc/shadow.
8+
#
9+
# As of pam 1.0.1-6, this file is managed by pam-auth-update by default.
10+
# To take advantage of this, it is recommended that you configure any
11+
# local modules either before or after the default block, and use
12+
# pam-auth-update to manage selection of other modules. See
13+
# pam-auth-update(8) for details.
14+
#
15+
16+
# here are the per-package modules (the "Primary" block)
17+
account [success=1 new_authtok_reqd=done default=ignore] pam_unix.so
18+
# here's the fallback if no module succeeds
19+
account requisite pam_deny.so
20+
# prime the stack with a positive return value if there isn't one already;
21+
# this avoids us returning an error just because nothing sets a success code
22+
# since the modules above will each just jump around
23+
account required pam_permit.so
24+
# and here are more per-package modules (the "Additional" block)
25+
# end of pam-auth-update config
26+
27+
# CIS
28+
account required pam_faillock.so

parts/linux/cloud-init/artifacts/pam-d-common-auth-2204

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ auth required pam_permit.so
2626

2727
# 5.3.2 Ensure lockout for failed password attempts is configured
2828
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=900
29+
auth [default=die] pam_faillock.so authfail audit deny=5 unlock_time=900

parts/linux/cloud-init/artifacts/pam-d-common-password

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
# here are the per-package modules (the "Primary" block)
2525
password requisite pam_pwquality.so retry=3
26+
password required pam_pwhistory.so use_authtok remember=24 enforce_for_root
27+
# 5.3.3 Ensure password reuse is limited
28+
# 5.3.4 Ensure password hashing algorithm is SHA-512
2629
password [success=1 default=ignore] pam_unix.so obscure use_authtok try_first_pass sha512
2730
# here's the fallback if no module succeeds
2831
password requisite pam_deny.so
@@ -32,7 +35,3 @@ password requisite pam_deny.so
3235
password required pam_permit.so
3336
# and here are more per-package modules (the "Additional" block)
3437
# end of pam-auth-update config
35-
36-
# 5.3.3 Ensure password reuse is limited
37-
# 5.3.4 Ensure password hashing algorithm is SHA-512
38-
password [success=1 default=ignore] pam_unix.so obscure use_authtok try_first_pass sha512 remember=5

parts/linux/cloud-init/artifacts/pam-d-su

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ auth sufficient pam_rootok.so
1414
# (Replaces the `SU_WHEEL_ONLY' option from login.defs)
1515

1616
# 5.6 Ensure access to the su command is restricted
17-
auth required pam_wheel.so use_uid
17+
auth required pam_wheel.so use_uid group=admin
1818

1919
# Uncomment this if you want wheel members to be able to
2020
# su without a password.

parts/linux/cloud-init/artifacts/profile-d-cis.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
# 5.4.4 Ensure default user umask is 027 or more restrictive
44
umask 027
5+
6+
# 5.4.3.2 Ensure default user shell timeout is configured
7+
readonly TMOUT=900 ; export TMOUT
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# 5.3.1 Ensure password creation requirements are configured (Scored)
22

3+
difok=2
34
minlen=14
45
dcredit=-1
56
ucredit=-1
67
ocredit=-1
7-
lcredit=-1
8+
lcredit=-1
9+
maxrepeat=3
10+
maxsequence=3
11+
enforce_for_root

0 commit comments

Comments
 (0)