在CentOS 8上使用Apache配置LibModsecurity
在本指南中,我們將學習如何在CentOS 8上使用Apache配置LibModsecurity。 LibMosecurity 也稱為ModSecurity版本3,是一個開源的跨平台Web應用程序防火牆(WAF)引擎,可提供針對各種Web應用程序攻擊的保護。
在CentOS 8上使用Apache配置LibModsecurity
運行系統更新
首先更新系統軟件包。
dnf update
安裝所需的構建工具和依賴項
Nginx和LibModsecurity都將從源代碼進行編譯,因此需要許多構建工具和依賴項。運行以下命令以安裝它們。
dnf config-manager --set-enabled PowerTools
安裝其他存儲庫。
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf config-manager --set-enabled remi
安裝所需的依賴項。
dnf install gcc-c++ flex bison yajl curl-devel curl zlib-devel pcre-devel autoconf automake git curl make libxml2-devel pkgconfig libtool httpd-devel redhat-rpm-config git wget openssl openssl-devel vim GeoIP-devel doxygen yajl-devel libmaxminddb libmaxminddb-devel GeoIP-devel lmdb lmdb-devel ssdeep-devel lua-devel
下載LibModsecurity源代碼
創建一個臨時目錄來存儲源tarball。
mkdir ~/modsec
您可以選擇使用 /opt
代替。
導航 ModSecurity發布頁面 並下載ModSecurity源代碼。您可以簡單地使用wget將其拉出。
cd ~/modsec
wget -P ~/modsec https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.4/modsecurity-v3.0.4.tar.gz
提取ModSecurity源代碼。
cd ~/modsec
tar xzf modsecurity-v3.0.4.tar.gz
編譯並安裝LibModsecurity
導航到LibModsecurity源目錄,配置,編譯並安裝它
cd modsecurity-v3.0.4
配置LibModsecurity使其適應您的系統,並檢查是否缺少任何必需的依賴項。
./build.sh
您可以放心地忽略 致命的:找不到名字,無法描述任何東西 消息。
./configure
修復任何依賴關係問題,以防萬一,您可以繼續在CentOS上使用Apache編譯和安裝LibModsecurity時
編譯並安裝LibModSecurity。
make
make install
在CentOS 8上安裝ModSecurity-Apache Connector
一旦完成LibModsecurity的安裝,請繼續安裝ModSecurity-apache連接器,該連接器提供Apache與libModsecurity之間的通信通道。
克隆ModSecurity Apache連接器的git存儲庫。
cd ~ git clone https://github.com/SpiderLabs/ModSecurity-apache
導航到ModSecurity-apache目錄,然後運行以下命令進行編譯和安裝。
cd ModSecurity-apache
./autogen.sh
./configure --with-libmodsecurity=/usr/local/modsecurity/
make
make install
在CentOS 8上使用LibModsecurity配置Apache
接下來,通過將以下行添加到主Apache配置文件中,配置Apache以加載Modsecurity Apache連接器模塊。
echo "LoadModule security3_module
/usr/lib64/httpd/modules/mod_security3.so" | sudo tee -a /etc/httpd/conf/httpd.conf
在下面創建ModSecurity配置目錄 /etc/httpd/conf.d
mkdir /etc/httpd/conf.d/modsecurity.d
將示例ModSecurity配置文件從源代碼目錄複製到上面創建的ModSec配置目錄中,重命名過程如下。
cp ~/modsec/modsecurity-v3.0.4/modsecurity.conf-recommended /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
同時複製 unicode.mapping
文件從ModSecurity源目錄到Apache Modsecurity配置目錄。
sudo cp ~/modsec/modsecurity-v3.0.4/unicode.mapping /etc/httpd/conf.d/modsecurity.d/
通過更改的值激活ModSecurity SecRuleEngine
至 On
。
sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
更改Modsecurity的默認日誌目錄
sed -i 's#/var/log/modsec_audit.log#/var/log/httpd/modsec_audit.log#' /etc/httpd/conf.d/modsecurity.d/modsecurity.conf
通過創建一個文件來配置ModSecurity規則,您可以在其中定義要包括的規則。
vim /etc/httpd/conf.d/modsecurity.d/rules.conf
Include "/etc/httpd/conf.d/modsecurity.d/modsecurity.conf" Include "/etc/httpd/conf.d/modsecurity.d/owasp-crs/crs-setup.conf" Include "/etc/httpd/conf.d/modsecurity.d/owasp-crs/rules/*.conf"
由於我們包含了OWASP規則,因此請繼續安裝它們。
安裝OWASP ModSecurity核心規則集(CRS)
的 OWASP ModSecurity核心規則集(CRS) 是與ModSecurity一起使用的一組通用攻擊檢測規則。它旨在保護Web應用程序免受各種各樣的攻擊,包括OWASP十大錯誤警報的最少性。
從克隆CRS GitHub存儲庫 至 /etc/apache2/modsecurity.d/
如下所示;
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /etc/httpd/conf.d/modsecurity.d/owasp-crs
接下來,重命名 crs-setup.conf.example
至 crs-setup.conf
。
cp /etc/httpd/conf.d/modsecurity.d/owasp-crs/crs-setup.conf{.example,}
在CentOS 8上激活ModSecurity 3
完成所有這些之後,在默認站點配置文件或任何虛擬主機配置文件上激活modsecurity。在本指南中,我們使用Apache的默認站點配置文件。
請注意,您必須為每個目錄上下文啟用ModSecurity。
vim /etc/httpd/conf/httpd.conf
請參閱下面的內容,在默認Apache配置上對默認Web根目錄進行的更改;
...
modsecurity on
modsecurity_rules_file /etc/httpd/conf.d/modsecurity.d/rules.conf
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
...
線;
modsecurity on modsecurity_rules_file /etc/httpd/conf.d/modsecurity.d/rules.conf
打開Modsecurity並分別指定Modsecurity規則的位置。
檢查Apache是否存在配置錯誤,然後重新啟動。
httpd -t
Syntax OK
systemctl restart httpd
測試Modsecurity
接下來,例如,使用命令注入,使用OWASP規則測試Modsecurity的有效性。運行下面的命令;
curl localhost/index.html?exec=/bin/bash
403 Forbidden
Forbidden
You don't have permission to access /index.html
on this server.
如果你看到, 403 Forbidden
那麼就意味着您已經釘牢了。
您也可以檢查Modsecurity日誌;
tail /var/log/httpd/modsec_audit.log
... ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:exec' (Value: `/bin/bash' ) [file "/etc/httpd/conf.d/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/bash found within ARGS:exec: /bin/bash"] [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 "centos8.kifarunix-demo.com"] [uri "/index.html"] [unique_id "158386776469.002836"] [ref "o1,8v21,9t: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 "/etc/httpd/conf.d/modsecurity.d/owasp-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "79"] [id "949110"] [rev ""] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [data ""] [severity "2"] [ver ""] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "centos8.kifarunix-demo.com"] [uri "/index.html"] [unique_id "158386776469.002836"] [ref ""]
好吧,你去。現在已安裝,激活ModSecurity 3或LibModSeceurity並保護您的站點免受Web攻擊。
隨意設置更多規則,並保護您的Web應用程序。
這標誌着我們關於如何在CentOS 8上使用Apache安裝和配置LibModsecurity的指南的結尾。
。