在本页

  • 要求
  • 入门
  • 安装 Docker
  • 为 ModSecurity 创建 Dockerfile
  • 构建 Apache ModSecurity 镜像并启动容器
  • 验证 ModSecurity
  • 结论

ModSecurity 是一个免费、开源且最受欢迎的 Web 应用防火墙(WAF),可保护您的 Web 应用免受各种第 7 层攻击。它专为 Apache Web 服务器的监控、日志记录和请求过滤而设计。它带有一个核心规则集(Core Rule Set),可检测并阻止多种攻击,包括 SQL 注入、跨站脚本、特洛伊木马、恶意用户代理、会话劫持等。

在本教程中,我将向您展示如何在 Docker 容器内使用 Apache 安装 ModSecurity 3。

要求

  • 一台运行 Ubuntu 20.04 的服务器。
  • 您的服务器上已配置 root 密码。

入门

首先,建议将您的系统更新到最新版本。您可以使用以下命令进行更新:

apt-get update -y

系统更新到最新后,您需要在系统中安装一些依赖项。您可以使用以下命令安装所有这些依赖项:

apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y

所有依赖项安装完成后,您可以继续下一步。

安装 Docker

接下来,您需要在系统中安装 Docker CE。默认情况下,Ubuntu 默认仓库中不包含最新版本的 Docker。因此您需要将 Docker 官方仓库添加到 APT 中。

首先,使用以下命令下载并添加 Docker GPG 密钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

接下来,使用以下命令将 Docker CE 仓库添加到 APT 源列表中:

echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker-ce.list

仓库添加完成后,使用以下命令更新仓库:

apt-get update -y

仓库更新完成后,使用以下命令安装最新版本的 Docker CE:

apt-get install docker-ce -y

安装 Docker CE 后,使用以下命令验证已安装的 Docker CE 版本:

docker --version

您应该得到以下输出:

Docker version 20.10.6, build 370c289

您还可以使用以下命令检查 Docker 服务的状态:

systemctl status docker

您应该得到以下输出:

? docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-05-16 06:49:29 UTC; 38s ago
TriggeredBy: ? docker.socket
       Docs: https://docs.docker.com
   Main PID: 8964 (dockerd)
      Tasks: 8
     Memory: 40.6M
     CGroup: /system.slice/docker.service
             ??8964 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.365433228Z" level=warning msg="Your kernel does not support swap memory li>
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.365916961Z" level=warning msg="Your kernel does not support cgroup blkio w>
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.366112111Z" level=warning msg="Your kernel does not support cgroup blkio w>
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.366653374Z" level=info msg="Loading containers: start."
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.498790388Z" level=info msg="Default bridge (docker0) is assigned with an I>
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.576691602Z" level=info msg="Loading containers: done."
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.610542206Z" level=info msg="Docker daemon" commit=8728dd2 graphdriver(s)=o>
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.611668583Z" level=info msg="Daemon has completed initialization"
May 16 06:49:29 ubuntu2004 systemd[1]: Started Docker Application Container Engine.
May 16 06:49:29 ubuntu2004 dockerd[8964]: time="2021-05-16T06:49:29.690496888Z" level=info msg="API listen on /run/docker.sock"
lines 1-21/21 (END)

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

为 ModSecurity 创建 Dockerfile

接下来,您需要创建一个 Dockerfile,以便在 Ubuntu 容器内安装 ModSecurity。

首先,将目录切换到 /opt,并使用以下命令创建一个 modsec_rules.conf 文件:

cd /opt
nano modsec_rules.conf

添加以下几行:

Include "/etc/apache2/modsecurity.d/modsecurity.conf"
Include "/etc/apache2/modsecurity.d/owasp-crs/crs-setup.conf"
Include "/etc/apache2/modsecurity.d/owasp-crs/rules/*.conf"

保存并关闭该文件,然后使用以下命令创建另一个文件:

nano 000-default.conf

添加以下几行:

<VirtualHost *:80>
	modsecurity on
	modsecurity_rules_file /etc/apache2/modsecurity.d/modsec_rules.conf 
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

完成后保存并关闭文件。Dockerfile 将在构建过程中将上述文件复制到 Docker 容器中。

最后,使用以下命令创建一个 Dockerfile:

nano Dockerfile

添加以下几行:

# Install Modsecurity in a Docker container;
FROM ubuntu:latest
ARG DEBIAN_FRONTEND=noninteractive
# update/upgrade your system
RUN apt-get update -y

# Install Required Dependencies
RUN apt-get install -y 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 wget tar apache2

# Download LibModsecurity 
RUN wget https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.4/modsecurity-v3.0.4.tar.gz

# Extract the Downloaded File
RUN tar xzf modsecurity-v3.0.4.tar.gz && rm -rf modsecurity-v3.0.4.tar.gz

# Compile and Install LibModsecurity
RUN cd modsecurity-v3.0.4 && \
	./build.sh && ./configure && \
	make && make install

# Install ModSecurity-Apache Connector
RUN cd ~ && git clone https://github.com/SpiderLabs/ModSecurity-apache

RUN cd ~/ModSecurity-apache && \
	./autogen.sh && \
	./configure --with-libmodsecurity=/usr/local/modsecurity/ && \
	make && \
	make install

# Load the Apache ModSecurity Connector Module
RUN echo "LoadModule security3_module /usr/lib/apache2/modules/mod_security3.so" >> /etc/apache2/apache2.conf

# Configure ModSecurity
RUN mkdir /etc/apache2/modsecurity.d && \
	cp modsecurity-v3.0.4/modsecurity.conf-recommended /etc/apache2/modsecurity.d/modsecurity.conf && \
	cp modsecurity-v3.0.4/unicode.mapping /etc/apache2/modsecurity.d/ && \
	sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/apache2/modsecurity.d/modsecurity.conf
ADD modsec_rules.conf /etc/apache2/modsecurity.d/

# Install OWASP ModSecurity Core Rule Set (CRS) on Ubuntu
RUN git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /etc/apache2/modsecurity.d/owasp-crs && \
	cp /etc/apache2/modsecurity.d/owasp-crs/crs-setup.conf.example /etc/apache2/modsecurity.d/owasp-crs/crs-setup.conf

# Activate ModSecurity
RUN mv /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.old
ADD 000-default.conf /etc/apache2/sites-available/

EXPOSE 80
CMD apachectl -D FOREGROUND

完成后保存并关闭文件。

上述文件将下载 Ubuntu 镜像,安装所有依赖项,下载 ModSecurity,对其进行编译,并配置 Apache 使其与 ModSecurity 一起工作。

此时,Dockerfile 已准备就绪。您现在可以继续下一步。

构建 Apache ModSecurity 镜像并启动容器

现在,将目录切换到 /opt,并使用以下命令构建 Apache ModSecurity 的 Docker 镜像:

cd /opt
docker build .

构建过程完成后,您应该得到以下输出:

Step 13/17 : RUN git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /etc/apache2/modsecurity.d/owasp-crs && 	cp /etc/apache2/modsecurity.d/owasp-crs/crs-setup.conf.example /etc/apache2/modsecurity.d/owasp-crs/crs-setup.conf
 ---> Running in 00dfa2a5cd23
Cloning into '/etc/apache2/modsecurity.d/owasp-crs'...
Removing intermediate container 00dfa2a5cd23
 ---> b38c1d874d2f
Step 14/17 : RUN mv /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.old
 ---> Running in 12c9e6d2c559
Removing intermediate container 12c9e6d2c559
 ---> 899e26019297
Step 15/17 : ADD 000-default.conf /etc/apache2/sites-available/
 ---> eb11751afd6c
Step 16/17 : EXPOSE 80
 ---> Running in 2f4ba47e2b66
Removing intermediate container 2f4ba47e2b66
 ---> dd59b0ac7c7c
Step 17/17 : CMD apachectl -D FOREGROUND
 ---> Running in 98b8cc77df0f
Removing intermediate container 98b8cc77df0f
 ---> f603dbc38018
Successfully built f603dbc38018

您现在可以使用以下命令列出所有 docker 镜像:

docker images

您应该得到以下输出:

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE

       
    f603dbc38018   32 seconds ago   2.48GB
ubuntu       latest    7e0aa2d69a15   3 weeks ago      72.7MB

现在,从上述输出中选择第一个镜像 id,并使用以下命令启动 Apache ModSecurity 容器:

docker run --name modsec-apache -ditp 80:80 f603dbc38018

您应该得到以下输出:

40eb0e77e61635c3cee2bfaffbd9489bc7d20aa3e1befb52749de079aaadb528

您现在可以使用以下命令验证正在运行的容器:

docker ps

您应该得到以下输出:

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
40eb0e77e616   f603dbc38018   "/bin/sh -c 'apachec…"   17 seconds ago   Up 15 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   modsec-apache

如您所见,ModSecurity 容器已启动并正在监听端口 80。

验证 ModSecurity

此时,ModSecurity 容器正在运行。现在,是时候测试 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.41 (Ubuntu) Server at localhost Port 80</address>
</body></html>

您应该看到 " 403 Forbidden " 错误。因为 ModSecurity 已阻止了上述请求。

您还可以查看 Apache 日志以获取更多信息。

为此,首先使用以下命令连接到容器:

docker exec -it modsec-apache /bin/bash

连接后,您应该得到以下 shell:

root@40eb0e77e616:/#

现在,使用以下命令检查 Apache 日志:

tail -f /var/log/apache2/error.log

您应该看到 ModSecurity 已阻止了该恶意请求:

[Sun May 16 07:24:54.456327 2021] [mpm_event:notice] [pid 15:tid 140204464299072] AH00489: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
[Sun May 16 07:24:54.456352 2021] [core:notice] [pid 15:tid 140204464299072] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
[Sun May 16 07:25:36.680515 2021] [:error] [pid 16:tid 140204216108800] [client 172.17.0.1:45298] ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:doc' (Value: `/bin/ls' ) [file "/etc/apache2/modsecurity.d/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 "172.17.0.2"] [uri "/"] [unique_id "162114993662.860969"] [ref "o1,6v10,7t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase"]

结论

祝贺您!您已成功在 Docker 容器内安装 ModSecurity。我希望您现在已具备足够的知识在 Docker 环境中实施该解决方案。如果您有任何问题,请随时向我提问。