Skip to content

Commit 636eb4d

Browse files
committed
Fix sshd hardening being overridden by drop-in configs
On Ubuntu, /etc/ssh/sshd_config includes drop-in files from sshd_config.d/ near the top, and OpenSSH uses first-match-wins. The previous policy only edited the main sshd_config, so drop-ins like 50-cloud-init.conf setting PasswordAuthentication yes took precedence. Changes: - Add sshd_hardening_directives slist to avoid repeating directive names - Comment out insecure values recursively across /etc/ssh (main config and all drop-ins), not just sshd_config - Prepend hardening directives to the top of sshd_config, before the Include directive, so they take precedence over any drop-in Ticket: ENT-13766 Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 9c58231 commit 636eb4d

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

ci/cfengine-build-host-setup.cf

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ 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");
185192

186193
classes:
187194
any::
@@ -278,29 +285,14 @@ findtime = 600",
278285
classes => if_repaired("fail2ban_config_changed"),
279286
comment => "Configure fail2ban to ban IPs after 5 failed SSH attempts within 10 minutes";
280287

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*$).*", "#"),
287-
classes => if_repaired("sshd_hardened"),
288-
comment => "Comment out insecure PasswordAuthentication value";
289-
"/etc/ssh/sshd_config"
290-
edit_line => comment_lines_matching("^KbdInteractiveAuthentication\s+(?!no\s*$).*", "#"),
291-
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*$).*", "#"),
288+
"$(sshd_config_files)"
289+
edit_line => comment_lines_matching("^$(sshd_hardening_directives)\s+(?!no\s*$).*", "#"),
295290
classes => if_repaired("sshd_hardened"),
296-
comment => "Comment out insecure ChallengeResponseAuthentication value (OpenSSH < 8.7)";
291+
comment => "Comment out insecure SSH auth directives in sshd_config and drop-ins";
297292
"/etc/ssh/sshd_config"
298-
edit_line => lines_present("PermitRootLogin no
299-
PasswordAuthentication no
300-
KbdInteractiveAuthentication no
301-
ChallengeResponseAuthentication no"),
293+
edit_line => prepend_if_no_line("$(sshd_hardening_directives) no"),
302294
classes => if_repaired("sshd_hardened"),
303-
comment => "Ensure SSH hardening directives are present";
295+
comment => "Ensure SSH hardening directives are at the top of sshd_config, before any Include";
304296

305297
ubuntu_16|ubuntu_18|redhat_9|redhat_10::
306298
"/etc/hosts" -> { "ENT-12437" }

0 commit comments

Comments
 (0)