Linux服务器(Debian系)包含UOS安全相关巡检shell脚本

bash 复制代码
#!/bin/bash

# Define output file
current_date=$(date "+%Y%m%d") # Gets the current date in YYYYMMDD format
output_file="server_security_inspection_report_${current_date}.txt"

# Empty the file initially
echo '' > $output_file

# 获取巡检时间 (Get the inspection time)
inspection_time=$(date "+%Y-%m-%d %H:%M:%S")

# 获取主机IP地址 (Get the host IP address)
host_ip=$(hostname -I | awk '{print $1}')

# 获取最后六次Session记录 (Get the last six session records)
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

# 获取防火墙状态 (Get the firewall status)
# UOS (Debian/Ubuntu-based) 通常使用 UFW,如果使用其他方式需要调整此部分脚本
firewall_status=$(ufw status | head -n 1)
echo "-- 防火墙状态:$firewall_status" >> $output_file
ufw_status=$(ufw status | grep Status | awk '{print $2}')
if [ "$ufw_status" != "active" ]; then
    firewall_open_ports_and_service="防火墙未激活"
else
    # 获取开放端口或服务, 逗号分隔
    firewall_open_ports_and_service=$(ufw status | grep ALLOW | awk '{print $1}' | sort | uniq | paste -sd, - )
fi
echo "-- 防火墙开放端口/服务:$firewall_open_ports_and_service" >> $output_file


# 密码有效期策略,脚本中username即用户名需要根据实际使用进行修改
password_policy=$(chage -l username 2>/dev/null || echo "chage 命令执行失败,请检查是否修改脚本中指定用户名(默认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 lockout policy)
echo "-- 账户锁定策略:" >> $output_file
grep "pam_tally2\|pam_faillock" /etc/pam.d/common-auth >> $output_file

# 密码强度策略 (Password strength policy)
echo "-- 密码强度策略:" >> "$output_file"
grep "pam_cracklib.so\|pam_pwquality.so" /etc/pam.d/common-password >> "$output_file"

echo "===============================================" >> $output_file

# 显示报告 (Display the report)
cat $output_file

巡检结果输出样例:

相关推荐
xywww1683 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
欢呼的太阳7 小时前
数据库设计Step by Step (10)——范式化
服务器·数据库·oracle
夜雪一千8 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
donoot9 小时前
Linux系统下图书馆级电子书全自动标准化分类整理完整实施方案
大数据·linux·运维·电子书管理
Felix-lxd10 小时前
Ubuntu 22.04 配置 Nginx
linux·nginx·ubuntu
大E帝国子民110 小时前
MacOS 安装Seismic Unix
服务器·macos·unix
Tim_Van11 小时前
在 CentOS 7 中安装 Chromium 的完整步骤
linux·运维·chrome·centos
德福危险11 小时前
富有想象力的XSS盲打:靶机练习之Tempus_fugit5
服务器·网络·xss
2023自学中11 小时前
C++ 内存追踪器
linux·c++
啵啵鱼爱吃小猫咪12 小时前
Ubuntu 20.04 + ROS Noetic 源码安装 libfranka 0.8.0 和 franka_ros 0.8.0 完整教程
linux·运维·ubuntu