1

我正在尝试在我的 CENTOS7 服务器 (VPS) 中配置 mod_evasive 以防止 DDOS 攻击。

我按照以下教程中提到的步骤进行操作。(此链接)虽然我没有使用 IPTABLES,但我使用的是 firewalld。

在 mod_evasive 中,当 IP 被列入黑名单时执行 shell 命令的指令是DOSSystemCommand. 但是当一个 IP 被阻止时,我想运行的脚本会将 IP 地址提交给 firewalld 以阻止,被 SeLinux 阻止。根据我查看 audit.log 后的理解,SeLinux 不允许 apache 用户以 sudo 身份运行命令。即使通过 sudoers 文件允许这样做。

(mod_evasive 工作正常,我在运行 mod_evasive 提供的测试脚本时验证了这一点。当 IP 被阻止时,我也会收到电子邮件)

以下是具体内容;

mod_evasive.conf 文件(仅 DOSSystemCommand 指令如下所示)

DOSSystemCommand    "sudo /var/www/mod_evasive/ban_ip_mod_evasive.sh"

我也尝试了以下变体(也有不同的用户,例如 root 用户)

DOSSystemCommand    "sudo -u apache '/var/www/mod_evasive/ban_ip_mod_evasive.sh %s'"

sudoers 文件(仅我为此目的修改的部分 [允许 apache 以 sudo 运行)

apache ALL=(ALL) NOPASSWD:ALL
Defaults:apache !requiretty

我允许 apache 以任何用户身份运行所有脚本,仅用于测试目的。最初我将命令限制如下

apache ALL=NOPASSWD: /usr/local/bin/myscripts/ban_ip.sh
Defaults:apache !requiretty

我的 ban_ip.sh 文件

#!/bin/sh
# IP that will be blocked, as detected by mod_evasive
IP=$1
sudo firewall-cmd --add-rich-rule 'rule family="ipv4" source address="`$IP" service name="https" reject'

我已经通过运行以下命令验证了配置是否正常工作

sudo -u apache '/usr/local/bin/myscripts/ban_ip.sh 111.111.111.111'

上述命令成功将规则添加到 firewalld。

当我禁用 SeLinux 时,整个设置工作。

启用 SeLinux 后,audit.log 中出现以下错误

type=AVC msg=audit(1593067671.808:1363): avc:  denied  { setuid } for  pid=3332 comm="sudo" capability=7  scontext=system_u:system_r:httpd_sys_script_t:s0 tcontext=system_u:system_r:httpd_sys_script_t:s0 tclass=capability permissive=0

        Was caused by:
                Missing type enforcement (TE) allow rule.

                You can use audit2allow to generate a loadable module to allow this access.

type=AVC msg=audit(1593067671.808:1364): avc:  denied  { setgid } for  pid=3332 comm="sudo" capability=6  scontext=system_u:system_r:httpd_sys_script_t:s0 tcontext=system_u:system_r:httpd_sys_script_t:s0 tclass=capability permissive=0

        Was caused by:
                Missing type enforcement (TE) allow rule.

                You can use audit2allow to generate a loadable module to allow this access.

SeLinux 中设置了以下标志

httpd_mod_auth_pam 
httpd_setrlimit 

我不确定为什么 SeLinux 会阻止这一点,因此我怀疑创建一个自定义策略来允许此行为,如 audit2allow 所建议的那样。

审计2允许输出

#============= httpd_sys_script_t ==============
allow httpd_sys_script_t self:capability { setgid setuid };

#============= httpd_t ==============
allow httpd_t systemd_logind_t:dbus send_msg;

如果我们不能阻止 IP 而不仅仅是向403 forbidden攻击者发送响应,我觉得 mod_evasive 将无助于阻止 DDOS。这仍然会消耗服务器资源。

我该如何解决这个问题以及我可以实施哪些其他替代方案来缓解 DDOS 并增强我的 VPS 保护?

感谢您的帮助

4

0 回答 0