Linux系统中Web服务安全防护

Centos7下创建 test 普通用户,然后给其设置一个密码,并修改该登录 shell 为 /bin/sh;最后将该用户禁用,修改 /etc/httpd/conf/httpd.conf 配置文件,设置网站根目录为 /www(目录自行创建),禁止目录列出,设置 404 错误页面为 /missing.html,设置 Apache 在错误页面时不回显任何版本信息。

以下用 root 操作。

1. 创建 test 用户,设置密码、Shell,最后禁用账号。

useradd test                 # 上一题已创建则跳过此行
passwd test                  # 按提示输入两次密码
usermod -s /bin/sh test
usermod -L -e 1 test         # 锁定密码,并使账号过期

2. 安装 Apache。

yum install -y httpd

如果默认 YUM 源失效,CentOS 7 x86_64 默认源可改用归档源后重试:

cd /etc/yum.repos.d
cp -p CentOS-Base.repo CentOS-Base.repo.bak
sed -i -e 's|^mirrorlist=|#mirrorlist=|' \
       -e 's|^#baseurl=http://mirror.centos.org/centos/$releasever/|baseurl=https://vault.centos.org/7.9.2009/|' CentOS-Base.repo
yum clean all
yum install -y httpd

3. 创建网站根目录、首页和 404 页面。

mkdir -p /www/testdir        # 空目录,用于验证禁止目录列出
echo 'Welcome to Apache!' > /www/index.html
echo '404 - Page Not Found!' > /www/missing.html
chmod 755 /www /www/testdir
chmod 644 /www/index.html /www/missing.html

4. 修改 Apache 配置。

cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
vi /etc/httpd/conf/httpd.conf

将原来的 DocumentRoot 改为 /www,并补齐以下配置。保留文件中的其他配置。

DocumentRoot "/www"

<Directory "/www">
    Options -Indexes
    AllowOverride None
    Require all granted
</Directory>

ErrorDocument 404 /missing.html
ServerSignature Off
ServerTokens Prod

Options -Indexes 禁止列出目录。ServerSignature Off 关闭错误页签名,ServerTokens Prod 隐藏响应头中的版本号。

5. 检查和重启。

httpd -t && systemctl restart httpd

6. 验证结果。

getent passwd test            # 最后一项为 /bin/sh
passwd -S test                # LK 表示密码已锁定
curl http://127.0.0.1/        # 显示 Welcome to Apache!
curl -I http://127.0.0.1/testdir/     # 返回 403,禁止目录列出
curl -i http://127.0.0.1/no-such-page # 返回 404 和自定义页面,Server 头无版本号

版权声明:
作者:Gweek
链接:https://www.ucpu.de/archives/311
来源:Gweek postHub
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>