Skip to content

Commit fba07a4

Browse files
committed
MOdify PAM to allow SSH Key logins with locked passwords
Signed-off-by: Martin Schurz <Martin.Schurz@telekom.de>
1 parent a40b1c8 commit fba07a4

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

molecule/os_hardening/verify.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- verify_tasks/pw_ageing.yml
2121
- verify_tasks/netrc.yml
2222
- verify_tasks/ignore_home_folders.yml
23+
- verify_tasks/ssh_auth_locked.yml
2324

2425
# temp. disabled - https://github.com/dev-sec/ansible-collection-hardening/issues/690
2526
# - name: Include PAM tests
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
- name: Install sshpass
3+
package:
4+
name:
5+
- sshpass
6+
state: present
7+
8+
- name: Set password for test
9+
ansible.builtin.set_fact:
10+
test_pw: myTest!pw
11+
12+
- name: Create locked_user
13+
user:
14+
name: locked_user
15+
password: "{{ test_pw | password_hash('sha512') }}"
16+
17+
- name: Create ssh-client-keypair
18+
community.crypto.openssh_keypair:
19+
path: /root/.ssh/locked_user_id
20+
type: ed25519
21+
state: present
22+
register: generated_key
23+
24+
- name: Add ssh-public-key to locked_user
25+
ansible.posix.authorized_key:
26+
user: locked_user
27+
key: "{{ generated_key.public_key }}"
28+
state: present
29+
30+
- name: Check successful login with password
31+
ansible.builtin.shell:
32+
cmd: sshpass -p {{ test_pw }} ssh locked_user@localhost echo "success"
33+
34+
- name: Check successful login with ssh key
35+
ansible.builtin.shell:
36+
cmd: ssh -i /root/.ssh/locked_user_id locked_user@localhost echo "success"
37+
38+
- name: Set password change date for locked_user
39+
ansible.builtin.shell:
40+
cmd: chage -d 2020-01-01 locked_user
41+
42+
- name: Check unsuccessful login with password
43+
ansible.builtin.shell:
44+
cmd: sshpass -p {{ test_pw }} ssh locked_user@localhost echo "success"
45+
register: output
46+
ignore_errors: true
47+
48+
- name: Assert check unsuccessful login
49+
ansible.builtin.assert:
50+
that:
51+
- output.rc | int == 1
52+
- "'WARNING: Your password has expired.' in output.stderr"
53+
- "'success' not in output.stdout"
54+
55+
- name: Check successful login with ssh key
56+
ansible.builtin.shell:
57+
cmd: ssh -i /root/.ssh/locked_user_id locked_user@localhost echo "success"

roles/os_hardening/tasks/pam_debian.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,10 @@
118118
state: absent
119119
when:
120120
- not os_auth_pam_passwdqc_enable
121+
122+
- name: Allow Login with SSH Keys, when user password is expired
123+
lineinfile:
124+
path: /etc/pam.d/common-account
125+
backrefs: yes
126+
regexp: "^(account.*pam_unix.so(?!.*no_pass_expiry).*)$"
127+
line: '\1 no_pass_expiry'

roles/os_hardening/templates/etc/pam.d/rhel_auth.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ auth required pam_deny.so
2424
{% if os_auth_retries|int > 0 %}
2525
account required pam_faillock.so
2626
{% endif %}
27-
account required pam_unix.so
27+
account required pam_unix.so no_pass_expiry
2828
account sufficient pam_localuser.so
2929
account sufficient pam_succeed_if.so uid < 1000 quiet
3030
{% if (os_auth_pam_sssd_enable | bool) %}

0 commit comments

Comments
 (0)