本教程适用于以下操作系统版本
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 16.04 (Xenial Xerus)
- Ubuntu 15.04 (Vivid Vervet)
本页内容
- 先决条件
- 开始之前
- 在 Ubuntu 22.04 上安装 ModSecurity
- 安装支持 ModSecurity 3 的 Nginx
- 将 Nginx 与 ModSecurity 进行配置
- 安装 ModSecurity Core Rule Set
- 为 Nginx 创建 Systemd 服务文件
- 验证 ModSecurity
- 总结
网站和 Web 应用安全对任何系统管理员来说都是一项具有挑战性的任务。有许多开源工具可用于保护您的网站免受 DDoS 攻击。ModSecurity 是一款免费的开源 Web 应用防火墙(WAF),可保护您的网站免受多种类型的攻击,包括跨站脚本(XSS)、SQL 注入、会话劫持以及更多攻击。
在本教程中,我将向您展示如何在 Ubuntu 22.04 上将 ModSecurity 与 Nginx 一起安装。
先决条件
- 一台运行 Ubuntu 22.04 的服务器。
- 服务器上已配置 root 密码。
开始之前
首先,建议将您的所有软件包更新并升级到最新版本。您可以通过运行以下命令来更新它们:
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
默认情况下,Ubuntu 默认仓库中不包含 ModSecurity 软件包。因此,您需要从源代码编译它。
首先,使用以下命令下载最新版本的 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
完成后,您可以继续将 Nginx 与 ModSecurity 进行配置。
将 Nginx 与 ModSecurity 进行配置
接下来,使用以下命令复制示例配置文件:
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 Core Rule Set
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 服务文件
接下来,您需要创建一个 systemd 服务文件来管理 Nginx 服务,以便您可以通过 systemd 启动和停止 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>
您也可以使用以下命令查看 ModSecurity 日志:
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 上将 ModSecurity 与 Nginx 一起安装。您现在可以在生产环境中部署 ModSecurity 以防御 DDoS 攻击。如果您有任何疑问,请随时向我提问。