如何在 Ubuntu 22.04 上使用 ModSecurity 安装 Apache如何在 Ubuntu 22.04 上使用 ModSecurity 安装 Apache如何在 Ubuntu 22.04 上使用 ModSecurity 安装 Apache如何在 Ubuntu 22.04 上使用 ModSecurity 安装 Apache
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Ubuntu 22.04 上使用 ModSecurity 安装 Apache

在此页

  1. 要求
  2. 安装 Apache Web 服务器
  3. 安装 ModSecurity
  4. 配置 ModSecurity
  5. 安装 OWASP 核心规则集
  6. 验证 ModSecurity
  7. 配置Logrotate
  8. 结论

ModSecurity 是一种免费的开源防火墙工具,受各种 Web 服务器(如 Apache、Nginx 和 IIS)支持。它是一种基于签名的防火墙,能够阻止多种类型的攻击,包括跨站点脚本 (XSS)、暴力攻击和已知代码注入攻击。它提供了不同的规则集,允许您自定义和配置您的服务器安全性。它还可以实时监控网络流量,帮助您检测和响应入侵。

在本教程中,我将解释如何在 Ubuntu 22.04 上使用 Apache 设置 ModSecurity。

要求

  • 一台运行 Ubuntu 22.04 的服务器。
  • 在您的服务器上配置了根密码。

安装 Apache Web 服务器

在开始之前,您需要在您的服务器上安装最新版本的 Apache Web 服务器包。首先,使用以下命令安装所有必需的依赖项:

apt install gnupg2 software-properties-common curl wget git unzip -y

安装所有依赖项后,使用以下命令添加 Apache 存储库:

add-apt-repository ppa:ondrej/apache2 -y

接下来,更新存储库缓存并使用以下命令安装 Apache 包:

apt update -y
apt install apache2 -y

安装 Apache 后,您可以继续下一步。

安装 ModSecurity

接下来,运行以下命令来安装 ModSecurity Apache 模块:

apt install libapache2-mod-security2 -y

安装包后,使用以下命令启用 ModSecurity 模块:

a2enmod security2

接下来,重新启动 Apache 服务以应用更改:

systemctl restart apache2

您还可以使用以下命令检查 Apache 状态:

systemctl status apache2

您将获得以下输出:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-18 10:49:42 UTC; 4s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 74445 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 74449 (apache2)
      Tasks: 55 (limit: 2242)
     Memory: 29.7M
        CPU: 229ms
     CGroup: /system.slice/apache2.service
             ??74449 /usr/sbin/apache2 -k start
             ??74450 /usr/sbin/apache2 -k start
             ??74451 /usr/sbin/apache2 -k start

Oct 18 10:49:42 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...

配置 ModSecurity

接下来,编辑 ModSecurity 配置文件:

nano /etc/apache2/mods-enabled/security2.conf

取消注释以下行:

IncludeOptional /etc/modsecurity/*.conf

保存并关闭文件,然后重命名 ModSecurity 配置文件:

mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

接下来,编辑 ModeSecurity 配置文件:

nano /etc/modsecurity/modsecurity.conf

更改以下行:

SecRuleEngine On
SecAuditLogParts ABCEFHJKZ

保存并关闭文件,然后重新启动 Apache 服务以应用更改:

systemctl restart apache2

安装 OWASP 核心规则集

为了保护您的 Web 应用程序免受恶意攻击,您需要下载 OWASP 核心规则集并将其安装到您的服务器。

首先,使用以下命令下载最新的规则集:

wget https://github.com/coreruleset/coreruleset/archive/v3.3.0.tar.gz

下载完成后,使用以下命令解压缩下载的文件:

tar xvf v3.3.0.tar.gz

接下来,为规则创建一个目录并将提取的规则移动到该目录中:

mkdir /etc/apache2/modsecurity-crs/
mv coreruleset-3.3.0/ /etc/apache2/modsecurity-crs

接下来,导航到您的 OWASP 规则目录并重命名配置文件:

cd /etc/apache2/modsecurity-crs/coreruleset-3.3.0/
mv crs-setup.conf.example crs-setup.conf

接下来,编辑 ModSecurity 配置文件并定义 OWASP 规则集的路径:

nano /etc/apache2/mods-enabled/security2.conf

添加以下行:

IncludeOptional /etc/apache2/modsecurity-crs/coreruleset-3.3.0/crs-setup.conf
IncludeOptional /etc/apache2/modsecurity-crs/coreruleset-3.3.0/rules/*.conf

保存并关闭文件,然后使用以下命令验证 Apache 是否存在任何语法错误:

apache2ctl -t

如果一切正常,您将获得以下输出:

Syntax OK

最后,重启 Apache 服务以应用配置:

systemctl restart apache2

验证 ModSecurity

此时,Apache Web 服务器配置了 ModSecurity。现在,是时候测试它是否有效了。

打开命令行界面并运行以下命令:

curl http://localhost/index.html?exec=/bin/bash

你应该得到 403 Forbidden 错误:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at localhost Port 80</address>
</body></html>

您还可以使用以下命令测试 ModSecurity:

curl localhost?doc=/bin/ls

您将获得以下输出:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at localhost Port 80</address>
</body></html>

您还可以查看 ModSecurity 日志以获取更多信息:

tail /var/log/apache2/modsec_audit.log

您将获得以下输出:

SecRule "TX:INBOUND_ANOMALY_SCORE" "@ge %{tx.inbound_anomaly_score_threshold}" "phase:5,id:980130,pass,t:none,log,noauditlog,msg:'Inbound Anomaly Score Exceeded (Total Inbound Score: %{TX.INBOUND_ANOMALY_SCORE} - SQLI=%{tx.sql_injection_score},XSS=%{tx.xss_score},RFI=%{tx.rfi_score},LFI=%{tx.lfi_score},RCE=%{tx.rce_score},PHPI=%{tx.php_injection_score},HTTP=%{tx.http_violation_score},SESS=%{tx.session_fixation_score}): individual paranoia level scores: %{TX.ANOMALY_SCORE_PL1}, %{TX.ANOMALY_SCORE_PL2}, %{TX.ANOMALY_SCORE_PL3}, %{TX.ANOMALY_SCORE_PL4}',tag:event-correlation,ver:OWASP_CRS/3.3.0"

SecAction "phase:5,id:980145,pass,t:none,nolog,noauditlog,ver:OWASP_CRS/3.3.0,setvar:tx.executing_anomaly_score=%{tx.outbound_anomaly_score_pl1},setvar:tx.executing_anomaly_score=+%{tx.outbound_anomaly_score_pl2},setvar:tx.executing_anomaly_score=+%{tx.outbound_anomaly_score_pl3},setvar:tx.executing_anomaly_score=+%{tx.outbound_anomaly_score_pl4}"

SecRule "TX:OUTBOUND_ANOMALY_SCORE" "@lt %{tx.outbound_anomaly_score_threshold}" "phase:5,id:980150,pass,t:none,log,noauditlog,msg:'Outbound Anomaly Score (Total Outbound Score: %{TX.OUTBOUND_ANOMALY_SCORE}): individual paranoia level scores: %{TX.OUTBOUND_ANOMALY_SCORE_PL1}, %{TX.OUTBOUND_ANOMALY_SCORE_PL2}, %{TX.OUTBOUND_ANOMALY_SCORE_PL3}, %{TX.OUTBOUND_ANOMALY_SCORE_PL4}',tag:event-correlation,ver:OWASP_CRS/3.3.0,chain"
#SecRule "TX:MONITOR_ANOMALY_SCORE" "@gt 1"


--c2d93508-Z--

配置Logrotate

如果您在高流量网站上使用 ModSecurity。 ModSecurity 审计日志会很快变得太大。在这种情况下,您可以为 ModSecurity 审计日志配置日志轮换。您可以使用以下命令对其进行配置:

nano /etc/logrotate.d/modsec

添加以下行:

/var/log/apache2/modsec_audit.log
{
        rotate 31
        daily
        missingok
        compress
        delaycompress
        notifempty
}

完成后保存并关闭文件。上面的配置文件会每天轮换日志文件并压缩。

结论

在本文中,我们解释了如何在 Ubuntu 22.04 上使用 Apache 设置 ModSecurity。我们还使用 ModSecurity 下载和配置 OWASP 规则集。您的网站没有完全免受不同类型的攻击。如果您有任何问题,请随时问我。

©2015-2025 艾丽卡 support@alaica.com