🌐如何在Ubuntu服务器上使用ModSecurity构建NGINX
ModSecurity是用于Web服务器的广泛使用且受人尊敬的开源Web应用程序防火墙(waf)。 它可以与Apache和NGINX一起使用,以针对WordPress和Nextcloud等Web应用程序提供针对各种HTTP攻击(例如SQL注入和XSS跨站点脚本)的保护。 换句话说,这个模块应该被认为是必须的! ModSecurity不能包含在与apt-get一起安装的NGINX中,因此您必须手动构建它。 接下来,我将引导您完成向NGINX Web服务器添加此安全功能的过程。
如何安装所需的依赖项
首先要做的是安装所需的依赖项。 这可以通过一个命令来完成:
sudo apt-get install -y git build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev automake pkgconf zlib1g-dev -y
如果已经安装了NGINX(从标准存储库中),请使用以下命令将其删除:
sudo apt-get purge nginx -y
使用以下命令删除所有剩余的依赖项:
sudo apt-get autoremove -y
接下来,我们可以继续进行ModSecurity。
如何建立ModSecurity
我们必须手动编译ModSecurity。 首先,使用以下命令切换到src目录:
cd /usr/src
然后使用以下命令克隆最新版本的ModSecurity:
git clone -b nginx_refactoring https://github.com/SpiderLabs/ModSecurity.git
使用以下命令转到新创建的目录:
cd ModSecurity
使用自动生成脚本配置ModSecurity,如下所示:
./autogen.sh./configure --enable-standalone-module --disable-mlogc
使用以下命令安装ModSecurity:
make sudo make install
如何构建NGINX
不幸的是,我们不能使用标准存储库中的NGINX安装,因为它必须在waf支持下编译。 使用以下命令返回到src目录:
cd /usr/src
下载最新版本的NGINX; 当前是1.18.0
http://nginx.org/en/download.html
但是请确保选择最新版本并相应地更改命令! 下载源代码的命令:
wget http://nginx.org/download/nginx-1.18.0.tar.gz
使用以下命令解压缩压缩文件:
tar xvzf nginx-1.18.0.tar.gz
使用以下命令转到新创建的目录:
cd nginx-1.18.0
使用以下命令配置具有ModSecurity支持的NGINX:
./configure --user=www-data --group=www-data --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
最后,使用以下命令安装NGINX:
make sudo make install
如何设置NGINX
现在我们需要修改默认的NGINX配置文件,以便它使用以下命令知道要在哪个用户下运行:
sed -i "s/#user nobody;/user www-data www-data;/" /usr/local/nginx/conf/nginx.conf
接下来,我们需要配置NGINX,以便它知道使用ModSecurity。 使用以下命令打开NGINX配置文件:
sudo nano /usr/local/nginx/conf/nginx.conf
在此文件中,替换以下部分:
location / { root html; index index.html index.htm; }
在
location / { ModSecurityEnabled on; ModSecurityConfig modsec_includes.conf; root html; index index.html index.htm; }
通过使用以下命令创建规则文件来启用OWASP核心规则:
sudo nano /usr/local/nginx/conf/modsec_includes.conf
将以下内容粘贴到该文件中:
include modsecurity.conf include owasp-modsecurity-crs/crs-setup.conf include owasp-modsecurity-crs/rules/*.conf
保存并关闭文件。
使用以下两个命令导入所需的ModSecurity配置文件:
sudo cp /usr/src/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf sudo cp /usr/src/ModSecurity/unicode.mapping /usr/local/nginx/conf/
通过输入以下命令在modsecurity.conf文件中启用SecRuleEngine选项:
sudo sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /usr/local/nginx/conf/modsecurity.conf
现在,我们可以通过运行以下七个命令来添加基本的OWASP ModSecurity规则集:
cd /usr/local/nginx/conf sudo git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git sudo cd owasp-modsecurity-crs sudo mv crs-setup.conf.example crs-setup.conf sudo cd rules sudo mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf sudo mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
如何为NGINX创建systemd启动文件
为了管理NGINX,我们需要创建一个systemd启动文件。 使用以下命令创建文件:
sudo nano /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
保存并关闭文件。
使用以下命令启动NGINX:
sudo systemctl start nginx
使用以下命令启用Web服务器以在启动时启动:
sudo systemctl enable nginx
如何测试ModSecurity
我们终于可以测试我们的ModSecurity设置了。 为此,我们将使用tail使用以下命令跟踪NGINX错误日志:
sudo tail -f /usr/local/nginx/logs/error.log
然后打开您的Web浏览器并输入:http:// SERVER /?Param =”>其中SERVER是NGINX服务器的IP地址或域。 回到tail命令,您应该看到几个“权限被拒绝的错误”! 恭喜,您现在已经拥有ModSecurity在Ubuntu上运行最新的NGINX!