概述

本文档介绍 Linux 系统密码有效期策略的配置方法、相关参数说明,以及如何检测和防范时间操纵攻击绕过密码过期策略。

密码策略参数说明

/etc/login.defs 配置文件

密码策略的全局默认配置位于 /etc/login.defs,主要参数如下:

参数 说明 推荐值 单位
PASS_MIN_DAYS 两次密码修改之间的最小间隔天数 1-15
PASS_MAX_DAYS 密码最大有效期 30-90
PASS_WARN_AGE 密码过期前开始警告的天数 7-15
PASS_MIN_LEN 密码最小长度(已废弃,使用 PAM 配置) 8+ 字符

/etc/shadow 文件字段

用户密码信息存储在 /etc/shadow,格式如下:

 username:password:lastchg:min:max:warn:inactive:expire:reserved

关键字段说明:

  • 字段3 (lastchg): 密码最后修改日期(从1970-01-01起的天数)

  • 字段4 (min): 对应 PASS_MIN_DAYS

  • 字段5 (max): 对应 PASS_MAX_DAYS

  • 字段6 (warn): 对应 PASS_WARN_AGE

  • 字段7 (inactive): 密码过期后账号被锁定前的宽限天数

  • 字段8 (expire): 账号过期日期(绝对日期)

配置步骤

1. 配置全局默认策略

编辑 /etc/login.defs

 # 编辑配置文件
 vi /etc/login.defs
 ​
 # 设置以下参数
 PASS_MIN_DAYS   7
 PASS_MAX_DAYS   90
 PASS_WARN_AGE   14

注意:此配置仅对新创建的用户生效,不影响已存在的用户。

2. 为现有用户应用策略

使用 chage 命令为现有用户设置密码策略:

 # 为单个用户设置
 chage -m 7 -M 90 -W 14 username
 ​
 # 批量设置所有普通用户
 awk -F: '$3>=1000 && $3<65534 {print $1}' /etc/passwd | while read user; do
     chage -m 7 -M 90 -W 14 "$user"
 done

3. 查看用户密码策略

 # 查看指定用户的密码策略
 chage -l username
 ​
 # 查看 shadow 文件中的原始数据
 grep "^username:" /etc/shadow

4. 强制用户下次登录时修改密码

 chage -d 0 username
 # 或
 passwd -e username

安全问题:时间操纵攻击

攻击场景

攻击者可以通过以下步骤绕过密码过期策略:

  1. 将系统时间调整到未来(如2050年)

  2. 修改密码

  3. 将系统时间调回正常时间(如2025年)

结果:即使设置了90天有效期,密码也不会过期,因为 /etc/shadow 中记录的 lastchg 是未来的日期。

检测方法

检测密码最后修改时间是否在未来:

 # 获取当前日期(从1970-01-01起的天数)
 current_days=$(($(date +%s) / 86400))
 ​
 # 检查 root 用户的密码修改时间
 awk -F: -v today="$current_days" '
     /^root:/ {
         if ($3 > today) {
             print "WARNING: Password last change date is in the future!"
             print "  Last change: "$3" days"
             print "  Current date: "today" days"
             print "  Possible time manipulation detected!"
         }
     }
 ' /etc/shadow

完整检测脚本

chk_pwdlife() {
   cat /etc/login.defs |awk '
     /^ *PASS_MIN_DAYS/{if($2>0&&$2<=15){
       print $0", check result=true"}else{print $0", check result=false"}}
     /^ *PASS_MAX_DAYS/{if($2>0&&$2<=90){
       print $0", check result=true"}else{print $0", check result=false"}}
     /^ *PASS_WARN_AGE/{if($2>0&&$2<=15){
       print $0", check result=true"}else{print $0", check result=false"}}'
   local root_user=$(cat /etc/passwd|awk -F: '{if($3==0){print $1}}')
   local current_days=$(($(date +%s) / 86400))
   cat /etc/shadow |grep -E "^$root_user"|awk -F: -v today="$current_days" '{
     last_change=$3
     if($2 !~ /^[!*]/ && last_change > today){
       print $1": Password last change date is in the future ("last_change" > "today"), possible time manipulation, check result=false"
     }
     if($2 !~ /^[!*]/){if($4<=0)print $1":"$4", PASS_MIN_DAYS check result=false"}
     if($2 !~ /^[!*]/){if($5<0 || $5>90)print $1":"$5", PASS_MAX_DAYS check result=false"}
     if($2 !~ /^[!*]/){if($6<=0)print $1":"$6", PASS_WARN_AGE check result=false"}
   }END{
     if(NR==0)print "Insufficient permissions, check result=false"
   }'
 }

防护建议

1. 系统时间保护

 # 限制普通用户修改系统时间
 # 确保只有 root 可以修改时间
 ​
 # 使用 NTP 同步时间
 systemctl enable chronyd
 systemctl start chronyd
 ​
 # 配置 chrony 使用可信时间源
 vi /etc/chrony.conf

2. 审计日志监控

监控时间修改操作:

 # 添加 auditd 规则监控时间修改
 auditctl -a always,exit -F arch=b64 -S adjtimex -S settimeofday -S clock_settime -k time-change
 auditctl -a always,exit -F arch=b32 -S adjtimex -S settimeofday -S stime -S clock_settime -k time-change
 auditctl -w /etc/localtime -p wa -k time-change
 ​
 # 持久化规则
 echo "-a always,exit -F arch=b64 -S adjtimex -S settimeofday -S clock_settime -k time-change" >> /etc/audit/rules.d/time-change.rules

3. 定期检查

将检测脚本加入定期任务:

 # 添加到 cron
 cat > /etc/cron.daily/check-password-policy << 'EOF'
 #!/bin/bash
 source /path/to/check_Script.sh
 chk_pwdlife | grep -i "false\|manipulation" | mail -s "Password Policy Alert" admin@example.com
 EOF
 ​
 chmod +x /etc/cron.daily/check-password-policy

4. 文件完整性监控

使用 AIDE 或 Tripwire 监控关键文件:

 # 监控 /etc/shadow 的修改
 aide --init
 aide --check

常用命令速查

 # 查看用户密码状态
 passwd -S username
 ​
 # 查看密码策略详情
 chage -l username
 ​
 # 锁定/解锁用户
 passwd -l username  # 锁定
 passwd -u username  # 解锁
 ​
 # 查看所有用户的密码过期信息
 for user in $(awk -F: '$3>=1000 && $3<65534 {print $1}' /etc/passwd); do
     echo "=== $user ==="
     chage -l "$user"
 done
 ​
 # 查看密码即将过期的用户(7天内)
 awk -F: -v today=$(($(date +%s)/86400)) '
     $2 !~ /^[!*]/ && $5>0 {
         expire = $3 + $5
         if (expire - today <= 7 && expire - today >= 0) {
             print $1": expires in "(expire-today)" days"
         }
     }
 ' /etc/shadow

参考资料

  • man 5 shadow - shadow 文件格式说明

  • man login.defs - login.defs 配置说明

  • man chage - 密码策略管理命令

  • man passwd - 密码管理命令

  • CIS Benchmark for Linux - 密码策略安全基线

总结

密码有效期策略是 Linux 系统安全的重要组成部分。除了正确配置策略参数外,还需要:

  1. 防止系统时间被恶意修改

  2. 定期检测密码策略的完整性

  3. 监控和审计时间修改操作

  4. 使用 NTP/Chrony 保持时间同步

  5. 定期审查用户密码状态

通过综合运用这些措施,可以有效防范密码策略被绕过的风险。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐