Linux服务器(RedHat、CentOS系)安全相关巡检shell脚本

提示:巡检脚本可以使用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
相关推荐
用户962377954482 小时前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机5 小时前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机5 小时前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户962377954487 小时前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star7 小时前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户9623779544810 小时前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
YuMiao11 小时前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux