目录
一、要求
二、脚本介绍
1、脚本内容
2、脚本解释
(1)函数定义
(2)防火墙状态检查
(3)SELinux/AppArmor状态检查
(4)SSH配置检查
(5)用户账户数量检查
(6)系统更新检查
3、使用方法
(1)脚本文件
(2)赋予权限
(3)执行结果
一、要求
由于工作需要,要编写一个全面的Linux系统安全性检查脚本,该脚本将检查防火墙状态、SELinux/AppArmor状态、SSH配置、用户账户数量、以及检查是否有可用的系统更新。
二、脚本介绍
经过在centos系统中测试,完成了脚本,这个脚本可能需要根据具体系统配置(如使用的发行版)进行调整。
闲话不说,直接介绍脚本吧。
1、脚本内容
#!/bin/bash  # 定义一个脚本函数来显示标题  
function show_title() {  echo -e "\n\e[1m$1\e[0m"  
}  # 检查防火墙状态  
function check_firewall() {  show_title "Checking Firewall Status"  if command -v firewalld >/dev/null 2>&1; then  status=$(firewall-cmd --state)  echo "firewalld is $status"  elif command -v ufw >/dev/null 2>&1; then  status=$(ufw status verbose | grep 'Status:' | awk '{print $2}')  echo "ufw is $status"  else  echo "No known firewall service detected."  fi  
}  # 检查SELinux/AppArmor状态  
function check_selinux_apparmor() {  show_title "Checking SELinux/AppArmor Status"  if [ -e /etc/selinux/config ]; then  status=$(grep '^SELINUX=' /etc/selinux/config | cut -d= -f2)  echo "SELinux is set to $status"  elif command -v aa-status >/dev/null 2>&1; then  status=$(aa-status | grep 'AppArmor status:' | awk '{print $3}')  echo "AppArmor is $status"  else  echo "SELinux and AppArmor not detected."  fi  
}  # 检查SSH配置  
function check_ssh_config() {  show_title "Checking SSH Configuration"  if [ -f /etc/ssh/sshd_config ]; then  echo "SSH configuration file exists."  # 这里可以添加更详细的SSH配置检查,如PermitRootLogin, PasswordAuthentication等  grep '^PermitRootLogin' /etc/ssh/sshd_config  grep '^PasswordAuthentication' /etc/ssh/sshd_config  else  echo "SSH configuration file not found."  fi  
}  # 检查用户账户数量  
function check_user_accounts() {  show_title "Checking User Accounts"  user_count=$(getent passwd | wc -l)  echo "Total number of user accounts: $user_count"  # 可以根据需要添加特定用户的检查  
}  # 检查系统更新  
function check_updates() {  show_title "Checking for System Updates"  if command -v apt-get >/dev/null 2>&1; then  updates=$(apt-get update && apt-get upgrade -s | grep 'upgraded, ' | awk '{print $2}')  if [ -n "$updates" ]; then  echo "$updates packages can be upgraded."  else  echo "No upgrades available."  fi  elif command -v yum >/dev/null 2>&1; then  # 注意:这里只是示例,yum 不直接支持类似 apt-get upgrade -s 的命令  echo "Checking for updates with yum (not as detailed as apt-get)."  yum check-update  elif command -v dnf >/dev/null 2>&1; then  updates=$(dnf list updates | grep '^ ' | wc -l)  if [ $updates -gt 0 ]; then  echo "$updates updates available."  else  echo "No updates available."  fi  else  echo "Unknown package manager."  fi  
}  # 调用函数  
check_firewall  
check_selinux_apparmor  
check_ssh_config  
check_user_accounts  
check_updates脚本提供了基本的安全性检查框架。然而,请注意,安全性的最佳实践通常涉及更详细的配置审核和持续监控。此外,SSH配置检查部分可以根据需要添加更多详细的检查项。
2、脚本解释
脚本中主要部分的解释如下:
(1)函数定义
function show_title() {echo -e "\n\e[1m$1\e[0m"}如下为具体说明:
- function show_title():定义了一个名为show_title的函数,它接受一个参数$1。
- echo -e "\n\e[1m$1\e[0m":使用echo命令打印一个换行符\n,然后通过ANSI转义序列\e[1m(注意:通常使用\033或$'\e'来表示ESC字符,但这里假设\e被shell正确识别)将文本加粗,$1是传递给函数的参数,即要显示的标题。\e[0m用于重置文本格式。
(2)防火墙状态检查
if command -v firewalld >/dev/null 2>&1; then检查firewalld
elif command -v ufw >/dev/null 2>&1; then检查ufw
elseecho "No known firewall service detected."
fi如下为具体说明:
- command -v firewalld >/dev/null 2>&1:检查firewalld命令是否存在,输出被重定向到/dev/null,错误也被重定向到标准输出(因为这里是检查命令是否存在,所以不关心输出内容)。
- 如果firewalld不存在,则检查ufw。
(3)SELinux/AppArmor状态检查
if [ -e /etc/selinux/config ]; then#检查SELinux
elif command -v aa-status >/dev/null 2>&1; then#检查AppArmor
elseecho "SELinux and AppArmor not detected."
fi如下为具体说明:
- [ -e /etc/selinux/config ]:检查SELinux配置文件是否存在。
- command -v aa-status:检查AppArmor的aa-status命令是否存在。
(4)SSH配置检查
grep '^PermitRootLogin' /etc/ssh/sshd_configgrep '^PasswordAuthentication' /etc/ssh/sshd_config如下为具体说明:
- 使用grep命令搜索SSH配置文件中的PermitRootLogin和PasswordAuthentication设置,以了解是否允许root用户登录和是否允许密码认证。
(5)用户账户数量检查
user_count=$(getent passwd | wc -l)echo "Total number of user accounts: $user_count"如下为具体说明:
- getent passwd:列出系统上所有的用户账户信息。
- wc -l:计算行数,即用户账户的数量。
(6)系统更新检查
对于不同的包管理器(apt-get, yum, dnf),检查方式略有不同,但基本思想都是查询可用的更新。
- apt-get update && apt-get upgrade -s:对于Debian系的系统,首先更新包索引,然后以模拟方式(-s)检查升级情况,但不实际执行升级。
- yum check-update:对于基于RPM的系统(如CentOS的旧版本),yum check-update可以列出所有可用的更新,但注意它并不直接提供可以升级的包的数量。
- dnf list updates:对于较新的基于RPM的系统(如Fedora和较新版本的CentOS),dnf list updates列出所有可用的更新,并通过管道传递给wc -l计算数量。
3、使用方法
(1)脚本文件
将上述脚本保存为一个文件,例如 linux_security_check.sh。
(2)赋予权限
赋予脚本执行权限,使用如下命令:
chmod +x linux_security_check.sh
(3)执行结果
运行脚本使用如下命令:
./linux_security_check.sh
结果如下所示:
[root@ecs-52a1 home]#
[root@ecs-52a1 home]# ./linux_security_check.shChecking Firewall Status
firewalld is runningChecking SELinux/AppArmor Status
SELinux is set to disabledChecking SSH Configuration
SSH configuration file exists.
PermitRootLogin no
PasswordAuthentication yesChecking User Accounts
Total number of user accounts: 22Checking for System Updates
Checking for updates with yum (not as detailed as apt-get).
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
base                                                                                                                                   | 3.6 kB  00:00:00
epel                                                                                                                                   | 4.3 kB  00:00:00
extras                                                                                                                                 | 2.9 kB  00:00:00
updates                                                                                                                                | 2.9 kB  00:00:00bind-export-libs.x86_64                                                   32:9.11.4-26.P2.el7_9.16                                                     updates
bind-libs-lite.x86_64                                                     32:9.11.4-26.P2.el7_9.16                                                     updates
bind-license.noarch                                                       32:9.11.4-26.P2.el7_9.16                                                     updates
dhclient.x86_64                                                           12:4.2.5-83.el7.centos.2                                                     updates
dhcp-common.x86_64                                                        12:4.2.5-83.el7.centos.2                                                     updates
dhcp-libs.x86_64                                                          12:4.2.5-83.el7.centos.2                                                     updates
glibc.x86_64                                                              2.17-326.el7_9.3                                                             updates
glibc-common.x86_64                                                       2.17-326.el7_9.3                                                             updates
glibc-devel.x86_64                                                        2.17-326.el7_9.3                                                             updates
glibc-headers.x86_64                                                      2.17-326.el7_9.3                                                             updates
iwl100-firmware.noarch                                                    39.31.5.1-83.el7_9                                                           updates
iwl1000-firmware.noarch                                                   1:39.31.5.1-83.el7_9                                                         updates
iwl105-firmware.noarch                                                    18.168.6.1-83.el7_9                                                          updates
iwl135-firmware.noarch                                                    18.168.6.1-83.el7_9                                                          updates
iwl2000-firmware.noarch                                                   18.168.6.1-83.el7_9                                                          updates
iwl2030-firmware.noarch                                                   18.168.6.1-83.el7_9                                                          updates
iwl3160-firmware.noarch                                                   25.30.13.0-83.el7_9                                                          updates
iwl3945-firmware.noarch                                                   15.32.2.9-83.el7_9                                                           updates
iwl4965-firmware.noarch                                                   228.61.2.24-83.el7_9                                                         updates
iwl5000-firmware.noarch                                                   8.83.5.1_1-83.el7_9                                                          updates
iwl5150-firmware.noarch                                                   8.24.2.2-83.el7_9                                                            updates
iwl6000-firmware.noarch                                                   9.221.4.1-83.el7_9                                                           updates
iwl6000g2a-firmware.noarch                                                18.168.6.1-83.el7_9                                                          updates
iwl6000g2b-firmware.noarch                                                18.168.6.1-83.el7_9                                                          updates
iwl6050-firmware.noarch                                                   41.28.5.1-83.el7_9                                                           updates
iwl7260-firmware.noarch                                                   25.30.13.0-83.el7_9                                                          updates
kernel.x86_64                                                             3.10.0-1160.119.1.el7                                                        updates
kernel-devel.x86_64                                                       3.10.0-1160.119.1.el7                                                        updates
kernel-headers.x86_64                                                     3.10.0-1160.119.1.el7                                                        updates
kernel-tools.x86_64                                                       3.10.0-1160.119.1.el7                                                        updates
kernel-tools-libs.x86_64                                                  3.10.0-1160.119.1.el7                                                        updates
less.x86_64                                                               458-10.el7_9                                                                 updates
linux-firmware.noarch                                                     20200421-83.git78c0348.el7_9                                                 updates
python-perf.x86_64                                                        3.10.0-1160.119.1.el7                                                        updates
[root@ecs-52a1 home]#
[root@ecs-52a1 home]#
文章正下方可以看到我的联系方式:鼠标“点击” 下面的 “威迪斯特-就是video system 微信名片”字样,就会出现我的二维码,欢迎沟通探讨。