如何在CentOS 7,Debian 8和Ubuntu 16.04上为Nginx安装ModSecurity

ModSecurity是一个开放源代码Web应用程序防火墙(WAF)模块,非常适合保护Apache,Nginx和IIS免受针对各种Web应用程序中潜在漏洞的各种网络攻击。

在本文中,我们将在CentOS 7,Debian 8和Ubuntu 16.04上为Nginx安装和配置ModSecurity。

先决条件

  • 一个向上最新安装的CentOS 7,8的Debian,或Ubuntu 16.04 64位的。
  • 以身份登录root

步骤1:更新系统

按照本指南,将服务器的内核和软件包更新为最新的可用版本。

步骤2:安装依赖项

在成功编译Nginx和ModSecurity之前,您需要按照以下步骤安装几个软件包。

a)在CentOS 7上:

yum groupinstall -y "Development Tools"
yum install -y httpd httpd-devel pcre pcre-devel libxml2 libxml2-devel curl curl-devel openssl openssl-devel
shutdown -r now

b)在Debian 8或Ubuntu 16.04上:

apt-get install -y git build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev automake pkgconf

步骤3:编译ModSecurity

由于ModSecurity for Nginx master分支上报告了一些不稳定性,因此,目前官方建议尽可能使用该nginx_refactoring分支的最新版本。

下载nginx_refactoringModSecurity for Nginx 的分支:

cd /usr/src
git clone -b nginx_refactoring https://github.com/SpiderLabs/ModSecurity.git

编译ModSecurity:

a)在CentOS 7上:

cd ModSecurity
sed -i '/AC_PROG_CC/a\AM_PROG_CC_C_O' configure.ac
sed -i '1 i\AUTOMAKE_OPTIONS = subdir-objects' Makefile.am
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make

注意:sed当使用较新的automake版本时,以上两个命令用于防止出现警告消息。

b)在Debian 8或Ubuntu 16.04上:

cd ModSecurity
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make

步骤4:编译Nginx

Nginx 1.10.3在撰写本文时,下载并取消存档Nginx的最新稳定版本:

cd /usr/src
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz && rm -f nginx-1.10.3.tar.gz

a)在CentOS 7上:

首先,您需要为Nginx 创建一个专用用户nginx和一个专用组nginx

groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx

然后在启用ModSecurity和SSL模块的同时编译Nginx:

cd nginx-1.10.3/
./configure --user=nginx --group=nginx --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install

修改Nginx的默认用户:

sed -i "s/#user  nobody;/user nginx nginx;/" /usr/local/nginx/conf/nginx.conf

b)在Debian 8或Ubuntu 16.04上:

首先,您应该使用现有用户www-data和现有组www-data

然后在启用ModSecurity和SSL模块的同时编译Nginx:

cd nginx-1.10.3/
./configure --user=www-data --group=www-data --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install

修改Nginx的默认用户:

sed -i "s/#user  nobody;/user www-data www-data;/" /usr/local/nginx/conf/nginx.conf

成功安装Nginx之后,相关文件将位于:

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"

您可以使用以下方法测试安装:

/usr/local/nginx/sbin/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单位文件:

cat <<EOF>> /lib/systemd/system/nginx.service
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillStop=/usr/local/nginx/sbin/nginx -s stop

KillMode=process
Restart=on-failure
RestartSec=42s

PrivateTmp=true
LimitNOFILE=200000

[Install]
WantedBy=multi-user.target
EOF

继续前进,您可以按照以下步骤启动/停止/重启Nginx:

systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service

步骤4:配置ModSecurity和Nginx

4.1配置Nginx

vi /usr/local/nginx/conf/nginx.conf

在细分中找到以下http {}细分:

location / {
    root   html;
    index  index.html index.htm;
}

将以下各行插入到该location / {}段中:

ModSecurityEnabled on;
ModSecurityConfig modsec_includes.conf;
#proxy_pass http://localhost:8011;
#proxy_read_timeout 180s;

最终结果应为:

location / {
    ModSecurityEnabled on;
    ModSecurityConfig modsec_includes.conf;
    #proxy_pass http://localhost:8011;
    #proxy_read_timeout 180s;
    root   html;
    index  index.html index.htm;
}

保存并退出:

:wq!

注意:上面的Nginx配置只是用于将Nginx用作Web服务器而不是反向代理的示例配置。如果您将Nginx用作反向代理,请删除#最后两行中的字符并对其进行适当的修改。

4.2创建一个名为/usr/local/nginx/conf/modsec_includes.conf

cat <<EOF>> /usr/local/nginx/conf/modsec_includes.conf
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/*.conf
EOF

注意:上面的配置将应用目录中的所有OWASP ModSecurity核心规则owasp-modsecurity-crs/rules/。如果只想应用选择性规则,则应删除include owasp-modsecurity-crs/rules/*.conf行,然后在步骤4.5之后指定所需的确切规则。

4.3导入ModSecurity配置文件:

cp /usr/src/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
cp /usr/src/ModSecurity/unicode.mapping /usr/local/nginx/conf/

4.4修改/usr/local/nginx/conf/modsecurity.conf文件:

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

4.5添加OWASP ModSecurity CRS(核心规则集)文件:

cd /usr/local/nginx/conf
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
cd owasp-modsecurity-crs
mv crs-setup.conf.example crs-setup.conf
cd rules
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

步骤5:测试ModSecurity

启动Nginx:

systemctl start nginx.service

打开端口80以允许外部访问:

a)在CentOS 7上:

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --reload

b)在Debian 8上:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
touch /etc/iptables
iptables-save > /etc/iptables

c)在Ubuntu 16.04上:

ufw allow OpenSSH
ufw allow 80
ufw default deny
ufw enable    

将您的网络浏览器指向:

http://203.0.113.1/?param="><script>alert(1);</script>

使用grep如下获取错误信息:

grep error /usr/local/nginx/logs/error.log

输出应包含多个错误消息,它们类似于:

2017/02/15 14:07:54 [error] 10776#0: [client 104.20.23.240] ModSecurity: Warning. detected XSS using libinjection. [file "/usr/local/nginx/conf/owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf"] [line "56"] [id "941100"] [rev "2"] [msg "XSS Attack Detected via libinjection"] [data "Matched Data:  found within ARGS:param: \x22><script>alert(1);</script>"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "9"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-xss"] [tag "OWASP_CRS/WEB_ATTACK/XSS"] [tag "WASCTC/WASC-8"] [tag "WASCTC/WASC-22"] [tag "OWASP_TOP_10/A3"] [tag "OWASP_AppSensor/IE1"] [tag "CAPEC-242"] [hostname ""] [uri "/index.html"] [unique_id "ATAcAcAkucAchGAcPLAcAcAY"]

而已。如您所见,ModSecurity模块已根据其默认操作策略成功记录了此攻击。如果您要进行更多的自定义设置,请仔细检查并编辑/usr/local/nginx/conf/modsecurity.conf/usr/local/nginx/conf/owasp-modsecurity-crs/crs-setup.conf归档。



Leave a Comment

在Arch Linux上使用Makepkg

在Arch Linux上使用Makepkg

在Arch Linux上使用Makepkg可以避免系统污染,确保仅安装必要的依赖关系。

如何在Ubuntu 16.04上安装OpenSIPS控制面板

如何在Ubuntu 16.04上安装OpenSIPS控制面板

快速学习如何在Ubuntu 16.04上安装OpenSIPS控制面板,为VoIP提供商提供支持的功能。

在Fedora 28上安装Akaunting

在Fedora 28上安装Akaunting

学习如何在Fedora 28上安装Akaunting,一款适合小型企业和自由职业者的开源会计软件。

如何在CentOS 7上安装Mailtrain新闻通讯应用程序

如何在CentOS 7上安装Mailtrain新闻通讯应用程序

使用其他系统?Mailtrain是一个基于Node.js和MySQL / MariaDB构建的开源自托管新闻通讯应用程序。

诊断Minecraft服务器延迟和低TPS

诊断Minecraft服务器延迟和低TPS

了解導致Minecraft延遲的原因和解決方案,包括優化伺服器性能和減少滯後的步驟。

AI 能否應對越來越多的勒索軟件攻擊?

AI 能否應對越來越多的勒索軟件攻擊?

勒索軟件攻擊呈上升趨勢,但人工智能能否幫助應對最新的計算機病毒?AI 是答案嗎?在這裡閱讀知道是 AI 布恩還是禍根

ReactOS:這是 Windows 的未來嗎?

ReactOS:這是 Windows 的未來嗎?

ReactOS,一個開源和免費的操作系統,這裡有最新版本。它能否滿足現代 Windows 用戶的需求並打倒微軟?讓我們更多地了解這種老式但更新的操作系統體驗。

通過 WhatsApp 桌面應用程序 24*7 保持聯繫

通過 WhatsApp 桌面應用程序 24*7 保持聯繫

Whatsapp 終於為 Mac 和 Windows 用戶推出了桌面應用程序。現在您可以輕鬆地從 Windows 或 Mac 訪問 Whatsapp。適用於 Windows 8+ 和 Mac OS 10.9+

人工智能如何將流程自動化提升到新的水平?

人工智能如何將流程自動化提升到新的水平?

閱讀本文以了解人工智能如何在小型公司中變得流行,以及它如何增加使它們成長並為競爭對手提供優勢的可能性。

macOS Catalina 10.15.4 補充更新引發的問題多於解決

macOS Catalina 10.15.4 補充更新引發的問題多於解決

最近,Apple 發布了 macOS Catalina 10.15.4 補充更新以修復問題,但似乎該更新引起了更多問題,導致 mac 機器變磚。閱讀這篇文章以了解更多信息