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
相关推荐
七歌杜金房4 小时前
我终于又有了自己的 Linux 电脑
linux·debian·mac
泯泷9 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
泯泷9 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
tntxia1 天前
linux curl命令详解_curl详解
linux
扛枪的书生1 天前
Linux 网络管理器用法速查
linux
顺风尿一寸1 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode2 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫2 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao4 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
Flynt5 天前
npm v12 来了:allowScripts 默认关闭,我的项目差点跑不起来
安全·npm·node.js