Skip to content

Commit 9397aa0

Browse files
authored
Merge pull request #2136 from larsewi/directives
ENT-13766: Fix sshd hardening being overridden by drop-in configs
2 parents 9c58231 + a5e9663 commit 9397aa0

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

ci/cfengine-build-host-setup.cf

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ bundle agent cfengine_build_host_setup
182182

183183
vars:
184184
"suse_users_and_groups" slist => { "daemon", "bin", "sys" };
185+
"sshd_hardening_directives" slist => {
186+
"PermitRootLogin",
187+
"PasswordAuthentication",
188+
"KbdInteractiveAuthentication",
189+
"ChallengeResponseAuthentication",
190+
};
191+
"sshd_config_files" slist => findfiles("/etc/ssh/sshd_config", "/etc/ssh/sshd_config.d/*.conf");
192+
debian|ubuntu::
193+
"sshd_service_name" string => "ssh";
194+
!(debian|ubuntu)::
195+
"sshd_service_name" string => "sshd";
185196

186197
classes:
187198
any::
@@ -278,29 +289,14 @@ findtime = 600",
278289
classes => if_repaired("fail2ban_config_changed"),
279290
comment => "Configure fail2ban to ban IPs after 5 failed SSH attempts within 10 minutes";
280291

281-
"/etc/ssh/sshd_config"
282-
edit_line => comment_lines_matching("^PermitRootLogin\s+(?!no\s*$).*", "#"),
283-
classes => if_repaired("sshd_hardened"),
284-
comment => "Comment out insecure PermitRootLogin values";
285-
"/etc/ssh/sshd_config"
286-
edit_line => comment_lines_matching("^PasswordAuthentication\s+(?!no\s*$).*", "#"),
292+
"$(sshd_config_files)"
293+
edit_line => comment_lines_matching("^$(sshd_hardening_directives)\s+(?!no\s*$).*", "#"),
287294
classes => if_repaired("sshd_hardened"),
288-
comment => "Comment out insecure PasswordAuthentication value";
295+
comment => "Comment out insecure SSH auth directives in sshd_config and drop-ins";
289296
"/etc/ssh/sshd_config"
290-
edit_line => comment_lines_matching("^KbdInteractiveAuthentication\s+(?!no\s*$).*", "#"),
297+
edit_line => prepend_if_no_line("$(sshd_hardening_directives) no"),
291298
classes => if_repaired("sshd_hardened"),
292-
comment => "Comment out insecure KbdInteractiveAuthentication value (OpenSSH 8.7+)";
293-
"/etc/ssh/sshd_config"
294-
edit_line => comment_lines_matching("^ChallengeResponseAuthentication\s+(?!no\s*$).*", "#"),
295-
classes => if_repaired("sshd_hardened"),
296-
comment => "Comment out insecure ChallengeResponseAuthentication value (OpenSSH < 8.7)";
297-
"/etc/ssh/sshd_config"
298-
edit_line => lines_present("PermitRootLogin no
299-
PasswordAuthentication no
300-
KbdInteractiveAuthentication no
301-
ChallengeResponseAuthentication no"),
302-
classes => if_repaired("sshd_hardened"),
303-
comment => "Ensure SSH hardening directives are present";
299+
comment => "Ensure SSH hardening directives are at the top of sshd_config, before any Include";
304300

305301
ubuntu_16|ubuntu_18|redhat_9|redhat_10::
306302
"/etc/hosts" -> { "ENT-12437" }
@@ -383,10 +379,25 @@ jenkins_builds ALL=NOPASSWD: /usr/bin/podman
383379
!have_sys_user.(suse|sles|opensuse)::
384380
"useradd -u 3 sys" contain => in_shell;
385381

382+
linux::
383+
"sshd -T 2>/dev/null | grep -qiE '^PermitRootLogin no'"
384+
depends_on => { "sshd_restarted" },
385+
contain => in_shell,
386+
comment => "Verify PermitRootLogin is disabled";
387+
"sshd -T 2>/dev/null | grep -qiE '^PasswordAuthentication no'"
388+
depends_on => { "sshd_restarted" },
389+
contain => in_shell,
390+
comment => "Verify PasswordAuthentication is disabled";
391+
"sshd -T 2>/dev/null | grep -qiE '^(KbdInteractive|ChallengeResponse)Authentication no'"
392+
depends_on => { "sshd_restarted" },
393+
contain => in_shell,
394+
comment => "Verify KbdInteractiveAuthentication (OpenSSH 8.7+) or ChallengeResponseAuthentication (older) is disabled";
395+
386396
services:
387397
sshd_hardened::
388-
"sshd"
398+
"$(sshd_service_name)"
389399
service_policy => "restart",
400+
handle => "sshd_restarted",
390401
comment => "Restart sshd to apply hardened configuration";
391402
any::
392403
"fail2ban"

0 commit comments

Comments
 (0)