使用PAM控制身份验证

时间:2020-03-21 11:43:09  来源:igfitidea点击:

通过使用可插拔身份验证模块(PAM)控制身份验证来管理RHEL系统上的安全性。

备份PAM配置

在执行任何操作之前,请备份PAM配置:

# authconfig --savebackup=/root/pam_backup

允许手动和authconfig配置

我们更喜欢使用authconfig,但也希望允许手动配置。

请注意,authconfig会修改“ /etc/pam.d/system-auth-ac”和“ /etc/pam.d/password-auth-ac”文件。

# cd /etc/pam.d
# cp system-auth-ac system-auth-local
# cp password-auth-ac password-auth-local
# rm -f system-auth password-auth
# ln -s system-auth-local system-auth
# ln -s password-auth-local password-auth

现在,我们可以使用自定义的“ * -local”文件进行手动配置,但包括“ * -ac”文件以进行通过authconfig进行的配置。

# cat system-auth-local
auth

include    system-auth-ac
account   include    system-auth-ac
password  include    system-auth-ac
session   include    system-auth-ac
# cat password-auth-local
auth

include    password-auth-ac
account   include    password-auth-ac
password  include    password-auth-ac
session   include    password-auth-ac

配置pam_time

pam_time PAM模块不对用户进行身份验证,而是限制在一天中的不同时间,特定日期或者通过各种终端线路访问系统和/或者特定应用程序。

配置PAM规则顺序

仅提供帐户类型。

编辑“ system-auth-local”和“ password-auth-local”文件,并在其他帐户规则之前添加新规则。

# cat system-auth-local
auth

include    system-auth-ac
account   required   pam_time.so
account   include    system-auth-ac
password  include    system-auth-ac
session   include    system-auth-ac
# cat password-auth-local
auth

include    password-auth-ac
account   required   pam_time.so
account   include    password-auth-ac
password  include    password-auth-ac
session   include    password-auth-ac

设定时间限制

防止用户在星期五和星期六的下午5点到晚上10点之间使用SSH登录。
此限制不适用于root,alice和vince。
请注意,该配置需要24小时格式。

将以下内容添加到“ /etc/security/time.conf”:

sshd;*;!root&alice&vince;!FrSa1700-2200

配置pam_access

pam_access PAM模块主要用于访问管理。

启用pam_access

提供了所有模块类型(身份验证,帐户,密码和会话)。

# authconfig --help|grep access

--enablepamaccess

check access.conf during account authorization

--disablepamaccess

do not check access.conf during account authorization
# authconfig --enablepamaccess --update

设置访问限制

  • 禁止从IP 10.11.1.10访问。
  • 允许root,alice,sandy和vince从任何位置登录。
  • 不允许其他用户登录。

编辑“ /etc/security/access.conf”配置文件并添加以下内容:

-:vince:10.11.1.10
+:root alice sandy vince:ALL
-:ALL:ALL

配置pam_sepermit

pam_sepermit模块根据SELinux强制状态允许或者拒绝登录。

我们将为sshd配置模块。

# grep sepermit /etc/pam.d/*
/etc/pam.d/sshd:auth	   required	pam_sepermit.so

SELinux staff_u用户仅允许一次登录会话,并且该用户的进程将在注销时被杀死。

# semanage login -l
Login Name

SELinux User

MLS/MCS Range

Service
__default__

unconfined_u

s0-s0:c0.c1023

*
root

unconfined_u

s0-s0:c0.c1023

*
system_u

system_u

s0-s0:c0.c1023

*
vince

staff_u

s0-s0:c0.c1023

*

编辑“ /etc/security/sepermit.conf”配置文件并添加以下内容:

%staff_u:exclusive

配置pam_pwquality

可以将该模块插入给定服务的密码堆栈中,以提供一些插件强度检查密码。

仅提供密码模块类型。

要定义高级密码要求,请编辑“ /etc/security/pwquality.conf”文件。

将密码长度设置为12个字符,至少需要三个小写字母,一个大写字母,至少两个数字和至少一个特殊字符:

minlen = 12
lcredit = -3
ucredit = -1
dcredit = -2
ocredit = -1

请注意,负值表示每个类所需的最少字符数。

配置pam_faillock

此模块维护在指定时间间隔内每个用户的身份验证尝试失败列表,并锁定帐户,以防连续失败的身份验证次数超过拒绝次数。

要启用和配置pam_faillock,我们可以手动编辑PAM配置文件,但是authconfig工具提供了一种更简单的方法。

# authconfig --enablefaillock \
  --faillockargs="deny=5 fail_interval=90 unlock_time=300" \
  --update

默认情况下,pam_faillock不会锁定根帐户。
要更改此设置,请使用even_deny_root参数。

# authconfig --enablefaillock \
  --faillockargs="deny=5 fail_interval=90 unlock_time=300 even_deny_root" \
  --update

我们可以使用faillock命令列出失败的登录尝试。

# faillock --user sandy
sandy:
When

Type  Source

Valid
2019-06-30 12:23:26 RHOST 10.11.1.10

V
2019-06-30 12:23:29 RHOST 10.11.1.10

V
2019-06-30 12:23:32 RHOST 10.11.1.10

V
2019-06-30 12:23:36 RHOST 10.11.1.10

V
2019-06-30 12:23:38 RHOST 10.11.1.10

V

配置pam_faildelay

此模块可用于设置每个应用程序失败的延迟。

仅提供身份验证模块类型。

要启用和配置pam_faildelay,我们可以手动编辑PAM配置文件,但是有时使用'/etc/login.defs'文件中的FAIL_DELAY变量会更容易:

FAIL_DELAY=5

以上将重试延迟设置为5秒。

配置pam_tty_audit

pam_tty_audit PAM模块用于启用或者禁用TTY审核。

仅支持会话类型。

为alice用户启用TTY审核:

# cat /etc/pam.d/system-auth-local
auth

include    system-auth-ac
account   required   pam_time.so
account   include    system-auth-ac
password  include    system-auth-ac
session   include    system-auth-ac
session   required   pam_tty_audit.so disable=* enable=alice
# cat /etc/pam.d/password-auth-local
auth

include    password-auth-ac
account   required   pam_time.so
account   include    password-auth-ac
password  include    password-auth-ac
session   include    password-auth-ac
session   required   pam_tty_audit.so disable=* enable=alice

验证审核日志:

# aureport -i --tty
TTY Report
===============================================
# date time event auid term sess comm data
===============================================
1. 30/06/19 12:31:33 3061 alice ? 208 bash "ls -l /tmp"
2. 30/06/19 12:33:29 3092 alice ? 209 top "q"

参考

$man pam_faildelay
$man pam_time

;# (see /etc/security/time.conf)
$man pam_access    ;# (see /etc/security/access.conf)
$man pam_pwquality ;# (see /etc/security/pwquality.conf)
$man pam_sepermit  ;# (see /etc/security/sepermit.conf)
$man sepermit.conf
$man pam_faillock  ;# (does not a have a dedicated configuration file)
$man pam_tty_audit