[Linux] PAM faillock 계정 잠금 임계값 설정

리눅스 보안 취약점 조치를 진행할 때 필수로 등장하는 작업 중 하나인 특정 횟수를 초과하여 인증에 실패한 사용자를 잠그는 방법에 대해서 소개합니다. 시스템 보안을 강화하기 위한 PAM 모듈 중 하나인 faillock 모듈을 사용하면 “deny”에 지정된 횟수를 초과했을 때 계정을 잠글 수 있습니다. 해당 모듈은 pam_tally2 모듈과 유사한 기능을 제공하지만 더 많은 유연성을 가지고 있습니다.

계정을 잠그는 것은 불법 사용자의 무차별 대입 공격으로부터 시스템을 안전하게 보호하는 효과적인 방법입니다.

환경

RHEL7 버전까지는 faillock 설정할 때 직접 system-auth나 password-auth 파일을 수동으로 설정하였는데 RHEL8 이상에서는 authselect 명령을 사용하여 수정할 것을 권장하고 있습니다. 이 문서에서는 authselect명령을 사용하여 설정하는 방법을 설명합니다.

  • RHEL8, 9
  • 패키지 버전: (RHEL 8.2 이하만 패키지 업데이트 필요)
    • authselect-1.2.1-2.el8 이상
    • pam-1.3.1-8.el8 이상

백업

PAM 모듈 작업 전 설정 파일 백업은 필수입니다. 오타 또는 잘못 입력된 라인이 발생하는 경우 로그인조차 안되는 무시무시한 문제가 발생할 수 있습니다.

[root@rhel8 ~]# cp /etc/pam.d/system-auth /etc/pam.d/system-auth_20231127
[root@rhel8 ~]# cp /etc/pam.d/password-auth /etc/pam.d/password-auth_20231127

방법1) authselect 명령어 사용하여 설정

 faillock 모듈 추가

authselect 명령을 쓸 때 주의할 점은 OS 보안 취약점 뿐 아닌 다른 솔루션 업체에서도 password-auth 및 system-auth를 수정하는 경우가 있을 수 있습니다. 이럴 때 authselect 명령을 사용하면 해당 내용이 초기화될 수 있으므로 주의가 필요합니다. authselect을 통한 faillock 설정이 다 끝나고 타 벤더사의 설정을 다시 추가하거나 아니면 authselect 명령을 사용하지 않고 수동으로 추가해야 할 수 있습니다.

1) 현재 버전 확인

[root@rhel8 ~]# authselect current
Profile ID: sssd
Enabled features:
- with-fingerprint
- with-silent-lastlog

## faillock 모듈 확인
[root@rhel8 ~]# cat /etc/pam.d/password-auth | grep faillock

2) faillock 기능 추가

# authselect enable-feature with-faillock 
Make sure that SSSD service is configured and enabled. See SSSD documentation for more information.

위 명령어로 진행이 안되고 아래처럼 오류가 나는 경우에는 명시적으로 한 번 더 sssd로 지정하면서 faillock을 강제로 활성화해줄 수 있습니다.

[root@rhel8 ~]#  authselect enable-feature with-faillock 
[error] [/etc/authselect/password-auth] has unexpected content!
[error] Unexpected changes to the configuration were detected.
[error] Refusing to activate profile unless those changes are removed or overwrite is requested.
Unable to enable feature [17]: File exists

## faillock 모듈 강제 활성화
[root@rhel8 ~]# authselect select sssd with-faillock --force
[error] [/etc/authselect/password-auth] has unexpected content!
Backup stored at /var/lib/authselect/backups/2023-11-27-22-40-46.9OLjvd
Profile "sssd" was selected.
The following nsswitch maps are overwritten by the profile:
- passwd
- group
- netgroup
- automount
- services

Make sure that SSSD service is configured and enabled. See SSSD documentation for more information.

3) 설정 내용 확인

  • authselect profile 확인
### 확인
# authselect current
Profile ID: sssd
Enabled features:
- with-fingerprint
- with-silent-lastlog
- with-faillock
  • faillock 모듈 확인

authselect 명령을 사용하고 나면 faillock 모듈이 추가된 부분을 볼 수 있습니다. diff 명령을 이용하여 기존 백업해둔 파일과 비교해 보면 faillock 모듈이 들어가는 3라인만 지정된 위치에 추가된 부분을 확인할 수 있습니다.

[root@rhel8 ~]# cat /etc/pam.d/password-auth | grep faillock
auth        required                                     pam_faillock.so preauth silent
auth        required                                     pam_faillock.so authfail
account     required                                     pam_faillock.so

[root@rhel8 ~]# diff /etc/pam.d/password-auth /etc/pam.d/password-auth_20231127 
1c1
< # Generated by authselect on Tue Nov 28 07:35:18 2023
---
> # Generated by authselect on Tue Nov 28 07:23:32 2023
6d5
< auth        required                                     pam_faillock.so preauth silent
12d10
< auth        required                                     pam_faillock.so authfail
15d12
< account     required                                     pam_faillock.so

 faillock 모듈 설정 ( 잠금 횟수 설정 )

faillock 모듈 설정은 /etc/security/faillock.conf에서 설정할 수 있습니다.

  • 3번 패스워드 틀릴 경우 계정 잠금 설정
[root@rhel8 ~]# cat /etc/security/faillock.conf | grep -v ^#
deny = 3
unlock_time = 600
silent

deny=<N>: N 번 패스워드가 틀릴 경우 계정 잠금

unlock_time=<N>: N 초가 지나면 계정 잠금 해제

silent: 접속자에게 안내 메시지를 제공하지 않음

 faillock 테스트

  • 패스워드 강제로 실패

2번은 su 명령 한 번은 ssh를 통하여 패스워드를 총 3번 실패하였습니다.

[hjun@rhel8 ~]$ su - testuser
Password: 
su: Authentication failure
[hjun@rhel8 ~]$ su - testuser
Password: 
su: Authentication failure
[hjun@rhel8 ~]$ ssh testuser@localhost
testuser@localhost's password:
  • faillock 명령어를 이용하여 실패 횟수 확인

패스워드를 실패하면 계정이 잠긴 걸 확인할 수 있습니다. 이제 정상적인 패스워드로 접속해도 접속이 되지 않습니다.

[root@rhel8 ~]# faillock --user testuser
testuser:
When                Type  Source                                           Valid
2023-11-28 07:58:13 TTY   pts/1                                                V
2023-11-28 07:58:20 TTY   pts/1                                                V
2023-11-28 07:58:34 RHOST ::1                                                  V
  • faillock 명령어를 이용하여 실패 횟수 초기화
[root@rhel8 ~]# faillock --user testuser --reset

[user@rhel8 ~]$ su - testuser
Password: 
Last failed login: Tue Nov 28 08:01:07 KST 2023 on pts/1
There were 29 failed login attempts since the last successful login.
[testuser@rhel8 ~]$

실패 횟수 초기화 후에 정상 접근이 확인됩니다.

방법 2) authselect 명령을 이용하지 않은 방법

authselect 명령을 이용하지 않으면 아래 모듈 파일에 faillock 설정 3줄만 추가해 주면 끝입니다.

  • system-auth

코드 보기: system-auth_with_faillock

[root@rhel8 ~]# cat /etc/pam.d/system-auth
# Generated by authselect on Tue Nov 28 08:24:36 2023
# Do not modify this file manually.

auth        required                                     pam_env.so
auth        required                                     pam_faildelay.so delay=2000000
auth        required                                     pam_faillock.so preauth silent audit deny=10 unlock_time=30      ## add line
auth        [default=1 ignore=ignore success=ok]         pam_usertype.so isregular
auth        [default=1 ignore=ignore success=ok]         pam_localuser.so
auth        sufficient                                   pam_unix.so nullok
auth        [default=1 ignore=ignore success=ok]         pam_usertype.so isregular
auth        sufficient                                   pam_sss.so forward_pass
auth        required                                     pam_faillock.so authfail audit deny=10 unlock_time=30           ## add line
auth        required                                     pam_deny.so

account     required                                     pam_faillock.so                                                 ## add line
account     required                                     pam_unix.so
account     sufficient                                   pam_localuser.so
account     sufficient                                   pam_usertype.so issystem
account     [default=bad success=ok user_unknown=ignore] pam_sss.so
account     required                                     pam_permit.so

password    requisite                                    pam_pwquality.so local_users_only
password    sufficient                                   pam_unix.so sha512 shadow nullok use_authtok
password    sufficient                                   pam_sss.so use_authtok
password    required                                     pam_deny.so

session     optional                                     pam_keyinit.so revoke
session     required                                     pam_limits.so
-session    optional                                     pam_systemd.so
session     [success=1 default=ignore]                   pam_succeed_if.so service in crond quiet use_uid
session     required                                     pam_unix.so
session     optional                                     pam_sss.so
  • password-auth

코드 보기: password-auth_with_faillock

[root@rhel8 ~]# cat /etc/pam.d/password-auth
# Generated by authselect on Tue Nov 28 08:24:36 2023
# Do not modify this file manually.

auth        required                                     pam_env.so
auth        required                                     pam_faildelay.so delay=2000000
auth        required                                     pam_faillock.so preauth silent audit deny=10 unlock_time=30   ## add line
auth        [default=1 ignore=ignore success=ok]         pam_usertype.so isregular
auth        [default=1 ignore=ignore success=ok]         pam_localuser.so
auth        sufficient                                   pam_unix.so nullok
auth        [default=1 ignore=ignore success=ok]         pam_usertype.so isregular
auth        sufficient                                   pam_sss.so forward_pass
auth        required                                     pam_faillock.so authfail  audit deny=10 unlock_time=30        ## add line
auth        required                                     pam_deny.so

account     required                                     pam_faillock.so                                               ## add line
account     required                                     pam_unix.so
account     sufficient                                   pam_localuser.so
account     sufficient                                   pam_usertype.so issystem
account     [default=bad success=ok user_unknown=ignore] pam_sss.so
account     required                                     pam_permit.so

password    requisite                                    pam_pwquality.so local_users_only
password    sufficient                                   pam_unix.so sha512 shadow nullok use_authtok
password    sufficient                                   pam_sss.so use_authtok
password    required                                     pam_deny.so

session     optional                                     pam_keyinit.so revoke
session     required                                     pam_limits.so
-session    optional                                     pam_systemd.so
session     [success=1 default=ignore]                   pam_succeed_if.so service in crond quiet use_uid
session     required                                     pam_unix.so
session     optional                                     pam_sss.so

faillock.conf VS password-auth/system-auth

이렇게 /etc/security/faillock.con f 파일에 잠금 설정을 하게 되면 /etc/pam.d/password-auth 와 /etc/pam.d/system-auth에는 해당 내용이 기록되지 않습니다. faillock.conf 파일을 참고해야만 설정 내용을 확인할 수 있습니다.

그럼 여기서 auth 파일을 수정하는 것과 faillock.conf 파일을 수정하는 것 중에서 어느 설정이 우선순위가 더 높은지 비교해 보도록 하겠습니다.

비교를 위하여 다음과 같이 서로 deny 숫자를 다르게 가져갑니다.

  • faillock.conf
[root@rhel8 ~]# cat /etc/security/faillock.conf  | grep -v ^#
audit
silent
deny = 3
unlock_time = 600
  • password-auth 및 system-auth (설정 동일)
[root@rhel8 ~]# cat /etc/pam.d/(system-auth/password-auth) | grep faillock
auth        required                                     pam_faillock.so preauth silent audit deny=10 unlock_time=30
auth        required                                     pam_faillock.so authfail  audit deny=10 unlock_time=30
account     required                                     pam_faillock.so
  • 접속 테스트 ( system-auth/password-auth 설정이 우선순위가 더 높습니다.)

6번 접속 실패하고 7번째 시도에 정상 패스워드를 기입했는데 로그인이 성공했습니다. faillock.conf 파일이 우선순위가 낮은 점을 확인할 수 있습니다.

### 접속 6번 실패
[root@rhel8 pam.d]# faillock --user testuser
testuser:
When                Type  Source                                           Valid
2023-11-28 08:16:11 TTY   pts/1                                                V
2023-11-28 08:16:16 TTY   pts/1                                                V
2023-11-28 08:16:22 TTY   pts/1                                                V
2023-11-28 08:16:27 TTY   pts/1                                                V
2023-11-28 08:16:34 TTY   pts/1                                                V
2023-11-28 08:16:40 TTY   pts/1                                                V

### 7번째 로그인 성공
[user@rhel8 ~]$ su - testuser
Password: 
Last login: Tue Nov 28 08:09:34 KST 2023 on pts/1
Last failed login: Tue Nov 28 08:16:43 KST 2023 on pts/1
There were 6 failed login attempts since the last successful login.
[testuser@rhel8 ~]$

마치며

패스워드 실패 시 계정 잠금을 제공하는 기능인 faillock에 대해 살펴봤습니다. RHEL8부터 권장되는 방법인 authselect 명령어 사용법부터 auth 파일을 직접 수정하는 2가지 방법에 대해 다뤘습니다. authselect이라는 새로운 방법이 권장되는 방법으로 나온 만큼 보안 취약점 스크립트도 두 가지 방식 모두 검사될 수 있도록 하는 방법이 필요할 것 같습니다.

참조

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/security_guide/index#sect-Security_Guide-Workstation_Security-Account_Locking

https://access.redhat.com/solutions/62949

https://atl.kr/dokuwiki/doku.php/계정관리

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Scroll to Top