我需要规则来删除一些恶意的Apache连接

除了我的网络服务器80以外,我删除了所有端口上的流量。

我在iptables上有这样的规则:

iptables -A INPUT -p tcp -m tcp --dport 80 -m string --string "cgi" --algo bm --to 1000 -j DROP 

有更多人可以分享? 我知道坏黑客总是在不断更新,但有些黑客总是以相同的代码开头。 我需要根据一些标准删除连接。 这里有一些Apache日志(我删除了ips,但每个atack来自同一个):

攻击1:我不知道该怎么办,但是从同一个ip做了50次

 GET / HTTP/1.1 301 224 - Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22 GET / HTTP/1.1 302 3387 - Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22 

攻击2:这只是尝试获取有关服务器的信息。

 GET / HTTP/1.1 301 224 http://myip:80/ Go-http-client/1.1 GET / HTTP/1.1 302 3228 http mywebsite Go-http-client/1.1 GET /es/ HTTP/1.1 200 40947 https mywebsite Go-http-client/1.1 

攻击3:他们尝试访问登录页面漏洞

 GET /userlogin/login.aspx HTTP/1.1 302 186 - - 

攻击4:这尝试在第一次请求时访问cgi,(请参阅我的第一个iptables规则来删除它)

 GET /hndUnblock.cgi HTTP/1.0 302 186 - Wget(linux) GET /tmUnblock.cgi HTTP/1.0 302 186 - Wget(linux) 

我对服务器非常新,这4次攻击仅持续12小时……每周有数千次攻击。

更新: 当前答案已完全更新。

根据这个讨论,我创建了一个名为WWW Security Assistant的GitHub存储库。 有一个名为ask_ubuntu的分支,致力于这个答案。 由于字符限制,以前在此处提供的所有引用都将被删除 – 它们可在GitHub上获得。

这里有几种方法,涉及到一个完整的机制, 如何在Ubuntu 16.04中增加Apache2安全性

表中的内容:

  • WWW安全助手脚本(WSAS)►Iptables
  • Iptables – 基本配置 – 保存和恢复
  • 适用于Apache2的ModEvasive
  • ModEvasive►WSAS►Iptables
  • 适用于Apache2的ModSecurity 2.9
  • ModSecurity OWASP核心规则集3.x.
  • ModSecurity规则白名单
  • ModSecurity规则►WSAS►Iptables
  • ModSecurity和Apache日志文件
  • ModSecurity日志文件►Wail2Ban►Iptables
  • ModSecurityGuardianLog►HTTPDGuardian►WSAS►Iptables
  • ModSecurityGuardianLog►HTTPDCustomAnalyze►WSAS►Iptables

另外,让我们说使用HTTPS总是好的:

  • 阅读HTTPS和常见Apache安全提示

WWW安全助手脚本►Iptables

这里是脚本www-security-assistant.bash 。 它可以帮助您处理恶意IP地址。 该脚本有两种模式。

自动模式

当外部程序(如Apache的mod_security )提供恶意$IP地址时。 在这种情况下,调用脚本的语法应该是:

 www-security-assistant.bash  Guardian www-security-assistant.bash  ModSecurity www-security-assistant.bash  ModEvasive www-security-assistant.bash  a2Analyst 

在此模式下,脚本提供两个操作阶段,并且对于每个操作,它将管理员发送电子邮件

  • 第一阶段:对于前几个“违规” ,源$IP将被禁止一段时间 ,该时间段等于$BAN_TIME的值。 此模式使用命令at

  • 第二阶段:当某些$IP的违规数量等于$LIMIT的值时,此$IP地址将通过Iptables 永久禁止 ,并将被添加到$BAN_LIST

手动模式

此模式接受以下选项:

  • www-security-assistant.bash --DROP "log notes"

    在文件/var/www-security-assistant/iptables-DROP.list创建一个条目,并生成一条规则:

     iptables -A GUARDIAN -s $IP -j DROP 
  • www-security-assistant.bash --DROP-CLEAR "log notes"

    创建文件/var/www-security-assistant/iptables-DROP-CLEAR.list的条目,删除某些Iptables规则,从历史记录和$BAN_LIST删除$IP

     iptables -D GUARDIAN -s $IP -j DROP 
  • www-security-assistant.bash --ACCEPT "log notes"

    仅创建文件/var/www-security-assistant/iptables-ACCEPT.list的条目。

  • www-security-assistant.bash --ACCEPT-CHAIN "log notes"

    创建文件/var/www-security-assistant/iptables-ACCEPT.list的条目,并生成以下规则:

     iptables -A GUARDIAN -s $IP -j ACCEPT 

依赖

该脚本使用iptables-save.shiptablesGUARDIAN ,将在下一节中介绍。 它将在$WORK_DIR创建和维护一些文件:

  • www-security-assistant.history – 包含先前IP违规的数据。
  • www-security-assistant.mail – 脚本发送的最后一封电子邮件的内容。
  • iptables-ACCEPT.list ; iptables-DROP.listiptables-DROP-CLEAR.list

该脚本需要最小配置才能发送电子邮件:

 sudo apt install s-nail mutt mailutils postfix sudo dpkg-reconfigure postfix # For General type: Internet Site echo 'Test passed.' | mail -s Test-Email email@example.com 

如果有任何已配置的HTTPS服务,则可以在Postfix服务中使用其TLS证书。

另外脚本用于: sudo apt install at

安装

  • 创建工作目录,我们称之为/var/www-security-assistant 。 下载www-security-assistant.bash并使其可执行:

     sudo mkdir /var/www-security-assistant sudo wget https://raw.githubusercontent.com/pa4080/www-security-assistant/ask_ubuntu/www-security-assistant.bash -O /var/www-security-assistant/www-security-assistant.bash sudo chmod +x /var/www-security-assistant/www-security-assistant.bash 
  • 使www-security-assistant.bash可用作自定义命令:

     sudo ln -s /var/www-security-assistant/www-security-assistant.bash /usr/local/bin/ 
  • 授予www-data权限,无需密码即可通过sudo运行www-security-assistant.bash 。 使用以下命令可以使用其他“ sudoers ”规则安全地创建和编辑新文件:

     sudo visudo -f /etc/sudoers.d/www-security-assistant 

    在文件中添加以下行 – 保存文件并退出:

     www-data ALL=(ALL) NOPASSWD: /var/www-security-assistant/www-security-assistant.bash 
  • Tweak www-security-assistant.bash 。 至少更改变量$EMAIL_TO的值。

检查

  • 将自己表示为$AGENT并检查自动模式是否正常工作:

     www-security-assistant.bash 192.168.1.177 Guardian 

    然后检查你的电子邮件,输入iptables -L GUARDIAN -n ,查看文件www-security-assistant.historywww-security-assistant.mail 。 运行上面的命令5次,查看文件iptables-DROP.listiptables-CURRENT.conf

  • 检查手动模式是否正常工作 – 将本地主机添加到白名单:

     www-security-assistant.bash 127.0.0.1 --ACCEPT "Server's localhost IP" 

    然后检查文件iptables-ACCEPT.list

本教程的其余部分是如何将www-security-assistant与您的系统集成。

Iptables – 基本配置 – 保存和恢复

基本配置

在添加以下规则之前,请阅读本手册 。

 sudo iptables -F sudo iptables -I INPUT 1 -i lo -j ACCEPT sudo iptables -I INPUT 2 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # This rule may lock you out of the system! sudo iptables -P INPUT DROP sudo iptables -P OUTPUT ACCEPT 

在您执行下一步操作之前,请打开一个新的SSH连接并尝试登录系统以检查一切是否正常!

保存并恢复

这可以通过自定义脚本来实现,这将在系统的停止 – 启动(或重启)过程中保存和恢复iptables coning。 (如果我们使用UFW来设置Iptables规则,则不需要此步骤。)

 printf '#!/bin/sh\n/sbin/iptables-save > /var/www-security-assistant/iptables-CURRENT.conf\nexit 0\n' | sudo tee /var/www-security-assistant/iptables-save.sh printf '#!/bin/sh\n/sbin/iptables-restore < /var/www-security-assistant/iptables-CURRENT.conf\nexit 0\n' | sudo tee /var/www-security-assistant/iptables-restore.sh sudo chmod +x /var/www-security-assistant/iptables-restore.sh /var/www-security-assistant/iptables-save.sh 
 sudo ln -s /var/www-security-assistant/iptables-save.sh /etc/network/if-post-down.d/iptables-save sudo ln -s /var/www-security-assistant/iptables-restore.sh /etc/network/if-pre-up.d/iptables-restore 

创建新的链

创建一个名为GUARDIAN新链,并将其作为数字3插入INPUT链:

 sudo iptables -N GUARDIAN sudo iptables -I INPUT 3 -j GUARDIAN 

检查

重新启动系统并检查配置。 请使用sudo systemctl reboot (不要使用强制选项reboot -f )。 当系统重新联机时,我们可以通过以下方式检查新创建的链是否存在:

 sudo iptables -L GUARDIAN -n 

适用于Apache2的ModEvasive

ModEvasive是一个回避操作模块,用于Apache在发生HTTP DoS或DDoS攻击或暴力攻击时提供规避操作。 阅读更多...

安装

  • 安装并启用该模块:

     sudo apt install libapache2-mod-evasive sudo a2enmod evasive 
  • 创建日志目录并使其可访问www-data

     sudo mkdir -p /var/log/apache2_mod_evasive sudo chown www-data /var/log/apache2_mod_evasive 
  • 调整基本配置 - 取消注释并编辑配置文件中的某些指令:

     /etc/apache2/mods-enabled/evasive.conf 
  • 重启Apache: sudo systemctl restart apache2.service

检查

  • 从服务器打开一个网页并集中刷新浏览器窗口几次(按F5 ) - 您必须得到403 Forbidden错误消息。 进入日志目录,将生成一个新的锁定文件。 应删除此文件,以便从此IP地址进一步检测违规行为。

ModEvasive►WSAS►Iptables

在这里,我们将配置mod_evasive通过上面创建的www-security-assistant.bashiptables

  • 以这种方式编辑/etc/apache2/mods-available/evasive.conf

      DOSHashTableSize 3097 DOSPageCount 9 DOSSiteCount 70 DOSPageInterval 2 DOSSiteInterval 2 DOSBlockingPeriod 10 #DOSEmailNotify your@email.foo DOSLogDir "/var/log/apache2_mod_evasive" DOSSystemCommand "sudo /var/www-security-assistant/www-security-assistant.bash %s 'ModEvasive' 'AutoMode' >> /var/www-security-assistant/www-security-assistant.execlog 2>&1"  
  • 创建日志文件并重新启动Apache:

     sudo touch /var/www-security-assistant/www-security-assistant.execlog && sudo chown www-data /var/www-security-assistant/www-security-assistant.execlog 

为了测试这种配置,我们可以通过上面提到的F5方法模拟DDOS攻击,或者我们可以使用abhping3等命令。

注意:请注意,因为WSAS中使用的iptables规则将DROP来自源$IP所有连接,包括您的SSH连接。 在测试期间,有一种备份方式可以连接到服务器。 您可以将此规则更改为仅适用于HTTP / HTTPS端口。

适用于Apache2的ModSecurity 2.9

ModSecurity是一个Web应用程序防火墙引擎,它本身提供的保护很少。 为了变得有用,必须使用规则配置ModSecurity。 为了使用户能够充分利用开箱即用的ModSecurity,Trustwave的Spider Labs提供免费的认证规则集... 阅读更多...

安装

  • 安装并启用该模块:

     sudo apt install libapache2-mod-security2 sudo a2enmod security2 
  • 创建配置文件:

    sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

    仔细阅读并编辑/etc/modsecurity/modsecurity.conf ! 添加或更改至少以下指令:

     # -- Rule engine initialization ---------------------------------------------- SecRuleEngine On # -- Debug log configuration ------------------------------------------------- SecDebugLogLevel 2 SecDebugLog "/var/log/apache2_mod_security/modsec_debug.log" # -- Audit log configuration ------------------------------------------------- SecAuditLog "/var/log/apache2_mod_security/modsec_audit.log" # -- Guardian log configuration ------------------------------------------------- SecGuardianLog /var/log/apache2_mod_security/modsec_guardian.log 
  • 文件/etc/apache2/mods-enabled/security2.conf /etc/modsecurity/modsecurity.conf包含在Apache的配置中。 在这个阶段, security2.conf看起来像这样:

      SecDataDir /var/cache/modsecurity IncludeOptional /etc/modsecurity/*.conf  
  • 创建日志目录:

     sudo mkdir -p /var/log/apache2_mod_security 
  • 设置日志轮换。 首先创建配置文件:

     sudo cp /etc/logrotate.d/apache2 /etc/logrotate.d/apache2-modsec 

    然后以这种方式编辑新文件:

     /var/log/apache2_mod_security/*.log { … } 
  • 重启Apache。

检查

  • /etc/modsecurity创建一个额外的配置文件,例如调用它为z-customrules.conf ,并添加以下规则作为其内容:

     # Directory traversal attacks SecRule REQUEST_URI "../" "t:urlDecodeUni, deny, log, id:109" 

    重启服务器: sudo systemctl restart apache2.service 。 打开浏览器并输入https://example.com/?abc=../ 。 结果将是: 403禁止 。 检查/var/log/apache2_mod_security的日志文件以获取更多详细信息。

  • 为了让事情更有趣 ,将脚本issues.php放在DocumentRoot中的适当位置(这里我假设这个地方是/var/www/html ):

     sudo wget https://raw.githubusercontent.com/pa4080/www-security-assistant/ask_ubuntu/appendix/var/www/html/issues.php -O /var/www/html/issues.php 

    然后按以下方式修改上述规则:

     # Directory traversal attacks with redirection (or use URL instead of URI: redirect:'https://example.com/issues.php') SecRule REQUEST_URI "../" "t:urlDecodeUni, deny, log, id:109, redirect:'/issues.php'" 

    重新启动Apache,然后打开浏览器并输入https://example.com/?abc=../ ;-)这个想法来自SE的脚本BotLovin.cs

  • 再次编辑/etc/modsecurity/z-customrules.conf并注释(禁用)规则 - 这只是测试示例,它由OWASP CRS涵盖,将在下一节中介绍。

  • 这是另一个示例,我们将重定向所有wp-admin页面请求,但除了这些来自某些IP地址(请注意chain ):

     # Block wp-admin access SecRule REQUEST_URI "^/wp-admin" "id:108, log, deny, status:403, t:lowercase, chain, redirect:'/issues.php'" SecRule REMOTE_ADDR "!@ipMatch 192.168.1.11,99.77.66.12" 

    这里我们有两个破坏性的行为:(1) deny, status:403和(2) redirect:'/issues.php' 。 实际上我们不需要deny动作,因为它将被redirect动作覆盖。

ModSecurity OWASP核心规则集3.x.

在Ubuntu 16.04中,您可以安装CSR 2.x: apt install modsecurity-crs 。 这里我们将安装CSR 3.x , 安装手册中提供了详细说明(需要git )。

安装

  • 在文件夹/usr/share/modsecurity-crs.3克隆CSR:

     sudo git clone https://github.com/SpiderLabs/owasp-modsecurity-crs /usr/share/modsecurity-crs.3 
  • 升级并自动续订GeoIP数据库。 (CRS不再包含GeoIP DB。相反,建议您定期下载。)脚本util/upgrade.py带来了此function。 你可以在cron- sudo crontab -e使用如下:

     0 2 * * * /usr/share/modsecurity-crs.3/util/upgrade.py --geoip --crs --cron >> /var/log/apache2_mod_security/owasp-crs-upgrade.log 2>&1 
  • 创建配置文件:

     sudo cp /usr/share/modsecurity-crs.3/crs-setup.conf{.example,} sudo cp /usr/share/modsecurity-crs.3/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf{.example,} sudo cp /usr/share/modsecurity-crs.3/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf{.example,} 

    仔细阅读和编辑这些文件! 至少取消注释SecGeoLookupDB指令:

     SecGeoLookupDB util/geo-location/GeoIP.dat 
  • 应用Apache的配置。 以这种方式编辑/etc/apache2/mods-available/security2.conf

      SecDataDir /var/cache/modsecurity IncludeOptional /etc/modsecurity/*.conf IncludeOptional /usr/share/modsecurity-crs.3/crs-setup.conf IncludeOptional /usr/share/modsecurity-crs.3/rules/*.conf  

    保存该文件,然后重新启动Apache。

ModSecurity规则白名单

ModSecurity规则的白名单可以通过以下ModSec指令完成,这些指令可以在系统范围内使用,也可以在虚拟主机的配置中使用,也可以在全局范围内用于特定目录或位置匹配:

 SecRuleRemoveById SecRuleRemoveByMsg SecRuleRemoveByTag SecRuleUpdateTargetById SecRuleUpdateTargetByMsg SecRuleUpdateTargetByTag SecRuleUpdateActionById 

为PhpMyAdmin禁用mod_security2 。 以这种方式更改/etc/phpmyadmin/apache.conf

   SecRuleEngine Off   

禁用某些目录的特定规则:

   SecRuleRemoveById 973301   

全局禁用规则。 为此,我们必须在Apache的配置文件/etc/modsecurity/z-customrules.conf某处添加我们的指令: /etc/modsecurity/z-customrules.conf是个好地方。

  • 禁用整个Apache配置中的规则:

     SecRuleRemoveById 973301 950907 
  • 将IP地址列入白名单,以便它可以通过ModSecurity:

     SecRule REMOTE_ADDR "@ipMatch 192.168.110.1" "phase:1,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off" 
  • 禁用目录匹配中的规则:

      SecRuleRemoveById 973301 950907  
  • 通过位置匹配中的ID更新规则的操作

      SecRuleUpdateActionById 973301 "pass" SecRuleUpdateActionById 950907 "pass"  

在上面的示例中,我们假设973301950907是阻碍我们的Web应用程序正常工作的规则ID。 我们可以通过分析modsec_audit.log找到这些规则。

ModSecurity规则►WSAS►Iptables

下面给出几个如何创建自定义SecRules的示例,以及我们如何通过它们调用WWW Security Assistant脚本(WSAS)。

初始设置

我们需要一个额外的启动脚本 - modsecurity-assistant.sh 。 原因是,ModSecurity的exec操作过于简单且有限的语法。

 sudo wget https://raw.githubusercontent.com/pa4080/www-security-assistant/ask_ubuntu/modsecurity-assistant.sh -O /var/www-security-assistant/modsecurity-assistant.sh sudo chmod +x /var/www-security-assistant/modsecurity-assistant.sh 

如果您查看脚本内部,您将看到ModSecurity导出的变量很少。 它们是: $REQUEST_URI$ARGS$SERVER_NAME$REMOTE_ADDR$REMOTE_HOST$UNIQUE_ID 。 其他变量在脚本中解释。

创建自定义规则并通过它调用我们的脚本

首先,让我们创建一个规则,当请求URI包含我们的黑名单中包含的单词时,该规则将执行modsecurity-assistant.sh (并调用www-security-assistant.bash )。 打开/etc/modsecurity/z-customrules.conf并在底部添加以下行:

 # REQUEST_URI words blacklist # SecRule REQUEST_URI "@pmFromFile /var/www-security-assistant/modsecurity-uri-black.list" \ "id:150, log, t:lowercase, chain, \ drop, deny, status:403, redirect:'/issues.php'" SecRule REMOTE_ADDR "!@ipMatchFromFile /var/www-security-assistant/modsecurity-ip-white.list" \ "setenv:REMOTE_HOST=%{REMOTE_HOST}, \ setenv:ARGS=%{ARGS}, \ exec:/var/www-security-assistant/modsecurity-assistant.sh" 
  • REQUEST_URI - 此变量包含当前请求的完整URI。 规则更广泛: SecRule REQUEST_URI|ARGS|REQUEST_BODY ...

  • @pmFromFile将读取包含短语列表的文件modsecurity-uri-black.list ,其中每个特定短语或单词都放在一个新行中。 您可以从日志文件中收集有趣的单词和短语。 如果REQUEST_URI与我们的模式列表之间存在特定匹配,则将应用该规则。 该文件可能为空,但您必须创建( touch )它。

  • log操作将在此规则的日志文件中创建日志条目,其id:150

  • dropdeny (with status )和redirect actions属于破坏性的一组动作,它们必须位于规则chain的开头(如果有一个链)。 第二个操作将覆盖第一个操作,第三个操作将覆盖第二个操作,因此您必须选择要执行的操作并删除其他操作。

  • chain动作将调用chain的下一个规则,注意第二个规则,没有id

  • REMOTE_ADDR包含请求的IP地址。

  • @ipMatchFromFile将文件modsecurity-ip-white.list包含以新行分隔的IP地址白名单。 CIDR条目也是可以接受的。 因为破坏性操作始终位于链的前导规则中,所以它将被应用,但是当某个IP位于此白名单中时,将不会应用exec操作。 该文件可能为空,但您必须创建( touch )它。

  • exec动作将调用我们的外部脚本。 此操作不具有破坏性 ,将在当前规则返回true时执行。 应用此操作时,将通过我们的脚本处理远程IP。

  • setenv此操作会将某些内部变量 =%{...}导出为envvars,导出的名称可能与内部不同。 一些变量必须手动导出,其他变量必须自动导出 - 可能是一个小错误(在某些情况下,手动导出具有相同名称,例如setenv:REQUEST_URI=%{REQUEST_URI} )将导致导出的空白值变量)。

检查

假设你的服务器上没有Joomla,编辑文件modsecurity-uri-black.list并添加一行内容/joomla 。 然后输入您的浏览器https://exemple.com/joomla 。 您应该通过Iptables重定向和阻止。 清除记录sudo www-security-assistant.bash --DROP-CLEAR 'some note' ,在modsecurity-ip-white.list添加你的IP并再次进行练习。 现在您应该被重定向,但不会被阻止。

使用OWASP核心规则集3.x连接我们的脚本

为此,我们将更新exception模式规则 (949110和959100)的默认操作。 为此,编辑文件/usr/share/modsecurity-crs.3/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf并将下一行添加到底部:

 # -- Anomaly Mode - Update actions by ID ----- # SecRuleUpdateActionById 949110 "t:none, drop, deny, status:403, redirect:'/issues.php', \ setenv:REMOTE_HOST=%{REMOTE_HOST}, setenv:ARGS=%{ARGS}, \ exec:/var/www-security-assistant/modsecurity-assistant.sh" SecRuleUpdateActionById 959100 "t:none, drop, deny, status:403, redirect:'/issues.php', \ setenv:REMOTE_HOST=%{REMOTE_HOST}, setenv:ARGS=%{ARGS}, \ exec:/var/www-security-assistant/modsecurity-assistant.sh" # -- Anomaly Mode - Whitelist some URI and IP addresses ----- # SecRule REQUEST_URI "^/wp-admin/admin-ajax.php*|^/index\.php\?title=.*&action=(submit|raw&ctype=text/javascript|raw&ctype=text/css)$" \ "id:'999010', t:none, phase:1, pass, \ ctl:ruleRemoveById=949110, \ ctl:ruleRemoveById=959100" SecRule REMOTE_ADDR "@ipMatchFromFile /var/www-security-assistant/modsecurity-ip-white.list" \ "id:'999020', t:none, phase:1, pass, \ ctl:ruleRemoveById=949110, \ ctl:ruleRemoveById=959100" 

检查

不要忘记重新启动(或重新加载)Apache以应用配置更改。 不要忘记在测试期间定期清除记录,否则可以永久阻止:-)

模拟目录遍历攻击:

 https://example.com/?abc=../../../ # This should be redirected and blocked https://example.com/wp-admin/admin-ajax.php?abc=../../../ # This should pass because of the whitelist rule 

模拟SQL注入攻击:

 https://example.com/?username=1'%20or%20'1'%20=%20'1&password=1'%20or%20'1'%20=%20'1 https://example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&password=foo 

ModSecurity和Apache日志文件

可以将Apache Web服务器配置为向服务器管理员提供有关其运行方式的重要信息...向管理员提供反馈的主要途径是使用日志文件。 阅读更多...

ModSecurity具有强大的日志记录机制。 通过SecGuardianLog指令,它提供了专门用于处理外部脚本的日志源。

目前,唯一可以使用监护人日志记录的工具是httpd-guardian ,它是Apache httpd工具项目的一部分 。 httpd-guardian工具旨在防御拒绝服务攻击。 它使用blacklist tool与基于iptables的防火墙进行交互,动态地将违规的IP地址列入黑名单。 阅读更多...

ModSecurity日志文件►Wail2Ban►Iptables

可以为Apache的日志文件的数据解析设置Fail2Ban。 modsec_audit.log可能是最好的选择,但另请参阅我们讨论SecGuardianLog

请注意 /etc/modsecurity/modsecurity.conf中的SecAuditLogRelevantStatus被注释。 否则,收到404错误页面的每个人都将被fail2ban阻止。

 SecAuditEngine RelevantOnly #SecAuditLogRelevantStatus "^(?:5|4(?!04))" 

目前,在该项目中没有以任何方式实现Fail2Ban。

ModSecGuardianLog►HTTPD-Guardian►WSAS►Iptables

httpd-guardian - 通过监控请求检测DoS攻击Apache Security,版权所有(C)2005 Ivan Ristic - 旨在通过管道日志记录机制监控所有Web服务器请求。 它跟踪从每个IP地址发送的请求数... httpd-guardian可以发出警告或执行脚本来阻止IP地址......

此脚本可与Apache2日志记录机制一起使用 ,也可与ModSecurity (更好)一起使用。

在当前情况下的安装和设置

下载httpd-guardian并使其可执行:

 sudo wget https://raw.githubusercontent.com/pa4080/www-security-assistant/ask_ubuntu/httpd-guardian.pl -O /var/www-security-assistant/httpd-guardian.pl sudo chmod +x /var/www-security-assistant/httpd-guardian.pl 

阅读第98-119行,了解脚本如何与我们的WSAS脚本连接。

在Apache的配置( /etc/modsecurity/modsecurity.conf )中应用以下更改,然后重新启动它:

 #SecGuardianLog /var/log/apache2_mod_security/modsec_guardian.log SecGuardianLog "|/var/www-security-assistant/httpd-guardian.pl" 

检查

要测试脚本禁用ModEvasive( sudo a2dismod evasive不要忘记稍后启用它)并重新启动Apache。 然后尾随exec日志:

 tail -F /var/www-security-assistant/www-security-assistant.execlog 

从另一个实例执行DoS攻击,例如以这种方式使用ab

 for i in {1..20}; do (ab -n 200 -c 10 https://example.com/ &); done 

ModSecGuardianLog►自定义分析►WSAS►Iptables

这里有一个简单的脚本,名为httpd-custom-analyze.bash ,这不是特别的,但可能是一个很好的例子。 它的function在脚本的主体中描述。

安装和设置

下载httpd-custom-analyze.bash并使其可执行:

 sudo wget https://raw.githubusercontent.com/pa4080/www-security-assistant/ask_ubuntu/httpd-custom-analyze.bash -O /var/www-security-assistant/httpd-custom-analyze.bash sudo chmod +x /var/www-security-assistant/httpd-custom-analyze.bash 

在Apache的配置( /etc/modsecurity/modsecurity.conf )中应用以下更改并重新启动它:

 #SecGuardianLog /var/log/apache2_mod_security/modsec_guardian.log #SecGuardianLog "|/var/www-security-assistant/httpd-guardian.pl" SecGuardianLog "|/var/www-security-assistant/httpd-custom-analyze.bash" 
  • 当达到阈值时,脚本将调用WSAS - 读取第86行和第35

  • 要使两个httpd-脚本同时工作,请编辑modsecurity.conf并将SecGuardianLog管道到两者。

  • 要执行测试,请遵循上一节中的提示。