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


    提示:巡检脚本可以使用crontab定时执行,人工根据执行结束时间点统一收集报告文件即可。

    1. #!/bin/bash
    2. # Define output file
    3. current_date=$(date "+%Y%m%d") # Gets the current date in YYYYMMDD format
    4. echo ''>server_security_inspection_report_${current_date}.txt
    5. output_file="server_security_inspection_report_${current_date}.txt"
    6. # 获取巡检时间
    7. inspection_time=$(date "+%Y-%m-%d %H:%M:%S")
    8. # 获取主机IP地址
    9. host_ip=$(hostname -I | awk '{print $1}')
    10. # 获取最后六次Session记录
    11. last_logins=$(last -n 6 -w)
    12. echo "===============================================" > $output_file
    13. echo "-- 巡检时间:$inspection_time" >> $output_file
    14. echo "-- 主机IP地址:$host_ip" >> $output_file
    15. echo "-- 最后六次Session记录:" >> $output_file
    16. echo "$last_logins" >> $output_file
    17. # 获取防火墙状态
    18. firewall_status=$(systemctl is-active firewalld)
    19. echo "-- 防火墙状态:$firewall_status" >> $output_file
    20. # 获取防火墙开放端口
    21. if [ "$firewall_status" = "active" ]; then
    22. firewall_open_ports=$(firewall-cmd --list-ports)
    23. firewall_open_services=$(firewall-cmd --list-services)
    24. else
    25. firewall_open_ports="防火墙未激活"
    26. firewall_open_services="防火墙未激活"
    27. fi
    28. echo "-- 防火墙开放端口:$firewall_open_ports" >> $output_file
    29. echo "-- 防火墙开放服务:$firewall_open_services" >> $output_file
    30. # 密码有效期策略,脚本中username即用户名需要根据实际使用进行修改****************
    31. password_policy=$(chage -l username)
    32. password_max_days=$(grep -w "PASS_MAX_DAYS" /etc/login.defs | grep -v ^#) #
    33. echo "-- 密码有效期策略:$password_max_days" >> $output_file
    34. echo "-- 指定用户有效期(非root):" >> $output_file
    35. echo "$password_policy" >> $output_file
    36. # 账户锁定策略
    37. account_lock_policy=$(grep -w "pam_tally2" /etc/pam.d/system-auth)
    38. echo "-- 账户锁定策略:" >> $output_file
    39. echo " $account_lock_policy" >> $output_file
    40. # 密码强度策略
    41. echo "-- 密码强度策略:" >> "$output_file"
    42. grep "pam_cracklib.so" /etc/pam.d/system-auth >> "$output_file"
    43. echo "===============================================" >> $output_file
    44. # 显示报告
    45. cat $output_file

  • 相关阅读:
    leetcode 29. 两数相除
    前端 | (十四)canvas基本用法 | 尚硅谷前端HTML5教程(html5入门经典)
    Window窗体。
    字典的基本概念
    【C++】——类和对象(中)
    「Verilog学习笔记」边沿检测
    html+css+javascript+jquery+bootstarp响应式旅行社旅游平台网站模板(14页)
    Elasticsearch:使用 Amazon Bedrock 的 semantic_text
    Vue(6)
    Servlet---请求的分发处理、HttpServlet实现程序、IDEA创建Servlet程序、Servlet继承体系
  • 原文地址:https://blog.csdn.net/arvinrong/article/details/136759198