在 Rocky Linux 8 上使用 Nginx 安装 Modsecurity

[ad_1]

在本指南中,我们将学习如何在 Rocky Linux 8 上使用 Nginx 安装 Modsecurity。LibMosecurity 是 ModSecurity 的最新版本。 因此,它被称为 ModSecurity 版本 3。

在 Rocky Linux 8 上使用 Nginx 安装 Modsecurity

运行系统更新

首先更新您的系统包。

dnf update

安装所需的构建工具和依赖项

Nginx 和 LibModsecurity 都将从源代码编译,因此需要许多构建工具和依赖项。 运行下面的命令来安装它们。

dnf install gcc-c++ flex bison yajl curl-devel curl zlib-devel pcre-devel autoconf automake git curl make libxml2-devel pkgconfig libtool httpd-devel redhat-rpm-config git wget openssl openssl-devel vim
dnf --enablerepo=powertools install doxygen yajl-devel -y

安装 GeoIP 库

dnf install epel-release https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
dnf --enablerepo=remi install GeoIP-devel -y

下载 Modsecurity 源代码

创建一个临时目录来存储源 tarball。

mkdir /tmp/modsec

运行以下命令以克隆最新的 LibModsecurity GitHub 存储库。

cd /tmp/modsec
git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity

在 Rocky Linux 8 上使用 Nginx 编译和安装 Modsecurity

导航到 Modsecurity 源目录,配置、编译和安装它

cd ModSecurity

下载 libInjection 代码,它以 git-submodule 格式作为 ModSecurity 源代码的一部分提供

git submodule init
git submodule update

配置 Modsecurity 以使其适应您的系统并检查是否缺少任何必需的依赖项。

./build.sh
./configure

致命:未找到名称,无法描述任何内容 可以安全地忽略。

修复任何依赖问题,以防万一,在您可以继续之前。

在 Rocky Linux 8 上使用 Nginx 编译和安装 ModSecurity。

make
make install

使用 LibModsecurity 支持安装 Nginx

要使用 LibModsecurity 设置 Nginx,您需要编译支持 LibModsecurity 的 Nginx。

因此,下载 ModSecurity-nginx 连接器,它通过克隆其 git 存储库来提供 Nginx 和 LibModsecurity 之间的通信通道。

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

接下来,从 Nginx 下载中下载 Nginx 的最新主要版本。 撰写本文时的最新版本是 nginx-1.19.10.tar.gz 版本。

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

提取存档。

tar xzf nginx-1.19.10.tar.gz

创建一个非特权 Nginx 系统用户和组。

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

导航到 Nginx 源目录并配置它。

cd nginx-1.19.10
./configure --user=nginx --group=nginx --with-pcre-jit --with-debug --with-http_ssl_module --with-http_realip_module --add-module=/tmp/modsec/ModSecurity-nginx

配置摘要

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

在 Rocky Linux 8 上编译并安装 Nginx。

make
make install

在 Rocky Linux 8 上使用 ModSecurity 配置 Nginx

首先,将源目录中的示例 ModSecurity 配置文件复制到 Nginx 配置目录。

cp /tmp/modsec/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf

复制 unicode.mapping 文件从 ModSecurity 源目录到 Nginx 配置目录。

cp /tmp/modsec/ModSecurity/unicode.mapping /usr/local/nginx/conf/

接下来,编辑 Nginx 配置文件并进行如下更改。

创建 Nginx 配置文件的备份。

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

打开配置文件进行编辑。

vim /usr/local/nginx/conf/nginx.conf

配置 Nginx,使您的配置看起来像;

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.kifarunix-demo.com;
        modsecurity  on;
        modsecurity_rules_file  /usr/local/nginx/conf/modsecurity.conf;
        access_log  /var/log/nginx/access_kifarunix-demo.log;
        error_log  /var/log/nginx/error_kifarunix-demo.log;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

请注意该行;

modsecurity  on;
modsecurity_rules_file  /usr/local/nginx/conf/modsecurity.conf;

打开 Modsecurity 并指定 Modsecurity 规则的位置。

请注意,可以在每个目录的基础上打开 ModSecurity 3。

创建 Nginx 日志目录。

mkdir /var/log/nginx

创建 Nginx Systemd 服务

为了能够将 Nginx 作为服务运行,请创建如下所示的 systemd 服务;

cat > /etc/systemd/system/nginx.service << 'EOL'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOL

创建 Nginx 二进制文件的符号链接到 /usr/sbin/ 小路。

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

重新加载 systemd 配置。

systemctl daemon-reload

运行 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 以在系统启动时运行。

systemctl enable --now nginx

检查状态;

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-08-19 20:46:33 EAT; 11s ago
  Process: 31623 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 31621 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 31620 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 31625 (nginx)
    Tasks: 2 (limit: 4938)
   Memory: 3.2M
   CGroup: /system.slice/nginx.service
           ├─31625 nginx: master process /usr/sbin/nginx
           └─31626 nginx: worker process

Aug 19 20:46:33 rocky8 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 19 20:46:33 rocky8 nginx[31621]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Aug 19 20:46:33 rocky8 nginx[31621]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Aug 19 20:46:33 rocky8 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Aug 19 20:46:33 rocky8 systemd[1]: Started The nginx HTTP and reverse proxy server.

打开 ModSecurity 规则引擎

默认情况下,ModSecurity 设置为仅检测模式,它仅根据触发的规则记录请求,而不会阻止任何内容。 这可以通过设置值来改变 SecRuleEngineOn.

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

您还可以更改 Modsecurity 的默认日志目录

sed -i 's#/var/log/modsec_audit.log#/var/log/nginx/modsec_audit.log#' /usr/local/nginx/conf/modsecurity.conf

安装 OWASP ModSecurity 核心规则集 (CRS)

OWASP ModSecurity 核心规则集 (CRS) 是一组与 ModSecurity 一起使用的通用攻击检测规则。 它旨在保护 Web 应用程序免受广泛的攻击,包括 OWASP 前十名,尽量减少误报。

将 CRS 从 GitHub 存储库克隆到 /usr/local/nginx/conf/ 如下所示;

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

接下来重命名 crs-setup.conf.examplecrs-setup.conf.

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

一旦 OWASP 规则就位,配置 ModSecurity 以使用这些规则。 因此,您需要在 ModSecurity 配置文件中输入以下几行以告诉它在哪里可以找到规则。

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

此命令会将以下行附加到 /usr/local/nginx/conf/modsecurity.conf.

Include owasp-crs/crs-setup.conf
Include owasp-crs/rules/*.conf

再次验证 Nginx 配置文件。

nginx -t

接下来,如果一切正常,重新启动 Nginx。

systemctl restart nginx

在 Nginx 上测试 ModSecurity

接下来,您可以使用 OWASP 规则测试 Modsecurity 的有效性,例如,使用命令注入。 运行下面的命令;

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

你应该得到 403 Forbidden 如果规则是文件。

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

检查日志文件。

tail -100 /var/log/nginx/modsec_audit.log
---iQWU4H4y---A--
[19/Aug/2021:20:48:26 +0300] 1629395306 127.0.0.1 37240 127.0.0.1 80
---iQWU4H4y---B--
GET /index.html?exec=/bin/bash HTTP/1.1
Host: localhost
User-Agent: curl/7.61.1
Accept: */*

---iQWU4H4y---D--

---iQWU4H4y---E--
<html>x0dx0a<head><title>403 Forbidden</title></head>x0dx0a<body>x0dx0a<center><h1>403 Forbidden</h1></center>x0dx0a<hr><center>nginx/1.19.10</center>x0dx0a</body>x0dx0a</html>x0dx0a

---iQWU4H4y---F--
HTTP/1.1 403
Server: nginx/1.19.10
Date: Thu, 19 Aug 2021 17:48:26 GMT
Content-Length: 154
Content-Type: text/html
Connection: keep-alive

---iQWU4H4y---H--
ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:exec' (Value: `/bin/bash' ) [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/bash found within ARGS:exec: /bin/bash"] [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 "/index.html"] [unique_id "1629395306"] [ref "o1,8v21,9t: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 "/index.html"] [unique_id "1629395306"] [ref ""]

---iQWU4H4y---I--

---iQWU4H4y---J--

---iQWU4H4y---Z--

同样,日志会写入 Nginx 错误日志文件。

您还可以创建自己的测试规则。 例如,将下面的行附加到您的 /usr/local/nginx/conf/modsecurity.conf.

SecRule ARGS "@streq test" "id:1,phase:1,deny,msg:'Mytest Rule'"

Save 配置文件。

您还可以创建自定义规则文件并确保它包含在主 ModSecurity 配置文件中。

重启 Nginx。

systemctl restart nginx

测试规则。

curl localhost/index.html?a=test
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.10</center>
</body>
</html>

检查日志文件;

tail /var/log/nginx/error_kifarunix-demo.log
2021/08/19 20:48:26 [error] 31687#0: *1 [client 127.0.0.1] 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 "/index.html"] [unique_id "1629395306"] [ref ""], client: 127.0.0.1, server: nginx.kifarunix-demo.com, request: "GET /index.html?exec=/bin/bash HTTP/1.1", host: "localhost"
2021/08/19 20:54:03 [error] 31765#0: *1 [client 127.0.0.1] ModSecurity: Access denied with code 403 (phase 1). Matched "Operator `StrEq' with parameter `test' against variable `ARGS:a' (Value: `test' ) [file "/usr/local/nginx/conf/modsecurity.conf"] [line "261"] [id "1"] [rev ""] [msg "'Mytest Rule'"] [data ""] [severity "0"] [ver ""] [maturity "0"] [accuracy "0"] [hostname "127.0.0.1"] [uri "/index.html"] [unique_id "1629395643"] [ref "v18,4"], client: 127.0.0.1, server: nginx.kifarunix-demo.com, request: "GET /index.html?a=test HTTP/1.1", host: "localhost"

ModSecurity 现在正在运行并保护您的 Web 服务器。

这标志着我们关于如何在 Rocky Linux 8 上使用 Nginx 安装 Modsecurity 的教程结束。

您可以通过以下链接查看我们关于 ModSecurity 的其他指南;

在 ELK Stack 上处理和可视化 ModSecurity 日志

为 ModSecurity 日志创建 Kibana 可视化仪表板

使用 libModSecurity 将 WordPress 登录页面的访问权限限制为特定 IP

[ad_2]

Related Posts