提示:巡检脚本可以使用crontab定时执行,人工根据执行结束时间点统一收集报告文件即可。
bash
#!/bin/bash
# Define output file
current_date=$(date "+%Y%m%d") # Gets the current date in YYYYMMDD format
echo ''>server_security_inspection_report_${current_date}.txt
output_file="server_security_inspection_report_${current_date}.txt"
# 获取巡检时间
inspection_time=$(date "+%Y-%m-%d %H:%M:%S")
# 获取主机IP地址
host_ip=$(hostname -I | awk '{print $1}')
# 获取最后六次Session记录
last_logins=$(last -n 6 -w)
echo "===============================================" > $output_file
echo "-- 巡检时间:$inspection_time" >> $output_file
echo "-- 主机IP地址:$host_ip" >> $output_file
echo "-- 最后六次Session记录:" >> $output_file
echo "$last_logins" >> $output_file
# 获取防火墙状态
firewall_status=$(systemctl is-active firewalld)
echo "-- 防火墙状态:$firewall_status" >> $output_file
# 获取防火墙开放端口
if [ "$firewall_status" = "active" ]; then
firewall_open_ports=$(firewall-cmd --list-ports)
firewall_open_services=$(firewall-cmd --list-services)
else
firewall_open_ports="防火墙未激活"
firewall_open_services="防火墙未激活"
fi
echo "-- 防火墙开放端口:$firewall_open_ports" >> $output_file
echo "-- 防火墙开放服务:$firewall_open_services" >> $output_file
# 密码有效期策略,脚本中username即用户名需要根据实际使用进行修改****************
password_policy=$(chage -l username)
password_max_days=$(grep -w "PASS_MAX_DAYS" /etc/login.defs | grep -v ^#) #
echo "-- 密码有效期策略:$password_max_days" >> $output_file
echo "-- 指定用户有效期(非root):" >> $output_file
echo "$password_policy" >> $output_file
# 账户锁定策略
account_lock_policy=$(grep -w "pam_tally2" /etc/pam.d/system-auth)
echo "-- 账户锁定策略:" >> $output_file
echo " $account_lock_policy" >> $output_file
# 密码强度策略
echo "-- 密码强度策略:" >> "$output_file"
grep "pam_cracklib.so" /etc/pam.d/system-auth >> "$output_file"
echo "===============================================" >> $output_file
# 显示报告
cat $output_file