Skip to content
This repository was archived by the owner on Dec 26, 2020. It is now read-only.

Commit 8a4046b

Browse files
authored
Merge pull request #123 from lazzurs/feature/2fa_auth
Feature/2fa auth
2 parents 9bb68ef + cef9f15 commit 8a4046b

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Warning: This role disables root-login on the target server! Please make sure yo
5151
|`ssh_challengeresponseauthentication` | false | Specifies whether challenge-response authentication is allowed (e.g. via PAM) |
5252
|`ssh_client_password_login` | false | `true` to allow password-based authentication with the ssh client |
5353
|`ssh_server_password_login` | false | `true` to allow password-based authentication with the ssh server |
54+
|`ssh_google_auth` | false | `true` to enable google authenticator based TOTP 2FA |
5455
|`ssh_banner` | `false` | `true` to print a banner on login |
5556
|`ssh_client_hardening` | `true` | `false` to stop harden the client |
5657
|`ssh_client_port` | `'22'` | Specifies the port number to connect on the remote host. |

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ ssh_allow_agent_forwarding: false # sshd
7676
# false to disable pam authentication.
7777
ssh_use_pam: false # sshd
7878

79+
# false to disable google 2fa authentication
80+
ssh_google_auth: false # sshd
81+
7982
# if specified, login is disallowed for user names that match one of the patterns.
8083
ssh_deny_users: '' # sshd
8184

tasks/main.yml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,47 @@
6363
changed_when: false
6464
check_mode: no
6565

66+
<<<<<<< HEAD
67+
# Install the 2FA packages and setup the config in PAM and SSH
68+
69+
- block:
70+
- name: Install google authenticator PAM module
71+
apt: name=libpam-google-authenticator state=installed
72+
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
73+
74+
- name: Install google authenticator PAM module
75+
yum: name=google-authenticator state=installed
76+
when: ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux'
77+
78+
- name: Add google auth module to PAM
79+
pamd:
80+
name: sshd
81+
type: auth
82+
control: required
83+
module_path: pam_google_authenticator.so
84+
85+
- name: Remove password auth from PAM
86+
pamd:
87+
name: sshd
88+
type: auth
89+
control: substack
90+
module_path: password-auth
91+
state: absent
92+
when: ansible_distribution == 'RedHat' or ansible_distribution == 'Oracle Linux'
93+
94+
- name: Remove password auth from PAM
95+
replace:
96+
dest: /etc/pam.d/sshd
97+
regexp: '^@include common-auth'
98+
replace: '#@include common-auth'
99+
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
100+
101+
when:
102+
- ssh_use_pam
103+
- ssh_challengeresponseauthentication
104+
- ssh_google_auth
105+
106+
66107
- block: # only runs when selinux is installed
67108
- name: install selinux dependencies when selinux is installed on RHEL or Oracle Linux
68109
package: name="{{item}}" state=installed
@@ -84,8 +125,8 @@
84125
failed_when: false
85126
changed_when: false
86127
check_mode: no
87-
88-
# The following tasks only get executed when selinux is in state permisive or enforcing, UsePam is "no" and the ssh_password module is installed.
128+
129+
# The following tasks only get executed when selinux is in state enforcing, UsePam is "no" and the ssh_password module is installed.
89130
# See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23
90131
- block:
91132
- name: Create selinux custom policy drop folder

templates/opensshd.conf.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ HostbasedAuthentication no
127127

128128
# Enable PAM to enforce system wide rules
129129
UsePAM {{ 'yes' if (ssh_use_pam|bool) else 'no' }}
130+
{% if ssh_google_auth %}
131+
# Force public key auth then ask for google auth code
132+
AuthenticationMethods publickey,keyboard-interactive
133+
{% endif %}
130134

131135
# Disable password-based authentication, it can allow for potentially easier brute-force attacks.
132136
PasswordAuthentication {{ 'yes' if (ssh_server_password_login|bool) else 'no' }}

0 commit comments

Comments
 (0)