🌐如何在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!