为账户设置锁定策略是服务器安全测评里面的一个要求,windows在组策略里面可以很简单设置,linux中可以使用pam的pam_tally2.so模块来实现。

     linux对账户的锁定功能比windows的要更加广泛,强大,windows组策略中的限制,只是在系统层面的限制,而linux借助pam(Pluggable Authentication Modules,插件式认证模块)的强大,不单止可以系统层面实现,还能在各中支持pam的应用中实现这种安全锁定策略。

了解pam_tally2模块:

[root@localhost ~]# man pam_tally2.so
PAM_TALLY2(8)                  Linux-PAM Manual                  PAM_TALLY2(8)
NAME 
       pam_tally2 - The login counter (tallying) module
SYNOPSIS 
       pam_tally2.so [file=/path/to/counter] [onerr=[fail|succeed]] 
                     [magic_root] [even_deny_root] [deny=n] [lock_time=n] 
                     [unlock_time=n] [root_unlock_time=n] [serialize] [audit] 
                     [silent] [no_log_info]
       pam_tally2 [--file /path/to/counter] [--user username] [--reset[=n]] 
                  [--quiet]
DESCRIPTION 
       This module maintains a count of attempted accesses, can reset count on 
       success, can deny access if too many attempts fail.
       pam_tally2 comes in two parts: pam_tally2.so and pam_tally2. The former 
       is the PAM module and the latter, a stand-alone program.  pam_tally2 is 
       an (optional) application which can be used to interrogate and 
       manipulate the counter file. It can display user’s counts, set 
       individual counts, or clear all counts. Setting artificially high 
       counts may be useful for blocking users without changing their 
       passwords. For example, one might find it useful to clear all counts 
       every midnight from a cron job.
       Normally, failed attempts to access root will not cause the root 
       account to become blocked, to prevent denial-of-service: if your users 
       aren’t given shell accounts and root may only login via su or at the 
       machine console (not telnet/rsh, etc), this is safe.

     pam_tally.so/pam_tally2.so模块会为用户配置一个失败登录计数器,每次的失败登录会使计数器加1,到达阈值后将账户锁定一段时间,由于pam_tally.so属于比较老的版本,只能实现一旦失败登录次数超过阈值后永久锁定账户,直到root用户将该用户解锁,pam_tally2.so则新增了unlock_time参数能实现自动解锁。

     pam_tally2由两部分组成:pam_tally2.so pam的模块和pam_tally2程序,pam_tally2用于查看和调整计数器,如手工解锁用户即是重置计数器。除非配置文件中加入even_root_deny参数,否则root用户是不受到限制的。计数器会在用户成功登录后被重置为零。

pam_tally2.so主要参数:

deny=n :失败登录了n次就锁定。

lock_time=n :用户每输错一次口令都要被锁定的时间

unlock_time=n:解锁时间,用户被锁定后,最后一次失败尝试后过了n秒解锁

even_deny_root:使root同样受限

root_unlock_time=n:加了这个参数能特别的对root的解锁时间设置,如果没有这个参数就会使用unlock_time参数的值。

file=/path/to/counter:指定计数器文件的位置,默认是/var/log/tallylog

 

实例操作:

     加大用户口令被暴力破解的难度,是设置账户锁定策略的目的。linux下的口令破解集中在sshd上,本文就以openssh服务作为实例来配置账户锁定策略。

策略要求如下:

1.设定锁定的阈值为5次

2.锁定时间为5分钟即300秒

3.必须所有用户都受限,包括root

修改openssh的pam配置文件,加入红色的配置。

vim /etc/pam.d/sshd
#%PAM-1.0 
auth       required        pam_tally2.so even_deny_root deny=5 unlock_time=300 
auth       required        pam_sepermit.so 
auth       include         password-auth 
……        ……              ……

使用root远程ssh登录,输入错误口令五次,在被登录的机器上输入pam_tally2

[root@localhost ~]# pam_tally2
Login           Failures  Latest failure            From 
root                5       09/18/11 02:49:51   192.168.1.3 
tom                5       09/18/11 02:50:00   192.168.1.3

第六次登录,使用正确的口令,ssh仍然提示用户名口令不正确,等待五分钟后才能登录成功,再次在被登录的机器上输入pam_tally2

[root@localhost ~]# pam_tally2
Login           Failures  Latest failure             From 
root                5        09/18/11 02:50:00   192.168.1.3

看到root登录成功后,计数器被重置了。

也可以使用命令:pam_tally2 –u tom--reset将用户的计数器重置清零。

总结:

     修改sshd的pam配置只能限制来自ssh的登录,如果把限制写在longin的配置里面,将能够限制得更广,telnet,rhost,本机登录都要受到限制。

     不足之处,不能为计数器设定一个重置的时间,只要用户没有成功登录过,计数器就一直在累计不会自动重置为零。最后,用锁定账号的方式来防暴力破解不是最佳的方式,真遇到暴力破解时容易造成用户全部锁死,影响系统的正常使用,最佳的方式是限制登录来源。