欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7加固 && Apache加固

程序员文章站 2022-06-16 16:38:14
...

一、CentOS7系统加固

1. SSH密码加固

服务器上所有账号的密码都要采用毫无关联的强密码,密码为不少于16位的大小写字母数字特殊符号的组合。

yum install -y expect
mkpasswd -l 16

密码保存可以保存在1password软件中

2. 修改SSH配置文件

改默认端口

  1. 禁止root帐号登录 PermitRootLogin no
  2. 指定允许登录帐号 AllowUsers 用户名
  3. 启用强制密码长度策略
/etc/login.defs
PASS_MIN_LEN 13
  1. 检查是否存在除root之外UID为0的用户
awk -F: '($3 == 0) { print $1 }' /etc/passwd
  1. 检测登陆系统是否需要密码
awk -F: '($2 == ""){print $1}' /etc/passwd
  1. 帐户口令复杂度及定期更换
  2. 禁用NAT
echo 0 > /proc/sys/net/ipv4/ip_forward
  1. Bash日志设置环境变量为只读
readonly HISTFILE
readonly HISTFILESIZE
readonly HISTSIZE
readonly HISTCMD
readonly HISTCONTROL
readonly HISTIGNORE
  1. 为history文件添加时间
export HISTTIMEFORMAT=‘%F %T’
  1. 设置history文件只能追加:
chatter +a ~/.bash_history

下面是一个centos基线检测脚本linux-check.sh 此脚本先在测试环境测试后再上生产

#!/bin/bash
#linux 基线检测脚本
ip=`ip add|grep enp|awk 'NR==2 {print}'|awk '{print $2}'|awk -F "/" '{print $1}'`
today=`date +%Y%m%d%H%M`
cat <<EOF
*************************************************************************************
***** linux 基线检测脚本*****
***** 合规输出结果 accord_$ip-$today.log
***** 不合规输出结果 noaccord_$ip-$today.log
*************************************************************************************
EOF

echo "***************************"
echo "账号策略检查中..."
echo "***************************"
passmax=`cat /etc/login.defs | grep PASS_MAX_DAYS | grep -v ^# | awk '{print $2}'`
if [ $passmax -le 90 -a $passmax -gt 0 ];then
  echo "口令生存周期为${passmax}天,符合要求" >> accord_$ip-$today.log
else
  echo "口令生存周期为${passmax}天,不符合要求,建议设置不大于 90 天" >> noaccord_$ip-$today.log
fi
passmin=`cat /etc/login.defs | grep PASS_MIN_DAYS | grep -v ^# | awk '{print $2}'`
if [ $passmin -ge 6 ];then
  echo "口令更改最小时间间隔为${passmin}天,符合要求" >> accord_$ip-$today.log
else
  echo "口令更改最小时间间隔为${passmin}天,不符合要求,建议设置大于等于 6 天" >> noaccord_$ip-$today.log
fi
passlen=`cat /etc/login.defs | grep PASS_MIN_LEN | grep -v ^# | awk '{print $2}'`
if [ -n "$passlen" ]; then
  if [ $passlen -ge 8 ]; then
    echo "口令最小长度为${passlen},符合要求" >> accord_$ip-$today.log
  else
    echo "口令最小长度为${passlen},建议设置最小长度大于等于 8" >> noaccord_$ip-$today.log
  fi
else
  echo "口令最小长度未设置,不符合要求,建议设置最小长度大于等于 8" >> noaccord_$ip-$today.log
fi
passage=`cat /etc/login.defs | grep PASS_WARN_AGE | grep -v ^# | awk '{print $2}'`
if [ $passage -ge 30 -a $passage -lt $passmax ];then
  echo "口令过期警告时间天数为${passage},符合要求" >> accord_$ip-$today.log
else
  echo "口令过期警告时间天数为${passage},不符合要求,建议设置大于等于 30 并小于口令生存周期" >> noaccord_$ip-$today.log
fi

echo "***************************"
echo "账号是否会主动注销检查中..."
echo "***************************"
#cat /etc/profile | grep TMOUT
if [ $? -eq 0 ];then
TMOUT=`cat /etc/profile | grep TMOUT | awk -F[=] '{print $2}'`
 # TMOUT=100
  if [ -n "$TMOUT" ];then
    if [ $TMOUT -le 600 -a $TMOUT -ge 10 ];then
      echo "账号超时时间$TMOUT秒,符合要求" >> accord_$ip-$today.log
    else
      echo "账号超时时间$TMOUT秒,不符合要求,建议设置小于 600 秒" >> noaccord_$ip-$today.log
    fi
  else
    echo "账号超时不存在自动注销,不符合要求,建议设置小于 600 秒" >> noaccord_$ip-$today.log
  fi
fi

echo "***************************"
echo "检查查找空口令账户..."
echo "***************************"
emptypwd=`grep -v "::::::$" /etc/shadow|awk -F ":" '{print $2}'`
  if [ `grep -v "::::::$" /etc/shadow|awk -F ":" '{print $2}' | grep "\!\!"|wc -l` -ge 1 ];then
        echo "存在空口令账户,不符合要求,请尽快删除非法账户" >> noaccord_$ip-$today.log
  else
        echo "不存在空口令账户,符合要求" >> accord_$ip-$today.log
  fi

echo "***************************"
echo "检查非 root 账号 UID 为 0 的账号..."
echo "***************************"
UIDS=`awk -F[:] 'NR!=1{print $3}' /etc/passwd`
flag=0
for i in $UIDS
do
  if [ $i = 0 ];then
    echo "存在非 root 账号的账号 UID 为 0,不符合要求" >> accord_$ip-$today.log
  else
    flag=1
  fi
done
if [ $flag = 1 ];then
  echo "不存在非 root 账号的账号 UID 为 0,符合要求" >> accord_$ip-$today.log
fi

echo "***************************"
echo "检查umask 设置..."
echo "***************************"
if [ -f "/etc/profile" ]; then
    umask=`cat /etc/profile | grep umask | grep -v ^# | awk '{print $2}'|awk 'NR==2{print}'`
 if [ "$umask" = "027" ]; then
        echo "/etc/profile 文件中所设置的 umask 为027,符合要求" >> accord_$ip-$today.log
    else
      echo "/etc/profile 文件中所设置的 umask 不为027,不符合要求,建议设置为 027" >> noaccord_$ip-$today.log
  fi
fi
if [ -f "/etc/csh.cshrc" ]; then
    umask=`cat /etc/csh.cshrc | grep umask | grep -v ^# | awk '{print $2}'|awk 'NR==2{print}'`
   if [ "$umask" = "027" ]; then
        echo "/etc/csh.cshrc 文件中所设置的 umask 为027,符合要求" >> accord_$ip-$today.log
  else
      echo "/etc/csh.cshrc 文件中所设置的 umask 不为027,不符合要求,建议设置为 027" >> noaccord_$ip-$today.log
    fi
fi
if [ -f "/etc/bashrc" ]; then
   umask=`cat /etc/bashrc | grep umask | grep -v ^# | awk 'NR!=1{print $2}'|awk 'NR==2{print}'`
 if [ "$umask" = "027" ]; then
        echo "/etc/bashrc 文件中所设置的 umask 为027,符合要求" >> accord_$ip-$today.log
 else
      echo "/etc/bashrc 文件中所设置的 umask 不为027,不符合要求,建议设置为 027" >> noaccord_$ip-$today.log
   fi
fi      

echo "***************************"
echo "检查重要文件权限中..."
echo "***************************"
file1=`ls -l /etc/passwd | awk '{print $1}'`
if [ $file1 = "-rw-r--r--" ];then
  echo "/etc/passwd 文件权限为 644,符合要求" >> accord_$ip-$today.log
else
  echo "/etc/passwd 文件权限不为 644,不符合要求,建议设置权限为 644" >> noaccord_$ip-$today.log
fi
file2=`ls -l /etc/shadow | awk '{print $1}'`
if [ $file2 = "-r--------" ];then
  echo "/etc/shadow 文件权限为 400,符合要求" >> accord_$ip-$today.log
else
  echo "/etc/shadow 文件权限不为 400,不符合要求,建议设置权限为 400" >> noaccord_$ip-$today.log
fi
file3=`ls -l /etc/group | awk '{print $1}'`
if [ $file3 = "-rw-r--r--" ];then
  echo "/etc/group 文件权限为 644,符合要求" >> accord_$ip-$today.log
else
  echo "/etc/group 文件权限不为 644,不符合要求,建议设置权限为 644" >> noaccord_$ip-$today.log
fi

echo "***************************"
echo "检查 ssh 配置文件中..."
echo "***************************"
cat /etc/ssh/sshd_config | grep -v ^# |grep "PermitRootLogin no"
if [ $? -eq 0 ];then
  echo "已经设置远程 root 不能登陆,符合要求" >> accord_$ip-$today.log
else
  echo "没有设置远程 root 不能登陆,不符合要求,建议/etc/ssh/sshd_config 添加 PermitRootLogin no" >> noaccord_$ip-$today.log
fi

echo "***************************"
echo "检查 telnet 是否开启..."
echo "***************************"
if [ -f /etc/xinetd.d/telnet ];then
    telnetd=`cat /etc/xinetd.d/telnet | grep disable | awk '{print $3}'`
    if [ $telnetd = "yes" ];then
      echo "检测到 telnet 服务开启,不符合要求,建议关闭 telnet" >> noaccord_$ip-$today.log
    fi
else
  echo "telnet服务已关闭,符合要求" >> accord_$ip-$today.log
fi

echo "***************************"
echo "检查重要文件的属性..."
echo "***************************"
if [ -f "/etc/passwd" ]; then
  flag=0
    for ((x=1;x<=15;x++))
  do
      apend=`lsattr /etc/passwd | cut -c $x`
   if [ $apend = "i" ];then
     echo "/etc/passwd 文件存在 i 安全属性" >> accord_$ip-$today.log
     flag=1
      fi
    if [ $apend = "a" ];then
     echo "/etc/passwd 文件存在 a 安全属性" >> accord_$ip-$today.log
     flag=1
      fi
  done
  if [ $flag = 0 ];then
      echo "/etc/passwd 文件不存在相关安全属性,建议使用 chattr +i 或 chattr +a 防止/etc/passwd 被删除或修改" >> noaccord_$ip-$today.log
 fi
fi

if [ -f "/etc/shadow" ]; then
 flag=0
    for ((x=1;x<=15;x++))
  do
      apend=`lsattr /etc/shadow | cut -c $x`
   if [ $apend = "i" ];then
     echo "/etc/shadow 文件存在 i 安全属性" >> accord_$ip-$today.log
     flag=1
      fi
    if [ $apend = "a" ];then
     echo "/etc/shadow 文件存在 a 安全属性" >> accord_$ip-$today.log
     flag=1
      fi
  done
  if [ $flag = 0 ];then
      echo "/etc/shadow 文件不存在相关安全属性,建议使用 chattr +i 或 chattr +a 防止/etc/shadow 被删除或修改" >> noaccord_$ip-$today.log
 fi
fi

if [ -f "/var/log/messages" ]; then
   flag=0
    for ((x=1;x<=15;x++))
  do
      apend=`lsattr /var/log/messages | cut -c $x`
     if [ $apend = "i" ];then
     echo "/var/log/messages 文件存在 i 安全属性" >> accord_$ip-$today.log
       flag=1
      fi
    if [ $apend = "a" ];then
     echo "/var/log/messages 文件存在 a 安全属性" >> accord_$ip-$today.log
       flag=1
      fi
  done
  if [ $flag = 0 ];then
      echo "/var/log/messages 文件不存在相关安全属性,建议使用 chattr +i 或 chattr +a 防止/etc/gshadow 被删除或修改" >> noaccord_$ip-$today.log
  fi
fi

if [ -f "/etc/group" ]; then
  flag=0
    for ((x=1;x<=15;x++))
  do
      apend=`lsattr /etc/group | cut -c $x`
    if [ $apend = "i" ];then
     echo "/etc/group 文件存在 i 安全属性" >> accord_$ip-$today.log
      flag=1
      fi
    if [ $apend = "a" ];then
     echo "/etc/group 文件存在 a 安全属性" >> accord_$ip-$today.log
      flag=1
      fi
  done
  if [ $flag = 0 ];then
      echo "/etc/group 文件不存在相关安全属性,建议使用 chattr +i 或 chattr +a 防止/etc/group 被删除或修改" >> noaccord_$ip-$today.log
   fi
fi

echo "***************************"
echo "检查防止 syn 攻击优化..."
echo "***************************"
tcp_max_syn_backlog=`sysctl net.ipv4.tcp_max_syn_backlog | awk '{print $3}'`
#   echo $tcp_max_syn_backlog
    if [ "$tcp_max_syn_backlog" -lt "2048" ];then
      echo "检测到 syn 未提高未连接队列大小,不符合要求,建议net.ipv4.tcp_max_syn_backlog设置为大或等于 2048" >> noaccord_$ip-$today.log
    else
      echo "syn 已优化设置,符合要求" >> accord_$ip-$today.log
fi

echo "***************************"
echo "检查日志审核功能是否开启..."
echo "***************************"
if [ `systemctl status  auditd.service|grep "running"|wc -l` -ge 1 ];then
  echo "系统日志审核功能已开启,符合要求" >> accord_$ip-$today.log
else
  echo "系统日志审核功能已关闭,不符合要求,建议 service auditd start 开启" >> noaccord_$ip-$today.log
fi

echo "***************************"
echo "*** 检查完毕      ***"
echo "***************************"

二、Apache加固

1. 隐藏server版本号

  • /etc/httpd/conf/httpd.confApache配置文件中添加
ServerTokens Prod
ServerSignature Off
  • /etc/php.ini配置中隐藏版本号
expose_php = Off

2. 防止列目录泄露敏感信息

CentOS7加固 && Apache加固

/etc/httpd/conf/httpd.confApache配置文件中把
Options Indexes FollowSymLinks改为Options FollowSymLinks

3. 指定目录(上传目录)禁止php解析

  • /etc/httpd/conf/httpd.confApache配置文件中添加
<Directory "/var/www/html/uploads">
php_flag engine off
</Directory>

4. 限制管理员后台特定IP访问

  • /etc/httpd/conf/httpd.confApache配置文件中添加
<Directory "/var/www/html/admin"> 
    Order Deny,Allow
  Deny from all
 Allow from 192.168.1.111
</Directory>

5. 关闭对.htaccess的支持

/etc/httpd/conf/httpd.confApache配置文件中添加AllowOverride None