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

加载更多搜索结果...

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

如何在 Ubuntu 22.04 上使用 Nginx 安装 ModSecurity 3

本教程适用于这些操作系统版本

  • Ubuntu 22.04(Jammy Jellyfish)
  • Ubuntu 15.04(Vivid Vervet)

在此页

  1. 先决条件
  2. 开始
  3. 在 Ubuntu 22.04 上安装 ModSecurity
  4. 安装支持 ModSecurity 3 的 Nginx
  5. 使用 ModSecurity 配置 Nginx
  6. 安装 ModSecurity 核心规则集
  7. 为 Nginx 创建一个 Systemd 服务文件
  8. 验证 ModSecurity
  9. 结论

网站和 Web 应用程序安全性对于任何系统管理员来说都是一项具有挑战性的任务。有许多开源工具可用于保护您的网站免受 DDoS 攻击。 ModSecurity 是一种免费的开源 Web 应用程序防火墙 (WAF),可保护您的网站免受多种类型的攻击,包括跨站点脚本 (XSS)、SQL 注入、会话劫持等。

在本教程中,我将向您展示如何在 Ubuntu 22.04 上使用 Nginx 安装 ModSecurity。

先决条件

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

入门

首先,建议将所有软件包更新和升级到最新版本。您可以通过运行以下命令来更新所有这些:

apt update -y
apt upgrade -y

更新所有包后,使用以下命令安装其他所需的包:

apt install g++ flex bison curl apache2-dev doxygen libyajl-dev ssdeep liblua5.2-dev libgeoip-dev libtool dh-autoreconf libcurl4-gnutls-dev libxml2 libpcre++-dev libxml2-dev git liblmdb-dev libpkgconf3 lmdb-doc pkgconf zlib1g-dev libssl-dev -y

完成后,您可以继续下一步。

在 Ubuntu 22.04 上安装 ModSecurity

默认情况下,ModSecurity 包不包含在 Ubuntu 默认存储库中。所以你需要从源代码编译它。

首先,使用以下命令下载最新版本的 ModSecurity:

wget https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.8/modsecurity-v3.0.8.tar.gz

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

tar -xvzf modsecurity-v3.0.8.tar.gz

接下来,导航到提取的目录并使用以下命令对其进行配置:

cd modsecurity-v3.0.8
./build.sh
./configure

接下来,使用以下命令安装它:

make
make install

安装支持 ModSecurity 3 的 Nginx

接下来,您需要安装支持 ModSecurity 的 Nginx。首先,使用以下命令下载 ModSecurity-nginx 连接器:

cd ~
git clone https://github.com/SpiderLabs/ModSecurity-nginx.git

接下来,使用以下命令下载 Nginx 源代码:

wget https://nginx.org/download/nginx-1.20.2.tar.gz

接下来,使用以下命令提取 Nginx 源:

tar xzf nginx-1.20.2.tar.gz

接下来,使用以下命令为 Nginx 创建一个用户:

useradd -r -M -s /sbin/nologin -d /usr/local/nginx nginx

接下来,将目录更改为 Nginx 源并使用以下命令对其进行配置:

cd nginx-1.20.2
./configure --user=nginx --group=nginx --with-pcre-jit --with-debug --with-compat --with-http_ssl_module --with-http_realip_module --add-dynamic-module=/root/ModSecurity-nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

接下来,使用以下命令安装它:

make
make modules
make install

接下来,使用以下命令创建 Nginx 的符号链接:

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

接下来,使用以下命令验证 Nginx 版本:

nginx -V

您将在以下输出中获得 Nginx 版本:

nginx version: nginx/1.20.2
built by gcc 11.2.0 (Ubuntu 11.2.0-19ubuntu1) 
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-pcre-jit --with-debug --with-compat --with-http_ssl_module --with-http_realip_module --add-dynamic-module=/root/ModSecurity-nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

完成后,您可以继续使用 ModSecurity 配置 Nginx。

使用 ModSecurity 配置 Nginx

接下来,使用以下命令复制示例配置文件:

cp ~/modsecurity-v3.0.8/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
cp ~/modsecurity-v3.0.8/unicode.mapping /usr/local/nginx/conf/

接下来,备份 Nginx 配置文件:

cp /usr/local/nginx/conf/nginx.conf{,.bak}

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

nano /usr/local/nginx/conf/nginx.conf

删除默认行并添加以下行:

load_module modules/ngx_http_modsecurity_module.so;
user  nginx;
worker_processes  1;
pid        /run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  nginx.example.com;
        modsecurity  on;
        modsecurity_rules_file  /usr/local/nginx/conf/modsecurity.conf;
        access_log  /var/log/nginx/access_example.log;
        error_log  /var/log/nginx/error_example.log;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

保存并关闭文件,然后使用以下命令启用 ModSecurity:

sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /usr/local/nginx/conf/modsecurity.conf

完成后,您可以继续下一步。

安装 ModSecurity 核心规则集

OWASP ModSecurity Core Rule Set 提供了一组规则来检测和保护范围广泛的攻击,包括 OWASP Top Ten,最少的误报。

首先,使用以下命令下载 OWASP 规则集:

cd
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /usr/local/nginx/conf/owasp-crs

接下来,将 crs-setup.conf.example 重命名为 crs-setup.conf 文件:

cp /usr/local/nginx/conf/owasp-crs/crs-setup.conf{.example,}

接下来,使用以下命令定义规则:

echo -e "Include owasp-crs/crs-setup.conf
Include owasp-crs/rules/*.conf" >> /usr/local/nginx/conf/modsecurity.conf

接下来,使用以下命令验证 Nginx 是否存在任何配置错误:

nginx -t

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

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

完成后,您可以继续下一步。

为 Nginx 创建系统服务文件

接下来,您需要创建一个 systemd 服务文件来管理 Nginx 服务。这样你就可以通过系统启动和停止Nginx服务。您可以使用以下命令创建它:

nano /etc/systemd/system/nginx.service

添加以下行:

[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

保存并关闭文件,然后重新加载 systemd 守护进程以应用更改:

systemctl daemon-reload

接下来,使用以下命令启动并启用 Nginx:

systemctl start nginx
systemctl enable nginx

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

systemctl status nginx

您应该看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-11 15:40:39 UTC; 6s ago
       Docs: man:nginx(8)
    Process: 68438 ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 68439 ExecStart=/usr/local/nginx/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 68440 (nginx)
      Tasks: 2 (limit: 4579)
     Memory: 20.0M
        CPU: 293ms
     CGroup: /system.slice/nginx.service
             ??68440 "nginx: master process /usr/local/nginx/sbin/nginx -g daemon on; master_process on;"
             ??68441 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Oct 11 15:40:38 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 11 15:40:39 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

完成后,您可以继续下一步。

验证 ModSecurity

使用 Nginx 安装和配置 Modsecurity 之后。它的时间来测试它。运行以下命令来测试 Modsecurity 对命令注入的影响:

curl localhost?doc=/bin/ls

如果一切正常,您将获得“403 Forbidden”按摩。

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>

您还可以使用以下命令检查模式安全日志:

tail /var/log/modsec_audit.log

您应该在以下输出中看到 ModSecurity 日志:

ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:doc' (Value: `/bin/ls' ) [file "/usr/local/nginx/conf/owasp-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"] [line "496"] [id "932160"] [rev ""] [msg "Remote Command Execution: Unix Shell Code Found"] [data "Matched Data: bin/ls found within ARGS:doc: /bin/ls"] [severity "2"] [ver "OWASP_CRS/3.2.0"] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-shell"] [tag "platform-unix"] [tag "attack-rce"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"] [hostname "127.0.0.1"] [uri "/"] [unique_id "166550286018.572845"] [ref "o1,6v10,7t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase"]
ModSecurity: Access denied with code 403 (phase 2). Matched "Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `5' ) [file "/usr/local/nginx/conf/owasp-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "80"] [id "949110"] [rev ""] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [data ""] [severity "2"] [ver "OWASP_CRS/3.2.0"] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "127.0.0.1"] [uri "/"] [unique_id "166550286018.572845"] [ref ""]

---IcTYGSZl---I--

---IcTYGSZl---J--

---IcTYGSZl---Z--

结论

恭喜!您已经在 Ubuntu 22.04 上成功安装了带有 Nginx 的 ModSecurity。您现在可以在生产环境中实施 ModSecurity 来抵御 DDoS 攻击。如果您有任何问题,请随时问我。

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